1
|
|
|
import os |
2
|
|
|
import sys |
3
|
|
|
import re |
4
|
|
|
import tempfile |
5
|
|
|
import unittest |
6
|
|
|
from pyprint.NullPrinter import NullPrinter |
7
|
|
|
from pyprint.ClosableObject import close_objects |
8
|
|
|
|
9
|
|
|
sys.path.insert(0, ".") |
10
|
|
|
from coalib.misc.Compatability import FileNotFoundError |
11
|
|
|
from coalib.misc.Constants import Constants |
12
|
|
|
from coalib.settings.ConfigurationGathering import (gather_configuration, |
13
|
|
|
find_user_config) |
14
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class ConfigurationGatheringTest(unittest.TestCase): |
18
|
|
|
def setUp(self): |
19
|
|
|
self.log_printer = LogPrinter(NullPrinter()) |
20
|
|
|
|
21
|
|
|
def tearDown(self): |
22
|
|
|
close_objects(self.log_printer) |
23
|
|
|
|
24
|
|
|
def test_gather_configuration(self): |
25
|
|
|
# We need to use a bad filename or this will parse coalas .coafile |
26
|
|
|
args = (lambda *args: True, self.log_printer) |
27
|
|
|
kwargs = {"arg_list": ['-S', "test=5", "-c", "some_bad_filename"]} |
28
|
|
|
with self.assertRaises(FileNotFoundError): |
29
|
|
|
gather_configuration(*args, **kwargs) |
30
|
|
|
|
31
|
|
|
kwargs["arg_list"].append("-s") |
32
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
33
|
|
|
*args, **kwargs) |
34
|
|
|
os.remove("some_bad_filename") |
35
|
|
|
|
36
|
|
|
self.assertEqual(str(sections["default"]), |
37
|
|
|
"Default {config : 'some_bad_filename', " |
38
|
|
|
"save : 'True', test : '5'}") |
39
|
|
|
|
40
|
|
|
kwargs["arg_list"] = ['-S test=5', |
41
|
|
|
'-c bad_filename', |
42
|
|
|
'-b LineCountBear'] |
43
|
|
|
with self.assertRaises(FileNotFoundError): |
44
|
|
|
gather_configuration(*args, **kwargs) |
45
|
|
|
|
46
|
|
|
kwargs["arg_list"].append("-s") |
47
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
48
|
|
|
*args, **kwargs) |
49
|
|
|
os.remove("bad_filename") |
50
|
|
|
|
51
|
|
|
self.assertEqual(len(local_bears["default"]), 1) |
52
|
|
|
|
53
|
|
|
def test_default_coafile_parsing(self): |
54
|
|
|
tmp = Constants.system_coafile |
55
|
|
|
Constants.system_coafile = os.path.abspath(os.path.join( |
56
|
|
|
os.path.dirname(os.path.realpath(__file__)), |
57
|
|
|
"section_manager_test_files", |
58
|
|
|
"default_coafile")) |
59
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
60
|
|
|
lambda *args: True, |
61
|
|
|
self.log_printer) |
62
|
|
|
self.assertEqual(str(sections["test"]), |
63
|
|
|
"test {value : '1', testval : '5'}") |
64
|
|
|
Constants.system_coafile = tmp |
65
|
|
|
|
66
|
|
|
def test_user_coafile_parsing(self): |
67
|
|
|
tmp = Constants.user_coafile |
68
|
|
|
Constants.user_coafile = os.path.abspath(os.path.join( |
69
|
|
|
os.path.dirname(os.path.realpath(__file__)), |
70
|
|
|
"section_manager_test_files", |
71
|
|
|
"default_coafile")) |
72
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
73
|
|
|
lambda *args: True, |
74
|
|
|
self.log_printer) |
75
|
|
|
|
76
|
|
|
self.assertEqual(str(sections["test"]), |
77
|
|
|
"test {value : '1', testval : '5'}") |
78
|
|
|
|
79
|
|
|
Constants.user_coafile = tmp |
80
|
|
|
|
81
|
|
|
def test_nonexistent_file(self): |
82
|
|
|
filename = "bad.one/test\neven with bad chars in it" |
83
|
|
|
with self.assertRaises(FileNotFoundError): |
84
|
|
|
gather_configuration(lambda *args: True, |
85
|
|
|
self.log_printer, |
86
|
|
|
arg_list=['-S', "config=" + filename]) |
87
|
|
|
|
88
|
|
|
tmp = Constants.system_coafile |
89
|
|
|
Constants.system_coafile = filename |
90
|
|
|
|
91
|
|
|
with self.assertRaises(FileNotFoundError): |
92
|
|
|
gather_configuration(lambda *args: True, self.log_printer) |
93
|
|
|
|
94
|
|
|
Constants.system_coafile = tmp |
95
|
|
|
|
96
|
|
|
def test_merge(self): |
97
|
|
|
tmp = Constants.system_coafile |
98
|
|
|
Constants.system_coafile = os.path.abspath(os.path.join( |
99
|
|
|
os.path.dirname(os.path.realpath(__file__)), |
100
|
|
|
"section_manager_test_files", |
101
|
|
|
"default_coafile")) |
102
|
|
|
|
103
|
|
|
config = os.path.abspath(os.path.join( |
104
|
|
|
os.path.dirname(os.path.realpath(__file__)), |
105
|
|
|
"section_manager_test_files", |
106
|
|
|
".coafile")) |
107
|
|
|
|
108
|
|
|
# Check merging of default_coafile and .coafile |
109
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
110
|
|
|
lambda *args: True, |
111
|
|
|
self.log_printer, |
112
|
|
|
arg_list=["-c", re.escape(config)]) |
113
|
|
|
self.assertEqual(str(sections["test"]), |
114
|
|
|
"test {value : '2'}") |
115
|
|
|
self.assertEqual(str(sections["test-2"]), |
116
|
|
|
"test-2 {files : '.', bears : 'LineCountBear'}") |
117
|
|
|
|
118
|
|
|
# Check merging of default_coafile, .coafile and cli |
119
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
120
|
|
|
lambda *args: True, |
121
|
|
|
self.log_printer, |
122
|
|
|
arg_list=["-c", |
123
|
|
|
re.escape(config), |
124
|
|
|
"-S", |
125
|
|
|
"test.value=3", |
126
|
|
|
"test-2.bears=", |
127
|
|
|
"test-5.bears=TestBear2"]) |
128
|
|
|
self.assertEqual(str(sections["test"]), "test {value : '3'}") |
129
|
|
|
self.assertEqual(str(sections["test-2"]), |
130
|
|
|
"test-2 {files : '.', bears : ''}") |
131
|
|
|
self.assertEqual(str(sections["test-3"]), |
132
|
|
|
"test-3 {files : 'MakeFile'}") |
133
|
|
|
self.assertEqual(str(sections["test-4"]), |
134
|
|
|
"test-4 {bears : 'TestBear'}") |
135
|
|
|
self.assertEqual(str(sections["test-5"]), |
136
|
|
|
"test-5 {bears : 'TestBear2'}") |
137
|
|
|
Constants.system_coafile = tmp |
138
|
|
|
|
139
|
|
|
def test_merge_defaults(self): |
140
|
|
|
args = (lambda *args: True, self.log_printer) |
141
|
|
|
kwargs = {"arg_list": ["-S", |
142
|
|
|
"value=1", |
143
|
|
|
"test.value=2", |
144
|
|
|
"-c", |
145
|
|
|
"bad_file_name"]} |
146
|
|
|
|
147
|
|
|
with self.assertRaises(FileNotFoundError): |
148
|
|
|
gather_configuration(*args, **kwargs) |
149
|
|
|
|
150
|
|
|
kwargs["arg_list"].append("-s") |
151
|
|
|
sections, local_bears, global_bears, targets = gather_configuration( |
152
|
|
|
*args, **kwargs) |
153
|
|
|
os.remove("bad_file_name") |
154
|
|
|
|
155
|
|
|
self.assertEqual(sections["default"], sections["test"].defaults) |
156
|
|
|
|
157
|
|
|
def test_back_saving(self): |
158
|
|
|
filename = os.path.join(tempfile.gettempdir(), |
159
|
|
|
"SectionManagerTestFile") |
160
|
|
|
|
161
|
|
|
# We need to use a bad filename or this will parse coalas .coafile |
162
|
|
|
gather_configuration( |
163
|
|
|
lambda *args: True, |
164
|
|
|
self.log_printer, |
165
|
|
|
arg_list=['-S', |
166
|
|
|
"save=" + re.escape(filename), |
167
|
|
|
"-c=some_bad_filename"]) |
168
|
|
|
|
169
|
|
|
with open(filename, "r") as f: |
170
|
|
|
lines = f.readlines() |
171
|
|
|
self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], |
172
|
|
|
lines) |
173
|
|
|
|
174
|
|
|
gather_configuration( |
175
|
|
|
lambda *args: True, |
176
|
|
|
self.log_printer, |
177
|
|
|
arg_list=['-S', |
178
|
|
|
"save=true", |
179
|
|
|
"config=" + re.escape(filename), |
180
|
|
|
"test.value=5"]) |
181
|
|
|
|
182
|
|
|
with open(filename, "r") as f: |
183
|
|
|
lines = f.readlines() |
184
|
|
|
os.remove(filename) |
185
|
|
|
self.assertEqual(["[Default]\n", |
186
|
|
|
"config = " + filename + "\n", |
187
|
|
|
"\n", |
188
|
|
|
"[test]\n", |
189
|
|
|
"value = 5\n"], lines) |
190
|
|
|
|
191
|
|
|
def test_targets(self): |
192
|
|
|
(sections, |
193
|
|
|
local_bears, |
194
|
|
|
global_bears, |
195
|
|
|
targets) = gather_configuration(lambda *args: True, |
196
|
|
|
self.log_printer, |
197
|
|
|
arg_list=["default", |
198
|
|
|
"test1", |
199
|
|
|
"test2"]) |
200
|
|
|
self.assertEqual(targets, ["default", "test1", "test2"]) |
201
|
|
|
|
202
|
|
|
def test_find_user_config(self): |
203
|
|
|
current_dir = os.path.abspath(os.path.dirname(__file__)) |
204
|
|
|
c_file = os.path.join(current_dir, |
205
|
|
|
"section_manager_test_files", |
206
|
|
|
"project", |
207
|
|
|
"test.c") |
208
|
|
|
|
209
|
|
|
retval = find_user_config(c_file, 1) |
210
|
|
|
self.assertEqual("", retval) |
211
|
|
|
|
212
|
|
|
retval = find_user_config(c_file, 2) |
213
|
|
|
self.assertEqual(os.path.join(current_dir, |
214
|
|
|
"section_manager_test_files", |
215
|
|
|
".coafile"), retval) |
216
|
|
|
|
217
|
|
|
# We need to use a bad filename or this will parse coalas .coafile |
218
|
|
|
with self.assertRaises(FileNotFoundError): |
219
|
|
|
sections, dummy, dummy, dummy = gather_configuration( |
220
|
|
|
lambda *args: True, |
221
|
|
|
self.log_printer, |
222
|
|
|
arg_list=["--find-config", "-c", "some_bad_filename"]) |
223
|
|
|
|
224
|
|
|
sections, dummy, dummy, dummy = gather_configuration( |
225
|
|
|
lambda *args: True, |
226
|
|
|
self.log_printer, |
227
|
|
|
arg_list=["--find-config"]) |
228
|
|
|
|
229
|
|
|
self.assertRegex(str(sections["default"]), |
230
|
|
|
".*find_config : 'True'.*, config : '.*'") |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
if __name__ == '__main__': |
234
|
|
|
unittest.main(verbosity=2) |
235
|
|
|
|