Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | 3e45f53 | 2014-04-03 21:36:03 -0700 | [diff] [blame] | 3 | import pytest |
| 4 | |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 5 | from pre_commit_hooks.check_yaml import main |
Anthony Sottile | 3e45f53 | 2014-04-03 21:36:03 -0700 | [diff] [blame] | 6 | from testing.util import get_resource_path |
| 7 | |
| 8 | |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 9 | @pytest.mark.parametrize( |
| 10 | ('filename', 'expected_retval'), ( |
| 11 | ('bad_yaml.notyaml', 1), |
| 12 | ('ok_yaml.yaml', 0), |
| 13 | ), |
| 14 | ) |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 15 | def test_main(filename, expected_retval): |
| 16 | ret = main([get_resource_path(filename)]) |
Anthony Sottile | 3e45f53 | 2014-04-03 21:36:03 -0700 | [diff] [blame] | 17 | assert ret == expected_retval |
Anthony Sottile | e87b81a | 2017-10-12 15:47:20 -0700 | [diff] [blame] | 18 | |
| 19 | |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 20 | def test_main_allow_multiple_documents(tmpdir): |
Anthony Sottile | e87b81a | 2017-10-12 15:47:20 -0700 | [diff] [blame] | 21 | f = tmpdir.join('test.yaml') |
| 22 | f.write('---\nfoo\n---\nbar\n') |
| 23 | |
Anthony Sottile | a21def3 | 2018-03-19 09:28:18 -0700 | [diff] [blame] | 24 | # should fail without the setting |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 25 | assert main((str(f),)) |
Anthony Sottile | e87b81a | 2017-10-12 15:47:20 -0700 | [diff] [blame] | 26 | |
| 27 | # should pass when we allow multiple documents |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 28 | assert not main(('--allow-multiple-documents', str(f))) |
Anthony Sottile | e87b81a | 2017-10-12 15:47:20 -0700 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def test_fails_even_with_allow_multiple_documents(tmpdir): |
| 32 | f = tmpdir.join('test.yaml') |
| 33 | f.write('[') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 34 | assert main(('--allow-multiple-documents', str(f))) |
Anthony Sottile | a21def3 | 2018-03-19 09:28:18 -0700 | [diff] [blame] | 35 | |
| 36 | |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 37 | def test_main_unsafe(tmpdir): |
Anthony Sottile | a21def3 | 2018-03-19 09:28:18 -0700 | [diff] [blame] | 38 | 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 Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 45 | assert main((str(f),)) |
Anthony Sottile | a21def3 | 2018-03-19 09:28:18 -0700 | [diff] [blame] | 46 | # should pass when we allow unsafe documents |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 47 | assert not main(('--unsafe', str(f))) |
Anthony Sottile | a21def3 | 2018-03-19 09:28:18 -0700 | [diff] [blame] | 48 | |
| 49 | |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 50 | def test_main_unsafe_still_fails_on_syntax_errors(tmpdir): |
Anthony Sottile | a21def3 | 2018-03-19 09:28:18 -0700 | [diff] [blame] | 51 | f = tmpdir.join('test.yaml') |
| 52 | f.write('[') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 53 | assert main(('--unsafe', str(f))) |