Avi Drissman | d654e85 | 2022-09-28 16:47:20 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 2 | # 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 McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 7 | This PRESUBMIT guards against rolling a broken version of WPT tooling. It does |
| 8 | some smoke checks of WPT functionality. |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 9 | """ |
| 10 | |
Jonathan Lee | 7282c7c | 2023-02-28 18:33:09 | [diff] [blame] | 11 | import pathlib |
| 12 | import textwrap |
| 13 | |
| 14 | |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 15 | def _TestWPTLint(input_api, output_api): |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 16 | # We test 'wpt lint' by deferring to the web_tests/external presubmit test, |
| 17 | # which runs 'wpt lint' against web_tests/external/wpt. |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 18 | abspath_to_test = input_api.os_path.join( |
| 19 | input_api.change.RepositoryRoot(), |
Kent Tamura | 77578cc | 2018-11-25 22:33:43 | [diff] [blame] | 20 | 'third_party', 'blink', 'web_tests', 'external', 'PRESUBMIT_test.py' |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 21 | ) |
| 22 | command = input_api.Command( |
Kent Tamura | 77578cc | 2018-11-25 22:33:43 | [diff] [blame] | 23 | name='web_tests/external/PRESUBMIT_test.py', |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 24 | cmd=[abspath_to_test], |
| 25 | kwargs={}, |
Luke Zielinski | 04b275de | 2021-03-24 19:30:00 | [diff] [blame] | 26 | message=output_api.PresubmitError, |
| 27 | python3=True |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 28 | ) |
| 29 | if input_api.verbose: |
| 30 | print('Running ' + abspath_to_test) |
| 31 | return input_api.RunTests([command]) |
| 32 | |
| 33 | |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 34 | def _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 Gans | d7ccea73 | 2021-06-21 20:51:23 | [diff] [blame] | 42 | with input_api.CreateTemporaryFile(mode = 'wt') as f: |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 43 | f.write(input_api.ReadFile(base_manifest)) |
| 44 | f.close() |
| 45 | |
| 46 | wpt_exec_path = input_api.os_path.join( |
Stephen McGruer | e934d52e | 2021-01-27 22:26:10 | [diff] [blame] | 47 | input_api.change.RepositoryRoot(), 'third_party', 'wpt_tools', 'wpt', 'wpt') |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 48 | external_wpt = input_api.os_path.join( |
| 49 | blink_path, 'web_tests', 'external', 'wpt') |
| 50 | try: |
| 51 | input_api.subprocess.check_output( |
Luke Zielinski | 04b275de | 2021-03-24 19:30:00 | [diff] [blame] | 52 | ['python3', wpt_exec_path, 'manifest', '--no-download', |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 53 | '--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 Lee | 7282c7c | 2023-02-28 18:33:09 | [diff] [blame] | 60 | def _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 Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 97 | def CheckChangeOnUpload(input_api, output_api): |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 98 | results = [] |
| 99 | results += _TestWPTLint(input_api, output_api) |
| 100 | results += _TestWPTManifest(input_api, output_api) |
Jonathan Lee | 7282c7c | 2023-02-28 18:33:09 | [diff] [blame] | 101 | results += _TestWPTRolled(input_api, output_api) |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 102 | return results |
Robert Ma | 08006c90 | 2018-06-01 15:35:39 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def CheckChangeOnCommit(input_api, output_api): |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 106 | results = [] |
| 107 | results += _TestWPTLint(input_api, output_api) |
| 108 | results += _TestWPTManifest(input_api, output_api) |
Jonathan Lee | 7282c7c | 2023-02-28 18:33:09 | [diff] [blame] | 109 | results += _TestWPTRolled(input_api, output_api) |
Stephen McGruer | 3a1e417 | 2020-06-25 22:40:33 | [diff] [blame] | 110 | return results |