|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +import os |
| 6 | +import tomllib |
| 7 | +import shutil |
| 8 | + |
| 9 | +BASE_PATH = os.path.join("src", "zen", "tests") |
| 10 | +EXTERNAL_TESTS_MANIFEST = os.path.join(BASE_PATH, "manifest.toml") |
| 11 | +EXTERNAL_TESTS_OUTPUT = os.path.join(BASE_PATH, "mochitests") |
| 12 | + |
| 13 | +FILE_PREFIX = """ |
| 14 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 15 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 16 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 17 | +
|
| 18 | +# This file is autogenerated by scripts/import_external_tests.py |
| 19 | +# Do not edit manually. |
| 20 | +
|
| 21 | +BROWSER_CHROME_MANIFESTS += [ |
| 22 | +""" |
| 23 | + |
| 24 | +FILE_SUFFIX = "]" |
| 25 | + |
| 26 | + |
| 27 | +def get_tests_manifest(): |
| 28 | + with open(EXTERNAL_TESTS_MANIFEST, "rb") as f: |
| 29 | + return tomllib.load(f) |
| 30 | + |
| 31 | + |
| 32 | +def die_with_error(message): |
| 33 | + print(f"ERROR: {message}") |
| 34 | + exit(1) |
| 35 | + |
| 36 | + |
| 37 | +def validate_tests_path(path, files, ignore_list): |
| 38 | + for ignore in ignore_list: |
| 39 | + if ignore not in files: |
| 40 | + die_with_error(f"Ignore file '{ignore}' not found in tests folder '{path}'") |
| 41 | + if "browser.toml" not in files or "browser.js" in ignore_list: |
| 42 | + die_with_error(f"'browser.toml' not found in tests folder '{path}'") |
| 43 | + |
| 44 | + |
| 45 | +def disable_and_replace_manifest(manifest, output_path): |
| 46 | + toml_file = os.path.join(output_path, "browser.toml") |
| 47 | + disabled_tests = manifest.get("disable", []) |
| 48 | + with open(toml_file, "r") as f: |
| 49 | + data = f.read() |
| 50 | + for test in disabled_tests: |
| 51 | + segment = f'["{test}"]' |
| 52 | + if segment not in data: |
| 53 | + die_with_error(f"Could not disable test '{test}' as it was not found in '{toml_file}'") |
| 54 | + replace_with = f'["{test}"]\ndisabled="Disabled by import_external_tests.py"' |
| 55 | + data = data.replace(segment, replace_with) |
| 56 | + for replacement in manifest.get("replace-manifest", {}).keys(): |
| 57 | + if replacement not in data: |
| 58 | + die_with_error(f"Could not replace manifest entry '{replacement}' as it was not found in '{toml_file}'") |
| 59 | + data = data.replace(replacement, manifest["replace-manifest"][replacement]) |
| 60 | + with open(toml_file, "w") as f: |
| 61 | + f.write(data) |
| 62 | + |
| 63 | + |
| 64 | +def import_test_suite(test_suite, source_path, output_path, ignore_list, manifest, is_direct_path=False): |
| 65 | + print(f"Importing test suite '{test_suite}' from '{source_path}'") |
| 66 | + tests_folder = os.path.join("engine", source_path) |
| 67 | + if not is_direct_path: |
| 68 | + tests_folder = os.path.join(tests_folder, "tests") |
| 69 | + if not os.path.exists(tests_folder): |
| 70 | + die_with_error(f"Tests folder not found: {tests_folder}") |
| 71 | + files = os.listdir(tests_folder) |
| 72 | + validate_tests_path(tests_folder, files, ignore_list) |
| 73 | + if os.path.exists(output_path): |
| 74 | + shutil.rmtree(output_path) |
| 75 | + os.makedirs(output_path, exist_ok=True) |
| 76 | + for item in files: |
| 77 | + if item in ignore_list: |
| 78 | + continue |
| 79 | + s = os.path.join(tests_folder, item) |
| 80 | + d = os.path.join(output_path, item) |
| 81 | + if os.path.isdir(s): |
| 82 | + shutil.copytree(s, d) |
| 83 | + else: |
| 84 | + shutil.copy2(s, d) |
| 85 | + disable_and_replace_manifest(manifest[test_suite], output_path) |
| 86 | + |
| 87 | + |
| 88 | +def write_moz_build_file(manifest): |
| 89 | + moz_build_path = os.path.join(EXTERNAL_TESTS_OUTPUT, "moz.build") |
| 90 | + print(f"Writing moz.build file to '{moz_build_path}'") |
| 91 | + with open(moz_build_path, "w") as f: |
| 92 | + f.write(FILE_PREFIX) |
| 93 | + for test_suite in manifest.keys(): |
| 94 | + f.write(f'\t"{test_suite}/browser.toml",\n') |
| 95 | + f.write(FILE_SUFFIX) |
| 96 | + |
| 97 | + |
| 98 | +def make_sure_ordered_tests(manifest): |
| 99 | + ordered_tests = sorted(manifest.keys()) |
| 100 | + if list(manifest.keys()) != ordered_tests: |
| 101 | + die_with_error("Test suites in manifest.toml are not in alphabetical order.") |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + manifest = get_tests_manifest() |
| 106 | + if os.path.exists(EXTERNAL_TESTS_OUTPUT): |
| 107 | + shutil.rmtree(EXTERNAL_TESTS_OUTPUT) |
| 108 | + os.makedirs(EXTERNAL_TESTS_OUTPUT, exist_ok=True) |
| 109 | + |
| 110 | + make_sure_ordered_tests(manifest) |
| 111 | + for test_suite, config in manifest.items(): |
| 112 | + import_test_suite( |
| 113 | + test_suite=test_suite, |
| 114 | + source_path=config["source"], |
| 115 | + output_path=os.path.join(EXTERNAL_TESTS_OUTPUT, test_suite), |
| 116 | + ignore_list=config.get("ignore", []), |
| 117 | + is_direct_path=config.get("is_direct_path", False), |
| 118 | + manifest=manifest |
| 119 | + ) |
| 120 | + write_moz_build_file(manifest) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments