Total Complexity | 40 |
Total Lines | 274 |
Duplicated Lines | 0 % |
Complex classes like coalib.tests.settings.ConfigurationGatheringTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import os |
||
19 | class ConfigurationGatheringTest(unittest.TestCase): |
||
20 | |||
21 | def setUp(self): |
||
22 | self.log_printer = LogPrinter(NullPrinter()) |
||
23 | |||
24 | def tearDown(self): |
||
25 | close_objects(self.log_printer) |
||
26 | |||
27 | def test_gather_configuration(self): |
||
28 | args = (lambda *args: True, self.log_printer) |
||
29 | |||
30 | # Passing the default coafile name only triggers a warning. |
||
31 | gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"]) |
||
32 | |||
33 | # Using a bad filename explicitly exits coala. |
||
34 | with self.assertRaises(SystemExit): |
||
35 | gather_configuration( |
||
36 | *args, |
||
37 | arg_list=["-S", "test=5", "-c", "some_bad_filename"]) |
||
38 | |||
39 | with make_temp() as temporary: |
||
40 | sections, local_bears, global_bears, targets = ( |
||
41 | gather_configuration( |
||
42 | *args, |
||
43 | arg_list=["-S", |
||
44 | "test=5", |
||
45 | "-c", |
||
46 | escape(temporary, "\\"), |
||
47 | "-s"])) |
||
48 | |||
49 | self.assertEqual(str(sections["default"]), |
||
50 | "Default {config : " + repr(temporary) + ", save : " |
||
51 | "'True', test : '5'}") |
||
52 | |||
53 | with make_temp() as temporary: |
||
54 | sections, local_bears, global_bears, targets = ( |
||
55 | gather_configuration(*args, |
||
56 | arg_list=["-S test=5", |
||
57 | "-c " + escape(temporary, "\\"), |
||
58 | "-b LineCountBear -s"])) |
||
59 | |||
60 | self.assertEqual(len(local_bears["default"]), 0) |
||
61 | |||
62 | def test_default_coafile_parsing(self): |
||
63 | tmp = Constants.system_coafile |
||
64 | |||
65 | Constants.system_coafile = os.path.abspath(os.path.join( |
||
66 | os.path.dirname(os.path.realpath(__file__)), |
||
67 | "section_manager_test_files", |
||
68 | "default_coafile")) |
||
69 | |||
70 | sections, local_bears, global_bears, targets = gather_configuration( |
||
71 | lambda *args: True, |
||
72 | self.log_printer, |
||
73 | arg_list=[]) |
||
74 | |||
75 | self.assertEqual(str(sections["test"]), |
||
76 | "test {value : '1', testval : '5'}") |
||
77 | |||
78 | Constants.system_coafile = tmp |
||
79 | |||
80 | def test_user_coafile_parsing(self): |
||
81 | tmp = Constants.user_coafile |
||
82 | |||
83 | Constants.user_coafile = os.path.abspath(os.path.join( |
||
84 | os.path.dirname(os.path.realpath(__file__)), |
||
85 | "section_manager_test_files", |
||
86 | "default_coafile")) |
||
87 | |||
88 | sections, local_bears, global_bears, targets = gather_configuration( |
||
89 | lambda *args: True, |
||
90 | self.log_printer, |
||
91 | arg_list=[]) |
||
92 | |||
93 | self.assertEqual(str(sections["test"]), |
||
94 | "test {value : '1', testval : '5'}") |
||
95 | |||
96 | Constants.user_coafile = tmp |
||
97 | |||
98 | def test_nonexistent_file(self): |
||
99 | filename = "bad.one/test\neven with bad chars in it" |
||
100 | with self.assertRaises(SystemExit): |
||
101 | gather_configuration(lambda *args: True, |
||
102 | self.log_printer, |
||
103 | arg_list=['-S', "config=" + filename]) |
||
104 | |||
105 | tmp = Constants.system_coafile |
||
106 | Constants.system_coafile = filename |
||
107 | |||
108 | with self.assertRaises(SystemExit): |
||
109 | gather_configuration(lambda *args: True, |
||
110 | self.log_printer, |
||
111 | arg_list=[]) |
||
112 | |||
113 | Constants.system_coafile = tmp |
||
114 | |||
115 | def test_merge(self): |
||
116 | tmp = Constants.system_coafile |
||
117 | Constants.system_coafile = os.path.abspath(os.path.join( |
||
118 | os.path.dirname(os.path.realpath(__file__)), |
||
119 | "section_manager_test_files", |
||
120 | "default_coafile")) |
||
121 | |||
122 | config = os.path.abspath(os.path.join( |
||
123 | os.path.dirname(os.path.realpath(__file__)), |
||
124 | "section_manager_test_files", |
||
125 | ".coafile")) |
||
126 | |||
127 | # Check merging of default_coafile and .coafile |
||
128 | sections, local_bears, global_bears, targets = gather_configuration( |
||
129 | lambda *args: True, |
||
130 | self.log_printer, |
||
131 | arg_list=["-c", re.escape(config)]) |
||
132 | |||
133 | self.assertEqual(str(sections["test"]), |
||
134 | "test {value : '2'}") |
||
135 | self.assertEqual(str(sections["test-2"]), |
||
136 | "test-2 {files : '.', bears : 'LineCountBear'}") |
||
137 | |||
138 | # Check merging of default_coafile, .coafile and cli |
||
139 | sections, local_bears, global_bears, targets = gather_configuration( |
||
140 | lambda *args: True, |
||
141 | self.log_printer, |
||
142 | arg_list=["-c", |
||
143 | re.escape(config), |
||
144 | "-S", |
||
145 | "test.value=3", |
||
146 | "test-2.bears=", |
||
147 | "test-5.bears=TestBear2"]) |
||
148 | |||
149 | self.assertEqual(str(sections["test"]), "test {value : '3'}") |
||
150 | self.assertEqual(str(sections["test-2"]), |
||
151 | "test-2 {files : '.', bears : ''}") |
||
152 | self.assertEqual(str(sections["test-3"]), |
||
153 | "test-3 {files : 'MakeFile'}") |
||
154 | self.assertEqual(str(sections["test-4"]), |
||
155 | "test-4 {bears : 'TestBear'}") |
||
156 | self.assertEqual(str(sections["test-5"]), |
||
157 | "test-5 {bears : 'TestBear2'}") |
||
158 | |||
159 | Constants.system_coafile = tmp |
||
160 | |||
161 | def test_merge_defaults(self): |
||
162 | with make_temp() as temporary: |
||
163 | sections, local_bears, global_bears, targets = ( |
||
164 | gather_configuration(lambda *args: True, |
||
165 | self.log_printer, |
||
166 | arg_list=["-S", |
||
167 | "value=1", |
||
168 | "test.value=2", |
||
169 | "-c", |
||
170 | escape(temporary, "\\")])) |
||
171 | |||
172 | self.assertEqual(sections["default"], |
||
173 | sections["test"].defaults) |
||
174 | |||
175 | def test_back_saving(self): |
||
176 | filename = os.path.join(tempfile.gettempdir(), |
||
177 | "SectionManagerTestFile") |
||
178 | |||
179 | # We need to use a bad filename or this will parse coalas .coafile |
||
180 | gather_configuration( |
||
181 | lambda *args: True, |
||
182 | self.log_printer, |
||
183 | arg_list=['-S', |
||
184 | "save=" + re.escape(filename), |
||
185 | "-c=some_bad_filename"]) |
||
186 | |||
187 | with open(filename, "r") as f: |
||
188 | lines = f.readlines() |
||
189 | self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], |
||
190 | 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 | sections, dummy, dummy, dummy = gather_configuration( |
||
235 | lambda *args: True, |
||
236 | self.log_printer, |
||
237 | arg_list=["--find-config"]) |
||
238 | |||
239 | self.assertRegex(str(sections["default"]), |
||
240 | ".*find_config : 'True'.*, config : '.*'") |
||
241 | |||
242 | def test_get_config_directory(self): |
||
243 | old_isfile = os.path.isfile |
||
244 | old_isdir = os.path.isdir |
||
245 | |||
246 | section = Section("default") |
||
247 | |||
248 | # Without section |
||
249 | config_dir = get_config_directory(None) |
||
250 | self.assertEqual(config_dir, os.getcwd()) |
||
251 | |||
252 | # With section, but without "config" |
||
253 | os.path.isfile = lambda *args: True |
||
254 | config_dir = get_config_directory(section) |
||
255 | self.assertEqual(config_dir, os.getcwd()) |
||
256 | |||
257 | os.path.isfile = lambda *args: False |
||
258 | config_dir = get_config_directory(section) |
||
259 | self.assertEqual(config_dir, None) |
||
260 | |||
261 | # With "config" in section |
||
262 | section.append(Setting("config", "/path/to/dir/config")) |
||
263 | |||
264 | os.path.isdir = lambda *args: True |
||
265 | config_dir = get_config_directory(section) |
||
266 | self.assertEqual(config_dir, "/path/to/dir/config") |
||
267 | |||
268 | os.path.isdir = lambda *args: False |
||
269 | config_dir = get_config_directory(section) |
||
270 | self.assertEqual(config_dir, "/path/to/dir") |
||
271 | |||
272 | os.path.isdir = old_isdir |
||
273 | os.path.isfile = old_isfile |
||
274 | |||
275 | def test_autoapply_arg(self): |
||
276 | sections, dummy, dummy, dummy = gather_configuration( |
||
277 | lambda *args: True, |
||
278 | self.log_printer, |
||
279 | autoapply=False, |
||
280 | arg_list=[]) |
||
281 | |||
282 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
283 | 'False') |
||
284 | |||
285 | sections, dummy, dummy, dummy = gather_configuration( |
||
286 | lambda *args: True, |
||
287 | self.log_printer, |
||
288 | autoapply=True, |
||
289 | arg_list=[]) |
||
290 | |||
291 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
292 | 'None') |
||
293 |