2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-04-04 12:36:03 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
)
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_main(filename, expected_retval):
|
|
|
|
ret = main([get_resource_path(filename)])
|
2014-04-04 12:36:03 +08:00
|
|
|
assert ret == expected_retval
|
2017-10-13 06:47:20 +08:00
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_main_allow_multiple_documents(tmpdir):
|
2017-10-13 06:47:20 +08:00
|
|
|
f = tmpdir.join('test.yaml')
|
|
|
|
f.write('---\nfoo\n---\nbar\n')
|
|
|
|
|
2018-03-20 00:28:18 +08:00
|
|
|
# should fail without the setting
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(f),))
|
2017-10-13 06:47:20 +08:00
|
|
|
|
|
|
|
# should pass when we allow multiple documents
|
2020-05-21 00:07:45 +08:00
|
|
|
assert not main(('--allow-multiple-documents', str(f)))
|
2017-10-13 06:47:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
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)))
|
2018-03-20 00:28:18 +08:00
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_main_unsafe(tmpdir):
|
2018-03-20 00:28:18 +08:00
|
|
|
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),))
|
2018-03-20 00:28:18 +08:00
|
|
|
# should pass when we allow unsafe documents
|
2020-05-21 00:07:45 +08:00
|
|
|
assert not main(('--unsafe', str(f)))
|
2018-03-20 00:28:18 +08:00
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_main_unsafe_still_fails_on_syntax_errors(tmpdir):
|
2018-03-20 00:28:18 +08:00
|
|
|
f = tmpdir.join('test.yaml')
|
|
|
|
f.write('[')
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main(('--unsafe', str(f)))
|