blob: 72f5becc242bcb5abb6f3e1ae8d4177199712ec6 [file] [log] [blame]
Daniel Gallagherb6eff3d2017-06-23 16:26:00 -07001from __future__ import absolute_import
2from __future__ import unicode_literals
3
4import os
5
6import pytest
7
8from pre_commit_hooks.sort_simple_yaml import first_key
9from pre_commit_hooks.sort_simple_yaml import main
10from pre_commit_hooks.sort_simple_yaml import parse_block
11from pre_commit_hooks.sort_simple_yaml import parse_blocks
12from pre_commit_hooks.sort_simple_yaml import sort
13
14RETVAL_GOOD = 0
15RETVAL_BAD = 1
16TEST_SORTS = [
17 (
18 ['c: true', '', 'b: 42', 'a: 19'],
19 ['b: 42', 'a: 19', '', 'c: true'],
20 RETVAL_BAD,
21 ),
22
23 (
24 ['# i am', '# a header', '', 'c: true', '', 'b: 42', 'a: 19'],
25 ['# i am', '# a header', '', 'b: 42', 'a: 19', '', 'c: true'],
26 RETVAL_BAD,
27 ),
28
29 (
30 ['# i am', '# a header', '', 'already: sorted', '', 'yup: i am'],
31 ['# i am', '# a header', '', 'already: sorted', '', 'yup: i am'],
32 RETVAL_GOOD,
33 ),
34
35 (
36 ['# i am', '# a header'],
37 ['# i am', '# a header'],
38 RETVAL_GOOD,
39 ),
40]
41
42
43@pytest.mark.parametrize('bad_lines,good_lines,retval', TEST_SORTS)
44def test_integration_good_bad_lines(tmpdir, bad_lines, good_lines, retval):
45 file_path = os.path.join(tmpdir.strpath, 'foo.yaml')
46
47 with open(file_path, 'w') as f:
48 f.write("\n".join(bad_lines) + "\n")
49
50 assert main([file_path]) == retval
51
52 with open(file_path, 'r') as f:
53 assert [line.rstrip() for line in f.readlines()] == good_lines
54
55
56def test_parse_header():
57 lines = ['# some header', '# is here', '', 'this is not a header']
58 assert parse_block(lines, header=True) == ['# some header', '# is here']
59 assert lines == ['', 'this is not a header']
60
61 lines = ['this is not a header']
62 assert parse_block(lines, header=True) == []
63 assert lines == ['this is not a header']
64
65
66def test_parse_block():
67 # a normal block
68 lines = ['a: 42', 'b: 17', '', 'c: 19']
69 assert parse_block(lines) == ['a: 42', 'b: 17']
70 assert lines == ['', 'c: 19']
71
72 # a block at the end
73 lines = ['c: 19']
74 assert parse_block(lines) == ['c: 19']
75 assert lines == []
76
77 # no block
78 lines = []
79 assert parse_block(lines) == []
80 assert lines == []
81
82
83def test_parse_blocks():
84 # normal blocks
85 lines = ['a: 42', 'b: 17', '', 'c: 19']
86 assert parse_blocks(lines) == [['a: 42', 'b: 17'], ['c: 19']]
87 assert lines == []
88
89 # a single block
90 lines = ['a: 42', 'b: 17']
91 assert parse_blocks(lines) == [['a: 42', 'b: 17']]
92 assert lines == []
93
94 # no blocks
95 lines = []
96 assert parse_blocks(lines) == []
97 assert lines == []
98
99
100def test_first_key():
101 # first line
102 lines = ['a: 42', 'b: 17', '', 'c: 19']
103 assert first_key(lines) == 'a: 42'
104
105 # second line
106 lines = ['# some comment', 'a: 42', 'b: 17', '', 'c: 19']
107 assert first_key(lines) == 'a: 42'
108
109 # second line with quotes
110 lines = ['# some comment', '"a": 42', 'b: 17', '', 'c: 19']
111 assert first_key(lines) == 'a": 42'
112
Anthony Sottile030bfac2019-01-31 19:19:10 -0800113 # no lines (not a real situation)
Daniel Gallagherb6eff3d2017-06-23 16:26:00 -0700114 lines = []
Anthony Sottile030bfac2019-01-31 19:19:10 -0800115 assert first_key(lines) == ''
Daniel Gallagherb6eff3d2017-06-23 16:26:00 -0700116
117
118@pytest.mark.parametrize('bad_lines,good_lines,_', TEST_SORTS)
119def test_sort(bad_lines, good_lines, _):
120 assert sort(bad_lines) == good_lines