blob: 670cdaee1b54fe607120c4b0ef807d2dc793b4c3 [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
Jonathan Lee7282c7c2023-02-28 18:33:0911import pathlib
12import textwrap
13
14
Robert Ma08006c902018-06-01 15:35:3915def _TestWPTLint(input_api, output_api):
Stephen McGruer3a1e4172020-06-25 22:40:3316 # We test 'wpt lint' by deferring to the web_tests/external presubmit test,
17 # which runs 'wpt lint' against web_tests/external/wpt.
Robert Ma08006c902018-06-01 15:35:3918 abspath_to_test = input_api.os_path.join(
19 input_api.change.RepositoryRoot(),
Kent Tamura77578cc2018-11-25 22:33:4320 'third_party', 'blink', 'web_tests', 'external', 'PRESUBMIT_test.py'
Robert Ma08006c902018-06-01 15:35:3921 )
22 command = input_api.Command(
Kent Tamura77578cc2018-11-25 22:33:4323 name='web_tests/external/PRESUBMIT_test.py',
Robert Ma08006c902018-06-01 15:35:3924 cmd=[abspath_to_test],
25 kwargs={},
Luke Zielinski04b275de2021-03-24 19:30:0026 message=output_api.PresubmitError,
27 python3=True
Robert Ma08006c902018-06-01 15:35:3928 )
29 if input_api.verbose:
30 print('Running ' + abspath_to_test)
31 return input_api.RunTests([command])
32
33
Stephen McGruer3a1e4172020-06-25 22:40:3334def _TestWPTManifest(input_api, output_api):
35 # We test 'wpt manifest' by making a copy of the base manifest and updating
36 # it. A copy is used so that this PRESUBMIT doesn't change files in the tree.
37 blink_path = input_api.os_path.join(
38 input_api.change.RepositoryRoot(), 'third_party', 'blink')
39
40 base_manifest = input_api.os_path.join(
41 blink_path, 'web_tests', 'external', 'WPT_BASE_MANIFEST_8.json')
Fabrice de Gansd7ccea732021-06-21 20:51:2342 with input_api.CreateTemporaryFile(mode = 'wt') as f:
Stephen McGruer3a1e4172020-06-25 22:40:3343 f.write(input_api.ReadFile(base_manifest))
44 f.close()
45
46 wpt_exec_path = input_api.os_path.join(
Stephen McGruere934d52e2021-01-27 22:26:1047 input_api.change.RepositoryRoot(), 'third_party', 'wpt_tools', 'wpt', 'wpt')
Stephen McGruer3a1e4172020-06-25 22:40:3348 external_wpt = input_api.os_path.join(
49 blink_path, 'web_tests', 'external', 'wpt')
50 try:
51 input_api.subprocess.check_output(
Luke Zielinski04b275de2021-03-24 19:30:0052 ['python3', wpt_exec_path, 'manifest', '--no-download',
Stephen McGruer3a1e4172020-06-25 22:40:3353 '--path', f.name, '--tests-root', external_wpt])
54 except input_api.subprocess.CalledProcessError as exc:
55 return [output_api.PresubmitError('wpt manifest failed:', long_text=exc.output)]
56
57 return []
58
59
Jonathan Lee7282c7c2023-02-28 18:33:0960def _TestWPTRolled(input_api, output_api):
61 """Warn developers making manual changes to `wpt_tools/`."""
62 lines = input_api.change.DescriptionText().splitlines()
63 # No lines will be present when not run against a change (e.g., ToT).
64 if not lines or input_api.re.search(r'\broll wpt tooling\b', lines[0],
65 input_api.re.IGNORECASE):
66 return []
67
68 include_file = input_api.os_path.join(input_api.PresubmitLocalPath(),
69 'WPTIncludeList')
70 rolled_files = {pathlib.PurePosixPath(line)
71 for line in input_api.ReadFile(include_file).splitlines()}
72 wpt_dir = pathlib.Path(input_api.PresubmitLocalPath()) / 'wpt'
73
74 def exclude_unrolled_files(affected_file):
75 try:
76 path_from_wpt = pathlib.Path(
77 affected_file.AbsoluteLocalPath()).relative_to(wpt_dir)
78 return pathlib.PurePosixPath(path_from_wpt.as_posix()) in rolled_files
79 except ValueError:
80 # Exclude file relative to `wpt_tools` but not to `wpt_tools/wpt`.
81 return False
82
83 if input_api.AffectedFiles(file_filter=exclude_unrolled_files):
84 message = textwrap.dedent(
85 """\
86 Thanks for your patch to `//third_party/wpt_tools/wpt`. This directory is
87 semiregularly overwritten by rolls from the upstream repo:
88 https://github.com/web-platform-tests/wpt
89
90 Please submit your change upstream as a pull request instead, then run
91 `//third_party/wpt_tools/roll_wpt.py` to pick up the change.
92 """)
93 return [output_api.PresubmitPromptWarning(message)]
94 return []
95
96
Robert Ma08006c902018-06-01 15:35:3997def CheckChangeOnUpload(input_api, output_api):
Stephen McGruer3a1e4172020-06-25 22:40:3398 results = []
99 results += _TestWPTLint(input_api, output_api)
100 results += _TestWPTManifest(input_api, output_api)
Jonathan Lee7282c7c2023-02-28 18:33:09101 results += _TestWPTRolled(input_api, output_api)
Stephen McGruer3a1e4172020-06-25 22:40:33102 return results
Robert Ma08006c902018-06-01 15:35:39103
104
105def CheckChangeOnCommit(input_api, output_api):
Stephen McGruer3a1e4172020-06-25 22:40:33106 results = []
107 results += _TestWPTLint(input_api, output_api)
108 results += _TestWPTManifest(input_api, output_api)
Jonathan Lee7282c7c2023-02-28 18:33:09109 results += _TestWPTRolled(input_api, output_api)
Stephen McGruer3a1e4172020-06-25 22:40:33110 return results