1import pytest
2import checkpackagelib.test_util as util
3import checkpackagelib.lib as m
4
5
6ConsecutiveEmptyLines = [
7    ('1 line (no newline)',
8     'any',
9     '',
10     []),
11    ('1 line',
12     'any',
13     '\n',
14     []),
15    ('2 lines',
16     'any',
17     '\n'
18     '\n',
19     [['any:2: consecutive empty lines']]),
20    ('more than 2 consecutive',
21     'any',
22     '\n'
23     '\n'
24     '\n',
25     [['any:2: consecutive empty lines'],
26      ['any:3: consecutive empty lines']]),
27    ('ignore whitespace 1',
28     'any',
29     '\n'
30     ' ',
31     [['any:2: consecutive empty lines']]),
32    ('ignore whitespace 2',
33     'any',
34     ' \n'
35     '\t\n',
36     [['any:2: consecutive empty lines']]),
37    ]
38
39
40@pytest.mark.parametrize('testname,filename,string,expected', ConsecutiveEmptyLines)
41def test_ConsecutiveEmptyLines(testname, filename, string, expected):
42    warnings = util.check_file(m.ConsecutiveEmptyLines, filename, string)
43    assert warnings == expected
44
45
46EmptyLastLine = [
47    ('ignore empty file',
48     'any',
49     '',
50     []),
51    ('empty line (newline)',
52     'any',
53     '\n',
54     [['any:1: empty line at end of file']]),
55    ('empty line (space, newline)',
56     'any',
57     ' \n',
58     [['any:1: empty line at end of file']]),
59    ('empty line (space, no newline)',
60     'any',
61     ' ',
62     [['any:1: empty line at end of file']]),
63    ('warn for the last of 2',
64     'any',
65     '\n'
66     '\n',
67     [['any:2: empty line at end of file']]),
68    ('warn for the last of 3',
69     'any',
70     '\n'
71     '\n'
72     '\n',
73     [['any:3: empty line at end of file']]),
74    ('ignore whitespace',
75     'any',
76     ' \n'
77     '\t\n',
78     [['any:2: empty line at end of file']]),
79    ]
80
81
82@pytest.mark.parametrize('testname,filename,string,expected', EmptyLastLine)
83def test_EmptyLastLine(testname, filename, string, expected):
84    warnings = util.check_file(m.EmptyLastLine, filename, string)
85    assert warnings == expected
86
87
88NewlineAtEof = [
89    ('good',
90     'any',
91     'text\n',
92     []),
93    ('text (bad)',
94     'any',
95     '\n'
96     'text',
97     [['any:2: missing newline at end of file',
98       'text']]),
99    ('space (bad)',
100     'any',
101     '\n'
102     ' ',
103     [['any:2: missing newline at end of file',
104       ' ']]),
105    ('tab (bad)',
106     'any',
107     '\n'
108     '\t',
109     [['any:2: missing newline at end of file',
110       '\t']]),
111    ('even for file with one line',
112     'any',
113     ' ',
114     [['any:1: missing newline at end of file',
115       ' ']]),
116    ]
117
118
119@pytest.mark.parametrize('testname,filename,string,expected', NewlineAtEof)
120def test_NewlineAtEof(testname, filename, string, expected):
121    warnings = util.check_file(m.NewlineAtEof, filename, string)
122    assert warnings == expected
123
124
125TrailingSpace = [
126    ('good',
127     'any',
128     'text\n',
129     []),
130    ('ignore missing newline',
131     'any',
132     '\n'
133     'text',
134     []),
135    ('spaces',
136     'any',
137     'text  \n',
138     [['any:1: line contains trailing whitespace',
139       'text  \n']]),
140    ('tabs after text',
141     'any',
142     'text\t\t\n',
143     [['any:1: line contains trailing whitespace',
144       'text\t\t\n']]),
145    ('mix of tabs and spaces',
146     'any',
147     ' \n'
148     ' ',
149     [['any:1: line contains trailing whitespace',
150       ' \n'],
151      ['any:2: line contains trailing whitespace',
152       ' ']]),
153    ('blank line with tabs',
154     'any',
155     '\n'
156     '\t',
157     [['any:2: line contains trailing whitespace',
158       '\t']]),
159    ]
160
161
162@pytest.mark.parametrize('testname,filename,string,expected', TrailingSpace)
163def test_TrailingSpace(testname, filename, string, expected):
164    warnings = util.check_file(m.TrailingSpace, filename, string)
165    assert warnings == expected
166
167
168Utf8Characters = [
169    ('usual',
170     'any',
171     'text\n',
172     []),
173    ('acceptable character',
174     'any',
175     '\x60',
176     []),
177    ('unacceptable character',
178     'any',
179     '\x81',
180     [['any:1: line contains UTF-8 characters',
181       '\x81']]),
182    ('2 warnings',
183     'any',
184     'text\n'
185     'text \xc8 text\n'
186     '\xc9\n',
187     [['any:2: line contains UTF-8 characters',
188       'text \xc8 text\n'],
189      ['any:3: line contains UTF-8 characters',
190       '\xc9\n']]),
191    ]
192
193
194@pytest.mark.parametrize('testname,filename,string,expected', Utf8Characters)
195def test_Utf8Characters(testname, filename, string, expected):
196    warnings = util.check_file(m.Utf8Characters, filename, string)
197    assert warnings == expected
198
199
200def test_all_check_functions_are_used():
201    import inspect
202    import checkpackagelib.lib_config as lib_config
203    import checkpackagelib.lib_hash as lib_hash
204    import checkpackagelib.lib_mk as lib_mk
205    import checkpackagelib.lib_patch as lib_patch
206    c_config = [c[0] for c in inspect.getmembers(lib_config, inspect.isclass)]
207    c_hash = [c[0] for c in inspect.getmembers(lib_hash, inspect.isclass)]
208    c_mk = [c[0] for c in inspect.getmembers(lib_mk, inspect.isclass)]
209    c_patch = [c[0] for c in inspect.getmembers(lib_patch, inspect.isclass)]
210    c_all = c_config + c_hash + c_mk + c_patch
211    c_common = [c[0] for c in inspect.getmembers(m, inspect.isclass)]
212    assert set(c_common) <= set(c_all)
213