mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Add checking for manifest files regex.
This commit is contained in:
@@ -4,12 +4,21 @@ import argparse
|
||||
import jsonschema
|
||||
import jsonschema.exceptions
|
||||
import os.path
|
||||
import re
|
||||
import yaml
|
||||
|
||||
from pre_commit.jsonschema_extensions import apply_defaults
|
||||
from pre_commit.util import entry
|
||||
|
||||
|
||||
def is_regex_valid(regex):
|
||||
try:
|
||||
re.compile(regex)
|
||||
return True
|
||||
except re.error:
|
||||
return False
|
||||
|
||||
|
||||
def get_validator(
|
||||
json_schema,
|
||||
exception_type,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import re
|
||||
import sys
|
||||
|
||||
from pre_commit.clientlib.validate_base import get_run_function
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
from pre_commit.clientlib.validate_base import is_regex_valid
|
||||
|
||||
|
||||
class InvalidConfigError(ValueError):
|
||||
@@ -42,9 +42,7 @@ CONFIG_JSON_SCHEMA = {
|
||||
|
||||
|
||||
def try_regex(repo, hook, value, field_name):
|
||||
try:
|
||||
re.compile(value)
|
||||
except re.error:
|
||||
if not is_regex_valid(value):
|
||||
raise InvalidConfigError(
|
||||
'Invalid {0} regex at {1}, {2}: {3}'.format(
|
||||
field_name, repo, hook, value,
|
||||
|
||||
@@ -2,6 +2,7 @@ import sys
|
||||
|
||||
from pre_commit.clientlib.validate_base import get_run_function
|
||||
from pre_commit.clientlib.validate_base import get_validator
|
||||
from pre_commit.clientlib.validate_base import is_regex_valid
|
||||
from pre_commit.languages.all import all_languages
|
||||
|
||||
|
||||
@@ -29,18 +30,31 @@ MANIFEST_JSON_SCHEMA = {
|
||||
}
|
||||
|
||||
|
||||
def validate_languages(hook_config):
|
||||
if hook_config['language'] not in all_languages:
|
||||
raise InvalidManifestError(
|
||||
'Expected language {0} for {1} to be one of {2!r}'.format(
|
||||
hook_config['id'],
|
||||
hook_config['language'],
|
||||
all_languages,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def validate_files(hook_config):
|
||||
if not is_regex_valid(hook_config['files']):
|
||||
raise InvalidManifestError(
|
||||
'Invalid files regex at {0}: {1}'.format(
|
||||
hook_config['id'],
|
||||
hook_config['files'],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def additional_manifest_check(obj):
|
||||
for hook_config in obj:
|
||||
language = hook_config['language']
|
||||
|
||||
if language not in all_languages:
|
||||
raise InvalidManifestError(
|
||||
'Expected language {0} for {1} to be one of {2!r}'.format(
|
||||
hook_config['id'],
|
||||
hook_config['language'],
|
||||
all_languages,
|
||||
)
|
||||
)
|
||||
validate_languages(hook_config)
|
||||
validate_files(hook_config)
|
||||
|
||||
|
||||
load_manifest = get_validator(
|
||||
|
||||
@@ -28,20 +28,25 @@ def test_additional_manifest_check_raises_for_bad_language():
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'obj', ([{'language': 'python'}], [{'language': 'ruby'}]),
|
||||
'obj',
|
||||
(
|
||||
[{'language': 'python', 'files': ''}],
|
||||
[{'language': 'ruby', 'files': ''}]
|
||||
),
|
||||
)
|
||||
def test_additional_manifest_check_languages(obj):
|
||||
def test_additional_manifest_check_passing(obj):
|
||||
additional_manifest_check(obj)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'obj',
|
||||
(
|
||||
[{'id': 'a', 'language': 'not a language'}],
|
||||
[{'id': 'a', 'language': 'python3'}],
|
||||
[{'id': 'a', 'language': 'not a language', 'files': ''}],
|
||||
[{'id': 'a', 'language': 'python3', 'files': ''}],
|
||||
[{'id': 'a', 'language': 'python', 'files': 'invalid regex('}],
|
||||
),
|
||||
)
|
||||
def test_additional_manifest_check_languages_failing(obj):
|
||||
def test_additional_manifest_failing(obj):
|
||||
with pytest.raises(InvalidManifestError):
|
||||
additional_manifest_check(obj)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user