def test_conditional_recurse_apply_defaults(tvalue): val = {'t': tvalue, 'v': {}} ret = apply_defaults(val, conditional_recurse) assert ret == {'t': tvalue, 'v': {'k': tvalue}} val = {'t': tvalue, 'v': {'k': not tvalue}} ret = apply_defaults(val, conditional_recurse) assert ret == {'t': tvalue, 'v': {'k': not tvalue}}
def _get_hook_no_install(repo_config, store, hook_id): config = {'repos': [repo_config]} config = cfgv.validate(config, CONFIG_SCHEMA) config = cfgv.apply_defaults(config, CONFIG_SCHEMA) hooks = all_hooks(config, store) hook, = [hook for hook in hooks if hook.id == hook_id] return hook
def test_meta_check_hooks_apply_only_at_top_level(): cfg = {'id': 'check-hooks-apply'} cfg = cfgv.apply_defaults(cfg, META_HOOK_DICT) files_re = re.compile(cfg['files']) assert files_re.search('.pre-commit-config.yaml') assert not files_re.search('foo/.pre-commit-config.yaml')
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_useless_excludes(config_file): config = load_config(config_file) files = git.get_all_files() retv = 0 exclude = config['exclude'] if not exclude_matches_any(files, '', 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) include, exclude = hook['files'], hook['exclude'] if not exclude_matches_any(files, include, exclude): print( 'The exclude pattern {!r} for {} does not match any files'. format(exclude, hook['id']), ) 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
def from_yaml(cls, dct: Dict[str, Any]) -> 'GenerateOptions': dct = cfgv.apply_defaults(cfgv.validate(dct, SCHEMA), SCHEMA) return cls( skip_default_metrics=dct['skip_default_metrics'], metric_package_names=dct['metric_package_names'], repo=dct['repo'], database=dct['database'], exclude=re.compile(dct['exclude'].encode()), )
def make_config_from_repo(repo_path, rev=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = { 'repo': 'file://{}'.format(repo_path), 'rev': rev or git.head_rev(repo_path), 'hooks': hooks or [{'id': hook['id']} for hook in manifest], } if check: wrapped = validate({'repos': [config]}, CONFIG_SCHEMA) wrapped = apply_defaults(wrapped, CONFIG_SCHEMA) config, = wrapped['repos'] return config else: return config
def test_apply_defaults(): ret = cfgv.apply_defaults({'repos': []}, SCHEMA) assert ret == { 'ci': { 'autofix_commit_msg': ( '[pre-commit.ci] auto fixes from pre-commit.com hooks\n\n' 'for more information, see https://pre-commit.ci\n' ), 'autofix_prs': True, 'autoupdate_commit_msg': '[pre-commit.ci] pre-commit autoupdate', 'autoupdate_schedule': 'weekly', 'skip': [], 'submodules': False, }, 'repos': [], }
def make_config_from_repo(repo_path, rev=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = OrderedDict(( ('repo', 'file://{}'.format(repo_path)), ('rev', rev or git.head_rev(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped = validate({'repos': [config]}, CONFIG_SCHEMA) wrapped = apply_defaults(wrapped, CONFIG_SCHEMA) config, = wrapped['repos'] return config else: return config
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
def test_apply_defaults_optional_recurse_missing(): ret = apply_defaults({}, optional_nested_schema) assert ret == {'links': []}
def test_apply_defaults_nested(): val = {'repo': 'repo1', 'hooks': [{}]} ret = apply_defaults(val, nested_schema_optional) assert ret == {'repo': 'repo1', 'hooks': [{'key': False}]}
def test_apply_defaults_map_in_list(): ret = apply_defaults([{}], Array(map_optional)) assert ret == [{'key': False}]
def test_apply_defaults_does_nothing_on_non_optional(): ret = apply_defaults({}, map_required) assert ret == {}
def test_apply_defaults_optional_recurse_already_present(): ret = apply_defaults({'links': [{'key': True}]}, optional_nested_schema) assert ret == {'links': [{'key': True}]}
def test_optional_optional_apply_defaults(): ret = apply_defaults({}, optional_nested_optional_schema) assert ret == {'builder': {'noop': True}}
def test_apply_defaults_copies_object(): val = {} ret = apply_defaults(val, map_optional) assert ret is not val
def test_conditional_optional_apply_default(tvalue): ret = apply_defaults({'t': tvalue}, conditional_optional) assert ret == {'t': tvalue, 'v': tvalue}
def test_apply_defaults_sets_default(): ret = apply_defaults({}, map_optional) assert ret == {'key': False}
def _hook_from_manifest_dct(dct): dct = validate(apply_defaults(dct, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT) dct = _hook(dct) return dct
def test_apply_defaults_does_not_change_non_default(): ret = apply_defaults({'key': True}, map_optional) assert ret == {'key': True}