| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| from collections.abc import Sequence | |
| import pathlib | |
| import tempfile | |
| import unittest | |
| import zipfile | |
| from parameterized import parameterized | |
| import host_runner | |
| import test_list_file | |
| import tests_json_file | |
| _OVERSIZED_MATCHED_CASES = tuple( | |
| f"OptSuite.Match_{i:04d}_{'a' * 1000}" for i in range(65) | |
| ) | |
| _OVERSIZED_UNMATCHED_CASES = tuple( | |
| f"OtherSuite.Skip_{i:04d}_{'b' * 1000}" for i in range(65) | |
| ) | |
| _OVERSIZED_SLOW_CASES = tuple( | |
| f"OptSuite.Slow_{i:04d}_{'c' * 1000}" for i in range(65) | |
| ) | |
| _OVERSIZED_BAD_CHAR_CLASS_CASES = tuple( | |
| f"OptSuite.Bad_1_{i:04d}_{'d' * 1000}" for i in range(65) | |
| ) | |
| def _make_host_test( | |
| rel_path: str, | |
| *, | |
| list_cases_argument: str | None = None, | |
| execution_entry: test_list_file.TestListExecutionEntry | None = None, | |
| ) -> test_list_file.Test: | |
| return test_list_file.Test( | |
| tests_json_file.TestEntry( | |
| tests_json_file.TestSection( | |
| rel_path, | |
| f"//{rel_path}", | |
| "linux", | |
| path=rel_path, | |
| list_cases_argument=list_cases_argument, | |
| ) | |
| ), | |
| test_list_file.TestListEntry(rel_path, [], execution=execution_entry), | |
| ) | |
| class TestHostRunner(unittest.TestCase): | |
| @parameterized.expand( | |
| ( | |
| ( | |
| "substring_match", | |
| ("case_2",), | |
| ("Suite.test_case_2",), | |
| ), | |
| ( | |
| "glob_wildcard_match", | |
| ("*case_[13]",), | |
| ("Suite.test_case_1", "Suite.test_case_3"), | |
| ), | |
| ( | |
| "multiple_positive_or_semantics", | |
| ("*case_1", "other_case"), | |
| ("Suite.test_case_1", "Other.other_case"), | |
| ), | |
| ( | |
| "negative_exclusion_only", | |
| ("-*case_*",), | |
| ("Other.other_case", "mod::rust_leaf_a"), | |
| ), | |
| ( | |
| "positive_and_negative", | |
| ("Suite.*", "-*case_2"), | |
| ("Suite.test_case_1", "Suite.test_case_3"), | |
| ), | |
| ( | |
| "unqualified_leaf_glob_matches_method_name", | |
| ("test_case_[12]",), | |
| ("Suite.test_case_1", "Suite.test_case_2"), | |
| ), | |
| ( | |
| "unqualified_leaf_glob_matches_rust_colon_leaf", | |
| ("rust_leaf_*",), | |
| ("mod::rust_leaf_a",), | |
| ), | |
| ) | |
| ) | |
| def test_filter_test_cases( | |
| self, | |
| _name: str, | |
| filters: Sequence[str], | |
| expected: Sequence[str], | |
| ) -> None: | |
| all_cases = ( | |
| "Suite.test_case_1", | |
| "Suite.test_case_2", | |
| "Suite.test_case_3", | |
| "Other.other_case", | |
| "mod::rust_leaf_a", | |
| ) | |
| self.assertSequenceEqual( | |
| host_runner._filter_test_cases(all_cases, filters), | |
| expected, | |
| ) | |
| def test_parse_gtest_list_output_gtest_and_zxtest(self) -> None: | |
| raw_output = "\n".join( | |
| ( | |
| "starting tool fbl-gmock-test", | |
| "Running main() from gmock_main.cc", | |
| "RefPtrGmockTest.", | |
| " PointeeProperty", | |
| " DISABLED_SkippedCase", | |
| "Instantiation/ParamSuite.", | |
| " MyCase/0 # GetParam() = 42", | |
| "Instantiation/DISABLED_Suite.", | |
| " ParamDisabledCase", | |
| "TypedSuite/0. # TypeParam = int", | |
| " MyTypedCase", | |
| "DISABLED_Suite.", | |
| " NeverRunsByDefault", | |
| "ZxtestArgsParser", | |
| " .General", | |
| " .OptionalTypes", | |
| ) | |
| ) | |
| runner = host_runner.GTestRunner("gtest") | |
| self.assertSequenceEqual( | |
| runner.parse_list_output(raw_output, include_disabled=False), | |
| ( | |
| "RefPtrGmockTest.PointeeProperty", | |
| "Instantiation/ParamSuite.MyCase/0", | |
| "TypedSuite/0.MyTypedCase", | |
| "ZxtestArgsParser.General", | |
| "ZxtestArgsParser.OptionalTypes", | |
| ), | |
| ) | |
| self.assertSequenceEqual( | |
| runner.parse_list_output(raw_output, include_disabled=True), | |
| ( | |
| "RefPtrGmockTest.PointeeProperty", | |
| "RefPtrGmockTest.DISABLED_SkippedCase", | |
| "Instantiation/ParamSuite.MyCase/0", | |
| "Instantiation/DISABLED_Suite.ParamDisabledCase", | |
| "TypedSuite/0.MyTypedCase", | |
| "DISABLED_Suite.NeverRunsByDefault", | |
| "ZxtestArgsParser.General", | |
| "ZxtestArgsParser.OptionalTypes", | |
| ), | |
| ) | |
| @parameterized.expand( | |
| ( | |
| ( | |
| "glob_subtest_and_top_exclude", | |
| ("Test*Builder/sub_*", "TestOther", "-TestAEMUBuilder"), | |
| ( | |
| "-test.run", | |
| "^Test.*Builder$/^sub_.*$|TestOther", | |
| "-test.skip", | |
| "TestAEMUBuilder", | |
| ), | |
| ), | |
| ( | |
| "char_classes_negation_and_unclosed_bracket", | |
| ("Test[AQ]EMU*/case_[ab]/not_[!ab]/raw_[",), | |
| ( | |
| "-test.run", | |
| r"^Test[AQ]EMU.*$/^case_[ab]$/^not_[^ab]$/^raw_\[$", | |
| ), | |
| ), | |
| ( | |
| "escapes_regex_metacharacters", | |
| ("TestFoo.v1*",), | |
| ("-test.run", r"^TestFoo\.v1.*$"), | |
| ), | |
| ( | |
| "subtest_exclusion_preserves_top_level", | |
| ("TestOther", "-TestOther/sub_bad"), | |
| ("-test.run", "TestOther", "-test.skip", "TestOther/sub_bad"), | |
| ), | |
| ( | |
| "negative_only_omits_test_run", | |
| ("-TestAEMU*",), | |
| ("-test.skip", "^TestAEMU.*$"), | |
| ), | |
| ) | |
| ) | |
| def test_go_build_filter_args( | |
| self, | |
| _name: str, | |
| filters: Sequence[str], | |
| expected: Sequence[str], | |
| ) -> None: | |
| top_cases = ( | |
| "TestAEMUBuilder", | |
| "TestQEMUBuilder", | |
| "TestFoo.v1_alpha", | |
| "TestOther", | |
| ) | |
| self.assertSequenceEqual( | |
| host_runner.GoRunner().build_filter_args(top_cases, filters), | |
| expected, | |
| ) | |
| @parameterized.expand( | |
| ( | |
| ("unmatched_positive", ("TestNonExistent",)), | |
| ("negative_only_excludes_all_top_level", ("-Test*",)), | |
| ) | |
| ) | |
| def test_go_build_filter_args_raises_no_matching_cases( | |
| self, _name: str, filters: Sequence[str] | |
| ) -> None: | |
| top_cases = ("TestAEMUBuilder", "TestQEMUBuilder", "TestOther") | |
| with self.assertRaises(host_runner.NoMatchingCasesError): | |
| host_runner.GoRunner().build_filter_args(top_cases, filters) | |
| @parameterized.expand( | |
| ( | |
| ( | |
| "exact_complement_when_unmatched_fits", | |
| (*_OVERSIZED_MATCHED_CASES, "Suite.Case_1", "Suite.Case_2"), | |
| ("-Case_[12]",), | |
| "--gtest_filter=*-Suite.Case_1:Suite.Case_2", | |
| ), | |
| ( | |
| "all_cases_matched_returns_universal_wildcard", | |
| _OVERSIZED_MATCHED_CASES, | |
| ("*",), | |
| "--gtest_filter=*", | |
| ), | |
| ( | |
| "qualified_glob_and_substring", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_UNMATCHED_CASES), | |
| ("OptSuite.Match*", "sub"), | |
| "--gtest_filter=OptSuite.Match*:*sub*", | |
| ), | |
| ( | |
| "unqualified_leaf_glob", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_UNMATCHED_CASES), | |
| ("Opt*",), | |
| "--gtest_filter=Opt*:*.Opt*", | |
| ), | |
| ( | |
| "leading_star_separator_and_positive_delimiter_translation", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_UNMATCHED_CASES), | |
| ("*Opt*", "Instantiation/Param*", "ns::Case*", "foo-bar"), | |
| "--gtest_filter=*Opt*:Instantiation/Param*:ns??Case*:*foo?bar*", | |
| ), | |
| ( | |
| "unclosed_negation_bracket_preserved_on_positive_fallback", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_UNMATCHED_CASES), | |
| ("*Match*", "Case_[!]_1"), | |
| "--gtest_filter=*Match*:Case_[!]_1:*.Case_[!]_1", | |
| ), | |
| ( | |
| "bare_dash_falls_back_to_star", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_BAD_CHAR_CLASS_CASES), | |
| ("-", "-Bad_[12]*"), | |
| "--gtest_filter=*", | |
| ), | |
| ( | |
| "positive_char_class_range_superset_approximation", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_UNMATCHED_CASES), | |
| ("Match_[0-9]*",), | |
| "--gtest_filter=Match_?*:*.Match_?*", | |
| ), | |
| ( | |
| "scoped_excluded_exact_ignores_unselected_suites_on_double_overflow", | |
| ( | |
| *_OVERSIZED_MATCHED_CASES, | |
| *_OVERSIZED_UNMATCHED_CASES, | |
| "OptSuite.Bad_1", | |
| ), | |
| ("Opt*", "-Other*", "-Bad_[12]*"), | |
| "--gtest_filter=Opt*:*.Opt*-OptSuite.Bad_1", | |
| ), | |
| ( | |
| "oversized_char_class_exclusion_dropped_without_widening", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_BAD_CHAR_CLASS_CASES), | |
| ("Opt*", "-Bad_[12]*"), | |
| "--gtest_filter=Opt*:*.Opt*", | |
| ), | |
| ( | |
| "negative_only_leaf_glob_unclosed_bracket_and_foreign_colon_guard", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_SLOW_CASES), | |
| ("-Slow*", "-Bad_[!]_1", "-ns::Case*"), | |
| "--gtest_filter=-Slow*:*.Slow*:Bad_[!]_1:*.Bad_[!]_1", | |
| ), | |
| ( | |
| "combined_positive_and_negative", | |
| (*_OVERSIZED_MATCHED_CASES, *_OVERSIZED_SLOW_CASES), | |
| ("Opt*", "-Slow*", "-Suite.Bad*"), | |
| "--gtest_filter=Opt*:*.Opt*-Slow*:*.Slow*:Suite.Bad*", | |
| ), | |
| ) | |
| ) | |
| def test_build_gtest_filter_arg_oversized_fallback( | |
| self, | |
| _name: str, | |
| all_cases: Sequence[str], | |
| filters: Sequence[str], | |
| expected: str, | |
| ) -> None: | |
| self.assertSequenceEqual( | |
| host_runner.GTestRunner("gtest").build_filter_args( | |
| all_cases, filters | |
| ), | |
| (expected,), | |
| ) | |
| self.assertSequenceEqual( | |
| host_runner.GTestRunner("gunit").build_filter_args( | |
| all_cases, filters | |
| ), | |
| (expected.replace("--gtest_filter=", "--gunit_filter=", 1),), | |
| ) | |
| def test_detect_elf_gtest_runner_mmap_markers_and_magic(self) -> None: | |
| with tempfile.TemporaryDirectory() as tmp: | |
| tmp_dir = pathlib.Path(tmp) | |
| zxtest_elf = tmp_dir / "zxtest_elf" | |
| zxtest_elf.write_bytes( | |
| b"\x7fELF" + (b"\x00" * 20) + b"_ZN6zxtest6Runner\x00" | |
| ) | |
| # Compat gunit binaries may also link InitGoogleTest; gunit must win. | |
| gunit_compat_elf = tmp_dir / "gunit_compat_elf" | |
| gunit_compat_elf.write_bytes( | |
| b"\x7fELF" | |
| + (b"\x00" * 20) | |
| + b"InitGoogleTest\x00InitGoogleUnit\x00" | |
| ) | |
| unmarked_elf = tmp_dir / "unmarked_elf" | |
| unmarked_elf.write_bytes(b"\x7fELF" + (b"\x00" * 80)) | |
| empty_file = tmp_dir / "empty_file" | |
| empty_file.write_bytes(b"") | |
| non_elf_with_marker = tmp_dir / "script.sh" | |
| non_elf_with_marker.write_bytes(b"#!/bin/sh\n--gtest_filter\n") | |
| self.assertEqual( | |
| host_runner._detect_elf_gtest_runner(zxtest_elf), | |
| host_runner.GTestRunner("gtest"), | |
| ) | |
| self.assertEqual( | |
| host_runner._detect_elf_gtest_runner(gunit_compat_elf), | |
| host_runner.GTestRunner("gunit"), | |
| ) | |
| self.assertIsNone( | |
| host_runner._detect_elf_gtest_runner(unmarked_elf) | |
| ) | |
| self.assertIsNone(host_runner._detect_elf_gtest_runner(empty_file)) | |
| self.assertIsNone( | |
| host_runner._detect_elf_gtest_runner(non_elf_with_marker) | |
| ) | |
| # Match fnmatchcase bracket semantics: [!a] negates, [^a] is literal ^ or a. | |
| self.assertEqual( | |
| host_runner._glob_segment_to_go_regex("sub_[!a]"), "^sub_[^a]$" | |
| ) | |
| self.assertEqual( | |
| host_runner._glob_segment_to_go_regex("sub_[^a]"), r"^sub_[\^a]$" | |
| ) | |
| def test_detect_host_test_runner_wrappers_and_elfs(self) -> None: | |
| with tempfile.TemporaryDirectory() as tmp: | |
| out_dir = pathlib.Path(tmp) | |
| # GN Rust wrapper | |
| (out_dir / "rust_test.sh").write_text( | |
| "#!/bin/bash\nexec env RUST_BACKTRACE=1 host_x64/rust_test_parser 'host_x64/my_bin' \"$@\"\n" | |
| ) | |
| # Bazel Go wrapper | |
| (out_dir / "go_test.sh").write_text( | |
| '#!/bin/bash\ncd -- "$(dirname "${BASH_SOURCE[0]}")/go_test.runtime_dir"\nexec "./go_test_parser" ./go_bin -test.timeout 5m -test.v "$@"\n' | |
| ) | |
| # Direct Go wrapper without go_test_parser | |
| (out_dir / "direct_go.sh").write_text( | |
| '#!/bin/bash\nexec host_x64/my_go_bin -test.v "$@"\n' | |
| ) | |
| # Direct GTest/zxtest ELF and gunit ELF | |
| (out_dir / "gtest_bin").write_bytes( | |
| b"\x7fELF\x02\x01\x01\x00--gtest_filter\x00" | |
| ) | |
| (out_dir / "gunit_bin").write_bytes( | |
| b"\x7fELF\x02\x01\x01\x00--gunit_filter\x00" | |
| ) | |
| # GN create_test.sh C++ wrapper (out_dir-relative binary, repeated env tokens, quoted spaces) | |
| host_subdir = out_dir / "host_x64" | |
| host_subdir.mkdir() | |
| (host_subdir / "gn_cc_bin").write_bytes( | |
| b"\x7fELF\x02\x01\x01\x00MakeAndRegisterTestInfo\x00" | |
| ) | |
| (host_subdir / "gn_cc_wrapper.sh").write_text( | |
| '#!/bin/bash\nexec env KEY="val with spaces" env EXTRA=1 host_x64/gn_cc_bin "$@"\n' | |
| ) | |
| # Bazel C++ wrapper -> GTest ELF | |
| bazel_cc_runtime = out_dir / "bazel_cc.runtime_dir" | |
| bazel_cc_runtime.mkdir() | |
| (bazel_cc_runtime / "cc_bin").write_bytes( | |
| b"\x7fELF\x02\x01\x01\x00InitGoogleTest\x00" | |
| ) | |
| (out_dir / "bazel_cc").write_text( | |
| '#!/bin/bash\ncd -- "$(dirname "${BASH_SOURCE[0]}")/bazel_cc.runtime_dir"\nexec "./cc_bin" "$@"\n' | |
| ) | |
| # Bazel C++ wrapper -> plain int main() ELF (no gtest/zxtest) | |
| plain_runtime = out_dir / "plain_cc.runtime_dir" | |
| plain_runtime.mkdir() | |
| (plain_runtime / "plain_bin").write_bytes( | |
| b"\x7fELF\x02\x01\x01\x00plain_main_only\x00" | |
| ) | |
| (out_dir / "plain_cc").write_text( | |
| '#!/bin/bash\ncd -- "$(dirname "${BASH_SOURCE[0]}")/plain_cc.runtime_dir"\nexec "./plain_bin" "$@"\n' | |
| ) | |
| # GN Python .pyz wrappers (unittest.main vs custom main_callable) | |
| def write_gn_py_test(name: str, callable_expr: str) -> None: | |
| pyz_path = out_dir / f"{name}.pyz" | |
| with zipfile.ZipFile(pyz_path, "w") as zf: | |
| zf.writestr( | |
| "__main__.py", | |
| f"import sys\nsys.exit({callable_expr}())\n", | |
| ) | |
| (out_dir / f"{name}.sh").write_text( | |
| "#!/bin/bash\n" | |
| 'if [[ "$1" == "--list_host_python_unittests" ]]; then\n' | |
| f" exec python3 lister.pyz --list_tests '{name}.pyz'\n" | |
| "fi\n" | |
| f"exec python3 '{name}.pyz' -v \"$@\"\n" | |
| ) | |
| write_gn_py_test("gn_py_ok", "unittest.main") | |
| write_gn_py_test("gn_py_custom", "gn_test_main") | |
| # Bazel host_py_test .runtime_dir wrappers (unittest.main vs custom_main) | |
| def write_bazel_py_test(name: str, callable_expr: str) -> None: | |
| rt_dir = out_dir / f"{name}.runtime_dir" | |
| runfiles = rt_dir / f"{name}_bin.runfiles" / "_main" | |
| runfiles.mkdir(parents=True) | |
| (rt_dir / f"{name}_bin").write_text( | |
| '#!/bin/bash\nSTAGE2_BOOTSTRAP="_main/stage2.py"\n' | |
| ) | |
| (runfiles / "stage2.py").write_text( | |
| 'MAIN_PATH = "_main/entry_main_callable.py"\n' | |
| ) | |
| (runfiles / "entry_main_callable.py").write_text( | |
| f"_main_callable: _typing.Any = {callable_expr}\n" | |
| ) | |
| (out_dir / f"{name}.sh").write_text( | |
| "#!/bin/bash\n" | |
| f'cd -- "$(dirname "${{BASH_SOURCE[0]}}")/{name}.runtime_dir"\n' | |
| 'if [[ "$1" == "--list_host_python_unittests" ]]; then\n' | |
| f' exec test_data/python3 -S -u test_data/list_host_python_unittests.py --list_tests "{name}_bin"\n' | |
| "fi\n" | |
| f'exec test_data/python3 -S "{name}_bin" "$@"\n' | |
| ) | |
| write_bazel_py_test("bazel_py_ok", "unittest.main") | |
| write_bazel_py_test("bazel_py_custom", "custom_main") | |
| def detect( | |
| rel_path: str, | |
| list_arg: str | None = None, | |
| execution_entry: test_list_file.TestListExecutionEntry | |
| | None = None, | |
| ) -> host_runner.HostTestRunner | None: | |
| return host_runner.detect_host_test_runner( | |
| _make_host_test( | |
| rel_path, | |
| list_cases_argument=list_arg, | |
| execution_entry=execution_entry, | |
| ), | |
| tmp, | |
| ) | |
| self.assertEqual( | |
| detect("missing", "list_mobly_tests"), | |
| host_runner.MoblyRunner(), | |
| ) | |
| self.assertEqual( | |
| detect("missing", "list_host_python_unittests"), | |
| host_runner.PythonUnittestRunner(), | |
| ) | |
| self.assertEqual( | |
| detect("gn_py_ok.sh", "list_host_python_unittests"), | |
| host_runner.PythonUnittestRunner(), | |
| ) | |
| self.assertIsNone( | |
| detect("gn_py_custom.sh", "list_host_python_unittests") | |
| ) | |
| self.assertEqual( | |
| detect("bazel_py_ok.sh", "list_host_python_unittests"), | |
| host_runner.PythonUnittestRunner(), | |
| ) | |
| self.assertIsNone( | |
| detect("bazel_py_custom.sh", "list_host_python_unittests") | |
| ) | |
| self.assertEqual(detect("rust_test.sh"), host_runner.RustRunner()) | |
| self.assertEqual(detect("go_test.sh"), host_runner.GoRunner()) | |
| self.assertEqual(detect("direct_go.sh"), host_runner.GoRunner()) | |
| self.assertEqual( | |
| detect("gtest_bin"), host_runner.GTestRunner("gtest") | |
| ) | |
| self.assertEqual( | |
| detect("gunit_bin"), host_runner.GTestRunner("gunit") | |
| ) | |
| self.assertEqual( | |
| detect("host_x64/gn_cc_wrapper.sh"), | |
| host_runner.GTestRunner("gtest"), | |
| ) | |
| self.assertEqual( | |
| detect("bazel_cc"), host_runner.GTestRunner("gtest") | |
| ) | |
| self.assertIsNone(detect("plain_cc")) | |
| self.assertIsNone( | |
| detect( | |
| "gtest_bin", | |
| list_arg="list_host_python_unittests", | |
| execution_entry=test_list_file.TestListExecutionEntry( | |
| "fuchsia-pkg://fuchsia.com/pkg#meta/comp.cm" | |
| ), | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| unittest.main() |