Failed Conditions
Pull Request — master (#2054)
by Lasse
01:57
created

ConfigurationTestLinter.generate_config()   A

Complexity

Conditions 1

Size

Total Lines 3

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 3
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_minimal_regex(self):
336
        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...
337
                      output_format="regex",
338
                      output_regex="an_issue")
339
               (self.EmptyTestLinter)
340
               (self.section, None))
341
342
        results = list(uut.process_output(['not an issue'], 'file', [""]))
343
        self.assertEqual(results, [])
344
345
        results = list(uut.process_output(['an_issue'], 'file', [""]))
346
        self.assertEqual(results, [Result.from_values("EmptyTestLinter", "",
347
                                                      file="file")])
348
349
    def test_get_non_optional_settings(self):
350
        class Handler(self.ManualProcessingTestLinter):
351
352
            @staticmethod
353
            def create_arguments(filename, file, config_file, param_x: int):
354
                pass
355
356
            @staticmethod
357
            def generate_config(filename, file, superparam):
358
                """
359
                :param superparam: A superparam!
360
                """
361
                return None
362
363
        uut = linter(sys.executable)(Handler)
364
365
        self.assertEqual(uut.get_non_optional_settings(),
366
                         {"param_x": ("No description given.", int),
367
                          "superparam": ("A superparam!", None)})
368
369
    def test_section_settings_forwarding(self):
370
        create_arguments_mock = Mock()
371
        generate_config_mock = Mock()
372
        process_output_mock = Mock()
373
374
        class Handler(self.ManualProcessingTestLinter):
375
376
            @staticmethod
377
            def create_arguments(filename, file, config_file, my_param: int):
378
                create_arguments_mock(filename, file, config_file, my_param)
379
                # Execute python and do nothing.
380
                return "-c", "print('coala!')"
381
382
            @staticmethod
383
            def generate_config(filename, file, my_config_param: int):
384
                generate_config_mock(filename, file, my_config_param)
385
                return None
386
387
            def process_output(self, output, filename, file, makman2: str):
388
                process_output_mock(output, filename, file, makman2)
389
390
        self.section["my_param"] = "109"
391
        self.section["my_config_param"] = "88"
392
        self.section["makman2"] = "is cool"
393
394
        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...
395
396
        self.assertIsNotNone(list(uut.execute(filename="some_file.cs",
397
                                              file=[])))
398
        create_arguments_mock.assert_called_once_with(
399
            "some_file.cs", [], None, 109)
400
        generate_config_mock.assert_called_once_with("some_file.cs", [], 88)
401
        process_output_mock.assert_called_once_with(
402
            "coala!\n", "some_file.cs", [], "is cool")
403
404
    def test_section_settings_defaults_forwarding(self):
405
        create_arguments_mock = Mock()
406
        generate_config_mock = Mock()
407
        process_output_mock = Mock()
408
409
        class Handler:
410
411
            @staticmethod
412
            def generate_config(filename, file, some_default: str="x"):
413
                generate_config_mock(filename, file, some_default)
414
                return None
415
416
            @staticmethod
417
            def create_arguments(filename, file, config_file, default: int=3):
418
                create_arguments_mock(
419
                    filename, file, config_file, default)
420
                return "-c", "print('hello')"
421
422
            @staticmethod
423
            def process_output(output, filename, file, xxx: int=64):
424
                process_output_mock(output, filename, file, xxx)
425
426
        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...
427
428
        self.assertIsNotNone(list(uut.execute(filename="abc.py", file=[])))
429
        create_arguments_mock.assert_called_once_with("abc.py", [], None, 3)
430
        generate_config_mock.assert_called_once_with("abc.py", [], "x")
431
        process_output_mock.assert_called_once_with(
432
            "hello\n", "abc.py", [], 64)
433
434
        create_arguments_mock.reset_mock()
435
        generate_config_mock.reset_mock()
436
        process_output_mock.reset_mock()
437
438
        self.section["default"] = "1000"
439
        self.section["some_default"] = "xyz"
440
        self.section["xxx"] = "-50"
441
        self.assertIsNotNone(list(uut.execute(filename="def.py", file=[])))
