/
test_configParser.py
186 lines (113 loc) · 3.07 KB
/
test_configParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
from buildutil.configParser import ConfigParser
from buildutil.configParser import array2Dict
import os
import pytest
iniFilepath = "/tmp/buildutil/test.ini"
def cleanup():
# Make sure the inifile is removed before the test
if os.path.exists(iniFilepath):
os.remove(iniFilepath)
assert os.path.exists(iniFilepath) is False
def check(section, key, expected_value):
value = os.environ.get(key)
assert value == expected_value
############################################################
def test_noDefaultsArgGiven():
cleanup()
ConfigParser(iniFilepath)
assert os.path.exists(iniFilepath) is False
cleanup()
def test_NoneDefaultsGiven():
cleanup()
ConfigParser(iniFilepath, None)
assert os.path.exists(iniFilepath) is False
cleanup()
def test_noIniFilepathGiven():
cleanup()
with pytest.raises(Exception):
ConfigParser(None)
assert os.path.exists(iniFilepath) is False
cleanup()
def test_readDefaults():
cleanup()
lst = array2Dict([
# Section Key Default Options
["MAKE", "PORT", "posix", {"posix", "stm32f072rb"}],
["MAKE", "TARGET", "dbg", {"dbg", "rel"}],
["MAKE", "test", "dbg", {"dbg", "rel"}],
])
parser = ConfigParser(iniFilepath, lst)
parser.setenv()
for d in lst:
section = d["section"]
key = d["key"]
expected_value = d["default"]
check(section, key, expected_value)
cleanup()
def test_types():
cleanup()
lst = array2Dict([
# Section Key Default Options
["MAKE", "PORT", "posix", {"posix", "stm32f072rb"}],
["MAKE", "TARGET", "True", {"True", "False"}],
# ["MAKE", "test", True, {True, False}],
])
parser = ConfigParser(iniFilepath, lst)
parser.setenv()
for d in lst:
section = d["section"]
key = d["key"]
expected_value = d["default"]
check(section, key, expected_value)
cleanup()
def test_writeRead():
cleanup()
parser = ConfigParser(iniFilepath)
parser.write("SECTION", "TEST", "Hello")
rv = parser.read("SECTION", "TEST")
assert rv == "Hello"
cleanup()
def test_readSomethingNotWritten():
cleanup()
parser = ConfigParser(iniFilepath)
rv = parser.read("SECTION", "TEST")
assert rv is None
cleanup()
def test_overWriteDefaultWithValid():
cleanup()
lst = array2Dict([
# Section Key Default Options
["MAKE", "PORT", "posix", {"posix", "stm32f072rb"}]
])
parser = ConfigParser(iniFilepath, lst)
# Read default
rv = parser.read("MAKE", "PORT")
assert rv == "posix"
# Overwrite default
parser.write("MAKE", "PORT", "stm32f072rb")
# Check
rv = parser.read("MAKE", "PORT")
assert rv == "stm32f072rb"
cleanup()
def test_overWriteDefaultWithInvalid():
cleanup()
lst = array2Dict([
# Section Key Default Options
["MAKE", "PORT", "posix", {"posix", "stm32f072rb"}]
])
parser = ConfigParser(iniFilepath, lst)
# Read default
rv = parser.read("MAKE", "PORT")
assert rv == "posix"
# Overwrite default
with pytest.raises(Exception):
parser.write("MAKE", "PORT", "new")
# Check that old value is still there
rv = parser.read("MAKE", "PORT")
assert rv == "posix"
cleanup()
def main():
pass
if __name__ == "__main__":
main()