pre-commit-hooks/tests/check_yaml_test.py

54 lines
1.3 KiB
Python
Raw Normal View History

from __future__ import annotations
2014-04-04 12:36:03 +08:00
import pytest
from pre_commit_hooks.check_yaml import main
2014-04-04 12:36:03 +08:00
from testing.util import get_resource_path
2017-07-16 03:56:51 +08:00
@pytest.mark.parametrize(
('filename', 'expected_retval'), (
('bad_yaml.notyaml', 1),
('ok_yaml.yaml', 0),
),
)
def test_main(filename, expected_retval):
ret = main([get_resource_path(filename)])
2014-04-04 12:36:03 +08:00
assert ret == expected_retval
def test_main_allow_multiple_documents(tmpdir):
f = tmpdir.join('test.yaml')
f.write('---\nfoo\n---\nbar\n')
# should fail without the setting
2020-05-21 00:07:45 +08:00
assert main((str(f),))
# should pass when we allow multiple documents
2020-05-21 00:07:45 +08:00
assert not main(('--allow-multiple-documents', str(f)))
def test_fails_even_with_allow_multiple_documents(tmpdir):
f = tmpdir.join('test.yaml')
f.write('[')
2020-05-21 00:07:45 +08:00
assert main(('--allow-multiple-documents', str(f)))
def test_main_unsafe(tmpdir):
f = tmpdir.join('test.yaml')
f.write(
'some_foo: !vault |\n'
' $ANSIBLE_VAULT;1.1;AES256\n'
' deadbeefdeadbeefdeadbeef\n',
)
# should fail "safe" check
2020-05-21 00:07:45 +08:00
assert main((str(f),))
# should pass when we allow unsafe documents
2020-05-21 00:07:45 +08:00
assert not main(('--unsafe', str(f)))
def test_main_unsafe_still_fails_on_syntax_errors(tmpdir):
f = tmpdir.join('test.yaml')
f.write('[')
2020-05-21 00:07:45 +08:00
assert main(('--unsafe', str(f)))