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