Total Complexity | 40 |
Total Lines | 281 |
Duplicated Lines | 12.1 % |
Changes | 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 |
||
18 | @pytest.mark.usefixtures("disable_bears") |
||
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 : " + |
||
51 | repr(temporary) + ", save : '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 | View Code Duplication | 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 | View Code Duplication | 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=" + 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"], lines) |
||
190 | |||
191 | gather_configuration( |
||
192 | lambda *args: True, |
||
193 | self.log_printer, |
||
194 | arg_list=['-S', |
||
195 | "save=true", |
||
196 | "config=" + escape(filename, '\\'), |
||
197 | "test.value=5"]) |
||
198 | |||
199 | with open(filename, "r") as f: |
||
200 | lines = f.readlines() |
||
201 | os.remove(filename) |
||
202 | if os.path.sep == '\\': |
||
203 | filename = escape(filename, '\\') |
||
204 | self.assertEqual(["[Default]\n", |
||
205 | "config = " + filename + "\n", |
||
206 | "\n", |
||
207 | "[test]\n", |
||
208 | "value = 5\n"], lines) |
||
209 | |||
210 | def test_targets(self): |
||
211 | sections, local_bears, global_bears, targets = gather_configuration( |
||
212 | lambda *args: True, |
||
213 | self.log_printer, |
||
214 | arg_list=["default", "test1", "test2"]) |
||
215 | |||
216 | self.assertEqual(targets, ["default", "test1", "test2"]) |
||
217 | |||
218 | def test_find_user_config(self): |
||
219 | current_dir = os.path.abspath(os.path.dirname(__file__)) |
||
220 | c_file = os.path.join(current_dir, |
||
221 | "section_manager_test_files", |
||
222 | "project", |
||
223 | "test.c") |
||
224 | |||
225 | retval = find_user_config(c_file, 1) |
||
226 | self.assertEqual("", retval) |
||
227 | |||
228 | retval = find_user_config(c_file, 2) |
||
229 | self.assertEqual(os.path.join(current_dir, |
||
230 | "section_manager_test_files", |
||
231 | ".coafile"), retval) |
||
232 | |||
233 | child_dir = os.path.join(current_dir, |
||
234 | "section_manager_test_files", |
||
235 | "child_dir") |
||
236 | retval = find_user_config(child_dir, 2) |
||
237 | self.assertEqual(os.path.join(current_dir, |
||
238 | "section_manager_test_files", |
||
239 | "child_dir", |
||
240 | ".coafile"), retval) |
||
241 | |||
242 | with change_directory(child_dir): |
||
243 | sections, _, _, _ = gather_configuration( |
||
244 | lambda *args: True, |
||
245 | self.log_printer, |
||
246 | arg_list=["--find-config"]) |
||
247 | self.assertEqual(bool(sections["default"]['find_config']), True) |
||
248 | |||
249 | def test_no_config(self): |
||
250 | current_dir = os.path.abspath(os.path.dirname(__file__)) |
||
251 | child_dir = os.path.join(current_dir, |
||
252 | "section_manager_test_files", |
||
253 | "child_dir") |
||
254 | with change_directory(child_dir): |
||
255 | sections, targets = load_configuration([], self.log_printer) |
||
256 | self.assertIn('value', sections["default"]) |
||
257 | |||
258 | sections, targets = load_configuration( |
||
259 | ['--no-config'], |
||
260 | self.log_printer) |
||
261 | self.assertNotIn('value', sections["default"]) |
||
262 | |||
263 | sections, targets = load_configuration( |
||
264 | ['--no-config', '-S', 'use_spaces=True'], |
||
265 | self.log_printer) |
||
266 | self.assertIn('use_spaces', sections["default"]) |
||
267 | self.assertNotIn('values', sections["default"]) |
||
268 | |||
269 | with self.assertRaises(SystemExit) as cm: |
||
270 | sections, target = load_configuration( |
||
271 | ['--no-config', '--save'], |
||
272 | self.log_printer) |
||
273 | self.assertEqual(cm.exception.code, 2) |
||
274 | |||
275 | with self.assertRaises(SystemExit) as cm: |
||
276 | sections, target = load_configuration( |
||
277 | ['--no-config', '--find-config'], |
||
278 | self.log_printer) |
||
279 | self.assertEqual(cm.exception.code, 2) |
||
280 | |||
281 | def test_autoapply_arg(self): |
||
282 | sections, _, _, _ = gather_configuration( |
||
283 | lambda *args: True, |
||
284 | self.log_printer, |
||
285 | autoapply=False, |
||
286 | arg_list=[]) |
||
287 | |||
288 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
289 | 'False') |
||
290 | |||
291 | sections, _, _, _ = gather_configuration( |
||
292 | lambda *args: True, |
||
293 | self.log_printer, |
||
294 | autoapply=True, |
||
295 | arg_list=[]) |
||
296 | |||
297 | self.assertEqual(str(sections['default'].get('autoapply', None)), |
||
298 | 'None') |
||
299 |