442
        create_arguments_mock.assert_called_once_with("def.py", [], None, 1000)
443
        generate_config_mock.assert_called_once_with("def.py", [], "xyz")
444
        process_output_mock.assert_called_once_with(
445
            "hello\n", "def.py", [], -50)
446
447
    def test_generate_config(self):
448
        uut = linter("")(self.ManualProcessingTestLinter)
449
        with uut._create_config("filename", []) as config_file:
450
            self.assertIsNone(config_file)
451
452
        class ConfigurationTestLinter(self.ManualProcessingTestLinter):
453
454
            @staticmethod
455
            def generate_config(filename, file, val):
456
                return "config_value = " + str(val)
457
458
        uut = linter("", config_suffix=".xml")(ConfigurationTestLinter)
459
        with uut._create_config("filename", [], val=88) as config_file:
460
            self.assertTrue(os.path.isfile(config_file))
461
            self.assertEqual(config_file[-4:], ".xml")
462
            with open(config_file, mode="r") as fl:
463
                self.assertEqual(fl.read(), "config_value = 88")
464
        self.assertFalse(os.path.isfile(config_file))
465
466
    def test_metaclass_repr(self):
467
        uut = linter("my-tool")(self.ManualProcessingTestLinter)
468
        self.assertEqual(
469
            repr(uut),
470
            "<ManualProcessingTestLinter linter class (wrapping 'my-tool')>")
471
472
        # Test also whether derivatives change the class name accordingly.
473
        class DerivedLinter(uut):
474
            pass
475
        self.assertEqual(repr(DerivedLinter),
476
                         "<DerivedLinter linter class (wrapping 'my-tool')>")
477
478
    def test_repr(self):
479
        uut = (linter(sys.executable)
0 ignored issues
show
Bug introduced by
linter(sys.executable)(s...alProcessingTestLinter) does not seem to be callable.
Loading history...
480
               (self.ManualProcessingTestLinter)
481
               (self.section, None))
482
483
        self.assertRegex(
484
            repr(uut),
485
            "<ManualProcessingTestLinter linter object \\(wrapping " +
486
            re.escape(repr(sys.executable)) + "\\) at 0x[a-fA-F0-9]+>")
487
488
489
class LinterReallifeTest(unittest.TestCase):
490
491
    def setUp(self):
492
        self.section = Section("REALLIFE_TEST_SECTION")
493
494
        self.test_program_path = get_testfile_name("test_linter.py")
495
        self.test_program_regex = (
496
            r"L(?P<line>\d+)C(?P<column>\d+)-"
497
            r"L(?P<end_line>\d+)C(?P<end_column>\d+):"
498
            r" (?P<message>.*) \| (?P<severity>.+) SEVERITY")
499
        self.test_program_severity_map = {"MAJOR": RESULT_SEVERITY.MAJOR}
500
501
        self.testfile_path = get_testfile_name("test_file.txt")
502
        with open(self.testfile_path, mode="r") as fl:
503
            self.testfile_content = fl.read().splitlines(keepends=True)
504
505
        self.testfile2_path = get_testfile_name("test_file2.txt")
506
        with open(self.testfile2_path, mode="r") as fl:
507
            self.testfile2_content = fl.read().splitlines(keepends=True)
508
509
    def test_nostdin_nostderr_noconfig_nocorrection(self):
510
        create_arguments_mock = Mock()
511
512
        class Handler:
513
514
            @staticmethod
515
            def create_arguments(filename, file, config_file):
516
                create_arguments_mock(filename, file, config_file)
517
                return self.test_program_path, filename
518
519
        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...
520
                      output_format="regex",
521
                      output_regex=self.test_program_regex,
522
                      severity_map=self.test_program_severity_map)
523
               (Handler)
524
               (self.section, None))
525
526
        results = list(uut.run(self.testfile_path, self.testfile_content))
