mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +00:00
Add an --allow-multiple-documents option to check-yaml
This commit is contained in:
parent
e09278e806
commit
e87b81afd9
3 changed files with 32 additions and 1 deletions
|
|
@ -43,6 +43,8 @@ Add this to your `.pre-commit-config.yaml`
|
|||
- `check-vcs-permalinks` - Ensures that links to vcs websites are permalinks.
|
||||
- `check-xml` - Attempts to load all xml files to verify syntax.
|
||||
- `check-yaml` - Attempts to load all yaml files to verify syntax.
|
||||
- `--allow-multiple-documents` - allow yaml files which use the
|
||||
[multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML)
|
||||
- `debug-statements` - Check for pdb / ipdb / pudb statements in code.
|
||||
- `detect-aws-credentials` - Checks for the existence of AWS secrets that you
|
||||
have set up with the AWS CLI.
|
||||
|
|
|
|||
|
|
@ -11,15 +11,24 @@ except ImportError: # pragma: no cover (no libyaml-dev / pypy)
|
|||
Loader = yaml.SafeLoader
|
||||
|
||||
|
||||
def _load_all(*args, **kwargs):
|
||||
# need to exhaust the generator
|
||||
return tuple(yaml.load_all(*args, **kwargs))
|
||||
|
||||
|
||||
def check_yaml(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'-m', '--allow-multiple-documents', dest='yaml_load_fn',
|
||||
action='store_const', const=_load_all, default=yaml.load,
|
||||
)
|
||||
parser.add_argument('filenames', nargs='*', help='Yaml filenames to check.')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retval = 0
|
||||
for filename in args.filenames:
|
||||
try:
|
||||
yaml.load(open(filename), Loader=Loader)
|
||||
args.yaml_load_fn(open(filename), Loader=Loader)
|
||||
except yaml.YAMLError as exc:
|
||||
print(exc)
|
||||
retval = 1
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.check_yaml import check_yaml
|
||||
|
|
@ -13,3 +16,20 @@ from testing.util import get_resource_path
|
|||
def test_check_yaml(filename, expected_retval):
|
||||
ret = check_yaml([get_resource_path(filename)])
|
||||
assert ret == expected_retval
|
||||
|
||||
|
||||
def test_check_yaml_allow_multiple_documents(tmpdir):
|
||||
f = tmpdir.join('test.yaml')
|
||||
f.write('---\nfoo\n---\nbar\n')
|
||||
|
||||
# should failw without the setting
|
||||
assert check_yaml((f.strpath,))
|
||||
|
||||
# should pass when we allow multiple documents
|
||||
assert not check_yaml(('--allow-multiple-documents', f.strpath))
|
||||
|
||||
|
||||
def test_fails_even_with_allow_multiple_documents(tmpdir):
|
||||
f = tmpdir.join('test.yaml')
|
||||
f.write('[')
|
||||
assert check_yaml(('--allow-multiple-documents', f.strpath))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue