예제 #1
0
def test_classifier_normalizes_filenames_on_windows_to_forward_slashes(tmpdir):
    with tmpdir.as_cwd():
        tmpdir.join('a/b/c').ensure()
        with mock.patch.object(os, 'altsep', '/'):
            with mock.patch.object(os, 'sep', '\\'):
                classifier = Classifier.from_config((r'a\b\c',), '', '^$')
                assert classifier.filenames == ['a/b/c']
예제 #2
0
def check_useless_excludes(config_file: str) -> int:
    config = load_config(config_file)
    filenames = git.get_all_files()
    classifier = Classifier.from_config(
        filenames,
        config["files"],
        config["exclude"],
    )
    retv = 0

    exclude = config["exclude"]
    if not exclude_matches_any(filenames, "", exclude):
        print(
            f"The global exclude pattern {exclude!r} does not match any files",
        )
        retv = 1

    for repo in config["repos"]:
        for hook in repo["hooks"]:
            # Not actually a manifest dict, but this more accurately reflects
            # the defaults applied during runtime
            hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
            names = classifier.filenames
            types, exclude_types = hook["types"], hook["exclude_types"]
            names = classifier.by_types(names, types, exclude_types)
            include, exclude = hook["files"], hook["exclude"]
            if not exclude_matches_any(names, include, exclude):
                print(
                    f'The exclude pattern {exclude!r} for {hook["id"]} does '
                    f"not match any files", )
                retv = 1

    return retv
def check_all_hooks_match_files(config_file: str) -> int:
    config = load_config(config_file)
    classifier = Classifier.from_config(
        git.get_all_files(), config['files'], config['exclude'],
    )
    retv = 0

    for hook in all_hooks(config, Store()):
        if hook.always_run or hook.language == 'fail':
            continue
        elif not classifier.filenames_for_hook(hook):
            print(f'{hook.id} does not apply to this repository')
            retv = 1

    return retv
예제 #4
0
def check_useless_excludes(config_file: str) -> int:
    config = load_config(config_file)
    filenames = git.get_all_files()
    classifier = Classifier.from_config(
        filenames,
        config['files'],
        config['exclude'],
    )
    retv = 0

    exclude = config['exclude']
    if not exclude_matches_any(filenames, '', exclude):
        print(
            f'The global exclude pattern {exclude!r} does not match any files',
        )
        retv = 1

    for repo in config['repos']:
        for hook in repo['hooks']:
            # the default of manifest hooks is `types: [file]` but we may
            # be configuring a symlink hook while there's a broken symlink
            hook.setdefault('types', [])
            # Not actually a manifest dict, but this more accurately reflects
            # the defaults applied during runtime
            hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
            names = classifier.filenames
            types = hook['types']
            types_or = hook['types_or']
            exclude_types = hook['exclude_types']
            names = classifier.by_types(names, types, types_or, exclude_types)
            include, exclude = hook['files'], hook['exclude']
            if not exclude_matches_any(names, include, exclude):
                print(
                    f'The exclude pattern {exclude!r} for {hook["id"]} does '
                    f'not match any files', )
                retv = 1

    return retv
예제 #5
0
def test_classifier_does_not_normalize_backslashes_non_windows(tmpdir):
    with mock.patch.object(os.path, 'lexists', return_value=True):
        with mock.patch.object(os, 'altsep', None):
            with mock.patch.object(os, 'sep', '/'):
                classifier = Classifier.from_config((r'a/b\c',), '', '^$')
                assert classifier.filenames == [r'a/b\c']