527
        expected = [Result.from_values(uut,
528
                                       "Invalid char ('0')",
529
                                       self.testfile_path,
530
                                       3, 0, 3, 1,
531
                                       RESULT_SEVERITY.MAJOR),
532
                    Result.from_values(uut,
533
                                       "Invalid char ('.')",
534
                                       self.testfile_path,
535
                                       5, 0, 5, 1,
536
                                       RESULT_SEVERITY.MAJOR),
537
                    Result.from_values(uut,
538
                                       "Invalid char ('p')",
539
                                       self.testfile_path,
540
                                       9, 0, 9, 1,
541
                                       RESULT_SEVERITY.MAJOR)]
542
543
        self.assertEqual(results, expected)
544
        create_arguments_mock.assert_called_once_with(
545
            self.testfile_path, self.testfile_content, None)
546
547
    def test_stdin_stderr_noconfig_nocorrection(self):
548
        create_arguments_mock = Mock()
549
550
        class Handler:
551
552
            @staticmethod
553
            def create_arguments(filename, file, config_file):
554
                create_arguments_mock(filename, file, config_file)
555
                return (self.test_program_path,
556
                        "--use_stderr",
557
                        "--use_stdin",
558
                        filename)
559
560
        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...
561
                      use_stdin=True,
562
                      use_stdout=False,
563
                      use_stderr=True,
564
                      output_format="regex",
565
                      output_regex=self.test_program_regex,
566
                      severity_map=self.test_program_severity_map)
567
               (Handler)
568
               (self.section, None))
569
570
        results = list(uut.run(self.testfile2_path, self.testfile2_content))
571
        expected = [Result.from_values(uut,
572
                                       "Invalid char ('X')",
573
                                       self.testfile2_path,
574
                                       0, 0, 0, 1,
575
                                       RESULT_SEVERITY.MAJOR),
576
                    Result.from_values(uut,
577
                                       "Invalid char ('i')",
578
                                       self.testfile2_path,
579
                                       4, 0, 4, 1,
580
                                       RESULT_SEVERITY.MAJOR)]
581
582
        self.assertEqual(results, expected)
583
        create_arguments_mock.assert_called_once_with(
584
            self.testfile2_path, self.testfile2_content, None)
585
586
    def test_nostdin_nostderr_noconfig_correction(self):
587
        create_arguments_mock = Mock()
588
589
        class Handler:
590
591
            @staticmethod
592
            def create_arguments(filename, file, config_file):
593
                create_arguments_mock(filename, file, config_file)
594
                return self.test_program_path, "--correct", filename
595
596
        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...
597
                      output_format="corrected",
598
                      diff_severity=RESULT_SEVERITY.INFO,
599
                      diff_message="Custom message")
600
               (Handler)
601
               (self.section, None))
602
603
        results = list(uut.run(self.testfile_path, self.testfile_content))
604
605
        expected_correction = [s + "\n"
606
                               for s in ["+", "-", "*", "++", "-", "-", "+"]]
607
608
        diffs = list(Diff.from_string_arrays(
609
            self.testfile_content,
610
            expected_correction).split_diff())
611
612
        expected = [Result.from_values(uut,
613
                                       "Custom message",
614
                                       self.testfile_path,
615
                                       4, None, 4, None,
616
                                       RESULT_SEVERITY.INFO,
617
                                       diffs={self.testfile_path: diffs[0]}),
618
                    Result.from_values(uut,
619
                                       "Custom message",
620
                                       self.testfile_path,
621
                                       6, None, 6, None,
622
                                       RESULT_SEVERITY.INFO,
623
                                       diffs={self.testfile_path: diffs[1]}),
624
                    Result.from_values(uut,
625
                                       "Custom message",
626
                                       self.testfile_path,
627
                                       10, None, 10, None,
628
                                       RESULT_SEVERITY.INFO,
629
                                       diffs={self.testfile_path: diffs[2]})]
630
631
        self.assertEqual(results, expected)
632
        create_arguments_mock.assert_called_once_with(
633
            self.testfile_path, self.testfile_content, None)
634
635
    def test_stdin_stdout_stderr_config_nocorrection(self):
636
        create_arguments_mock = Mock()
637
        generate_config_mock = Mock()
638
639
        class Handler:
640
641
            @staticmethod
642
            def generate_config(filename, file, some_val):
643
                # some_val shall only test the argument delegation from run().
