Example #1
0
def _config_file_path(style_type="pylint"):
    cli_repo_path = get_azdev_config().get("cli", "repo_path")

    ext_repo_path = filter(
        lambda x: "azure-cli-extension" in x,
        get_azdev_config().get("ext", "repo_paths").split(),
    )
    try:
        ext_repo_path = next(ext_repo_path)
    except StopIteration:
        ext_repo_path = []

    if style_type not in ["pylint", "flake8"]:
        raise ValueError("style_tyle value allows only: pylint, flake8.")

    config_file_mapping = {
        "pylint": "pylintrc",
        "flake8": ".flake8",
    }
    default_config_file_mapping = {
        "cli": {
            "pylint": "cli_pylintrc",
            "flake8": "cli.flake8"
        },
        "ext": {
            "pylint": "ext_pylintrc",
            "flake8": "ext.flake8"
        }
    }

    if cli_repo_path:
        cli_config_path = os.path.join(cli_repo_path,
                                       config_file_mapping[style_type])
    else:
        cli_config_path = os.path.join(
            get_azdev_config_dir(),
            "config_files",
            default_config_file_mapping["cli"][style_type],
        )

    if ext_repo_path:
        ext_config_path = os.path.join(ext_repo_path,
                                       config_file_mapping[style_type])
    else:
        ext_config_path = os.path.join(
            get_azdev_config_dir(),
            "config_files",
            default_config_file_mapping["ext"][style_type],
        )

    return cli_config_path, ext_config_path
Example #2
0
def main():
    try:
        azdev = AzDevCli(cli_name='azdev', commands_loader_cls=AzDevCommandsLoader,
                         config_dir=get_azdev_config_dir())
        exit_code = azdev.invoke(sys.argv[1:])
        sys.exit(exit_code)
    except KeyboardInterrupt:
        sys.exit(1)
Example #3
0
 def run(paths, config_file, desc):
     if not paths:
         return None
     config_path = os.path.join(get_azdev_config_dir(), 'config_files',
                                config_file)
     logger.info('Using config file: %s', config_path)
     logger.info('Running on %s: %s', desc, ' '.join(paths))
     command = 'flake8 --statistics --append-config={} {}'.format(
         config_path, ' '.join(paths))
     return py_cmd(command, message='Running flake8 on {}...'.format(desc))
Example #4
0
 def run(paths, rcfile, desc):
     if not paths:
         return None
     config_path = os.path.join(get_azdev_config_dir(), 'config_files',
                                rcfile)
     logger.info('Using rcfile file: %s', config_path)
     logger.info('Running on %s: %s', desc, ' '.join(paths))
     command = 'pylint {} --ignore vendored_sdks,privates --rcfile={} -j {}'.format(
         ' '.join(paths), config_path, multiprocessing.cpu_count())
     return py_cmd(command, message='Running pylint on {}...'.format(desc))
Example #5
0
def _copy_config_files():
    from glob import glob
    from importlib import import_module

    config_mod = import_module('azdev.config')
    config_dir_path = config_mod.__dict__['__path__'][0]
    dest_path = os.path.join(get_azdev_config_dir(), 'config_files')
    if os.path.exists(dest_path):
        rmtree(dest_path)
    copytree(config_dir_path, dest_path)
    # remove the python __init__ files
    pattern = os.path.join(dest_path, '*.py*')
    for path in glob(pattern):
        os.remove(path)
Example #6
0
def _get_test_index(profile, discover):
    config_dir = get_azdev_config_dir()
    test_index_dir = os.path.join(config_dir, 'test_index')
    make_dirs(test_index_dir)
    test_index_path = os.path.join(test_index_dir, '{}.json'.format(profile))
    test_index = {}
    if discover:
        test_index = _discover_tests(profile)
        with open(test_index_path, 'w') as f:
            f.write(json.dumps(test_index))
        display('\ntest index updated: {}'.format(test_index_path))
    elif os.path.isfile(test_index_path):
        with open(test_index_path, 'r') as f:
            test_index = json.loads(''.join(f.readlines()))
        display('\ntest index found: {}'.format(test_index_path))
    else:
        test_index = _discover_tests(profile)
        with open(test_index_path, 'w') as f:
            f.write(json.dumps(test_index))
        display('\ntest index created: {}'.format(test_index_path))
    return test_index
