Total Complexity | 40 |
Total Lines | 286 |
Duplicated Lines | 11.89 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like 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 |
||
17 | class ConfigurationGatheringTest(unittest.TestCase): |
||
|
|||
18 | |||
19 | def setUp(self): |
||
20 | self.log_printer = LogPrinter(NullPrinter()) |
||
21 | |||
22 | def tearDown(self): |
||
23 | close_objects(self.log_printer) |
||
24 | |||
25 | def test_gather_configuration(self): |
||
26 | args = (lambda *args: True, self.log_printer) |
||
27 | |||
28 | # Passing the default coafile name only triggers a warning. |
||
29 | gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"]) |
||
30 | |||
31 | # Using a bad filename explicitly exits coala. |
||
32 | with self.assertRaises(SystemExit): |
||
33 | gather_configuration( |
||
34 | *args, |
||
35 | arg_list=["-S", "test=5", "-c", "some_bad_filename"]) |
||
36 | |||
37 | with make_temp() as temporary: |
||
38 | sections, local_bears, global_bears, targets = ( |
||
39 | gather_configuration( |
||
40 | *args, |
||
41 | arg_list=["-S", |
||
42 | "test=5", |
||
43 | "-c", |
||
44 | escape(temporary, "\\"), |
||
45 | "-s"])) |
||
46 | |||
47 | self.assertEqual(str(sections["default"]), |
||
48 | "Default {config : " + |
||
49 | repr(temporary) + ", save : 'True', test : '5'}") |
||
50 | |||
51 | with make_temp() as temporary: |
||
52 | sections, local_bears, global_bears, targets = ( |
||
53 | gather_configuration(*args, |
||
54 | arg_list=["-S test=5", |
||
55 | "-c " + escape(temporary, "\\"), |
||
56 | "-b LineCountBear -s"])) |
||
57 | |||
58 | self.assertEqual(len(local_bears["default"]), 0) |
||
59 | |||
60 | View Code Duplication | def test_default_coafile_parsing(self): |
|
61 | tmp = Constants.system_coafile |
||
62 | |||
63 | Constants.system_coafile = os.path.abspath(os.path.join( |
||
64 | os.path.dirname(os.path.realpath(__file__)), |
||
65 | "section_manager_test_files", |
||
66 | "default_coafile")) |
||
67 | |||
68 | sections, local_bears, global_bears, targets = gather_configuration( |
||
69 | lambda *args: True, |
||
70 | self.log_printer, |
||
71 | arg_list=[]) |
||
72 | |||
73 | self.assertEqual(str(sections["test"]), |
||
74 | "test {value : '1', testval : '5'}") |
||
75 | |||
76 | Constants.system_coafile = tmp |
||
77 | |||
78 | View Code Duplication | def test_user_coafile_parsing(self): |
|
79 | tmp = Constants.user_coafile |
||
80 | |||
81 | Constants.user_coafile = os.path.abspath(os.path.join( |
||
82 | os.path.dirname(os.path.realpath(__file__)), |
||
83 | "section_manager_test_files", |
||
84 | "default_coafile")) |
||
85 | |||
86 | sections, local_bears, global_bears, targets = gather_configuration( |
||
87 | lambda *args: True, |
||
88 | self.log_printer, |
||
89 | arg_list=[]) |
||
90 | |||
91 | self.assertEqual(str(sections["test"]), |
||
92 | "test {value : '1', testval : '5'}") |
||
93 | |||
94 | Constants.user_coafile = tmp |
||
95 | |||
96 | def test_nonexistent_file(self): |
||
97 | filename = "bad.one/test\neven with bad chars in it" |
||
98 | with self.assertRaises(SystemExit): |
||
99 | gather_configuration(lambda *args: True, |
||
100 | self.log_printer, |
||
101 | arg_list=['-S', "config=" + filename]) |
||
102 | |||
103 | tmp = Constants.system_coafile |
||
104 | Constants.system_coafile = filename |
||
105 | |||
106 | with self.assertRaises(SystemExit): |
||
107 | gather_configuration(lambda *args: True, |
||
108 | self.log_printer, |
||
109 | arg_list=[]) |
||
110 | |||
111 | Constants.system_coafile = tmp |
||
112 | |||
113 | def test_merge(self): |
||
114 | tmp = Constants.system_coafile |
||
115 | Constants.system_coafile = os.path.abspath(os.path.join( |
||
116 | os.path.dirname(os.path.realpath(__file__)), |
||
117 | "section_manager_test_files", |
||
118 | "default_coafile")) |
||
119 | |||
120 | config = os.path.abspath(os.path.join( |
||
121 | os.path.dirname(os.path.realpath(__file__)), |
||
122 | "section_manager_test_files", |
||
123 | ".coafile")) |
||
124 | |||
125 | # Check merging of default_coafile and .coafile |
||
126 | sections, local_bears, global_bears, targets = gather_configuration( |
||
127 | lambda *args: True, |
||
128 | self.log_printer, |
||
129 | arg_list=["-c", re.escape(config)]) |
||
130 | |||
131 | self.assertEqual(str(sections["test"]), |
||
132 | "test {value : '2'}") |
||
133 | self.assertEqual(str(sections["test-2"]), |
||
134 | "test-2 {files : '.', bears : 'LineCountBear'}") |
||
135 | |||
136 | # Check merging of default_coafile, .coafile and cli |
||
137 | sections, local_bears, global_bears, targets = gather_configuration( |
||
138 | lambda *args: True, |
||
139 | self.log_printer, |
||
140 | arg_list=["-c", |
||
141 | re.escape(config), |
||
142 | "-S", |
||
143 | "test.value=3", |
||
144 | "test-2.bears=", |
||
145 | "test-5.bears=TestBear2"]) |
||
146 | |||
147 | self.assertEqual(str(sections["test"]), "test {value : '3'}") |
||
148 | self.assertEqual(str(sections["test-2"]), |
||
149 | "test-2 {files : '.', bears : ''}") |
||
150 | self.assertEqual(str(sections["test-3"]), |
||
151 | "test-3 {files : 'MakeFile'}") |
||
152 | self.assertEqual(str(sections["test-4"]), |
||
153 | "test-4 {bears : 'TestBear'}") |
||
154 | self.assertEqual(str(sections["test-5"]), |
||
155 | "test-5 {bears : 'TestBear2'}") |
||
156 | |||
157 | Constants.system_coafile = tmp |
||
158 | |||
159 | def test_merge_defaults(self): |
||
160 | with make_temp() as temporary: |
||
161 | sections, local_bears, global_bears, targets = ( |
||
162 | gather_configuration(lambda *args: True, |
||
163 | self.log_printer, |
||
164 | arg_list=["-S", |
||
165 | "value=1", |
||
166 | "test.value=2", |
||
167 | "-c", |
||
168 | escape(temporary, "\\")])) |
||
169 | |||
170 | self.assertEqual(sections["default"], |
||
171 | sections["test"].defaults) |
||
172 | |||
173 | def test_back_saving(self): |
||
174 | filename = os.path.join(tempfile.gettempdir(), |
||
175 | "SectionManagerTestFile") |
||
176 | |||
177 | # We need to use a bad filename or this will parse coalas .coafile |
||
178 | gather_configuration( |
||
179 | lambda *args: True, |
||
180 | self.log_printer, |
||
181 | arg_list=['-S', |
||
182 | "save=" + escape(filename, '\\'), |
||
183 | "-c=some_bad_filename"]) |
||
184 | |||
185 | with open(filename, "r") as f: |
||
186 | lines = f.readlines() |
||
187 | self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines) |
||
188 | |||
189 | gather_configuration( |
||
190 | lambda *args: True, |
||
191 | self.log_printer, |
||
192 | arg_list=['-S', |
||
193 | "save=true", |
||
194 | "config=" + 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 | child_dir = os.path.join(current_dir, |
||
232 | "section_manager_test_files", |
||
233 | "child_dir") |
||
234 | retval = find_user_config(child_dir, 2) |
||
235 | self.assertEqual(os.path.join(current_dir, |
||
236 | "section_manager_test_files", |
||
237 | "child_dir", |
||
238 | ".coafile"), retval) |
||
239 | |||
240 | with change_directory(child_dir): |
||
241 | sections, _, _, _ = gather_configuration( |
||
242 | lambda *args: True, |
||
243 | self.log_printer, |
||
244 | arg_list=["--find-config"]) |
||
245 | self.assertEqual(bool(sections["default"]['find_config']), True) |
||
246 | |||
247 | def test_no_config(self): |
||
248 | current_dir = os.path.abspath(os.path.dirname(__file__)) |
||
249 | child_dir = os.path.join(current_dir, |
||
250 | "section_manager_test_files", |
||
251 | "child_dir") |
||
252 | with change_directory(child_dir): |
||
253 | sections, targets = load_configuration([], self.log_printer) |
||
254 | self.assertIn('value', sections["default"]) |
||
255 | |||
256 | sections, targets = load_configuration( |
||
257 | ['--no-config'], |
||
258 | self.log_printer) |
||
259 | self.assertNotIn('value', sections["default"]) |
||
260 | |||
261 | sections, targets = load_configuration( |
||
262 | ['--no-config', '-S', 'use_spaces=True'], |
||
263 | self.log_printer) |
||
264 | self.assertIn('use_spaces', sections["default"]) |
||
265 | self.assertNotIn('values', sections["default"]) |
||
266 | |||
267 | sections, targets = load_configuration( |
||
268 | ['--no-config', 'False', '-S', 'use_spaces=True'], |
||
269 | self.log_printer) |
||
270 | self.assertIn('use_spaces', sections["default"]) |
||
271 | self.assertIn('value', sections["default"]) |
||
272 | |||
273 | with self.assertRaises(SystemExit) as cm: |
||
274 | sections, target = load_configuration( |
||
275 | ['--no-config', '--save'], |
||
276 | self.log_printer) |
||
277 | self.assertEqual(cm.exception.code, 2) |
||
278 | |||
279 | with self.assertRaises(SystemExit) as cm: |
||
280 | sections, target = load_configuration( |
||
281 | ['--no-config', '--find-config'], |
||
282 | self.log_printer) |
||
283 | self.assertEqual(cm.exception.code, 2) |
||
284 | |||
285 | def test_autoapply_arg(self): |
||
286 | sections, _, _, _ = gather_configuration( |
||
287 | lambda *args: True, |
||
288 | self.log_printer, |
||
289 | autoapply=False, |
||
290 | arg_list=[]) |
||
291 | |||
292 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
293 | 'False') |
||
294 | |||
295 | sections, _, _, _ = gather_configuration( |
||
296 | lambda *args: True, |
||
297 | self.log_printer, |
||
298 | autoapply=True, |
||
299 | arg_list=[]) |
||
300 | |||
301 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
302 | 'None') |
||
303 |