644
                generate_config_mock(filename, file, some_val)
645
                return "\n".join(["use_stdin", "use_stderr"])
646
647
            @staticmethod
648
            def create_arguments(filename, file, config_file, some_val):
649
                create_arguments_mock(filename, file, config_file, some_val)
650
                return self.test_program_path, "--config", config_file
651
652
        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...
653
                      use_stdin=True,
654
                      use_stderr=True,
655
                      output_format="regex",
656
                      output_regex=self.test_program_regex,
657
                      severity_map=self.test_program_severity_map)
658
               (Handler)
659
               (self.section, None))
660
661
        results = list(uut.run(self.testfile_path,
662
                               self.testfile_content,
663
                               some_val=33))
664
        expected = [Result.from_values(uut,
665
                                       "Invalid char ('0')",
666
                                       self.testfile_path,
667
                                       3, 0, 3, 1,
668
                                       RESULT_SEVERITY.MAJOR),
669
                    Result.from_values(uut,
670
                                       "Invalid char ('.')",
671
                                       self.testfile_path,
672
                                       5, 0, 5, 1,
673
                                       RESULT_SEVERITY.MAJOR),
674
                    Result.from_values(uut,
675
                                       "Invalid char ('p')",
676
                                       self.testfile_path,
677
                                       9, 0, 9, 1,
678
                                       RESULT_SEVERITY.MAJOR)]
679
680
        self.assertEqual(results, expected)
681
        create_arguments_mock.assert_called_once_with(
682
            self.testfile_path, self.testfile_content, ANY, 33)
683
        self.assertIsNotNone(create_arguments_mock.call_args[0][2])
684
        generate_config_mock.assert_called_once_with(
685
            self.testfile_path, self.testfile_content, 33)
686
687
    def test_stdin_stderr_config_correction(self):
688
        create_arguments_mock = Mock()
689
        generate_config_mock = Mock()
690
691
        # `some_value_A` and `some_value_B` are used to test the different
692
        # delegation to `generate_config()` and `create_arguments()`
693
        # accordingly.
694
        class Handler:
695
696
            @staticmethod
697
            def generate_config(filename, file, some_value_A):
698
                generate_config_mock(filename, file, some_value_A)
699
                return "\n".join(["use_stdin", "use_stderr", "correct"])
700
701
            @staticmethod
702
            def create_arguments(filename, file, config_file, some_value_B):
703
                create_arguments_mock(filename, file, config_file,
704
                                      some_value_B)
705
                return self.test_program_path, "--config", config_file
706
707
        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...
708
                      use_stdin=True,
709
                      use_stdout=False,
710
                      use_stderr=True,
711
                      output_format="corrected",
712
                      config_suffix=".conf")
713
               (Handler)
714
               (self.section, None))
715
716
        results = list(uut.run(self.testfile2_path,
717
                               self.testfile2_content,
718
                               some_value_A=124,
719
                               some_value_B=-78))
720
721
        expected_correction = [s + "\n" for s in ["+", "/", "/", "-"]]
722
723
        diffs = list(Diff.from_string_arrays(
724
            self.testfile2_content,
725
            expected_correction).split_diff())
726
727
        expected = [Result.from_values(uut,
728
                                       "Inconsistency found.",
729
                                       self.testfile2_path,
730
                                       1, None, 1, None,
731
                                       RESULT_SEVERITY.NORMAL,
732
                                       diffs={self.testfile2_path: diffs[0]}),
733
                    Result.from_values(uut,
734
                                       "Inconsistency found.",
735
                                       self.testfile2_path,
736
                                       5, None, 5, None,
737
                                       RESULT_SEVERITY.NORMAL,
738
                                       diffs={self.testfile2_path: diffs[1]})]
739
740
        self.assertEqual(results, expected)
741
        create_arguments_mock.assert_called_once_with(
742
            self.testfile2_path, self.testfile2_content, ANY, -78)
743
        self.assertEqual(create_arguments_mock.call_args[0][2][-5:], ".conf")
744
        generate_config_mock.assert_called_once_with(
745
            self.testfile2_path, self.testfile2_content, 124)
746