1
|
|
|
from collections import OrderedDict |
2
|
|
|
import os |
3
|
|
|
import sys |
4
|
|
|
import tempfile |
5
|
|
|
import unittest |
6
|
|
|
|
7
|
|
|
sys.path.insert(0, ".") |
8
|
|
|
from coalib.misc.Compatability import FileNotFoundError |
9
|
|
|
from coalib.parsing.ConfParser import ConfParser |
10
|
|
|
from coalib.settings.Section import Section |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class ConfParserTest(unittest.TestCase): |
14
|
|
|
example_file = """to be ignored |
15
|
|
|
a_default, another = val |
16
|
|
|
TEST = tobeignored # do you know that thats a comment |
17
|
|
|
test = push |
18
|
|
|
t = |
19
|
|
|
[MakeFiles] |
20
|
|
|
j , another = a |
21
|
|
|
multiline |
22
|
|
|
value |
23
|
|
|
# just a omment |
24
|
|
|
# just a omment |
25
|
|
|
nokey. = value |
26
|
|
|
default.test = content |
27
|
|
|
makefiles.lastone = val |
28
|
|
|
|
29
|
|
|
[EMPTY_ELEM_STRIP] |
30
|
|
|
A = a, b, c |
31
|
|
|
B = a, ,, d |
32
|
|
|
C = ,,, |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
def setUp(self): |
36
|
|
|
self.tempdir = tempfile.gettempdir() |
37
|
|
|
self.file = os.path.join(self.tempdir, ".coafile") |
38
|
|
|
self.nonexistentfile = os.path.join(self.tempdir, "e81k7bd98t") |
39
|
|
|
with open(self.file, "w") as filehandler: |
40
|
|
|
filehandler.write(self.example_file) |
41
|
|
|
|
42
|
|
|
self.uut = ConfParser() |
43
|
|
|
try: |
44
|
|
|
os.remove(self.nonexistentfile) |
45
|
|
|
except FileNotFoundError: |
46
|
|
|
pass |
47
|
|
|
|
48
|
|
|
def tearDown(self): |
49
|
|
|
os.remove(self.file) |
50
|
|
|
|
51
|
|
|
def test_parse(self): |
52
|
|
|
default_should = OrderedDict([ |
53
|
|
|
('a_default', 'val'), |
54
|
|
|
('another', 'val'), |
55
|
|
|
('comment0', '# do you know that thats a comment'), |
56
|
|
|
('test', 'content'), |
57
|
|
|
('t', '')]) |
58
|
|
|
|
59
|
|
|
makefiles_should = OrderedDict([ |
60
|
|
|
('j', 'a\nmultiline\nvalue'), |
61
|
|
|
('another', 'a\nmultiline\nvalue'), |
62
|
|
|
('comment1', '# just a omment'), |
63
|
|
|
('comment2', '# just a omment'), |
64
|
|
|
('lastone', 'val'), |
65
|
|
|
('comment3', ''), |
66
|
|
|
('a_default', 'val'), |
67
|
|
|
('comment0', '# do you know that thats a comment'), |
68
|
|
|
('test', 'content'), |
69
|
|
|
('t', '')]) |
70
|
|
|
|
71
|
|
|
empty_elem_strip_should = OrderedDict([ |
72
|
|
|
('a', 'a, b, c'), |
73
|
|
|
('b', 'a, ,, d'), |
74
|
|
|
('c', ',,,'), |
75
|
|
|
('comment4', ''), |
76
|
|
|
('a_default', 'val'), |
77
|
|
|
('another', 'val'), |
78
|
|
|
('comment0', '# do you know that thats a comment'), |
79
|
|
|
('test', 'content'), |
80
|
|
|
('t', '')]) |
81
|
|
|
|
82
|
|
|
self.assertRaises(FileNotFoundError, |
83
|
|
|
self.uut.parse, |
84
|
|
|
self.nonexistentfile) |
85
|
|
|
sections = self.uut.parse(self.file) |
86
|
|
|
self.assertNotEqual(self.uut.parse(self.file, True), sections) |
87
|
|
|
|
88
|
|
|
# Check section "DEFAULT". |
89
|
|
|
key, val = sections.popitem(last=False) |
90
|
|
|
self.assertTrue(isinstance(val, Section)) |
91
|
|
|
self.assertEqual(key, 'default') |
92
|
|
|
|
93
|
|
|
is_dict = OrderedDict() |
94
|
|
|
for k in val: |
95
|
|
|
is_dict[k] = str(val[k]) |
96
|
|
|
self.assertEqual(is_dict, default_should) |
97
|
|
|
|
98
|
|
|
# Check section "MakeFiles". |
99
|
|
|
key, val = sections.popitem(last=False) |
100
|
|
|
self.assertTrue(isinstance(val, Section)) |
101
|
|
|
self.assertEqual(key, 'makefiles') |
102
|
|
|
|
103
|
|
|
is_dict = OrderedDict() |
104
|
|
|
for k in val: |
105
|
|
|
is_dict[k] = str(val[k]) |
106
|
|
|
self.assertEqual(is_dict, makefiles_should) |
107
|
|
|
|
108
|
|
|
self.assertEqual(val["comment1"].key, "comment1") |
109
|
|
|
|
110
|
|
|
# Check section "EMPTY_ELEM_STRIP". |
111
|
|
|
key, val = sections.popitem(last=False) |
112
|
|
|
self.assertTrue(isinstance(val, Section)) |
113
|
|
|
self.assertEqual(key, 'empty_elem_strip') |
114
|
|
|
|
115
|
|
|
is_dict = OrderedDict() |
116
|
|
|
for k in val: |
117
|
|
|
is_dict[k] = str(val[k]) |
118
|
|
|
self.assertEqual(is_dict, empty_elem_strip_should) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
# Check inexistent Section. |
122
|
|
|
self.assertRaises(IndexError, |
123
|
|
|
self.uut.get_section, |
124
|
|
|
"inexistent section") |
125
|
|
|
|
126
|
|
|
def test_remove_empty_iter_elements(self): |
127
|
|
|
# Test with empty-elem stripping. |
128
|
|
|
uut = ConfParser(remove_empty_iter_elements=True) |
129
|
|
|
uut.parse(self.file) |
130
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["A"]), |
131
|
|
|
["a", "b", "c"]) |
132
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["B"]), |
133
|
|
|
["a", "d"]) |
134
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["C"]), |
135
|
|
|
[]) |
136
|
|
|
|
137
|
|
|
# Test without stripping. |
138
|
|
|
uut = ConfParser(remove_empty_iter_elements=False) |
139
|
|
|
uut.parse(self.file) |
140
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["A"]), |
141
|
|
|
["a", "b", "c"]) |
142
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["B"]), |
143
|
|
|
["a", "", "", "d"]) |
144
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["C"]), |
145
|
|
|
["", "", "", ""]) |
146
|
|
|
|
147
|
|
|
def test_config_directory(self): |
148
|
|
|
self.uut.parse(self.tempdir) |
149
|
|
|
|
150
|
|
|
if __name__ == '__main__': |
151
|
|
|
unittest.main(verbosity=2) |
152
|
|
|
|