mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-18 16:44:42 +04:00
Consolidated file validation
This commit is contained in:
parent
9fa237fbe0
commit
28857c9a74
5 changed files with 149 additions and 74 deletions
63
tests/clientlib/validate_base_test.py
Normal file
63
tests/clientlib/validate_base_test.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
|
||||
import __builtin__
|
||||
|
||||
import os.path
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
|
||||
|
||||
class AdditionalValidatorError(ValueError): pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def noop_validator():
|
||||
return get_validator('example_manifest.yaml', {}, ValueError)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def array_validator():
|
||||
return get_validator('', {'type': 'array'}, ValueError)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def additional_validator():
|
||||
def raises_always(obj):
|
||||
raise AdditionalValidatorError
|
||||
|
||||
return get_validator(
|
||||
'example_manifest.yaml',
|
||||
{},
|
||||
ValueError,
|
||||
additional_validation_strategy=raises_always,
|
||||
)
|
||||
|
||||
|
||||
def test_raises_for_non_existent_file(noop_validator):
|
||||
with pytest.raises(ValueError):
|
||||
noop_validator('file_that_does_not_exist.yaml')
|
||||
|
||||
|
||||
def test_raises_for_invalid_yaml_file(noop_validator):
|
||||
with pytest.raises(ValueError):
|
||||
noop_validator('tests/data/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_manifest.yaml'), 'r',
|
||||
)
|
||||
|
||||
|
||||
def test_raises_for_failing_schema(array_validator):
|
||||
with pytest.raises(ValueError):
|
||||
array_validator('tests/data/non_parseable_yaml_file.yaml')
|
||||
|
||||
|
||||
def test_raises_when_additional_validation_fails(additional_validator):
|
||||
with pytest.raises(AdditionalValidatorError):
|
||||
additional_validator()
|
||||
Loading…
Add table
Add a link
Reference in a new issue