def test_translate(self, _info, mock_testmapping):
        """Test translate method."""
        # Check that we can find a class.
        targets, test_infos = self.ctr.translate([uc.CLASS_NAME])
        unittest_utils.assert_strict_equal(
            self, targets, uc.CLASS_BUILD_TARGETS)
        unittest_utils.assert_strict_equal(self, test_infos, {uc.CLASS_INFO})

        # Check that we get all the build targets we expect.
        targets, test_infos = self.ctr.translate([uc.MODULE_NAME,
                                                  uc.CLASS_NAME])
        unittest_utils.assert_strict_equal(
            self, targets, uc.MODULE_CLASS_COMBINED_BUILD_TARGETS)
        unittest_utils.assert_strict_equal(self, test_infos, {uc.MODULE_INFO,
                                                              uc.CLASS_INFO})

        # Check that test mappings feeds into get_test_info properly.
        test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
        test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
        mock_testmapping.return_value = ([test_detail1, test_detail2], None)
        targets, test_infos = self.ctr.translate([])
        unittest_utils.assert_strict_equal(
            self, targets, uc.MODULE_CLASS_COMBINED_BUILD_TARGETS)
        unittest_utils.assert_strict_equal(self, test_infos, {uc.MODULE_INFO,
                                                              uc.CLASS_INFO})
    def test_get_test_infos(self, mock_getfindmethods):
        """Test _get_test_infos method."""
        ctr = cli_t.CLITranslator()
        find_method_return_module_info = lambda x, y: uc.MODULE_INFO
        # pylint: disable=invalid-name
        find_method_return_module_class_info = (lambda x, test: uc.MODULE_INFO
                                                if test == uc.MODULE_NAME
                                                else uc.CLASS_INFO)
        find_method_return_nothing = lambda x, y: None
        one_test = [uc.MODULE_NAME]
        mult_test = [uc.MODULE_NAME, uc.CLASS_NAME]

        # Let's make sure we return what we expect.
        expected_test_infos = {uc.MODULE_INFO}
        mock_getfindmethods.return_value = [
            test_finder_base.Finder(None, find_method_return_module_info)]
        unittest_utils.assert_strict_equal(
            self, ctr._get_test_infos(one_test), expected_test_infos)

        # Check we receive multiple test infos.
        expected_test_infos = {uc.MODULE_INFO, uc.CLASS_INFO}
        mock_getfindmethods.return_value = [
            test_finder_base.Finder(None, find_method_return_module_class_info)]
        unittest_utils.assert_strict_equal(
            self, ctr._get_test_infos(mult_test), expected_test_infos)

        # Let's make sure we raise an error when we have no tests found.
        mock_getfindmethods.return_value = [
            test_finder_base.Finder(None, find_method_return_nothing)]
        self.assertRaises(atest_error.NoTestFoundError, ctr._get_test_infos,
                          one_test)

        # Check the method works for test mapping.
        test_detail1 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
        test_detail2 = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
        expected_test_infos = {uc.MODULE_INFO, uc.CLASS_INFO}
        mock_getfindmethods.return_value = [
            test_finder_base.Finder(None, find_method_return_module_class_info)]
        test_infos = ctr._get_test_infos(
            mult_test, [test_detail1, test_detail2])
        unittest_utils.assert_strict_equal(
            self, test_infos, expected_test_infos)
        for test_info in test_infos:
            if test_info == uc.MODULE_INFO:
                self.assertEqual(
                    test_detail1.options,
                    test_info.data[constants.TI_MODULE_ARG])
            else:
                self.assertEqual(
                    test_detail2.options,
                    test_info.data[constants.TI_MODULE_ARG])
Пример #3
0
    def _find_tests_by_test_mapping(self,
                                    path='',
                                    test_group=constants.TEST_GROUP_PRESUBMIT,
                                    file_name=TEST_MAPPING):
        """Find tests defined in TEST_MAPPING in the given path.

        Args:
            path: A string of path in source. Default is set to '', i.e., CWD.
            test_group: Group of tests to run. Default is set to `presubmit`.
            file_name: Name of TEST_MAPPING file. Default is set to
                    `TEST_MAPPING`. The argument is added for testing purpose.

        Returns:
            A tuple of (tests, all_tests), where,
            tests is a set of tests (test_mapping.TestDetail) defined in
            TEST_MAPPING file of the given path, and its parent directories,
            with matching test_group.
            all_tests is a dictionary of all tests in TEST_MAPPING files,
            grouped by test group.
        """
        path = os.path.realpath(path)
        if path == constants.ANDROID_BUILD_TOP or path == os.sep:
            return None, None
        tests = set()
        all_tests = {}
        test_mapping_dict = None
        test_mapping_file = os.path.join(path, file_name)
        if os.path.exists(test_mapping_file):
            with open(test_mapping_file) as json_file:
                test_mapping_dict = json.load(json_file)
            for test_group_name, test_list in test_mapping_dict.items():
                grouped_tests = all_tests.setdefault(test_group_name, set())
                grouped_tests.update(
                    [test_mapping.TestDetail(test) for test in test_list])
            for test in test_mapping_dict.get(test_group, []):
                tests.add(test_mapping.TestDetail(test))
        parent_dir_tests, parent_dir_all_tests = (
            self._find_tests_by_test_mapping(os.path.dirname(path), test_group,
                                             file_name))
        if parent_dir_tests:
            tests.update(parent_dir_tests)
        if parent_dir_all_tests:
            for test_group_name, test_list in parent_dir_all_tests.items():
                grouped_tests = all_tests.setdefault(test_group_name, set())
                grouped_tests.update(test_list)
        if test_group == constants.TEST_GROUP_POSTSUBMIT:
            tests.update(all_tests.get(constants.TEST_GROUP_PRESUBMIT, set()))
        return tests, all_tests
 def test_parsing_with_bad_option(self):
     """Test creating TestDetail object with bad option configured"""
     with self.assertRaises(Exception) as context:
         test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_BAD_OPTION)
     self.assertEqual('Each option can only have one key.',
                      str(context.exception))
 def test_parsing_with_option(self):
     """Test creating TestDetail object with option configured"""
     detail = test_mapping.TestDetail(uc.TEST_MAPPING_TEST_WITH_OPTION)
     self.assertEqual(uc.TEST_MAPPING_TEST_WITH_OPTION['name'], detail.name)
     self.assertEqual(uc.TEST_MAPPING_TEST_WITH_OPTION_STR, str(detail))
 def test_parsing(self):
     """Test creating TestDetail object"""
     detail = test_mapping.TestDetail(uc.TEST_MAPPING_TEST)
     self.assertEqual(uc.TEST_MAPPING_TEST['name'], detail.name)
     self.assertEqual([], detail.options)