|
3 | 3 | import os
|
4 | 4 | import sys
|
5 | 5 | import unittest
|
6 |
| -import unittest.mock |
7 | 6 | import collections
|
8 |
| -import tempfile |
9 |
| -import sys |
10 |
| -import argparse |
11 | 7 |
|
12 | 8 | # Monkey-patch paramunittest for Python 3.10+
|
13 | 9 | if sys.version_info[:2] >= (3, 10):
|
|
17 | 13 | import httpretty
|
18 | 14 | from paramunittest import parametrized
|
19 | 15 |
|
20 |
| -import xenon |
21 | 16 | from xenon import core, api, main
|
22 | 17 |
|
23 | 18 |
|
@@ -193,305 +188,5 @@ def test_api(self):
|
193 | 188 | 'message': 'Resource creation started'})
|
194 | 189 |
|
195 | 190 |
|
196 |
| -class TestCustomConfigParserGetStr(unittest.TestCase): |
197 |
| - '''Test class for class CustomConfigParser - getstr method.''' |
198 |
| - |
199 |
| - @staticmethod |
200 |
| - def get_configuration(text): |
201 |
| - '''Get CustomConfigParser object with loaded text.''' |
202 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
203 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
204 |
| - with open(pyproject_path, "w") as toml_file: |
205 |
| - toml_file.write(text) |
206 |
| - |
207 |
| - configuration = xenon.CustomConfigParser() |
208 |
| - configuration.read(pyproject_path) |
209 |
| - |
210 |
| - return configuration |
211 |
| - |
212 |
| - def test_missing_section_case(self): |
213 |
| - '''Test missing section case.''' |
214 |
| - configuration = self.get_configuration( |
215 |
| - '[tool.xenon]\n' |
216 |
| - 'path = ["path_1", "path_2", "path_3"]\n' |
217 |
| - 'max_average = "A"\n' |
218 |
| - 'max_average_num = 1.2\n') |
219 |
| - |
220 |
| - self.assertEqual( |
221 |
| - configuration.getstr(xenon.PYPROJECT_SECTION, "maxaverage", "None"), "None") |
222 |
| - |
223 |
| - def test_with_trim_value_case(self): |
224 |
| - '''Test with trim value case.''' |
225 |
| - configuration = self.get_configuration( |
226 |
| - '[tool.xenon]\n' |
227 |
| - 'path = ["path_1", "path_2", "path_3"]\n' |
228 |
| - 'max_average = "A"\n' |
229 |
| - 'max_modules = \'B\'\n' |
230 |
| - 'max_average_num = 1.2\n') |
231 |
| - |
232 |
| - self.assertEqual( |
233 |
| - configuration.getstr(xenon.PYPROJECT_SECTION, "max_average", "None"), "A") |
234 |
| - |
235 |
| - self.assertEqual( |
236 |
| - configuration.getstr(xenon.PYPROJECT_SECTION, "max_modules", "None"), "B") |
237 |
| - |
238 |
| - def test_without_trim_value_case(self): |
239 |
| - '''Test without trim value case.''' |
240 |
| - configuration = self.get_configuration( |
241 |
| - '[tool.xenon]\n' |
242 |
| - 'path = ["path_1", "path_2", "path_3"]\n' |
243 |
| - 'max_average = A\n' |
244 |
| - 'max_average-num = 1.2\n') |
245 |
| - |
246 |
| - self.assertEqual( |
247 |
| - configuration.getstr(xenon.PYPROJECT_SECTION, "max_average", "None"), "A") |
248 |
| - |
249 |
| - |
250 |
| -class TestCustomConfigParserGetListStr(unittest.TestCase): |
251 |
| - '''Test class for class CustomConfigParser - getliststr method.''' |
252 |
| - |
253 |
| - @staticmethod |
254 |
| - def get_configuration(text): |
255 |
| - '''Get CustomConfigParser object with loaded text.''' |
256 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
257 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
258 |
| - with open(pyproject_path, "w") as toml_file: |
259 |
| - toml_file.write(text) |
260 |
| - |
261 |
| - configuration = xenon.CustomConfigParser() |
262 |
| - configuration.read(pyproject_path) |
263 |
| - |
264 |
| - return configuration |
265 |
| - |
266 |
| - def test_missing_section_case(self): |
267 |
| - '''Test missing section case.''' |
268 |
| - configuration = self.get_configuration( |
269 |
| - '[tool.xenon]\n' |
270 |
| - 'path = ["path_1", "path_2", "path_3"]\n' |
271 |
| - 'max_average = "A"\n' |
272 |
| - 'max_average-num = 1.2\n') |
273 |
| - |
274 |
| - self.assertEqual( |
275 |
| - configuration.getliststr(xenon.PYPROJECT_SECTION, "maxaverage", "None"), "None") |
276 |
| - |
277 |
| - def test_parse_error_case(self): |
278 |
| - '''Test parse error case.''' |
279 |
| - configuration = self.get_configuration( |
280 |
| - '[tool.xenon]\n' |
281 |
| - 'path = ["path_1", "path_2", "path_3"\n' |
282 |
| - 'max_average = "A"\n' |
283 |
| - 'max_average-num = 1.2\n') |
284 |
| - |
285 |
| - self.assertRaisesRegex( |
286 |
| - xenon.PyProjectParseError, "path", configuration.getliststr, |
287 |
| - xenon.PYPROJECT_SECTION, "path") |
288 |
| - |
289 |
| - def test_single_value_case(self): |
290 |
| - '''Test single value case.''' |
291 |
| - configuration = self.get_configuration( |
292 |
| - '[tool.xenon]\n' |
293 |
| - 'path = "path_1"\n' |
294 |
| - 'max_average = "A"\n' |
295 |
| - 'max_average-num = 1.2\n') |
296 |
| - |
297 |
| - self.assertListEqual( |
298 |
| - configuration.getliststr(xenon.PYPROJECT_SECTION, "path", None), ["path_1"]) |
299 |
| - |
300 |
| - def test_invalid_format_case(self): |
301 |
| - '''Test invalid format case''' |
302 |
| - # Not a list case |
303 |
| - configuration = self.get_configuration( |
304 |
| - '[tool.xenon]\n' |
305 |
| - 'path = {"path_1": "path_2"}\n' |
306 |
| - 'max_average = "A"\n' |
307 |
| - 'max_average-num = 1.2\n') |
308 |
| - |
309 |
| - self.assertRaisesRegex( |
310 |
| - xenon.PyProjectParseError, "path", configuration.getliststr, |
311 |
| - xenon.PYPROJECT_SECTION, "path", None) |
312 |
| - |
313 |
| - # Not a list of str case |
314 |
| - configuration = self.get_configuration( |
315 |
| - '[tool.xenon]\n' |
316 |
| - 'path = ["path_1", "path_2", true]\n' |
317 |
| - 'max_average = "A"\n' |
318 |
| - 'max_average-num = 1.2\n') |
319 |
| - |
320 |
| - self.assertRaisesRegex( |
321 |
| - xenon.PyProjectParseError, "path", configuration.getliststr, |
322 |
| - xenon.PYPROJECT_SECTION, "path", None) |
323 |
| - |
324 |
| - def test_multiple_values_case(self): |
325 |
| - '''Test multiple values case.''' |
326 |
| - configuration = self.get_configuration( |
327 |
| - '[tool.xenon]\n' |
328 |
| - 'path = ["path_1", "path_2", "path_3"]\n' |
329 |
| - 'max_average = "A"\n' |
330 |
| - 'max_average-num = 1.2\n') |
331 |
| - |
332 |
| - self.assertListEqual( |
333 |
| - configuration.getliststr(xenon.PYPROJECT_SECTION, "path", None), |
334 |
| - ["path_1", "path_2", "path_3"]) |
335 |
| - |
336 |
| - |
337 |
| -class TestParsePyproject(unittest.TestCase): |
338 |
| - '''Test class for function parse_pyproject.''' |
339 |
| - |
340 |
| - def test_parse_error_case(self): |
341 |
| - '''Parse error case.''' |
342 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
343 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
344 |
| - with open(pyproject_path, "w") as toml_file: |
345 |
| - toml_file.write('parameter = value') |
346 |
| - |
347 |
| - self.assertRaisesRegex( |
348 |
| - xenon.PyProjectParseError, "Unable", xenon.parse_pyproject, pyproject_path) |
349 |
| - |
350 |
| - def test_duplicate_parameteres_case(self): |
351 |
| - '''Test duplicate parameters case''' |
352 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
353 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
354 |
| - with open(pyproject_path, "w") as toml_file: |
355 |
| - toml_file.write( |
356 |
| - '[tool.xenon]\n' |
357 |
| - 'max_average_num = value_1\n' |
358 |
| - 'max_average_num = value_2') |
359 |
| - |
360 |
| - self.assertRaisesRegex( |
361 |
| - xenon.PyProjectParseError, "duplicate parameters", xenon.parse_pyproject, pyproject_path) |
362 |
| - |
363 |
| - def test_missing_file_case(self): |
364 |
| - '''Test missing file path case.''' |
365 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
366 |
| - self.assertDictEqual(xenon.parse_pyproject(tmp_dir), {}) |
367 |
| - |
368 |
| - def test_invalid_max_average_num_type_case(self): |
369 |
| - '''Test invalid max-average-num parameter type case''' |
370 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
371 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
372 |
| - with open(pyproject_path, "w") as toml_file: |
373 |
| - toml_file.write('[tool.xenon]\nmax_average_num = value') |
374 |
| - |
375 |
| - self.assertRaisesRegex( |
376 |
| - xenon.PyProjectParseError, "max_average_num", xenon.parse_pyproject, pyproject_path) |
377 |
| - |
378 |
| - def test_invalid_no_assert_type_case(self): |
379 |
| - '''Test invalid no_assert parameter type case''' |
380 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
381 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
382 |
| - with open(pyproject_path, "w") as toml_file: |
383 |
| - toml_file.write('[tool.xenon]\nno_assert = next') |
384 |
| - |
385 |
| - self.assertRaisesRegex( |
386 |
| - xenon.PyProjectParseError, "no_assert", xenon.parse_pyproject, pyproject_path) |
387 |
| - |
388 |
| - def test_all_parameters_case(self): |
389 |
| - '''Test all parameters case.''' |
390 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
391 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
392 |
| - with open(pyproject_path, "w") as toml_file: |
393 |
| - toml_file.write( |
394 |
| - '[tool.xenon]\n' |
395 |
| - 'path = ["path_1", "path_2", "path_3"]\n' |
396 |
| - 'max_average = "A"\n' |
397 |
| - 'max_average_num = 1.2\n' |
398 |
| - 'max_modules = "B"\n' |
399 |
| - 'max_absolute = "C"\n' |
400 |
| - 'exclude = ["path_4", "path_5"]\n' |
401 |
| - 'ignore = ["path_6", "path_7"]\n' |
402 |
| - 'no_assert = true') |
403 |
| - |
404 |
| - result = xenon.parse_pyproject(pyproject_path) |
405 |
| - |
406 |
| - self.assertDictEqual( |
407 |
| - xenon.parse_pyproject(pyproject_path), { |
408 |
| - "path": ["path_1", "path_2", "path_3"], |
409 |
| - "average": 'A', |
410 |
| - "averagenum": 1.2, |
411 |
| - "modules": 'B', |
412 |
| - "absolute": 'C', |
413 |
| - "url": None, |
414 |
| - "config": None, |
415 |
| - "exclude": 'path_4,path_5', |
416 |
| - "ignore": 'path_6,path_7', |
417 |
| - "no_assert": True}) |
418 |
| - |
419 |
| - |
420 |
| -class TestGetParserDefaultsAndDeleteDefaults(unittest.TestCase): |
421 |
| - '''Test class for function get_parser_defaults_and_delete_defaults.''' |
422 |
| - |
423 |
| - def test_valid_case(self): |
424 |
| - '''Test valid case''' |
425 |
| - parser = argparse.ArgumentParser(add_help=False) |
426 |
| - |
427 |
| - parser.add_argument("-t", "--test", default="test") |
428 |
| - parser.add_argument("-v", "--values", default="values", dest="val") |
429 |
| - parser.add_argument("-w", "--without") |
430 |
| - |
431 |
| - args_defualt = xenon.get_parser_defaults_and_delete_defaults(parser) |
432 |
| - |
433 |
| - self.assertEqual(args_defualt, {"test": "test", "val": "values"}) |
434 |
| - |
435 |
| - sys.argv = ["file.py"] |
436 |
| - |
437 |
| - parser.parse_args() |
438 |
| - |
439 |
| - self.assertEqual( |
440 |
| - vars(parser.parse_args()), {"test": None, "val": None, "without": None}) |
441 |
| - |
442 |
| - |
443 |
| -class TestParseArgs(unittest.TestCase): |
444 |
| - '''Test class for function parse_args.''' |
445 |
| - |
446 |
| - def test_cmd_pyproject_case(self): |
447 |
| - '''Test parameter set from cmd and pyproject case.''' |
448 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
449 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
450 |
| - with open(pyproject_path, "w") as toml_file: |
451 |
| - toml_file.write( |
452 |
| - '[tool.xenon]\n' |
453 |
| - 'max_average = "A"\n') |
454 |
| - |
455 |
| - sys.argv = ["file", "-a", "B"] |
456 |
| - |
457 |
| - args = xenon.parse_args(pyproject_path) |
458 |
| - self.assertEqual(args.average, "B") |
459 |
| - |
460 |
| - def test_cmd_default_value_case(self): |
461 |
| - '''Test parameter not set from cmd (with default value) and pyproject case.''' |
462 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
463 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
464 |
| - with open(pyproject_path, "w") as toml_file: |
465 |
| - toml_file.write( |
466 |
| - '[tool.xenon]\n' |
467 |
| - 'config_file = config_file_path\n') |
468 |
| - |
469 |
| - sys.argv = ["file"] |
470 |
| - |
471 |
| - args = xenon.parse_args(pyproject_path) |
472 |
| - self.assertEqual(args.config, "config_file_path") |
473 |
| - |
474 |
| - def test_cmd_non_default_value_case(self): |
475 |
| - '''Test parameter set from cmd (with default value) and pyproject case.''' |
476 |
| - with tempfile.TemporaryDirectory() as tmp_dir: |
477 |
| - pyproject_path = tmp_dir + '/pyproject.toml' |
478 |
| - with open(pyproject_path, "w") as toml_file: |
479 |
| - toml_file.write( |
480 |
| - '[tool.xenon]\n' |
481 |
| - 'config_file = config_file_path\n') |
482 |
| - |
483 |
| - # Set different value in cmd then default |
484 |
| - sys.argv = ["file", "--config-file", "cfg_file_path"] |
485 |
| - |
486 |
| - args = xenon.parse_args(pyproject_path) |
487 |
| - self.assertEqual(args.config, "cfg_file_path") |
488 |
| - |
489 |
| - # Set same value in cmd as default |
490 |
| - sys.argv = ["file", "--config-file", ".xenon.yml"] |
491 |
| - |
492 |
| - args = xenon.parse_args(pyproject_path) |
493 |
| - self.assertEqual(args.config, ".xenon.yml") |
494 |
| - |
495 |
| - |
496 | 191 | if __name__ == '__main__':
|
497 | 192 | unittest.main()
|
0 commit comments