diff --git a/src/rez/cli/test.py b/src/rez/cli/test.py index 338269a0b..0a8552d03 100644 --- a/src/rez/cli/test.py +++ b/src/rez/cli/test.py @@ -96,19 +96,14 @@ def command(opts, parser, extra_arg_groups=None): print('\n'.join(test_names)) sys.exit(0) - if opts.TEST: - run_test_names = opts.TEST - else: - # if no tests are explicitly specified, then run only those with a - # 'default' run_on tag - run_test_names = runner.get_test_names(run_on=["default"]) - - if not run_test_names: - print( - "No tests with 'default' run_on tag found in %s" % uri, - file=sys.stderr - ) - sys.exit(0) + run_test_names = runner.find_requested_test_names(opts.TEST) + + if not run_test_names: + print( + "No tests with 'default' run_on tag found in %s" % uri, + file=sys.stderr + ) + sys.exit(0) exitcode = 0 diff --git a/src/rez/package_test.py b/src/rez/package_test.py index 4c9e3d917..a8863c94c 100644 --- a/src/rez/package_test.py +++ b/src/rez/package_test.py @@ -11,6 +11,7 @@ from rez.utils.logging_ import print_info, print_warning, print_error from rez.version import Requirement, RequirementList from shlex import quote +import fnmatch import time import sys import os @@ -206,6 +207,16 @@ def get_test_names(self, run_on=None): return self.get_package_test_names(package, run_on=run_on) + def find_requested_test_names(self, requested_tests): + # if no tests are explicitly specified, then run only those with a + # 'default' run_on tag + run_on = ["default"] if not requested_tests else None + pkg_test_names = self.get_test_names(run_on=run_on) + requested_test_names = set() + for requested_test in requested_tests: + requested_test_names.update(set(fnmatch.filter(pkg_test_names, requested_test))) + return sorted(requested_test_names) + @property def num_tests(self): """Get the number of tests, regardless of stats. diff --git a/src/rez/tests/test_test.py b/src/rez/tests/test_test.py index 2a5074555..7eaeb51e1 100644 --- a/src/rez/tests/test_test.py +++ b/src/rez/tests/test_test.py @@ -83,3 +83,185 @@ def _get_test_result(self, runner, test_name): (result for result in runner.test_results.test_results if result.get("test_name") == test_name), None ) + + def test_wildcard_01(self): + """package.py unit tests are correctly found with a wildcard then run in a testing environment""" + self.inject_python_repo() + context = ResolvedContext(["testing_obj", "python"]) + # This will get us more code coverage :) + self.inject_python_repo() + runner = PackageTestRunner( + package_request="testing_obj", + package_paths=context.package_paths, + ) + + test_names = runner.find_requested_test_names(["command_as_*"]) + self.assertEqual(2, len(test_names)) + + for test_name in test_names: + runner.run_test(test_name) + + self.assertEqual(runner.test_results.num_tests, 2) + + self.assertEqual( + self._get_test_result(runner, "command_as_string_success")["status"], + "success", + "command_as_string_success did not succeed", + ) + self.assertEqual( + self._get_test_result(runner, "command_as_string_fail")["status"], + "failed", + "command_as_string_fail did not fail", + ) + + def test_wildcard_02(self): + """ + package.py unit tests are correctly found with a wildcard + a package name then run + in a testing environment + """ + self.inject_python_repo() + context = ResolvedContext(["testing_obj", "python"]) + # This will get us more code coverage :) + self.inject_python_repo() + runner = PackageTestRunner( + package_request="testing_obj", + package_paths=context.package_paths, + + ) + + test_names = runner.find_requested_test_names(["command_as_*", "check_car_ideas"]) + self.assertEqual(3, len(test_names)) + + for test_name in test_names: + runner.run_test(test_name) + + self.assertEqual(runner.test_results.num_tests, 3) + + self.assertEqual( + self._get_test_result(runner, "check_car_ideas")["status"], + "success", + "check_car_ideas did not succeed", + ) + + self.assertEqual( + self._get_test_result(runner, "command_as_string_success")["status"], + "success", + "command_as_string_success did not succeed", + ) + self.assertEqual( + self._get_test_result(runner, "command_as_string_fail")["status"], + "failed", + "command_as_string_fail did not fail", + ) + + def test_wildcard_03(self): + """ + package.py unit tests are correctly found with a wildcard equivalent to 'default' then run + in a testing environment + """ + self.inject_python_repo() + context = ResolvedContext(["testing_obj", "python"]) + # This will get us more code coverage :) + self.inject_python_repo() + runner = PackageTestRunner( + package_request="testing_obj", + package_paths=context.package_paths, + + ) + + test_names = runner.find_requested_test_names(["*"]) + self.assertEqual(4, len(test_names)) + + for test_name in test_names: + runner.run_test(test_name) + + self.assertEqual(runner.test_results.num_tests, 4) + + self.assertEqual( + self._get_test_result(runner, "check_car_ideas")["status"], + "success", + "check_car_ideas did not succeed", + ) + self.assertEqual( + self._get_test_result(runner, "move_meeting_to_noon")["status"], + "failed", + "move_meeting_to_noon did not fail", + ) + self.assertEqual( + self._get_test_result(runner, "command_as_string_success")["status"], + "success", + "command_as_string_success did not succeed", + ) + self.assertEqual( + self._get_test_result(runner, "command_as_string_fail")["status"], + "failed", + "command_as_string_fail did not fail", + ) + + def test_wildcard_04(self): + """ + package.py unit tests are correctly found with a wildcard which get all test starting by 'c' and + the second letter is 'h' or 'o' then run in a testing environment + """ + self.inject_python_repo() + context = ResolvedContext(["testing_obj", "python"]) + # This will get us more code coverage :) + self.inject_python_repo() + runner = PackageTestRunner( + package_request="testing_obj", + package_paths=context.package_paths, + + ) + + test_names = runner.find_requested_test_names(["c[ho]*"]) + self.assertEqual(3, len(test_names)) + + for test_name in test_names: + runner.run_test(test_name) + + self.assertEqual(runner.test_results.num_tests, 3) + + self.assertEqual( + self._get_test_result(runner, "check_car_ideas")["status"], + "success", + "check_car_ideas did not succeed", + ) + self.assertEqual( + self._get_test_result(runner, "command_as_string_success")["status"], + "success", + "command_as_string_success did not succeed", + ) + self.assertEqual( + self._get_test_result(runner, "command_as_string_fail")["status"], + "failed", + "command_as_string_fail did not fail", + ) + + def test_wildcard_05(self): + """ + package.py unit tests are correctly found with a wildcard which get all test which is not starting by 'c' + then run in a testing environment + """ + self.inject_python_repo() + context = ResolvedContext(["testing_obj", "python"]) + # This will get us more code coverage :) + self.inject_python_repo() + runner = PackageTestRunner( + package_request="testing_obj", + package_paths=context.package_paths, + + ) + + test_names = runner.find_requested_test_names(["[!c]*"]) + self.assertEqual(1, len(test_names)) + + for test_name in test_names: + runner.run_test(test_name) + + self.assertEqual(runner.test_results.num_tests, 1) + + self.assertEqual( + self._get_test_result(runner, "move_meeting_to_noon")["status"], + "failed", + "move_meeting_to_noon did not fail", + )