| Total Lines | 447 |
| Duplicated Lines | 16.78 % |
| Changes | 3 | ||
| 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:
| 1 | import os |
||
| 26 | class LinterComponentTest(unittest.TestCase): |
||
| 27 | |||
| 28 | # Using `object` instead of an empty class results in inheritance problems |
||
| 29 | # inside the linter decorator. |
||
| 30 | class EmptyTestLinter: |
||
| 31 | pass |
||
| 32 | |||
| 33 | class ManualProcessingTestLinter: |
||
| 34 | |||
| 35 | def process_output(self, *args, **kwargs): |
||
| 36 | pass |
||
| 37 | |||
| 38 | def setUp(self): |
||
| 39 | self.section = Section("TEST_SECTION") |
||
| 40 | View Code Duplication | ||
|
|
|||
| 41 | def test_decorator_invalid_parameters(self): |
||
| 42 | with self.assertRaises(ValueError) as cm: |
||
| 43 | linter("some-executable", invalid_arg=88, ABC=2000) |
||
| 44 | self.assertEqual( |
||
| 45 | str(cm.exception), |
||
| 46 | "Invalid keyword arguments provided: 'ABC', 'invalid_arg'") |
||
| 47 | |||
| 48 | with self.assertRaises(ValueError) as cm: |
||
| 49 | linter("some-executable", diff_severity=RESULT_SEVERITY.MAJOR) |
||
| 50 | self.assertEqual(str(cm.exception), |
||
| 51 | "Invalid keyword arguments provided: 'diff_severity'") |
||
| 52 | |||
| 53 | with self.assertRaises(ValueError) as cm: |
||
| 54 | linter("some-executable", diff_message="Custom message") |
||
| 55 | self.assertEqual(str(cm.exception), |
||
| 56 | "Invalid keyword arguments provided: 'diff_message'") |
||
| 57 | |||
| 58 | with self.assertRaises(ValueError) as cm: |
||
| 59 | linter("some-executable", |
||
| 60 | output_format="corrected", |
||
| 61 | output_regex=".*") |
||
| 62 | self.assertEqual(str(cm.exception), |
||
| 63 | "Invalid keyword arguments provided: 'output_regex'") |
||
| 64 | |||
| 65 | with self.assertRaises(ValueError) as cm: |
||
| 66 | linter("some-executable", |
||
| 67 | output_format="corrected", |
||
| 68 | severity_map={}) |
||
| 69 | self.assertEqual(str(cm.exception), |
||
| 70 | "Invalid keyword arguments provided: 'severity_map'") |
||
| 71 | |||
| 72 | with self.assertRaises(ValueError) as cm: |
||
| 73 | linter("some-executable", |
||
| 74 | prerequisite_check_fail_message="some_message") |
||
| 75 | self.assertEqual(str(cm.exception), |
||
| 76 | "Invalid keyword arguments provided: " |
||
| 77 | "'prerequisite_check_fail_message'") |
||
| 78 | View Code Duplication | ||
| 79 | def test_decorator_invalid_states(self): |
||
| 80 | with self.assertRaises(ValueError) as cm: |
||
| 81 | linter("some-executable", use_stdout=False, use_stderr=False) |
||
| 82 | self.assertEqual(str(cm.exception), |
||
| 83 | "No output streams provided at all.") |
||
| 84 | |||
| 85 | with self.assertRaises(ValueError) as cm: |
||
| 86 | linter("some-executable", output_format="INVALID") |
||
| 87 | self.assertEqual(str(cm.exception), |
||
| 88 | "Invalid `output_format` specified.") |
||
| 89 | |||
| 90 | with self.assertRaises(ValueError) as cm: |
||
| 91 | linter("some-executable", output_format="regex") |
||
| 92 | self.assertEqual( |
||
| 93 | str(cm.exception), |
||
| 94 | "`output_regex` needed when specified output-format 'regex'.") |
||
| 95 | |||
| 96 | with self.assertRaises(ValueError) as cm: |
||
| 97 | linter("some-executable", |
||
| 98 | output_format="regex", |
||
| 99 | output_regex="", |
||
| 100 | severity_map={}) |
||
| 101 | self.assertEqual( |
||
| 102 | str(cm.exception), |
||
| 103 | "Provided `severity_map` but named group `severity` is not used " |
||
| 104 | "in `output_regex`.") |
||
| 105 | |||
| 106 | with self.assertRaises(ValueError) as cm: |
||
| 107 | linter("some-executable")(object) |
||
| 108 | self.assertEqual( |
||
| 109 | str(cm.exception), |
||
| 110 | "`process_output` not provided by given class 'object'.") |
||
| 111 | |||
| 112 | with self.assertRaises(ValueError) as cm: |
||
| 113 | (linter("some-executable", output_format="regex", output_regex="") |
||
| 114 | (self.ManualProcessingTestLinter)) |
||
| 115 | self.assertEqual( |
||
| 116 | str(cm.exception), |
||
| 117 | "Found `process_output` already defined by class " |
||
| 118 | "'ManualProcessingTestLinter', but 'regex' output-format is " |
||
| 119 | "specified.") |
||
| 120 | |||
| 121 | def test_decorator_generated_default_interface(self): |
||
| 122 | uut = linter("some-executable")(self.ManualProcessingTestLinter) |
||
| 123 | with self.assertRaises(NotImplementedError): |
||
| 124 | uut.create_arguments("filename", "content", None) |
||
| 125 | |||
| 126 | def test_decorator_invalid_parameter_types(self): |
||
| 127 | # Provide some invalid severity maps. |
||
| 128 | with self.assertRaises(TypeError): |
||
| 129 | linter("some-executable", |
||
| 130 | output_format="regex", |
||
| 131 | output_regex="(?P<severity>)", |
||
| 132 | severity_map=list()) |
||
| 133 | |||
| 134 | with self.assertRaises(TypeError): |
||
| 135 | linter("some-executable", |
||
| 136 | output_format="regex", |
||
| 137 | output_regex="(?P<severity>)", |
||
| 138 | severity_map={3: 0}) |
||
| 139 | |||
| 140 | with self.assertRaises(TypeError) as cm: |
||
| 141 | linter("some-executable", |
||
| 142 | output_format="regex", |
||
| 143 | output_regex="(?P<severity>)", |
||
| 144 | severity_map={"critical": "invalid"}) |
||
| 145 | self.assertEqual(str(cm.exception), |
||
| 146 | "The value 'invalid' for key 'critical' inside given " |
||
| 147 | "severity-map is no valid severity value.") |
||
| 148 | |||
| 149 | with self.assertRaises(TypeError) as cm: |
||
| 150 | linter("some-executable", |
||
| 151 | output_format="regex", |
||
| 152 | output_regex="(?P<severity>)", |
||
| 153 | severity_map={"critical-error": 389274234}) |
||
| 154 | self.assertEqual(str(cm.exception), |
||
| 155 | "Invalid severity value 389274234 for key " |
||
| 156 | "'critical-error' inside given severity-map.") |
||
| 157 | |||
| 158 | # Other type-error test cases. |
||
| 159 | |||
| 160 | with self.assertRaises(TypeError): |
||
| 161 | linter("some-executable", |
||
| 162 | output_format="corrected", |
||
| 163 | diff_message=list()) |
||
| 164 | |||
| 165 | with self.assertRaises(TypeError) as cm: |
||
| 166 | linter("some-executable", |
||
| 167 | output_format="corrected", |
||
| 168 | diff_severity=999888777) |
||
| 169 | self.assertEqual(str(cm.exception), |
||
| 170 | "Invalid value for `diff_severity`: 999888777") |
||
| 171 | |||
| 172 | with self.assertRaises(TypeError): |
||
| 173 | linter("some-executable", |
||
| 174 | prerequisite_check_command=("command",), |
||
| 175 | prerequisite_check_fail_message=382983) |
||
| 176 | |||
| 177 | def test_get_executable(self): |
||
| 178 | uut = linter("some-executable")(self.ManualProcessingTestLinter) |
||
| 179 | self.assertEqual(uut.get_executable(), "some-executable") |
||
| 180 | |||
| 181 | def test_check_prerequisites(self): |
||
| 182 | uut = linter(sys.executable)(self.ManualProcessingTestLinter) |
||
| 183 | self.assertTrue(uut.check_prerequisites()) |
||
| 184 | |||
| 185 | uut = (linter("invalid_nonexisting_programv412") |
||
| 186 | (self.ManualProcessingTestLinter)) |
||
| 187 | self.assertEqual(uut.check_prerequisites(), |
||
| 188 | "'invalid_nonexisting_programv412' is not installed.") |
||
| 189 | |||
| 190 | uut = (linter(sys.executable, |
||
| 191 | prerequisite_check_command=(sys.executable, "--version")) |
||
| 192 | (self.ManualProcessingTestLinter)) |
||
| 193 | self.assertTrue(uut.check_prerequisites()) |
||
| 194 | |||
| 195 | uut = (linter(sys.executable, |
||
| 196 | prerequisite_check_command=("invalid_programv413",)) |
||
| 197 | (self.ManualProcessingTestLinter)) |
||
| 198 | self.assertEqual(uut.check_prerequisites(), |
||
| 199 | "Prerequisite check failed.") |
||
| 200 | |||
| 201 | uut = (linter(sys.executable, |
||
| 202 | prerequisite_check_command=("invalid_programv413",), |
||
| 203 | prerequisite_check_fail_message="NOPE") |
||
| 204 | (self.ManualProcessingTestLinter)) |
||
| 205 | self.assertEqual(uut.check_prerequisites(), "NOPE") |
||
| 206 | |||
| 207 | def test_output_stream(self): |
||
| 208 | process_output_mock = Mock() |
||
| 209 | |||
| 210 | class TestLinter: |
||
| 211 | |||
| 212 | @staticmethod |
||
| 213 | def process_output(output, filename, file): |
||
| 214 | process_output_mock(output, filename, file) |
||
| 215 | |||
| 216 | @staticmethod |
||
| 217 | def create_arguments(filename, file, config_file): |
||
| 218 | code = "\n".join(["import sys", |
||
| 219 | "print('hello stdout')", |
||
| 220 | "print('hello stderr', file=sys.stderr)"]) |
||
| 221 | return "-c", code |
||
| 222 | |||
| 223 | uut = (linter(sys.executable, use_stdout=True) |
||
| 224 | (TestLinter) |
||
| 225 | (self.section, None)) |
||
| 226 | uut.run("", []) |
||
| 227 | |||
| 228 | process_output_mock.assert_called_once_with("hello stdout\n", "", []) |
||
| 229 | process_output_mock.reset_mock() |
||
| 230 | |||
| 231 | uut = (linter(sys.executable, use_stdout=False, use_stderr=True) |
||
| 232 | (TestLinter) |
||
| 233 | (self.section, None)) |
||
| 234 | uut.run("", []) |
||
| 235 | |||
| 236 | process_output_mock.assert_called_once_with("hello stderr\n", "", []) |
||
| 237 | process_output_mock.reset_mock() |
||
| 238 | |||
| 239 | uut = (linter(sys.executable, use_stdout=True, use_stderr=True) |
||
| 240 | (TestLinter) |
||
| 241 | (self.section, None)) |
||
| 242 | |||
| 243 | uut.run("", []) |
||
| 244 | |||
| 245 | process_output_mock.assert_called_once_with(("hello stdout\n", |
||
| 246 | "hello stderr\n"), "", []) |
||
| 247 | |||
| 248 | def test_process_output_corrected(self): |
||
| 249 | uut = (linter(sys.executable, output_format="corrected") |
||
| 250 | (self.EmptyTestLinter) |
||
| 251 | (self.section, None)) |
||
| 252 | |||
| 253 | original = ["void main() {\n", "return 09;\n", "}\n"] |
||
| 254 | fixed = ["void main()\n", "{\n", "return 9;\n", "}\n"] |
||
| 255 | fixed_string = "".join(fixed) |
||
| 256 | |||
| 257 | results = list(uut.process_output(fixed_string, |
||
| 258 | "some-file.c", |
||
| 259 | original)) |
||
| 260 | |||
| 261 | diffs = list(Diff.from_string_arrays(original, fixed).split_diff()) |
||
| 262 | expected = [Result.from_values(uut, |
||
| 263 | "Inconsistency found.", |
||
| 264 | "some-file.c", |
||
| 265 | 1, None, 2, None, |
||
| 266 | RESULT_SEVERITY.NORMAL, |
||
| 267 | diffs={"some-file.c": diffs[0]})] |
||
| 268 | |||
| 269 | self.assertEqual(results, expected) |
||
| 270 | |||
| 271 | # Test when providing a sequence as output. |
||
| 272 | |||
| 273 | results = list(uut.process_output([fixed_string, fixed_string], |
||
| 274 | "some-file.c", |
||
| 275 | original)) |
||
| 276 | self.assertEqual(results, 2 * expected) |
||
| 277 | |||
| 278 | def test_process_output_regex(self): |
||
| 279 | # Also test the case when an unknown severity is matched. |
||
| 280 | test_output = ("12:4-14:0-Serious issue (error) -> ORIGIN=X\n" |
||
| 281 | "0:0-0:1-This is a warning (warning) -> ORIGIN=Y\n" |
||
| 282 | "813:77-1024:32-Just a note (info) -> ORIGIN=Z\n" |
||
| 283 | "0:0-0:0-Some unknown sev (???) -> ORIGIN=W\n") |
||
| 284 | regex = (r"(?P<line>\d+):(?P<column>\d+)-" |
||
| 285 | r"(?P<end_line>\d+):(?P<end_column>\d+)-" |
||
| 286 | r"(?P<message>.*) \((?P<severity>.*)\) -> " |
||
| 287 | r"ORIGIN=(?P<origin>.*)") |
||
| 288 | |||
| 289 | uut = (linter(sys.executable, |
||
| 290 | output_format="regex", |
||
| 291 | output_regex=regex) |
||
| 292 | (self.EmptyTestLinter) |
||
| 293 | (self.section, None)) |
||
| 294 | uut.warn = Mock() |
||
| 295 | |||
| 296 | sample_file = "some-file.xtx" |
||
| 297 | results = list(uut.process_output(test_output, sample_file, [""])) |
||
| 298 | expected = [Result.from_values("EmptyTestLinter (X)", |
||
| 299 | "Serious issue", |
||
| 300 | sample_file, |
||
| 301 | 12, 4, 14, 0, |
||
| 302 | RESULT_SEVERITY.MAJOR), |
||
| 303 | Result.from_values("EmptyTestLinter (Y)", |
||
| 304 | "This is a warning", |
||
| 305 | sample_file, |
||
| 306 | 0, 0, 0, 1, |
||
| 307 | RESULT_SEVERITY.NORMAL), |
||
| 308 | Result.from_values("EmptyTestLinter (Z)", |
||
| 309 | "Just a note", |
||
| 310 | sample_file, |
||
| 311 | 813, 77, 1024, 32, |
||
| 312 | RESULT_SEVERITY.INFO), |
||
| 313 | Result.from_values("EmptyTestLinter (W)", |
||
| 314 | "Some unknown sev", |
||
| 315 | sample_file, |
||
| 316 | 0, 0, 0, 0, |
||
| 317 | RESULT_SEVERITY.NORMAL)] |
||
| 318 | |||
| 319 | self.assertEqual(results, expected) |
||
| 320 | uut.warn.assert_called_once_with( |
||
| 321 | "'???' not found in severity-map. Assuming " |
||
| 322 | "`RESULT_SEVERITY.NORMAL`.") |
||
| 323 | |||
| 324 | # Test when providing a sequence as output. |
||
| 325 | test_output = ["", "12:4-14:0-Serious issue (error) -> ORIGIN=X\n"] |
||
| 326 | results = list(uut.process_output(test_output, sample_file, [""])) |
||
| 327 | expected = [Result.from_values("EmptyTestLinter (X)", |
||
| 328 | "Serious issue", |
||
| 329 | sample_file, |
||
| 330 | 12, 4, 14, 0, |
||
| 331 | RESULT_SEVERITY.MAJOR)] |
||
| 332 | |||
| 333 | self.assertEqual(results, expected) |
||
| 334 | |||
| 335 | def test_get_non_optional_settings(self): |
||
| 336 | class Handler(self.ManualProcessingTestLinter): |
||
| 337 | |||
| 338 | @staticmethod |
||
| 339 | def create_arguments(filename, file, config_file, param_x: int): |
||
| 340 | pass |
||
| 341 | |||
| 342 | @staticmethod |
||
| 343 | def generate_config(filename, file, superparam): |
||
| 344 | """ |
||
| 345 | :param superparam: A superparam! |
||
| 346 | """ |
||
| 347 | return None |
||
| 348 | |||
| 349 | uut = linter(sys.executable)(Handler) |
||
| 350 | |||
| 351 | self.assertEqual(uut.get_non_optional_settings(), |
||
| 352 | {"param_x": ("No description given.", int), |
||
| 353 | "superparam": ("A superparam!", None)}) |
||
| 354 | |||
| 355 | def test_section_settings_forwarding(self): |
||
| 356 | create_arguments_mock = Mock() |
||
| 357 | generate_config_mock = Mock() |
||
| 358 | process_output_mock = Mock() |
||
| 359 | |||
| 360 | class Handler(self.ManualProcessingTestLinter): |
||
| 361 | |||
| 362 | @staticmethod |
||
| 363 | def create_arguments(filename, file, config_file, my_param: int): |
||
| 364 | create_arguments_mock(filename, file, config_file, my_param) |
||
| 365 | # Execute python and do nothing. |
||
| 366 | return "-c", "print('coala!')" |
||
| 367 | |||
| 368 | @staticmethod |
||
| 369 | def generate_config(filename, file, my_config_param: int): |
||
| 370 | generate_config_mock(filename, file, my_config_param) |
||
| 371 | return None |
||
| 372 | |||
| 373 | def process_output(self, output, filename, file, makman2: str): |
||
| 374 | process_output_mock(output, filename, file, makman2) |
||
| 375 | |||
| 376 | self.section["my_param"] = "109" |
||
| 377 | self.section["my_config_param"] = "88" |
||
| 378 | self.section["makman2"] = "is cool" |
||
| 379 | |||
| 380 | uut = linter(sys.executable)(Handler)(self.section, None) |
||
|
1 ignored issue
–
show
|
|||
| 381 | |||
| 382 | self.assertIsNotNone(list(uut.execute(filename="some_file.cs", |
||
| 383 | file=[]))) |
||
| 384 | create_arguments_mock.assert_called_once_with( |
||
| 385 | "some_file.cs", [], None, 109) |
||
| 386 | generate_config_mock.assert_called_once_with("some_file.cs", [], 88) |
||
| 387 | process_output_mock.assert_called_once_with( |
||
| 388 | "coala!\n", "some_file.cs", [], "is cool") |
||
| 389 | |||
| 390 | def test_section_settings_defaults_forwarding(self): |
||
| 391 | create_arguments_mock = Mock() |
||
| 392 | generate_config_mock = Mock() |
||
| 393 | process_output_mock = Mock() |
||
| 394 | |||
| 395 | class Handler: |
||
| 396 | |||
| 397 | @staticmethod |
||
| 398 | def generate_config(filename, file, some_default: str="x"): |
||
| 399 | generate_config_mock(filename, file, some_default) |
||
| 400 | return None |
||
| 401 | |||
| 402 | @staticmethod |
||
| 403 | def create_arguments(filename, file, config_file, default: int=3): |
||
| 404 | create_arguments_mock( |
||
| 405 | filename, file, config_file, default) |
||
| 406 | return "-c", "print('hello')" |
||
| 407 | |||
| 408 | @staticmethod |
||
| 409 | def process_output(output, filename, file, xxx: int=64): |
||
| 410 | process_output_mock(output, filename, file, xxx) |
||
| 411 | |||
| 412 | uut = linter(sys.executable)(Handler)(self.section, None) |
||
|
1 ignored issue
–
show
|
|||
| 413 | |||
| 414 | self.assertIsNotNone(list(uut.execute(filename="abc.py", file=[]))) |
||
| 415 | create_arguments_mock.assert_called_once_with("abc.py", [], None, 3) |
||
| 416 | generate_config_mock.assert_called_once_with("abc.py", [], "x") |
||
| 417 | process_output_mock.assert_called_once_with( |
||
| 418 | "hello\n", "abc.py", [], 64) |
||
| 419 | |||
| 420 | create_arguments_mock.reset_mock() |
||
| 421 | generate_config_mock.reset_mock() |
||
| 422 | process_output_mock.reset_mock() |
||
| 423 | |||
| 424 | self.section["default"] = "1000" |
||
| 425 | self.section["some_default"] = "xyz" |
||
| 426 | self.section["xxx"] = "-50" |
||
| 427 | self.assertIsNotNone(list(uut.execute(filename="def.py", file=[]))) |
||
| 428 | create_arguments_mock.assert_called_once_with("def.py", [], None, 1000) |
||
| 429 | generate_config_mock.assert_called_once_with("def.py", [], "xyz") |
||
| 430 | process_output_mock.assert_called_once_with( |
||
| 431 | "hello\n", "def.py", [], -50) |
||
| 432 | |||
| 433 | def test_generate_config(self): |
||
| 434 | uut = linter("")(self.ManualProcessingTestLinter) |
||
| 435 | with uut._create_config("filename", []) as config_file: |
||
| 436 | self.assertIsNone(config_file) |
||
| 437 | |||
| 438 | class ConfigurationTestLinter(self.ManualProcessingTestLinter): |
||
| 439 | |||
| 440 | @staticmethod |
||
| 441 | def generate_config(filename, file, val): |
||
| 442 | return "config_value = " + str(val) |
||
| 443 | |||
| 444 | uut = linter("", config_suffix=".xml")(ConfigurationTestLinter) |
||
| 445 | with uut._create_config("filename", [], val=88) as config_file: |
||
| 446 | self.assertTrue(os.path.isfile(config_file)) |
||
| 447 | self.assertEqual(config_file[-4:], ".xml") |
||
| 448 | with open(config_file, mode="r") as fl: |
||
| 449 | self.assertEqual(fl.read(), "config_value = 88") |
||
| 450 | self.assertFalse(os.path.isfile(config_file)) |
||
| 451 | |||
| 452 | def test_metaclass_repr(self): |
||
| 453 | uut = linter("my-tool")(self.ManualProcessingTestLinter) |
||
| 454 | self.assertEqual( |
||
| 455 | repr(uut), |
||
| 456 | "<ManualProcessingTestLinter linter class (wrapping 'my-tool')>") |
||
| 457 | |||
| 458 | # Test also whether derivatives change the class name accordingly. |
||
| 459 | class DerivedLinter(uut): |
||
| 460 | pass |
||
| 461 | self.assertEqual(repr(DerivedLinter), |
||
| 462 | "<DerivedLinter linter class (wrapping 'my-tool')>") |
||
| 463 | |||
| 464 | def test_repr(self): |
||
| 465 | uut = (linter(sys.executable) |
||
| 466 | (self.ManualProcessingTestLinter) |
||
| 467 | (self.section, None)) |
||
| 468 | |||
| 469 | self.assertRegex( |
||
| 470 | repr(uut), |
||
| 471 | "<ManualProcessingTestLinter linter object \\(wrapping " + |
||
| 472 | re.escape(repr(sys.executable)) + "\\) at 0x[a-fA-F0-9]+>") |
||
| 473 | |||
| 732 |