blob: 54eb16e874b6d5f69eb0ca8a1b7c8d1bf17284c8 [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottile3e45f532014-04-03 21:36:03 -07003import pytest
4
Anthony Sottile030bfac2019-01-31 19:19:10 -08005from pre_commit_hooks.check_yaml import main
Anthony Sottile3e45f532014-04-03 21:36:03 -07006from testing.util import get_resource_path
7
8
Anthony Sottilee9aea742017-07-15 12:56:51 -07009@pytest.mark.parametrize(
10 ('filename', 'expected_retval'), (
11 ('bad_yaml.notyaml', 1),
12 ('ok_yaml.yaml', 0),
13 ),
14)
Anthony Sottile030bfac2019-01-31 19:19:10 -080015def test_main(filename, expected_retval):
16 ret = main([get_resource_path(filename)])
Anthony Sottile3e45f532014-04-03 21:36:03 -070017 assert ret == expected_retval
Anthony Sottilee87b81a2017-10-12 15:47:20 -070018
19
Anthony Sottile030bfac2019-01-31 19:19:10 -080020def test_main_allow_multiple_documents(tmpdir):
Anthony Sottilee87b81a2017-10-12 15:47:20 -070021 f = tmpdir.join('test.yaml')
22 f.write('---\nfoo\n---\nbar\n')
23
Anthony Sottilea21def32018-03-19 09:28:18 -070024 # should fail without the setting
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040025 assert main((str(f),))
Anthony Sottilee87b81a2017-10-12 15:47:20 -070026
27 # should pass when we allow multiple documents
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040028 assert not main(('--allow-multiple-documents', str(f)))
Anthony Sottilee87b81a2017-10-12 15:47:20 -070029
30
31def test_fails_even_with_allow_multiple_documents(tmpdir):
32 f = tmpdir.join('test.yaml')
33 f.write('[')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040034 assert main(('--allow-multiple-documents', str(f)))
Anthony Sottilea21def32018-03-19 09:28:18 -070035
36
Anthony Sottile030bfac2019-01-31 19:19:10 -080037def test_main_unsafe(tmpdir):
Anthony Sottilea21def32018-03-19 09:28:18 -070038 f = tmpdir.join('test.yaml')
39 f.write(
40 'some_foo: !vault |\n'
41 ' $ANSIBLE_VAULT;1.1;AES256\n'
42 ' deadbeefdeadbeefdeadbeef\n',
43 )
44 # should fail "safe" check
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040045 assert main((str(f),))
Anthony Sottilea21def32018-03-19 09:28:18 -070046 # should pass when we allow unsafe documents
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040047 assert not main(('--unsafe', str(f)))
Anthony Sottilea21def32018-03-19 09:28:18 -070048
49
Anthony Sottile030bfac2019-01-31 19:19:10 -080050def test_main_unsafe_still_fails_on_syntax_errors(tmpdir):
Anthony Sottilea21def32018-03-19 09:28:18 -070051 f = tmpdir.join('test.yaml')
52 f.write('[')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040053 assert main(('--unsafe', str(f)))