Failed Conditions
Pull Request — master (#1990)
by Mischa
02:35 queued 43s
created

ManualProcessingTestLinter.process_output()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
import os
2
import re
3
import sys
4
import unittest
5
from unittest.mock import ANY, Mock
6
7
from coalib.bearlib.abstractions.Linter import linter
8
from coalib.results.Diff import Diff
9
from coalib.results.Result import Result
10
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
11
from coalib.settings.Section import Section
12
13
14
def get_testfile_name(name):
15
    """
16
    Gets the full path to a testfile inside ``linter_test_files`` directory.
17
18
    :param name: The filename of the testfile to get the full path for.
19
    :return:     The full path to given testfile name.
20
    """
21
    return os.path.join(os.path.dirname(os.path.realpath(__file__)),
22
                        "linter_test_files",
23
                        name)
24
25
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
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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)
0 ignored issues
show
Bug introduced by
linter(sys.executable, u...tdout=True)(TestLinter) does not seem to be callable.
Loading history...
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)
0 ignored issues
show
Bug introduced by
linter(sys.executable, u...tderr=True)(TestLinter) does not seem to be callable.
Loading history...
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)
0 ignored issues
show
Bug introduced by
linter(sys.executable, u...tderr=True)(TestLinter) does not seem to be callable.
Loading history...
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")
0 ignored issues
show
Bug introduced by
linter(sys.executable, o...)(self.EmptyTestLinter) does not seem to be callable.
Loading history...
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,
0 ignored issues
show
Bug introduced by
linter(sys.executable, o...)(self.EmptyTestLinter) does not seem to be callable.
Loading history...
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
Bug introduced by
linter(sys.executable)(Handler) does not seem to be callable.
Loading history...
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
Bug introduced by
linter(sys.executable)(Handler) does not seem to be callable.
Loading history...
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)
0 ignored issues
show
Bug introduced by
linter(sys.executable)(s...alProcessingTestLinter) does not seem to be callable.
Loading history...
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
474
475
class LinterReallifeTest(unittest.TestCase):
476
477
    def setUp(self):
478
        self.section = Section("REALLIFE_TEST_SECTION")
479
480
        self.test_program_path = get_testfile_name("test_linter.py")
481
        self.test_program_regex = (
482
            r"L(?P<line>\d+)C(?P<column>\d+)-"
483
            r"L(?P<end_line>\d+)C(?P<end_column>\d+):"
484
            r" (?P<message>.*) \| (?P<severity>.+) SEVERITY")
485
        self.test_program_severity_map = {"MAJOR": RESULT_SEVERITY.MAJOR}
486
487
        self.testfile_path = get_testfile_name("test_file.txt")
488
        with open(self.testfile_path, mode="r") as fl:
489
            self.testfile_content = fl.read().splitlines(keepends=True)
490
491
        self.testfile2_path = get_testfile_name("test_file2.txt")
492
        with open(self.testfile2_path, mode="r") as fl:
493
            self.testfile2_content = fl.read().splitlines(keepends=True)
494
495
    def test_nostdin_nostderr_noconfig_nocorrection(self):
496
        create_arguments_mock = Mock()
497
498
        class Handler:
499
500
            @staticmethod
501
            def create_arguments(filename, file, config_file):
502
                create_arguments_mock(filename, file, config_file)
503
                return self.test_program_path, filename
504
505
        uut = (linter(sys.executable,
0 ignored issues
show
Bug introduced by
linter(sys.executable, o..._severity_map)(Handler) does not seem to be callable.
Loading history...
506
                      output_format="regex",
507
                      output_regex=self.test_program_regex,
508
                      severity_map=self.test_program_severity_map)
509
               (Handler)
510
               (self.section, None))
511
512
        results = list(uut.run(self.testfile_path, self.testfile_content))
513
        expected = [Result.from_values(uut,
514
                                       "Invalid char ('0')",
515
                                       self.testfile_path,
516
                                       3, 0, 3, 1,
517
                                       RESULT_SEVERITY.MAJOR),
518
                    Result.from_values(uut,
519
                                       "Invalid char ('.')",
520
                                       self.testfile_path,
521
                                       5, 0, 5, 1,
522
                                       RESULT_SEVERITY.MAJOR),
523
                    Result.from_values(uut,
524
                                       "Invalid char ('p')",
525
                                       self.testfile_path,
526
                                       9, 0, 9, 1,
527
                                       RESULT_SEVERITY.MAJOR)]
