1
|
|
|
import os |
2
|
|
|
import tempfile |
3
|
|
|
import unittest |
4
|
|
|
from collections import OrderedDict |
5
|
|
|
|
6
|
|
|
from coalib.parsing.ConfParser import ConfParser |
7
|
|
|
from coalib.settings.Section import Section |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class ConfParserTest(unittest.TestCase): |
|
|
|
|
11
|
|
|
example_file = """to be ignored |
12
|
|
|
a_default, another = val |
13
|
|
|
TEST = tobeignored # do you know that thats a comment |
14
|
|
|
test = push |
15
|
|
|
t = |
16
|
|
|
escaped_\\=equal = escaped_\\#hash |
17
|
|
|
escaped_\\\\backslash = escaped_\\ space |
18
|
|
|
escaped_\\,comma = escaped_\\.dot |
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 file: |
|
|
|
|
40
|
|
|
file.write(self.example_file) |
41
|
|
|
|
42
|
|
|
self.uut = ConfParser() |
43
|
|
|
try: |
44
|
|
|
os.remove(self.nonexistentfile) |
45
|
|
|
except FileNotFoundError: |
|
|
|
|
46
|
|
|
pass |
47
|
|
|
|
48
|
|
|
self.sections = self.uut.parse(self.file) |
49
|
|
|
|
50
|
|
|
def tearDown(self): |
51
|
|
|
os.remove(self.file) |
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_parse_nonexisting_file(self): |
54
|
|
|
self.assertRaises(FileNotFoundError, |
|
|
|
|
55
|
|
|
self.uut.parse, |
|
|
|
|
56
|
|
|
self.nonexistentfile) |
57
|
|
|
self.assertNotEqual(self.uut.parse(self.file, True), self.sections) |
58
|
|
|
|
59
|
|
|
def test_parse_nonexisting_section(self): |
60
|
|
|
self.assertRaises(IndexError, |
|
|
|
|
61
|
|
|
self.uut.get_section, |
|
|
|
|
62
|
|
|
"inexistent section") |
63
|
|
|
|
64
|
|
|
def test_parse_default_section(self): |
65
|
|
|
default_should = OrderedDict([ |
66
|
|
|
('a_default', 'val'), |
67
|
|
|
('another', 'val'), |
68
|
|
|
('comment0', '# do you know that thats a comment'), |
69
|
|
|
('test', 'content'), |
70
|
|
|
('t', ''), |
71
|
|
|
('escaped_=equal', 'escaped_#hash'), |
72
|
|
|
('escaped_\\backslash', 'escaped_ space'), |
73
|
|
|
('escaped_,comma', 'escaped_.dot')]) |
74
|
|
|
|
75
|
|
|
key, val = self.sections.popitem(last=False) |
76
|
|
|
self.assertTrue(isinstance(val, Section)) |
|
|
|
|
77
|
|
|
self.assertEqual(key, 'default') |
|
|
|
|
78
|
|
|
|
79
|
|
|
is_dict = OrderedDict() |
80
|
|
|
for k in val: |
81
|
|
|
is_dict[k] = str(val[k]) |
|
|
|
|
82
|
|
|
self.assertEqual(is_dict, default_should) |
|
|
|
|
83
|
|
|
|
84
|
|
View Code Duplication |
def test_parse_makefiles_section(self): |
|
|
|
|
85
|
|
|
makefiles_should = OrderedDict([ |
86
|
|
|
('j', 'a\nmultiline\nvalue'), |
87
|
|
|
('another', 'a\nmultiline\nvalue'), |
88
|
|
|
('comment1', '# just a omment'), |
89
|
|
|
('comment2', '# just a omment'), |
90
|
|
|
('lastone', 'val'), |
91
|
|
|
('comment3', ''), |
92
|
|
|
('a_default', 'val'), |
93
|
|
|
('comment0', '# do you know that thats a comment'), |
94
|
|
|
('test', 'content'), |
95
|
|
|
('t', ''), |
96
|
|
|
('escaped_=equal', 'escaped_#hash'), |
97
|
|
|
('escaped_\\backslash', 'escaped_ space'), |
98
|
|
|
('escaped_,comma', 'escaped_.dot')]) |
99
|
|
|
|
100
|
|
|
# Pop off the default section. |
101
|
|
|
self.sections.popitem(last=False) |
102
|
|
|
|
103
|
|
|
key, val = self.sections.popitem(last=False) |
104
|
|
|
self.assertTrue(isinstance(val, Section)) |
|
|
|
|
105
|
|
|
self.assertEqual(key, 'makefiles') |
|
|
|
|
106
|
|
|
|
107
|
|
|
is_dict = OrderedDict() |
108
|
|
|
for k in val: |
109
|
|
|
is_dict[k] = str(val[k]) |
|
|
|
|
110
|
|
|
self.assertEqual(is_dict, makefiles_should) |
|
|
|
|
111
|
|
|
|
112
|
|
|
self.assertEqual(val["comment1"].key, "comment1") |
113
|
|
|
|
114
|
|
View Code Duplication |
def test_parse_empty_elem_strip_section(self): |
|
|
|
|
115
|
|
|
empty_elem_strip_should = OrderedDict([ |
116
|
|
|
('a', 'a, b, c'), |
117
|
|
|
('b', 'a, ,, d'), |
118
|
|
|
('c', ',,,'), |
119
|
|
|
('comment4', ''), |
120
|
|
|
('a_default', 'val'), |
121
|
|
|
('another', 'val'), |
122
|
|
|
('comment0', '# do you know that thats a comment'), |
123
|
|
|
('test', 'content'), |
124
|
|
|
('t', ''), |
125
|
|
|
('escaped_=equal', 'escaped_#hash'), |
126
|
|
|
('escaped_\\backslash', 'escaped_ space'), |
127
|
|
|
('escaped_,comma', 'escaped_.dot')]) |
128
|
|
|
|
129
|
|
|
# Pop off the default and makefiles section. |
130
|
|
|
self.sections.popitem(last=False) |
131
|
|
|
self.sections.popitem(last=False) |
132
|
|
|
|
133
|
|
|
key, val = self.sections.popitem(last=False) |
134
|
|
|
self.assertTrue(isinstance(val, Section)) |
|
|
|
|
135
|
|
|
self.assertEqual(key, 'empty_elem_strip') |
|
|
|
|
136
|
|
|
|
137
|
|
|
is_dict = OrderedDict() |
138
|
|
|
for k in val: |
139
|
|
|
is_dict[k] = str(val[k]) |
|
|
|
|
140
|
|
|
self.assertEqual(is_dict, empty_elem_strip_should) |
|
|
|
|
141
|
|
|
|
142
|
|
|
def test_remove_empty_iter_elements(self): |
143
|
|
|
# Test with empty-elem stripping. |
144
|
|
|
uut = ConfParser(remove_empty_iter_elements=True) |
145
|
|
|
uut.parse(self.file) |
|
|
|
|
146
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["A"]), |
147
|
|
|
["a", "b", "c"]) |
148
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["B"]), |
149
|
|
|
["a", "d"]) |
150
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["C"]), |
151
|
|
|
[]) |
152
|
|
|
|
153
|
|
|
# Test without stripping. |
154
|
|
|
uut = ConfParser(remove_empty_iter_elements=False) |
155
|
|
|
uut.parse(self.file) |
156
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["A"]), |
157
|
|
|
["a", "b", "c"]) |
158
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["B"]), |
159
|
|
|
["a", "", "", "d"]) |
160
|
|
|
self.assertEqual(list(uut.get_section("EMPTY_ELEM_STRIP")["C"]), |
161
|
|
|
["", "", "", ""]) |
162
|
|
|
|
163
|
|
|
def test_config_directory(self): |
164
|
|
|
self.uut.parse(self.tempdir) |
|
|
|
|
165
|
|
|
|