mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 12:34:17 +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-vcs-permalinks` - Ensures that links to vcs websites are permalinks.
|
||||||
- `check-xml` - Attempts to load all xml files to verify syntax.
|
- `check-xml` - Attempts to load all xml files to verify syntax.
|
||||||
- `check-yaml` - Attempts to load all yaml 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.
|
- `debug-statements` - Check for pdb / ipdb / pudb statements in code.
|
||||||
- `detect-aws-credentials` - Checks for the existence of AWS secrets that you
|
- `detect-aws-credentials` - Checks for the existence of AWS secrets that you
|
||||||
have set up with the AWS CLI.
|
have set up with the AWS CLI.
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,24 @@ except ImportError: # pragma: no cover (no libyaml-dev / pypy)
|
||||||
Loader = yaml.SafeLoader
|
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):
|
def check_yaml(argv=None):
|
||||||
parser = argparse.ArgumentParser()
|
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.')
|
parser.add_argument('filenames', nargs='*', help='Yaml filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
retval = 0
|
retval = 0
|
||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
try:
|
try:
|
||||||
yaml.load(open(filename), Loader=Loader)
|
args.yaml_load_fn(open(filename), Loader=Loader)
|
||||||
except yaml.YAMLError as exc:
|
except yaml.YAMLError as exc:
|
||||||
print(exc)
|
print(exc)
|
||||||
retval = 1
|
retval = 1
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.check_yaml import check_yaml
|
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):
|
def test_check_yaml(filename, expected_retval):
|
||||||
ret = check_yaml([get_resource_path(filename)])
|
ret = check_yaml([get_resource_path(filename)])
|
||||||
assert ret == expected_retval
|
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