528
529
        self.assertEqual(results, expected)
530
        create_arguments_mock.assert_called_once_with(
531
            self.testfile_path, self.testfile_content, None)
532
533
    def test_stdin_stderr_noconfig_nocorrection(self):
534
        create_arguments_mock = Mock()
535
536
        class Handler:
537
538
            @staticmethod
539
            def create_arguments(filename, file, config_file):
540
                create_arguments_mock(filename, file, config_file)
541
                return (self.test_program_path,
542
                        "--use_stderr",
543
                        "--use_stdin",
544
                        filename)
545
546
        uut = (linter(sys.executable,
0 ignored issues
show
Bug introduced by
linter(sys.executable, u..._severity_map)(Handler) does not seem to be callable.
Loading history...
547
                      use_stdin=True,
548
                      use_stdout=False,
549
                      use_stderr=True,
550
                      output_format="regex",
551
                      output_regex=self.test_program_regex,
552
                      severity_map=self.test_program_severity_map)
553
               (Handler)
554
               (self.section, None))
555
556
        results = list(uut.run(self.testfile2_path, self.testfile2_content))
557
        expected = [Result.from_values(uut,
558
                                       "Invalid char ('X')",
559
                                       self.testfile2_path,
560
                                       0, 0, 0, 1,
561
                                       RESULT_SEVERITY.MAJOR),
562
                    Result.from_values(uut,
563
                                       "Invalid char ('i')",
564
                                       self.testfile2_path,
565
                                       4, 0, 4, 1,
566
                                       RESULT_SEVERITY.MAJOR)]
567
568
        self.assertEqual(results, expected)
569
        create_arguments_mock.assert_called_once_with(
570
            self.testfile2_path, self.testfile2_content, None)
571
572
    def test_nostdin_nostderr_noconfig_correction(self):
573
        create_arguments_mock = Mock()
574
575
        class Handler:
576
577
            @staticmethod
578
            def create_arguments(filename, file, config_file):
579
                create_arguments_mock(filename, file, config_file)
580
                return self.test_program_path, "--correct", filename
