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