예제 #1
0
def check_useless_excludes(config_file: str) -> int:
    config = load_config(config_file)
    classifier = Classifier(git.get_all_files())
    retv = 0

    exclude = config['exclude']
    if not exclude_matches_any(classifier.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_useless_excludes(config_file):
    config = load_config(config_file)
    classifier = Classifier(git.get_all_files())
    retv = 0

    exclude = config['exclude']
    if not exclude_matches_any(classifier.filenames, '', exclude):
        print(
            'The global exclude pattern {!r} does not match any files'
            .format(exclude),
        )
        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(
                    'The exclude pattern {!r} for {} does not match any files'
                    .format(exclude, hook['id']),
                )
                retv = 1

    return retv
예제 #3
0
def check_all_hooks_match_files(config_file: str) -> int:
    classifier = Classifier(git.get_all_files())
    retv = 0

    for hook in all_hooks(load_config(config_file), 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_all_hooks_match_files(config_file):
    classifier = Classifier(git.get_all_files())
    retv = 0

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

    return retv
예제 #5
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']
예제 #6
0
def test_classifier_empty_types_or(tmpdir):
    tmpdir.join('bar').ensure()
    os.symlink(tmpdir.join('bar'), tmpdir.join('foo'))
    with tmpdir.as_cwd():
        classifier = Classifier(('foo', 'bar'))
        for_symlink = classifier.by_types(
            classifier.filenames,
            types=['symlink'],
            types_or=[],
            exclude_types=[],
        )
        for_file = classifier.by_types(
            classifier.filenames,
            types=['file'],
            types_or=[],
            exclude_types=[],
        )
        assert for_symlink == ['foo']
        assert for_file == ['bar']
예제 #7
0
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
예제 #8
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
예제 #9
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']
예제 #10
0
def test_classifier_removes_dne():
    classifier = Classifier(('this_file_does_not_exist',))
    assert classifier.filenames == []