blob: 6493deafa7e875d972582acd456db9d4d14869ca [file] [log] [blame]
Avi Drissmand654e852022-09-28 16:47:201# Copyright 2018 The Chromium Authors
Robert Ma08006c902018-06-01 15:35:392# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Check the basic functionalities of WPT tools.
6
Stephen McGruer3a1e4172020-06-25 22:40:337This PRESUBMIT guards against rolling a broken version of WPT tooling. It does
8some smoke checks of WPT functionality.
Robert Ma08006c902018-06-01 15:35:399"""
10
Fabrice de Gansd7ccea732021-06-21 20:51:2311USE_PYTHON3 = True
Stephen McGruer3a1e4172020-06-25 22:40:3312
Jonathan Lee7282c7c2023-02-28 18:33:0913import pathlib
14import textwrap
15
16
Robert Ma08006c902018-06-01 15:35:3917def _TestWPTLint(input_api, output_api):
Stephen McGruer3a1e4172020-06-25 22:40:3318 # We test 'wpt lint' by deferring to the web_tests/external presubmit test,
19 # which runs 'wpt lint' against web_tests/external/wpt.
Robert Ma08006c902018-06-01 15:35:3920 abspath_to_test = input_api.os_path.join(
21 input_api.change.RepositoryRoot(),
Kent Tamura77578cc2018-11-25 22:33:4322 'third_party', 'blink', 'web_tests', 'external', 'PRESUBMIT_test.py'
Robert Ma08006c902018-06-01 15:35:3923 )
24 command = input_api.Command(
Kent Tamura77578cc2018-11-25 22:33:4325 name='web_tests/external/PRESUBMIT_test.py',
Robert Ma08006c902018-06-01 15:35:3926 cmd=[abspath_to_test],
27 kwargs={},
Luke Zielinski04b275de2021-03-24 19:30:0028 message=output_api.PresubmitError,
29 python3=True
Robert Ma08006c902018-06-01 15:35:3930 )
31 if input_api.verbose:
32 print('Running ' + abspath_to_test)
33 return input_api.RunTests([command])
34
35
Stephen McGruer3a1e4172020-06-25 22:40:3336def _TestWPTManifest(input_api, output_api):
37 # We test 'wpt manifest' by making a copy of the base manifest and updating
38 # it. A copy is used so that this PRESUBMIT doesn't change files in the tree.
39 blink_path = input_api.os_path.join(
40 input_api.change.RepositoryRoot(), 'third_party', 'blink')
41
42 base_manifest = input_api.os_path.join(
43 blink_path, 'web_tests', 'external', 'WPT_BASE_MANIFEST_8.json')
Fabrice de Gansd7ccea732021-06-21 20:51:2344 with input_api.CreateTemporaryFile(mode = 'wt') as f:
Stephen McGruer3a1e4172020-06-25 22:40:3345 f.write(input_api.ReadFile(base_manifest))
46 f.close()
47
48 wpt_exec_path = input_api.os_path.join(
Stephen McGruere934d52e2021-01-27 22:26:1049 input_api.change.RepositoryRoot(), 'third_party', 'wpt_tools', 'wpt', 'wpt')
Stephen McGruer3a1e4172020-06-25 22:40:3350 external_wpt = input_api.os_path.join(
51 blink_path, 'web_tests', 'external', 'wpt')
52 try:
53 input_api.subprocess.check_output(
Luke Zielinski04b275de2021-03-24 19:30:0054 ['python3', wpt_exec_path, 'manifest', '--no-download',
Stephen McGruer3a1e4172020-06-25 22:40:3355 '--path', f.name, '--tests-root', external_wpt])
56 except input_api.subprocess.CalledProcessError as exc:
57 return [output_api.PresubmitError('wpt manifest failed:', long_text=exc.output)]
58
59 return []
60
61
Jonathan Lee7282c7c2023-02-28 18:33:0962def _TestWPTRolled(input_api, output_api):
63 """Warn developers making manual changes to `wpt_tools/`."""
64 lines = input_api.change.DescriptionText().splitlines()
65 # No lines will be present when not run against a change (e.g., ToT).
66 if not lines or input_api.re.search(r'\broll wpt tooling\b', lines[0],
67 input_api.re.IGNORECASE):
68 return []
69
70 include_file = input_api.os_path.join(input_api.PresubmitLocalPath(),
71 'WPTIncludeList')
72 rolled_files = {pathlib.PurePosixPath(line)
73 for line in input_api.ReadFile(include_file).splitlines()}
74 wpt_dir = pathlib.Path(input_api.PresubmitLocalPath()) / 'wpt'
75
76 def exclude_unrolled_files(affected_file):
77 try:
78 path_from_wpt = pathlib.Path(
79 affected_file.AbsoluteLocalPath()).relative_to(wpt_dir)
80 return pathlib.PurePosixPath(path_from_wpt.as_posix()) in rolled_files
81 except ValueError:
82 # Exclude file relative to `wpt_tools` but not to `wpt_tools/wpt`.
83 return False
84
85 if input_api.AffectedFiles(file_filter=exclude_unrolled_files):
86 message = textwrap.dedent(
87 """\
88 Thanks for your patch to `//third_party/wpt_tools/wpt`. This directory is
89 semiregularly overwritten by rolls from the upstream repo:
90 https://github.com/web-platform-tests/wpt
91
92 Please submit your change upstream as a pull request instead, then run
93 `//third_party/wpt_tools/roll_wpt.py` to pick up the change.
94 """)
95 return [output_api.PresubmitPromptWarning(message)]
96 return []
97
98
Robert Ma08006c902018-06-01 15:35:3999def CheckChangeOnUpload(input_api, output_api):
Stephen McGruer3a1e4172020-06-25 22:40:33100 results = []
101 results += _TestWPTLint(input_api, output_api)
102 results += _TestWPTManifest(input_api, output_api)
Jonathan Lee7282c7c2023-02-28 18:33:09103 results += _TestWPTRolled(input_api, output_api)
Stephen McGruer3a1e4172020-06-25 22:40:33104 return results
Robert Ma08006c902018-06-01 15:35:39105
106
107def CheckChangeOnCommit(input_api, output_api):
Stephen McGruer3a1e4172020-06-25 22:40:33108 results = []
109 results += _TestWPTLint(input_api, output_api)
110 results += _TestWPTManifest(input_api, output_api)
Jonathan Lee7282c7c2023-02-28 18:33:09111 results += _TestWPTRolled(input_api, output_api)
Stephen McGruer3a1e4172020-06-25 22:40:33112 return results