def test_get_path_to_module_info(self, mock_load_module):
     """Test that we correctly create the path to module info dict."""
     mod_one = 'mod1'
     mod_two = 'mod2'
     mod_path_one = '/path/to/mod1'
     mod_path_two = '/path/to/mod2'
     mod_info_dict = {
         mod_one: {
             constants.MODULE_PATH: [mod_path_one]
         },
         mod_two: {
             constants.MODULE_PATH: [mod_path_two]
         }
     }
     mock_load_module.return_value = ('mod_target', mod_info_dict)
     path_to_mod_info = {
         mod_path_one: [{
             constants.MODULE_NAME: mod_one,
             constants.MODULE_PATH: [mod_path_one]
         }],
         mod_path_two: [{
             constants.MODULE_NAME: mod_two,
             constants.MODULE_PATH: [mod_path_two]
         }]
     }
     mod_info = module_info.ModuleInfo()
     self.assertDictEqual(path_to_mod_info,
                          mod_info._get_path_to_module_info(mod_info_dict))
 def test_get_path(self):
     """Test that we get the module path when it's properly loaded."""
     # Load up the test json file and check that module is in it
     mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
     self.assertEqual(mod_info.get_paths(EXPECTED_MOD_TARGET),
                      EXPECTED_MOD_TARGET_PATH)
     self.assertEqual(mod_info.get_paths(MOD_NO_PATH), [])
def main(argv):
    """Entry point of atest script.

    Args:
        argv: A list of arguments.

    Returns:
        Exit code.
    """
    args = _parse_args(argv)
    _configure_logging(args.verbose)
    if _missing_environment_variables():
        return constants.EXIT_CODE_ENV_NOT_SETUP
    if args.generate_baseline and args.generate_new_metrics:
        logging.error(
            'Cannot collect both baseline and new metrics at the same time.')
        return constants.EXIT_CODE_ERROR
    if not _has_valid_regression_detection_args(args):
        return constants.EXIT_CODE_ERROR
    results_dir = make_test_run_dir()
    mod_info = module_info.ModuleInfo(force_build=args.rebuild_module_info)
    translator = cli_translator.CLITranslator(module_info=mod_info)
    build_targets = set()
    test_infos = set()
    if _will_run_tests(args):
        try:
            build_targets, test_infos = translator.translate(args.tests)
        except atest_error.TestDiscoveryException:
            logging.exception('Error occured in test discovery:')
            logging.info(
                'This can happen after a repo sync or if the test is '
                'new. Running: with "%s"  may resolve the issue.',
                REBUILD_MODULE_INFO_FLAG)
            return constants.EXIT_CODE_TEST_NOT_FOUND
    build_targets |= test_runner_handler.get_test_runner_reqs(
        mod_info, test_infos)
    extra_args = get_extra_args(args)
    if args.detect_regression:
        build_targets |= (regression_test_runner.RegressionTestRunner(
            '').get_test_runner_build_reqs())
    # args.steps will be None if none of -bit set, else list of params set.
    steps = args.steps if args.steps else ALL_STEPS
    if build_targets and BUILD_STEP in steps:
        # Add module-info.json target to the list of build targets to keep the
        # file up to date.
        build_targets.add(mod_info.module_info_target)
        success = atest_utils.build(build_targets, args.verbose)
        if not success:
            return constants.EXIT_CODE_BUILD_FAILURE
    elif TEST_STEP not in steps:
        logging.warn('Install step without test step currently not '
                     'supported, installing AND testing instead.')
        steps.append(TEST_STEP)
    if TEST_STEP in steps:
        test_runner_handler.run_all_tests(results_dir, test_infos, extra_args)
    if args.detect_regression:
        regression_args = _get_regression_detection_args(args, results_dir)
        regression_test_runner.RegressionTestRunner('').run_tests(
            None, regression_args)
    return constants.EXIT_CODE_SUCCESS
    def test_load_mode_info_file_out_dir_handling(self, _isfile, _open, _json):
        """Test _load_module_info_file out dir handling."""
        # Test out default out dir is used.
        build_top = '/path/to/top'
        default_out_dir = os.path.join(build_top, 'out/dir/here')
        os_environ_mock = {
            constants.ANDROID_OUT: default_out_dir,
            'ANDROID_PRODUCT_OUT': default_out_dir,
            constants.ANDROID_BUILD_TOP: build_top
        }
        default_out_dir_mod_targ = 'out/dir/here/module-info.json'
        # Make sure module_info_target is what we think it is.
        with mock.patch.dict('os.environ', os_environ_mock, clear=True):
            mod_info = module_info.ModuleInfo()
            self.assertEqual(default_out_dir_mod_targ,
                             mod_info.module_info_target)

        # Test out custom out dir is used (OUT_DIR=dir2).
        custom_out_dir = os.path.join(build_top, 'out2/dir/here')
        os_environ_mock = {
            constants.ANDROID_OUT: custom_out_dir,
            'OUT_DIR': 'out2',
            'ANDROID_PRODUCT_OUT': custom_out_dir,
            constants.ANDROID_BUILD_TOP: build_top
        }
        custom_out_dir_mod_targ = 'out2/dir/here/module-info.json'
        # Make sure module_info_target is what we think it is.
        with mock.patch.dict('os.environ', os_environ_mock, clear=True):
            mod_info = module_info.ModuleInfo()
            self.assertEqual(custom_out_dir_mod_targ,
                             mod_info.module_info_target)

        # Test out custom abs out dir is used (OUT_DIR=/tmp/out/dir2).
        abs_custom_out_dir = '/tmp/out/dir'
        os_environ_mock = {
            constants.ANDROID_OUT: abs_custom_out_dir,
            'OUT_DIR': abs_custom_out_dir,
            'ANDROID_PRODUCT_OUT': abs_custom_out_dir,
            constants.ANDROID_BUILD_TOP: build_top
        }
        custom_abs_out_dir_mod_targ = '/tmp/out/dir/module-info.json'
        # Make sure module_info_target is what we think it is.
        with mock.patch.dict('os.environ', os_environ_mock, clear=True):
            mod_info = module_info.ModuleInfo()
            self.assertEqual(custom_abs_out_dir_mod_targ,
                             mod_info.module_info_target)
 def test_get_module_names(self):
     """test that we get the module name properly."""
     mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
     self.assertEqual(
         mod_info.get_module_names(EXPECTED_MOD_TARGET_PATH[0]),
         [EXPECTED_MOD_TARGET])
     self.assertEqual(mod_info.get_module_names(PATH_TO_MULT_MODULES),
                      MULT_MOODULES_WITH_SHARED_PATH)
 def test_is_module(self):
     """Test that we get the module when it's properly loaded."""
     # Load up the test json file and check that module is in it
     mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
     self.assertTrue(mod_info.is_module(EXPECTED_MOD_TARGET))
     self.assertFalse(mod_info.is_module(UNEXPECTED_MOD_TARGET))