581
582
        uut = (linter(sys.executable,
0 ignored issues
show
Bug introduced by
linter(sys.executable, o...stom message')(Handler) does not seem to be callable.
Loading history...
583
                      output_format="corrected",
584
                      diff_severity=RESULT_SEVERITY.INFO,
585
                      diff_message="Custom message")
586
               (Handler)
587
               (self.section, None))
588
589
        results = list(uut.run(self.testfile_path, self.testfile_content))
590
591
        expected_correction = [s + "\n"
592
                               for s in ["+", "-", "*", "++", "-", "-", "+"]]
593
594
        diffs = list(Diff.from_string_arrays(
595
            self.testfile_content,
596
            expected_correction).split_diff())
597
598
        expected = [Result.from_values(uut,
599
                                       "Custom message",
600
                                       self.testfile_path,
601
                                       4, None, 4, None,
602
                                       RESULT_SEVERITY.INFO,
603
                                       diffs={self.testfile_path: diffs[0]}),
604
                    Result.from_values(uut,
605
                                       "Custom message",
606
                                       self.testfile_path,
607
                                       6, None, 6, None,
608
                                       RESULT_SEVERITY.INFO,
609
                                       diffs={self.testfile_path: diffs[1]}),
610
                    Result.from_values(uut,
611
                                       "Custom message",
612
                                       self.testfile_path,
613
                                       10, None, 10, None,
614
                                       RESULT_SEVERITY.INFO,
615
                                       diffs={self.testfile_path: diffs[2]})]
616
617
        self.assertEqual(results, expected)
618
        create_arguments_mock.assert_called_once_with(
619
            self.testfile_path, self.testfile_content, None)
620
621
    def test_stdin_stdout_stderr_config_nocorrection(self):
622
        create_arguments_mock = Mock()
623
        generate_config_mock = Mock()
624
625
        class Handler:
626
627
            @staticmethod
628
            def generate_config(filename, file, some_val):
629
                # some_val shall only test the argument delegation from run().
630
                generate_config_mock(filename, file, some_val)
631
                return "\n".join(["use_stdin", "use_stderr"])
632
633
            @staticmethod
634
            def create_arguments(filename, file, config_file, some_val):
635
                create_arguments_mock(filename, file, config_file, some_val)
636
                return self.test_program_path, "--config", config_file
637
638
        uut = (linter(sys.executable,
0 ignored issues
show
Bug introduced by
linter(sys.executable, u..._severity_map)(Handler) does not seem to be callable.
Loading history...
639
                      use_stdin=True,
640
                      use_stderr=True,
641
                      output_format="regex",
642
                      output_regex=self.test_program_regex,
643
                      severity_map=self.test_program_severity_map)
644
               (Handler)
645
               (self.section, None))
646
647
        results = list(uut.run(self.testfile_path,
648
                               self.testfile_content,
649
                               some_val=33))
650
        expected = [Result.from_values(uut,
651
                                       "Invalid char ('0')",
652
                                       self.testfile_path,
653
                                       3, 0, 3, 1,
654
                                       RESULT_SEVERITY.MAJOR),
655
                    Result.from_values(uut,
656
                                       "Invalid char ('.')",
657
                                       self.testfile_path,
658
                                       5, 0, 5, 1,
659
                                       RESULT_SEVERITY.MAJOR),
660
                    Result.from_values(uut,
661
                                       "Invalid char ('p')",
662
                                       self.testfile_path,
663
                                       9, 0, 9, 1,
664
                                       RESULT_SEVERITY.MAJOR)]
665
666
        self.assertEqual(results, expected)
667
        create_arguments_mock.assert_called_once_with(
668
            self.testfile_path, self.testfile_content, ANY, 33)
669
        self.assertIsNotNone(create_arguments_mock.call_args[0][2])
670
        generate_config_mock.assert_called_once_with(
671
            self.testfile_path, self.testfile_content, 33)
672
673
    def test_stdin_stderr_config_correction(self):
674
        create_arguments_mock = Mock()
675
        generate_config_mock = Mock()
676
677
        # `some_value_A` and `some_value_B` are used to test the different
678
        # delegation to `generate_config()` and `create_arguments()`
679
        # accordingly.
680
        class Handler:
681
682
            @staticmethod
683
            def generate_config(filename, file, some_value_A):
684
                generate_config_mock(filename, file, some_value_A)
685
                return "\n".join(["use_stdin", "use_stderr", "correct"])
686
687
            @staticmethod
688
            def create_arguments(filename, file, config_file, some_value_B):
689
                create_arguments_mock(filename, file, config_file,
690
                                      some_value_B)
691
                return self.test_program_path, "--config", config_file
692
693
        uut = (linter(sys.executable,
0 ignored issues
show
Bug introduced by
linter(sys.executable, u...uffix='.conf')(Handler) does not seem to be callable.
Loading history...
694
                      use_stdin=True,
695
                      use_stdout=False,
696
                      use_stderr=True,
697
                      output_format="corrected",
698
                      config_suffix=".conf")
699
               (Handler)
700
               (self.section, None))
701
702
        results = list(uut.run(self.testfile2_path,
703
                               self.testfile2_content,
704
                               some_value_A=124,
705
                               some_value_B=-78))
706
707
        expected_correction = [s + "\n" for s in ["+", "/", "/", "-"]]
708
709
        diffs = list(Diff.from_string_arrays(
710
            self.testfile2_content,
711
            expected_correction).split_diff())
712
713
        expected = [Result.from_values(uut,
714
                                       "Inconsistency found.",
715
                                       self.testfile2_path,
716
                                       1, None, 1, None,
717
                                       RESULT_SEVERITY.NORMAL,
718
                                       diffs={self.testfile2_path: diffs[0]}),
719
                    Result.from_values(uut,
720
                                       "Inconsistency found.",
721
                                       self.testfile2_path,
722
                                       5, None, 5, None,
723
                                       RESULT_SEVERITY.NORMAL,
724
                                       diffs={self.testfile2_path: diffs[1]})]
725
726
        self.assertEqual(results, expected)
727
        create_arguments_mock.assert_called_once_with(
728
            self.testfile2_path, self.testfile2_content, ANY, -78)
729
        self.assertEqual(create_arguments_mock.call_args[0][2][-5:], ".conf")
730
        generate_config_mock.assert_called_once_with(
731
            self.testfile2_path, self.testfile2_content, 124)
732