1 | import os |
||
2 | import re |
||
3 | import tempfile |
||
4 | import unittest |
||
5 | |||
6 | from pyprint.ClosableObject import close_objects |
||
7 | from pyprint.NullPrinter import NullPrinter |
||
8 | |||
9 | from coalib.misc import Constants |
||
10 | from coalib.misc.ContextManagers import make_temp, change_directory |
||
11 | from coalib.output.printers.LogPrinter import LogPrinter |
||
12 | from coalib.parsing.StringProcessing import escape |
||
13 | from coalib.settings.ConfigurationGathering import ( |
||
14 | find_user_config, gather_configuration, get_config_directory, |
||
15 | load_configuration) |
||
16 | from coalib.settings.Section import Section |
||
17 | from coalib.settings.Setting import Setting |
||
18 | |||
19 | |||
20 | class ConfigurationGatheringTest(unittest.TestCase): |
||
21 | |||
22 | def setUp(self): |
||
23 | self.log_printer = LogPrinter(NullPrinter()) |
||
24 | |||
25 | def tearDown(self): |
||
26 | close_objects(self.log_printer) |
||
27 | |||
28 | def test_gather_configuration(self): |
||
29 | args = (lambda *args: True, self.log_printer) |
||
30 | |||
31 | # Passing the default coafile name only triggers a warning. |
||
32 | gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"]) |
||
33 | |||
34 | # Using a bad filename explicitly exits coala. |
||
35 | with self.assertRaises(SystemExit): |
||
36 | gather_configuration( |
||
37 | *args, |
||
38 | arg_list=["-S", "test=5", "-c", "some_bad_filename"]) |
||
39 | |||
40 | with make_temp() as temporary: |
||
41 | sections, local_bears, global_bears, targets = ( |
||
42 | gather_configuration( |
||
43 | *args, |
||
44 | arg_list=["-S", |
||
45 | "test=5", |
||
46 | "-c", |
||
47 | escape(temporary, "\\"), |
||
48 | "-s"])) |
||
49 | |||
50 | self.assertEqual(str(sections["default"]), |
||
51 | "Default {config : " + |
||
52 | repr(temporary) + ", save : 'True', test : '5'}") |
||
53 | |||
54 | with make_temp() as temporary: |
||
55 | sections, local_bears, global_bears, targets = ( |
||
56 | gather_configuration(*args, |
||
57 | arg_list=["-S test=5", |
||
58 | "-c " + escape(temporary, "\\"), |
||
59 | "-b LineCountBear -s"])) |
||
60 | |||
61 | self.assertEqual(len(local_bears["default"]), 0) |
||
62 | |||
63 | View Code Duplication | def test_default_coafile_parsing(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
64 | tmp = Constants.system_coafile |
||
65 | |||
66 | Constants.system_coafile = os.path.abspath(os.path.join( |
||
67 | os.path.dirname(os.path.realpath(__file__)), |
||
68 | "section_manager_test_files", |
||
69 | "default_coafile")) |
||
70 | |||
71 | sections, local_bears, global_bears, targets = gather_configuration( |
||
72 | lambda *args: True, |
||
73 | self.log_printer, |
||
74 | arg_list=[]) |
||
75 | |||
76 | self.assertEqual(str(sections["test"]), |
||
77 | "test {value : '1', testval : '5'}") |
||
78 | |||
79 | Constants.system_coafile = tmp |
||
80 | |||
81 | View Code Duplication | def test_user_coafile_parsing(self): |
|
0 ignored issues
–
show
|
|||
82 | tmp = Constants.user_coafile |
||
83 | |||
84 | Constants.user_coafile = os.path.abspath(os.path.join( |
||
85 | os.path.dirname(os.path.realpath(__file__)), |
||
86 | "section_manager_test_files", |
||
87 | "default_coafile")) |
||
88 | |||
89 | sections, local_bears, global_bears, targets = gather_configuration( |
||
90 | lambda *args: True, |
||
91 | self.log_printer, |
||
92 | arg_list=[]) |
||
93 | |||
94 | self.assertEqual(str(sections["test"]), |
||
95 | "test {value : '1', testval : '5'}") |
||
96 | |||
97 | Constants.user_coafile = tmp |
||
98 | |||
99 | def test_nonexistent_file(self): |
||
100 | filename = "bad.one/test\neven with bad chars in it" |
||
101 | with self.assertRaises(SystemExit): |
||
102 | gather_configuration(lambda *args: True, |
||
103 | self.log_printer, |
||
104 | arg_list=['-S', "config=" + filename]) |
||
105 | |||
106 | tmp = Constants.system_coafile |
||
107 | Constants.system_coafile = filename |
||
108 | |||
109 | with self.assertRaises(SystemExit): |
||
110 | gather_configuration(lambda *args: True, |
||
111 | self.log_printer, |
||
112 | arg_list=[]) |
||
113 | |||
114 | Constants.system_coafile = tmp |
||
115 | |||
116 | def test_merge(self): |
||
117 | tmp = Constants.system_coafile |
||
118 | Constants.system_coafile = os.path.abspath(os.path.join( |
||
119 | os.path.dirname(os.path.realpath(__file__)), |
||
120 | "section_manager_test_files", |
||
121 | "default_coafile")) |
||
122 | |||
123 | config = os.path.abspath(os.path.join( |
||
124 | os.path.dirname(os.path.realpath(__file__)), |
||
125 | "section_manager_test_files", |
||
126 | ".coafile")) |
||
127 | |||
128 | # Check merging of default_coafile and .coafile |
||
129 | sections, local_bears, global_bears, targets = gather_configuration( |
||
130 | lambda *args: True, |
||
131 | self.log_printer, |
||
132 | arg_list=["-c", re.escape(config)]) |
||
133 | |||
134 | self.assertEqual(str(sections["test"]), |
||
135 | "test {value : '2'}") |
||
136 | self.assertEqual(str(sections["test-2"]), |
||
137 | "test-2 {files : '.', bears : 'LineCountBear'}") |
||
138 | |||
139 | # Check merging of default_coafile, .coafile and cli |
||
140 | sections, local_bears, global_bears, targets = gather_configuration( |
||
141 | lambda *args: True, |
||
142 | self.log_printer, |
||
143 | arg_list=["-c", |
||
144 | re.escape(config), |
||
145 | "-S", |
||
146 | "test.value=3", |
||
147 | "test-2.bears=", |
||
148 | "test-5.bears=TestBear2"]) |
||
149 | |||
150 | self.assertEqual(str(sections["test"]), "test {value : '3'}") |
||
151 | self.assertEqual(str(sections["test-2"]), |
||
152 | "test-2 {files : '.', bears : ''}") |
||
153 | self.assertEqual(str(sections["test-3"]), |
||
154 | "test-3 {files : 'MakeFile'}") |
||
155 | self.assertEqual(str(sections["test-4"]), |
||
156 | "test-4 {bears : 'TestBear'}") |
||
157 | self.assertEqual(str(sections["test-5"]), |
||
158 | "test-5 {bears : 'TestBear2'}") |
||
159 | |||
160 | Constants.system_coafile = tmp |
||
161 | |||
162 | def test_merge_defaults(self): |
||
163 | with make_temp() as temporary: |
||
164 | sections, local_bears, global_bears, targets = ( |
||
165 | gather_configuration(lambda *args: True, |
||
166 | self.log_printer, |
||
167 | arg_list=["-S", |
||
168 | "value=1", |
||
169 | "test.value=2", |
||
170 | "-c", |
||
171 | escape(temporary, "\\")])) |
||
172 | |||
173 | self.assertEqual(sections["default"], |
||
174 | sections["test"].defaults) |
||
175 | |||
176 | def test_back_saving(self): |
||
177 | filename = os.path.join(tempfile.gettempdir(), |
||
178 | "SectionManagerTestFile") |
||
179 | |||
180 | # We need to use a bad filename or this will parse coalas .coafile |
||
181 | gather_configuration( |
||
182 | lambda *args: True, |
||
183 | self.log_printer, |
||
184 | arg_list=['-S', |
||
185 | "save=" + re.escape(filename), |
||
186 | "-c=some_bad_filename"]) |
||
187 | |||
188 | with open(filename, "r") as f: |
||
189 | lines = f.readlines() |
||
190 | self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines) |
||
191 | |||
192 | gather_configuration( |
||
193 | lambda *args: True, |
||
194 | self.log_printer, |
||
195 | arg_list=['-S', |
||
196 | "save=true", |
||
197 | "config=" + re.escape(filename), |
||
198 | "test.value=5"]) |
||
199 | |||
200 | with open(filename, "r") as f: |
||
201 | lines = f.readlines() |
||
202 | os.remove(filename) |
||
203 | if os.path.sep == '\\': |
||
204 | filename = escape(filename, '\\') |
||
205 | self.assertEqual(["[Default]\n", |
||
206 | "config = " + filename + "\n", |
||
207 | "\n", |
||
208 | "[test]\n", |
||
209 | "value = 5\n"], lines) |
||
210 | |||
211 | def test_targets(self): |
||
212 | sections, local_bears, global_bears, targets = gather_configuration( |
||
213 | lambda *args: True, |
||
214 | self.log_printer, |
||
215 | arg_list=["default", "test1", "test2"]) |
||
216 | |||
217 | self.assertEqual(targets, ["default", "test1", "test2"]) |
||
218 | |||
219 | def test_find_user_config(self): |
||
220 | current_dir = os.path.abspath(os.path.dirname(__file__)) |
||
221 | c_file = os.path.join(current_dir, |
||
222 | "section_manager_test_files", |
||
223 | "project", |
||
224 | "test.c") |
||
225 | |||
226 | retval = find_user_config(c_file, 1) |
||
227 | self.assertEqual("", retval) |
||
228 | |||
229 | retval = find_user_config(c_file, 2) |
||
230 | self.assertEqual(os.path.join(current_dir, |
||
231 | "section_manager_test_files", |
||
232 | ".coafile"), retval) |
||
233 | |||
234 | child_dir = os.path.join(current_dir, |
||
235 | "section_manager_test_files", |
||
236 | "child_dir") |
||
237 | retval = find_user_config(child_dir, 2) |
||
238 | self.assertEqual(os.path.join(current_dir, |
||
239 | "section_manager_test_files", |
||
240 | "child_dir", |
||
241 | ".coafile"), retval) |
||
242 | |||
243 | with change_directory(child_dir): |
||
244 | sections, _, _, _ = gather_configuration( |
||
245 | lambda *args: True, |
||
246 | self.log_printer, |
||
247 | arg_list=["--find-config"]) |
||
248 | self.assertEqual(bool(sections["default"]['find_config']), True) |
||
249 | |||
250 | def test_no_config(self): |
||
251 | current_dir = os.path.abspath(os.path.dirname(__file__)) |
||
252 | child_dir = os.path.join(current_dir, |
||
253 | "section_manager_test_files", |
||
254 | "child_dir") |
||
255 | with change_directory(child_dir): |
||
256 | sections, targets = load_configuration([], self.log_printer) |
||
257 | self.assertIn('value', sections["default"]) |
||
258 | |||
259 | sections, targets = load_configuration( |
||
260 | ['--no-config'], |
||
261 | self.log_printer) |
||
262 | self.assertNotIn('value', sections["default"]) |
||
263 | |||
264 | sections, targets = load_configuration( |
||
265 | ['--no-config', '-S', 'use_spaces=True'], |
||
266 | self.log_printer) |
||
267 | self.assertIn('use_spaces', sections["default"]) |
||
268 | self.assertNotIn('values', sections["default"]) |
||
269 | |||
270 | sections, targets = load_configuration( |
||
271 | ['--no-config', 'False', '-S', 'use_spaces=True'], |
||
272 | self.log_printer) |
||
273 | self.assertIn('use_spaces', sections["default"]) |
||
274 | self.assertIn('value', sections["default"]) |
||
275 | |||
276 | with self.assertRaises(SystemExit) as cm: |
||
277 | sections, target = load_configuration( |
||
278 | ['--no-config', '--save'], |
||
279 | self.log_printer) |
||
280 | self.assertEqual(cm.exception.code, 2) |
||
281 | |||
282 | with self.assertRaises(SystemExit) as cm: |
||
283 | sections, target = load_configuration( |
||
284 | ['--no-config', '--find-config'], |
||
285 | self.log_printer) |
||
286 | self.assertEqual(cm.exception.code, 2) |
||
287 | |||
288 | def test_get_config_directory(self): |
||
289 | old_isfile = os.path.isfile |
||
290 | old_isdir = os.path.isdir |
||
291 | |||
292 | section = Section("default") |
||
293 | |||
294 | # Without section |
||
295 | config_dir = get_config_directory(None) |
||
296 | self.assertEqual(config_dir, os.getcwd()) |
||
297 | |||
298 | # With section, but without "config" |
||
299 | os.path.isfile = lambda *args: True |
||
300 | config_dir = get_config_directory(section) |
||
301 | self.assertEqual(config_dir, os.getcwd()) |
||
302 | |||
303 | os.path.isfile = lambda *args: False |
||
304 | config_dir = get_config_directory(section) |
||
305 | self.assertEqual(config_dir, None) |
||
306 | |||
307 | # With "config" in section |
||
308 | section.append(Setting("config", "/path/to/dir/config")) |
||
309 | |||
310 | os.path.isdir = lambda *args: True |
||
311 | config_dir = get_config_directory(section) |
||
312 | self.assertEqual(config_dir, "/path/to/dir/config") |
||
313 | |||
314 | os.path.isdir = lambda *args: False |
||
315 | config_dir = get_config_directory(section) |
||
316 | self.assertEqual(config_dir, "/path/to/dir") |
||
317 | |||
318 | os.path.isdir = old_isdir |
||
319 | os.path.isfile = old_isfile |
||
320 | |||
321 | def test_autoapply_arg(self): |
||
322 | sections, _, _, _ = gather_configuration( |
||
323 | lambda *args: True, |
||
324 | self.log_printer, |
||
325 | autoapply=False, |
||
326 | arg_list=[]) |
||
327 | |||
328 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
329 | 'False') |
||
330 | |||
331 | sections, _, _, _ = gather_configuration( |
||
332 | lambda *args: True, |
||
333 | self.log_printer, |
||
334 | autoapply=True, |
||
335 | arg_list=[]) |
||
336 | |||
337 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
338 | 'None') |
||
339 |