Example #7
0
def run_tests(tests, xml_path=None, discover=False, in_series=False,
              run_live=False, profile=None, last_failed=False, pytest_args=None,
              git_source=None, git_target=None, git_repo=None):

    require_virtual_env()

    DEFAULT_RESULT_FILE = 'test_results.xml'
    DEFAULT_RESULT_PATH = os.path.join(get_azdev_config_dir(), DEFAULT_RESULT_FILE)

    from .pytest_runner import get_test_runner

    heading('Run Tests')

    original_profile = _get_profile(profile)
    if not profile:
        profile = original_profile
    path_table = get_path_table()
    test_index = _get_test_index(profile, discover)
    if not tests:
        tests = list(path_table['mod'].keys()) + list(path_table['core'].keys()) + list(path_table['ext'].keys())
    if tests == ['CLI']:
        tests = list(path_table['mod'].keys()) + list(path_table['core'].keys())
    elif tests == ['EXT']:
        tests = list(path_table['ext'].keys())

    # filter out tests whose modules haven't changed
    tests = _filter_by_git_diff(tests, test_index, git_source, git_target, git_repo)

    if tests:
        display('\nTESTS: {}\n'.format(', '.join(tests)))

    # resolve the path at which to dump the XML results
    xml_path = xml_path or DEFAULT_RESULT_PATH
    if not xml_path.endswith('.xml'):
        xml_path = os.path.join(xml_path, DEFAULT_RESULT_FILE)

    # process environment variables
    if run_live:
        logger.warning('RUNNING TESTS LIVE')
        os.environ[ENV_VAR_TEST_LIVE] = 'True'

    def _find_test(index, name):
        name_comps = name.split('.')
        num_comps = len(name_comps)
        key_error = KeyError()
        for i in range(num_comps):
            check_name = '.'.join(name_comps[(-1 - i):])
            try:
                match = index[check_name]
                if check_name != name:
                    logger.info("Test found using just '%s'. The rest of the name was ignored.\n", check_name)
                return match
            except KeyError as ex:
                key_error = ex
                continue
        raise key_error

    # lookup test paths from index
    test_paths = []
    for t in tests:
        try:
            test_path = os.path.normpath(_find_test(test_index, t))
            test_paths.append(test_path)
        except KeyError:
            logger.warning("'%s' not found. If newly added, re-run with --discover", t)
            continue

    # Tests have been collected. Now run them.
    if not test_paths:
        raise CLIError('No tests selected to run.')

    runner = get_test_runner(parallel=not in_series, log_path=xml_path, last_failed=last_failed)
    exit_code = runner(test_paths=test_paths, pytest_args=pytest_args)
    _summarize_test_results(xml_path)

    # attempt to restore the original profile
    if profile != original_profile:
        result = raw_cmd('az cloud update --profile {}'.format(original_profile),
                         "Restoring profile '{}'.".format(original_profile))
        if result.exit_code != 0:
            logger.warning("Failed to restore profile '%s'.", original_profile)

    sys.exit(0 if not exit_code else 1)
def run_tests(tests,
              xml_path=None,
              discover=False,
              in_series=False,
              run_live=False,
              profile=None,
              last_failed=False,
              pytest_args=None,
              no_exit_first=False,
              git_source=None,
              git_target=None,
              git_repo=None,
              cli_ci=False):

    require_virtual_env()

    DEFAULT_RESULT_FILE = 'test_results.xml'
    DEFAULT_RESULT_PATH = os.path.join(get_azdev_config_dir(),
                                       DEFAULT_RESULT_FILE)

    heading('Run Tests')

    path_table = get_path_table()

    test_index = _get_test_index(profile or current_profile(), discover)

    if not tests:
        tests = list(path_table['mod'].keys()) + list(
            path_table['core'].keys()) + list(path_table['ext'].keys())
    if tests == ['CLI']:
        tests = list(path_table['mod'].keys()) + list(
            path_table['core'].keys())
    elif tests == ['EXT']:
        tests = list(path_table['ext'].keys())

    # filter out tests whose modules haven't changed
    modified_mods = _filter_by_git_diff(tests, test_index, git_source,
                                        git_target, git_repo)
    if modified_mods:
        display('\nTest on modules: {}\n'.format(', '.join(modified_mods)))

    if cli_ci is True:
        ctx = CLIAzureDevOpsContext(git_repo, git_source, git_target)
        modified_mods = ctx.filter(test_index)

    # resolve the path at which to dump the XML results
    xml_path = xml_path or DEFAULT_RESULT_PATH
    if not xml_path.endswith('.xml'):
        xml_path = os.path.join(xml_path, DEFAULT_RESULT_FILE)

    # process environment variables
    if run_live:
        logger.warning('RUNNING TESTS LIVE')
        os.environ[ENV_VAR_TEST_LIVE] = 'True'

    def _find_test(index, name):
        name_comps = name.split('.')
        num_comps = len(name_comps)
        key_error = KeyError()

        for i in range(num_comps):
            check_name = '.'.join(name_comps[(-1 - i):])
            try:
                match = index[check_name]
                if check_name != name:
                    logger.info(
                        "Test found using just '%s'. The rest of the name was ignored.\n",
                        check_name)
                return match
            except KeyError as ex:
                key_error = ex
                continue
        raise key_error

    # lookup test paths from index
    test_paths = []
    for t in modified_mods:
        try:
            test_path = os.path.normpath(_find_test(test_index, t))
            test_paths.append(test_path)
        except KeyError:
            logger.warning(
                "'%s' not found. If newly added, re-run with --discover", t)
            continue

    exit_code = 0

    # Tests have been collected. Now run them.
    if not test_paths:
        logger.warning('No tests selected to run.')
        sys.exit(exit_code)

    exit_code = 0
    with ProfileContext(profile):
        runner = get_test_runner(parallel=not in_series,
                                 log_path=xml_path,
                                 last_failed=last_failed,
                                 no_exit_first=no_exit_first)
        exit_code = runner(test_paths=test_paths, pytest_args=pytest_args)

    sys.exit(0 if not exit_code else 1)