mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 20:40:08 -06:00
Remove defaulting behavior of validate_*. It was complicated and unnecessary
This commit is contained in:
@@ -4,11 +4,8 @@ import jsonschema.exceptions
|
||||
import os.path
|
||||
import yaml
|
||||
|
||||
from pre_commit import git
|
||||
|
||||
|
||||
def get_validator(
|
||||
default_filename,
|
||||
json_schema,
|
||||
exception_type,
|
||||
additional_validation_strategy=lambda obj: None,
|
||||
@@ -16,17 +13,13 @@ def get_validator(
|
||||
"""Returns a function which will validate a yaml file for correctness
|
||||
|
||||
Args:
|
||||
default_filename - Default filename to look for if none is specified
|
||||
json_schema - JSON schema to validate file with
|
||||
exception_type - Error type to raise on failure
|
||||
additional_validation_strategy - Strategy for additional validation of
|
||||
the object read from the file. The function should either raise
|
||||
exception_type on failure.
|
||||
"""
|
||||
|
||||
def validate(filename=None, load_strategy=yaml.load):
|
||||
filename = filename or os.path.join(git.get_root(), default_filename)
|
||||
|
||||
def validate(filename, load_strategy=yaml.load):
|
||||
if not os.path.exists(filename):
|
||||
raise exception_type('File {0} does not exist'.format(filename))
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import argparse
|
||||
import re
|
||||
import sys
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
from pre_commit.util import entry
|
||||
|
||||
@@ -58,7 +57,6 @@ def validate_config_extra(config):
|
||||
|
||||
|
||||
load_config = get_validator(
|
||||
C.CONFIG_FILE,
|
||||
CONFIG_JSON_SCHEMA,
|
||||
InvalidConfigError,
|
||||
additional_validation_strategy=validate_config_extra,
|
||||
@@ -68,19 +66,11 @@ load_config = get_validator(
|
||||
@entry
|
||||
def run(argv):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'filenames',
|
||||
nargs='*', default=None,
|
||||
help='Config filenames. Defaults to {0} at root of git repo'.format(
|
||||
C.CONFIG_FILE,
|
||||
)
|
||||
)
|
||||
parser.add_argument('filenames', nargs='*', help='Config filenames.')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
filenames = args.filenames or [C.CONFIG_FILE]
|
||||
retval = 0
|
||||
|
||||
for filename in filenames:
|
||||
for filename in args.filenames:
|
||||
try:
|
||||
load_config(filename)
|
||||
except InvalidConfigError as e:
|
||||
|
||||
@@ -4,7 +4,6 @@ from __future__ import print_function
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
from pre_commit.languages.all import all_languages
|
||||
from pre_commit.util import entry
|
||||
@@ -48,7 +47,6 @@ def additional_manifest_check(obj):
|
||||
|
||||
|
||||
load_manifest = get_validator(
|
||||
C.MANIFEST_FILE,
|
||||
MANIFEST_JSON_SCHEMA,
|
||||
InvalidManifestError,
|
||||
additional_manifest_check,
|
||||
@@ -58,19 +56,11 @@ load_manifest = get_validator(
|
||||
@entry
|
||||
def run(argv):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'filenames',
|
||||
nargs='*', default=None,
|
||||
help='Manifest filenames. Defaults to {0} at root of git repo'.format(
|
||||
C.MANIFEST_FILE,
|
||||
)
|
||||
)
|
||||
parser.add_argument('filenames', nargs='*', help='Manifest filenames.')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
filenames = args.filenames or [C.MANIFEST_FILE]
|
||||
retval = 0
|
||||
|
||||
for filename in filenames:
|
||||
for filename in args.filenames:
|
||||
try:
|
||||
load_manifest(filename)
|
||||
except InvalidManifestError as e:
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
|
||||
import __builtin__
|
||||
|
||||
import os.path
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
from pre_commit.ordereddict import OrderedDict
|
||||
from pre_commit.yaml_extensions import ordered_load
|
||||
@@ -17,12 +12,12 @@ class AdditionalValidatorError(ValueError): pass
|
||||
|
||||
@pytest.fixture
|
||||
def noop_validator():
|
||||
return get_validator('example_hooks.yaml', {}, ValueError)
|
||||
return get_validator({}, ValueError)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def array_validator():
|
||||
return get_validator('', {'type': 'array'}, ValueError)
|
||||
return get_validator({'type': 'array'}, ValueError)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -31,7 +26,6 @@ def additional_validator():
|
||||
raise AdditionalValidatorError
|
||||
|
||||
return get_validator(
|
||||
'example_hooks.yaml',
|
||||
{},
|
||||
ValueError,
|
||||
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'))
|
||||
|
||||
|
||||
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):
|
||||
with pytest.raises(ValueError):
|
||||
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):
|
||||
with pytest.raises(AdditionalValidatorError):
|
||||
additional_validator()
|
||||
additional_validator(get_resource_path('array_yaml_file.yaml'))
|
||||
|
||||
|
||||
def test_returns_object_after_validating(noop_validator):
|
||||
|
||||
Reference in New Issue
Block a user