|
18 | 18 | from __future__ import print_function |
19 | 19 | from __future__ import unicode_literals |
20 | 20 |
|
| 21 | +import unittest |
| 22 | + |
21 | 23 | from attributecode import CRITICAL |
22 | 24 | from attributecode import DEBUG |
23 | 25 | from attributecode import ERROR |
|
27 | 29 | from attributecode import cmd |
28 | 30 | from attributecode import Error |
29 | 31 |
|
| 32 | +from testing_utils import run_about_command_test |
| 33 | +from testing_utils import get_test_loc |
| 34 | + |
30 | 35 |
|
31 | | -# NB: these tests depends on py.test stdout/err capture capabilities |
| 36 | +# NB: the test_report_errors* tests depend on py.test stdout/err capture capabilities |
32 | 37 |
|
33 | 38 | def test_report_errors(capsys): |
34 | 39 | errors = [ |
@@ -146,86 +151,138 @@ def test_report_errors_with_verbose_flag(capsys): |
146 | 151 | assert expected_out == out |
147 | 152 | assert '' == err |
148 | 153 |
|
149 | | -def test_filter_errors_default(): |
150 | | - errors = [ |
151 | | - Error(CRITICAL, 'msg1'), |
152 | | - Error(ERROR, 'msg2'), |
153 | | - Error(INFO, 'msg3'), |
154 | | - Error(WARNING, 'msg4'), |
155 | | - Error(DEBUG, 'msg4'), |
156 | | - Error(NOTSET, 'msg4'), |
157 | | - ] |
158 | | - expected = [ |
159 | | - Error(CRITICAL, 'msg1'), |
160 | | - Error(ERROR, 'msg2'), |
161 | | - Error(WARNING, 'msg4'), |
162 | | - ] |
163 | | - assert expected == cmd.filter_errors(errors) |
164 | 154 |
|
| 155 | +class TestFilterError(unittest.TestCase): |
| 156 | + def test_filter_errors_default(self): |
| 157 | + errors = [ |
| 158 | + Error(CRITICAL, 'msg1'), |
| 159 | + Error(ERROR, 'msg2'), |
| 160 | + Error(INFO, 'msg3'), |
| 161 | + Error(WARNING, 'msg4'), |
| 162 | + Error(DEBUG, 'msg4'), |
| 163 | + Error(NOTSET, 'msg4'), |
| 164 | + ] |
| 165 | + expected = [ |
| 166 | + Error(CRITICAL, 'msg1'), |
| 167 | + Error(ERROR, 'msg2'), |
| 168 | + Error(WARNING, 'msg4'), |
| 169 | + ] |
| 170 | + assert expected == cmd.filter_errors(errors) |
165 | 171 |
|
166 | | -def test_filter_errors_with_min(): |
167 | | - errors = [ |
168 | | - Error(CRITICAL, 'msg1'), |
169 | | - Error(ERROR, 'msg2'), |
170 | | - Error(INFO, 'msg3'), |
171 | | - Error(WARNING, 'msg4'), |
172 | | - Error(DEBUG, 'msg4'), |
173 | | - Error(NOTSET, 'msg4'), |
174 | | - ] |
175 | | - expected = [ |
176 | | - Error(CRITICAL, 'msg1'), |
177 | | - ] |
178 | | - assert expected == cmd.filter_errors(errors, CRITICAL) |
179 | 172 |
|
| 173 | + def test_filter_errors_with_min(self): |
| 174 | + errors = [ |
| 175 | + Error(CRITICAL, 'msg1'), |
| 176 | + Error(ERROR, 'msg2'), |
| 177 | + Error(INFO, 'msg3'), |
| 178 | + Error(WARNING, 'msg4'), |
| 179 | + Error(DEBUG, 'msg4'), |
| 180 | + Error(NOTSET, 'msg4'), |
| 181 | + ] |
| 182 | + expected = [ |
| 183 | + Error(CRITICAL, 'msg1'), |
| 184 | + ] |
| 185 | + assert expected == cmd.filter_errors(errors, CRITICAL) |
180 | 186 |
|
181 | | -def test_filter_errors_no_errors(): |
182 | | - errors = [ |
183 | | - Error(INFO, 'msg3'), |
184 | | - Error(DEBUG, 'msg4'), |
185 | | - Error(NOTSET, 'msg4'), |
186 | | - ] |
187 | | - assert [] == cmd.filter_errors(errors) |
188 | 187 |
|
| 188 | + def test_filter_errors_no_errors(self): |
| 189 | + errors = [ |
| 190 | + Error(INFO, 'msg3'), |
| 191 | + Error(DEBUG, 'msg4'), |
| 192 | + Error(NOTSET, 'msg4'), |
| 193 | + ] |
| 194 | + assert [] == cmd.filter_errors(errors) |
189 | 195 |
|
190 | | -def test_filter_errors_none(): |
191 | | - assert [] == cmd.filter_errors([]) |
192 | 196 |
|
| 197 | + def test_filter_errors_none(self): |
| 198 | + assert [] == cmd.filter_errors([]) |
193 | 199 |
|
194 | | -def test_parse_key_values_empty(): |
195 | | - assert ({}, []) == cmd.parse_key_values([]) |
196 | | - assert ({}, []) == cmd.parse_key_values(None) |
197 | 200 |
|
| 201 | +class TestParseKeyValues(unittest.TestCase): |
198 | 202 |
|
199 | | -def test_parse_key_values_simple(): |
200 | | - test = [ |
201 | | - 'key=value', |
202 | | - 'This=THat', |
203 | | - 'keY=bar', |
204 | | - ] |
205 | | - expected = { |
206 | | - 'key': ['value', 'bar'], |
207 | | - 'this': ['THat'] |
| 203 | + def test_parse_key_values_empty(self): |
| 204 | + assert ({}, []) == cmd.parse_key_values([]) |
| 205 | + assert ({}, []) == cmd.parse_key_values(None) |
| 206 | + |
| 207 | + |
| 208 | + def test_parse_key_values_simple(self): |
| 209 | + test = [ |
| 210 | + 'key=value', |
| 211 | + 'This=THat', |
| 212 | + 'keY=bar', |
| 213 | + ] |
| 214 | + expected = { |
| 215 | + 'key': ['value', 'bar'], |
| 216 | + 'this': ['THat'] |
| 217 | + } |
| 218 | + keyvals, errors = cmd.parse_key_values(test) |
| 219 | + assert expected == keyvals |
| 220 | + assert not errors |
| 221 | + |
| 222 | + |
| 223 | + def test_parse_key_values_with_errors(self): |
| 224 | + test = [ |
| 225 | + 'key', |
| 226 | + '=THat', |
| 227 | + 'keY=', |
| 228 | + 'FOO=bar' |
| 229 | + ] |
| 230 | + expected = { |
| 231 | + 'foo': ['bar'], |
208 | 232 | } |
209 | | - keyvals, errors = cmd.parse_key_values(test) |
210 | | - assert expected == keyvals |
211 | | - assert not errors |
| 233 | + keyvals, errors = cmd.parse_key_values(test) |
| 234 | + assert expected == keyvals |
| 235 | + expected = [ |
| 236 | + 'missing <key> in "=THat".', |
| 237 | + 'missing <value> in "keY=".', |
| 238 | + 'missing <value> in "key".' |
| 239 | + ] |
| 240 | + assert expected == errors |
212 | 241 |
|
213 | 242 |
|
214 | | -def test_parse_key_values_with_errors(): |
215 | | - test = [ |
216 | | - 'key', |
217 | | - '=THat', |
218 | | - 'keY=', |
219 | | - 'FOO=bar' |
220 | | - ] |
221 | | - expected = { |
222 | | - 'foo': ['bar'], |
223 | | - } |
224 | | - keyvals, errors = cmd.parse_key_values(test) |
225 | | - assert expected == keyvals |
226 | | - expected = [ |
227 | | - 'missing <key> in "=THat".', |
228 | | - 'missing <value> in "keY=".', |
229 | | - 'missing <value> in "key".' |
230 | | - ] |
231 | | - assert expected == errors |
| 243 | +############################################################################### |
| 244 | +# Run full cli command |
| 245 | +############################################################################### |
| 246 | + |
| 247 | +def check_about_stdout(options, expected_loc, regen=False): |
| 248 | + """ |
| 249 | + Run the about command with the `options` list of options. Assert that |
| 250 | + command success and that the stdout is equal to the `expected_loc` test file |
| 251 | + content. |
| 252 | + """ |
| 253 | + stdout, _stderr = run_about_command_test(options) |
| 254 | + if regen: |
| 255 | + expected_file = get_test_loc(expected_loc, must_exists=False) |
| 256 | + with open(expected_file, 'wb') as ef: |
| 257 | + ef.write(stdout) |
| 258 | + |
| 259 | + expected_file = get_test_loc(expected_loc, must_exists=True) |
| 260 | + assert open(expected_file).read() == stdout |
| 261 | + |
| 262 | + |
| 263 | +def test_about_help_text(regen=False): |
| 264 | + check_about_stdout(['--help'], 'test_cmd/help/about_help.txt') |
| 265 | + |
| 266 | + |
| 267 | +def test_about_inventory_help_text(regen=False): |
| 268 | + check_about_stdout( |
| 269 | + ['inventory', '--help'], |
| 270 | + 'test_cmd/help/about_inventory_help.txt') |
| 271 | + |
| 272 | + |
| 273 | +def test_about_gen_help_text(regen=False): |
| 274 | + check_about_stdout( |
| 275 | + ['gen', '--help'], |
| 276 | + 'test_cmd/help/about_gen_help.txt') |
| 277 | + |
| 278 | + |
| 279 | +def test_about_check_help_text(regen=False): |
| 280 | + check_about_stdout( |
| 281 | + ['check', '--help'], |
| 282 | + 'test_cmd/help/about_check_help.txt') |
| 283 | + |
| 284 | + |
| 285 | +def test_about_attrib_help_text(regen=False): |
| 286 | + check_about_stdout( |
| 287 | + ['attrib', '--help'], |
| 288 | + 'test_cmd/help/about_attrib_help.txt') |
0 commit comments