def make_config_from_repo( repo_path, sha=None, hooks=None, check=True, legacy=False, ): filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE manifest = load_manifest(os.path.join(repo_path, filename)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']), )) for hook in manifest], ), )) if check: wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(wrapped_config) return wrapped_config[0] else: return config
def test_config_with_local_hooks_definition_fails(config_obj): with pytest.raises(( jsonschema.exceptions.ValidationError, InvalidConfigError )): jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA) config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA) validate_config_extra(config)
def mock_repo_config(): config = { "repo": "[email protected]:pre-commit/pre-commit-hooks", "sha": "5e713f8878b7d100c0e059f8cc34be4fc2e8f897", "hooks": [{"id": "pyflakes", "files": "\\.py$"}], } config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(config_wrapped) return config_wrapped[0]
def test_config_with_ok_regexes_passes(): config = apply_defaults( [{ 'repo': 'foo', 'sha': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\\.py$'}], }], CONFIG_JSON_SCHEMA, ) validate_config_extra(config)
def test_config_with_ok_exclude_regex_passes(): config = apply_defaults( [{ 'repo': 'foo', 'sha': 'foo', 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}], }], CONFIG_JSON_SCHEMA, ) validate_config_extra(config)
def test_config_with_invalid_exclude_regex_fails(): with pytest.raises(InvalidConfigError): # Note the regex '(' is invalid (unbalanced parens) config = apply_defaults( [{ 'repo': 'foo', 'sha': 'foo', 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '('}], }], CONFIG_JSON_SCHEMA, ) validate_config_extra(config)
def mock_repo_config(): config = { 'repo': '[email protected]:pre-commit/pre-commit-hooks', 'sha': '5e713f8878b7d100c0e059f8cc34be4fc2e8f897', 'hooks': [{ 'id': 'pyflakes', 'files': '\\.py$', }], } config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(config_wrapped) return config_wrapped[0]
def mock_repo_config(): config = { 'repo': '[email protected]:pre-commit/pre-commit-hooks', 'sha': '5e713f8878b7d100c0e059f8cc34be4fc2e8f897', 'hooks': [{ 'id': 'pyflakes', 'files': '\\.py$', }], } config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(config_wrapped) return config_wrapped[0]
def test_config_with_ok_regexes_passes(): config = apply_defaults( [{ 'repo': 'foo', 'sha': 'foo', 'hooks': [{ 'id': 'hook_id', 'files': '\\.py$' }], }], CONFIG_JSON_SCHEMA, ) validate_config_extra(config)
def test_config_with_ok_exclude_regex_passes(): config = apply_defaults( [{ 'repo': 'foo', 'sha': 'foo', 'hooks': [{ 'id': 'hook_id', 'files': '', 'exclude': '^vendor/' }], }], CONFIG_JSON_SCHEMA, ) validate_config_extra(config)
def test_config_with_failing_regexes_fails(): with pytest.raises(InvalidConfigError): # Note the regex '(' is invalid (unbalanced parens) config = apply_defaults( [{ 'repo': 'foo', 'sha': 'foo', 'hooks': [{ 'id': 'hook_id', 'files': '(' }], }], CONFIG_JSON_SCHEMA, ) validate_config_extra(config)
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(wrapped_config) return wrapped_config[0] else: return config
def test_config_with_local_hooks_definition_passes(config_obj): jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA) config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA) validate_config_extra(config)
def test_config_with_local_hooks_definition_passes(config_obj): jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA) config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA) validate_config_extra(config)
def test_config_with_local_hooks_definition_fails(config_obj): with pytest.raises( (jsonschema.exceptions.ValidationError, InvalidConfigError)): jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA) config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA) validate_config_extra(config)