1
|
|
|
import argparse |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from coalib.parsing.CliParsing import parse_cli, check_conflicts |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class CliParserTest(unittest.TestCase): |
|
|
|
|
8
|
|
|
|
9
|
|
|
def setUp(self): |
10
|
|
|
self.test_arg_parser = argparse.ArgumentParser() |
11
|
|
|
self.test_arg_parser.add_argument('-t', nargs='+', dest='test') |
12
|
|
|
self.test_arg_parser.add_argument('-S', |
13
|
|
|
'--settings', |
14
|
|
|
nargs='+', |
15
|
|
|
dest='settings') |
16
|
|
|
|
17
|
|
|
@staticmethod |
|
|
|
|
18
|
|
|
def dict_from_sections(parsed_sections): |
19
|
|
|
parsed_dict = {} |
20
|
|
|
for section_name, section in parsed_sections.items(): |
21
|
|
|
parsed_dict[section_name] = ( |
22
|
|
|
set([(key, |
|
|
|
|
23
|
|
|
str(value)) for key, value in section.contents.items()])) |
|
|
|
|
24
|
|
|
return parsed_dict |
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_parse_cli(self): |
27
|
|
|
# regular parse |
28
|
|
|
parsed_sections = parse_cli( |
29
|
|
|
['-t', 'ignored1', 'ignored2', |
30
|
|
|
'-t', 'taken', |
31
|
|
|
'-S', 'section1.key1,section2.key2=value1,value2', |
32
|
|
|
'section2.key2=only_this_value', |
33
|
|
|
'SECTION2.key2a=k2a', |
34
|
|
|
'invalid.=shouldnt_be_shown', |
35
|
|
|
'.=not_either', |
36
|
|
|
'.key=only_in_default', |
37
|
|
|
'default_key1,default_key2=single_value', |
38
|
|
|
'default_key3=first_value,second_value'], |
39
|
|
|
arg_parser=self.test_arg_parser) |
|
|
|
|
40
|
|
|
expected_dict = { |
41
|
|
|
'default': { |
42
|
|
|
("test", "taken"), |
43
|
|
|
("key", "only_in_default"), |
44
|
|
|
("default_key1", "single_value"), |
45
|
|
|
("default_key2", "single_value"), |
46
|
|
|
("default_key3", "first_value,second_value")}, |
47
|
|
|
'section1': { |
48
|
|
|
("key1", "value1,value2")}, |
49
|
|
|
'section2': { |
50
|
|
|
("key2", "only_this_value"), |
51
|
|
|
("key2a", "k2a")}} |
52
|
|
|
self.assertEqual(parsed_sections["default"].name, "Default") |
|
|
|
|
53
|
|
|
self.assertEqual(self.dict_from_sections(parsed_sections), |
54
|
|
|
expected_dict) |
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_check_conflicts(self): |
57
|
|
|
sections = parse_cli(arg_list=["--save", "--no-config"]) |
58
|
|
|
with self.assertRaises(SystemExit) as cm: |
|
|
|
|
59
|
|
|
check_conflicts(sections) |
|
|
|
|
60
|
|
|
self.assertEqual(cm.exception.code, 2) |
61
|
|
|
|
62
|
|
|
sections = parse_cli(arg_list=["--no-config", "-S", "val=42"]) |
63
|
|
|
self.assertTrue(check_conflicts(sections)) |
64
|
|
|
|