mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 09:04:41 +04:00
Merge pull request #54 from pre-commit/remove_validator_defaulting
Remove defaulting behavior of validate_*. It was complicated and unnecessary
This commit is contained in:
commit
770e48a8f7
4 changed files with 8 additions and 49 deletions
|
|
@ -4,11 +4,8 @@ import jsonschema.exceptions
|
||||||
import os.path
|
import os.path
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from pre_commit import git
|
|
||||||
|
|
||||||
|
|
||||||
def get_validator(
|
def get_validator(
|
||||||
default_filename,
|
|
||||||
json_schema,
|
json_schema,
|
||||||
exception_type,
|
exception_type,
|
||||||
additional_validation_strategy=lambda obj: None,
|
additional_validation_strategy=lambda obj: None,
|
||||||
|
|
@ -16,17 +13,13 @@ def get_validator(
|
||||||
"""Returns a function which will validate a yaml file for correctness
|
"""Returns a function which will validate a yaml file for correctness
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
default_filename - Default filename to look for if none is specified
|
|
||||||
json_schema - JSON schema to validate file with
|
json_schema - JSON schema to validate file with
|
||||||
exception_type - Error type to raise on failure
|
exception_type - Error type to raise on failure
|
||||||
additional_validation_strategy - Strategy for additional validation of
|
additional_validation_strategy - Strategy for additional validation of
|
||||||
the object read from the file. The function should either raise
|
the object read from the file. The function should either raise
|
||||||
exception_type on failure.
|
exception_type on failure.
|
||||||
"""
|
"""
|
||||||
|
def validate(filename, load_strategy=yaml.load):
|
||||||
def validate(filename=None, load_strategy=yaml.load):
|
|
||||||
filename = filename or os.path.join(git.get_root(), default_filename)
|
|
||||||
|
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
raise exception_type('File {0} does not exist'.format(filename))
|
raise exception_type('File {0} does not exist'.format(filename))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import argparse
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pre_commit.constants as C
|
|
||||||
from pre_commit.clientlib.validate_base import get_validator
|
from pre_commit.clientlib.validate_base import get_validator
|
||||||
from pre_commit.util import entry
|
from pre_commit.util import entry
|
||||||
|
|
||||||
|
|
@ -58,7 +57,6 @@ def validate_config_extra(config):
|
||||||
|
|
||||||
|
|
||||||
load_config = get_validator(
|
load_config = get_validator(
|
||||||
C.CONFIG_FILE,
|
|
||||||
CONFIG_JSON_SCHEMA,
|
CONFIG_JSON_SCHEMA,
|
||||||
InvalidConfigError,
|
InvalidConfigError,
|
||||||
additional_validation_strategy=validate_config_extra,
|
additional_validation_strategy=validate_config_extra,
|
||||||
|
|
@ -68,19 +66,11 @@ load_config = get_validator(
|
||||||
@entry
|
@entry
|
||||||
def run(argv):
|
def run(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument('filenames', nargs='*', help='Config filenames.')
|
||||||
'filenames',
|
|
||||||
nargs='*', default=None,
|
|
||||||
help='Config filenames. Defaults to {0} at root of git repo'.format(
|
|
||||||
C.CONFIG_FILE,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
filenames = args.filenames or [C.CONFIG_FILE]
|
|
||||||
retval = 0
|
retval = 0
|
||||||
|
for filename in args.filenames:
|
||||||
for filename in filenames:
|
|
||||||
try:
|
try:
|
||||||
load_config(filename)
|
load_config(filename)
|
||||||
except InvalidConfigError as e:
|
except InvalidConfigError as e:
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ from __future__ import print_function
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pre_commit.constants as C
|
|
||||||
from pre_commit.clientlib.validate_base import get_validator
|
from pre_commit.clientlib.validate_base import get_validator
|
||||||
from pre_commit.languages.all import all_languages
|
from pre_commit.languages.all import all_languages
|
||||||
from pre_commit.util import entry
|
from pre_commit.util import entry
|
||||||
|
|
@ -48,7 +47,6 @@ def additional_manifest_check(obj):
|
||||||
|
|
||||||
|
|
||||||
load_manifest = get_validator(
|
load_manifest = get_validator(
|
||||||
C.MANIFEST_FILE,
|
|
||||||
MANIFEST_JSON_SCHEMA,
|
MANIFEST_JSON_SCHEMA,
|
||||||
InvalidManifestError,
|
InvalidManifestError,
|
||||||
additional_manifest_check,
|
additional_manifest_check,
|
||||||
|
|
@ -58,19 +56,11 @@ load_manifest = get_validator(
|
||||||
@entry
|
@entry
|
||||||
def run(argv):
|
def run(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument('filenames', nargs='*', help='Manifest filenames.')
|
||||||
'filenames',
|
|
||||||
nargs='*', default=None,
|
|
||||||
help='Manifest filenames. Defaults to {0} at root of git repo'.format(
|
|
||||||
C.MANIFEST_FILE,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
filenames = args.filenames or [C.MANIFEST_FILE]
|
|
||||||
retval = 0
|
retval = 0
|
||||||
|
for filename in args.filenames:
|
||||||
for filename in filenames:
|
|
||||||
try:
|
try:
|
||||||
load_manifest(filename)
|
load_manifest(filename)
|
||||||
except InvalidManifestError as e:
|
except InvalidManifestError as e:
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
|
|
||||||
import __builtin__
|
|
||||||
|
|
||||||
import os.path
|
|
||||||
import mock
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit import git
|
|
||||||
from pre_commit.clientlib.validate_base import get_validator
|
from pre_commit.clientlib.validate_base import get_validator
|
||||||
from pre_commit.ordereddict import OrderedDict
|
from pre_commit.ordereddict import OrderedDict
|
||||||
from pre_commit.yaml_extensions import ordered_load
|
from pre_commit.yaml_extensions import ordered_load
|
||||||
|
|
@ -17,12 +12,12 @@ class AdditionalValidatorError(ValueError): pass
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def noop_validator():
|
def noop_validator():
|
||||||
return get_validator('example_hooks.yaml', {}, ValueError)
|
return get_validator({}, ValueError)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def array_validator():
|
def array_validator():
|
||||||
return get_validator('', {'type': 'array'}, ValueError)
|
return get_validator({'type': 'array'}, ValueError)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
@ -31,7 +26,6 @@ def additional_validator():
|
||||||
raise AdditionalValidatorError
|
raise AdditionalValidatorError
|
||||||
|
|
||||||
return get_validator(
|
return get_validator(
|
||||||
'example_hooks.yaml',
|
|
||||||
{},
|
{},
|
||||||
ValueError,
|
ValueError,
|
||||||
additional_validation_strategy=raises_always,
|
additional_validation_strategy=raises_always,
|
||||||
|
|
@ -48,14 +42,6 @@ def test_raises_for_invalid_yaml_file(noop_validator):
|
||||||
noop_validator(get_resource_path('non_parseable_yaml_file.yaml'))
|
noop_validator(get_resource_path('non_parseable_yaml_file.yaml'))
|
||||||
|
|
||||||
|
|
||||||
def test_defaults_to_backup_filename(noop_validator):
|
|
||||||
with mock.patch.object(__builtin__, 'open', side_effect=open) as mock_open:
|
|
||||||
noop_validator()
|
|
||||||
mock_open.assert_called_once_with(
|
|
||||||
os.path.join(git.get_root(), 'example_hooks.yaml'), 'r',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_raises_for_failing_schema(array_validator):
|
def test_raises_for_failing_schema(array_validator):
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
array_validator(get_resource_path('valid_yaml_but_invalid_manifest.yaml'))
|
array_validator(get_resource_path('valid_yaml_but_invalid_manifest.yaml'))
|
||||||
|
|
@ -67,7 +53,7 @@ def test_passes_array_schema(array_validator):
|
||||||
|
|
||||||
def test_raises_when_additional_validation_fails(additional_validator):
|
def test_raises_when_additional_validation_fails(additional_validator):
|
||||||
with pytest.raises(AdditionalValidatorError):
|
with pytest.raises(AdditionalValidatorError):
|
||||||
additional_validator()
|
additional_validator(get_resource_path('array_yaml_file.yaml'))
|
||||||
|
|
||||||
|
|
||||||
def test_returns_object_after_validating(noop_validator):
|
def test_returns_object_after_validating(noop_validator):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue