| # 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 | |
| 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", | |
| ), | |
| ) | |
| def test_go_build_filter_args(self) -> None: | |
| top_cases = ("TestAEMUBuilder", "TestQEMUBuilder", "TestOther") | |
| runner = host_runner.GoRunner() | |
| self.assertSequenceEqual( | |
| runner.build_filter_args( | |
| top_cases, | |
| ("Test*Builder/sub_*", "TestOther", "-TestAEMUBuilder"), | |
| ), | |
| ( | |
| "-test.run", | |
| "^Test.*Builder$/^sub_.*$|TestOther", | |
| "-test.skip", | |
| "TestAEMUBuilder", | |
| ), | |
| ) | |
| with self.assertRaises(host_runner.NoMatchingCasesError): | |
| runner.build_filter_args(top_cases, ("TestNonExistent",)) | |
| # 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 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" | |
| ) | |
| # 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 | |
| ) -> host_runner.HostTestRunner | None: | |
| return host_runner.detect_host_test_runner( | |
| _make_host_test(rel_path, list_cases_argument=list_arg), 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("gtest_bin"), host_runner.GTestRunner("gtest") | |
| ) | |
| self.assertEqual( | |
| detect("gunit_bin"), host_runner.GTestRunner("gunit") | |
| ) | |
| self.assertEqual( | |
| detect("bazel_cc"), host_runner.GTestRunner("gtest") | |
| ) | |
| self.assertIsNone(detect("plain_cc")) | |
| if __name__ == "__main__": | |
| unittest.main() |