| 1 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from coalib.bearlib.abstractions.Lint import Lint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from coalib.misc.ContextManagers import prepare_file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from coalib.misc.Shell import escape_path_argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from coalib.results.SourceRange import SourceRange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from coalib.settings.Section import Section | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class LintTest(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         section = Section("some_name") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         self.uut = Lint(section, None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     def test_invalid_output(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         out = list(self.uut.process_output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             ["1.0|0: Info message\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |              "2.2|1: Normal message\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |              "3.4|2: Major message\n"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             "a/file.py", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ['original_file_lines_placeholder'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self.assertEqual(len(out), 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         self.assertEqual(out[0].origin, "Lint") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         self.assertEqual(out[0].affected_code[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                          SourceRange.from_values("a/file.py", 1, 0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         self.assertEqual(out[0].severity, RESULT_SEVERITY.INFO) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self.assertEqual(out[0].message, "Info message") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         self.assertEqual(out[1].affected_code[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                          SourceRange.from_values("a/file.py", 2, 2)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         self.assertEqual(out[1].severity, RESULT_SEVERITY.NORMAL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self.assertEqual(out[1].message, "Normal message") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self.assertEqual(out[2].affected_code[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                          SourceRange.from_values("a/file.py", 3, 4)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self.assertEqual(out[2].severity, RESULT_SEVERITY.MAJOR) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         self.assertEqual(out[2].message, "Major message") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     def test_custom_regex(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         self.uut.output_regex = (r'(?P<origin>\w+)\|' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                                  r'(?P<line>\d+)\.(?P<column>\d+)\|' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                                  r'(?P<end_line>\d+)\.(?P<end_column>\d+)\|' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                                  r'(?P<severity>\w+): (?P<message>.*)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self.uut.severity_map = {"I": RESULT_SEVERITY.INFO} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         out = list(self.uut.process_output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ["info_msg|1.0|2.3|I: Info message\n"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'a/file.py', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ['original_file_lines_placeholder'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self.assertEqual(len(out), 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         self.assertEqual(out[0].affected_code[0].start.line, 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self.assertEqual(out[0].affected_code[0].start.column, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self.assertEqual(out[0].affected_code[0].end.line, 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         self.assertEqual(out[0].affected_code[0].end.column, 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         self.assertEqual(out[0].severity, RESULT_SEVERITY.INFO) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         self.assertEqual(out[0].origin, 'Lint (info_msg)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     def test_valid_output(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         out = list(self.uut.process_output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ["Random line that shouldn't be captured\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |              "*************\n"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'a/file.py', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ['original_file_lines_placeholder'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         self.assertEqual(len(out), 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     def test_stdin_input(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         with prepare_file(["abcd", "efgh"], None) as (lines, filename): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             # Use more which is a command that can take stdin and show it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             # This is available in windows and unix. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             self.uut.executable = "more" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             self.uut.use_stdin = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             self.uut.use_stderr = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             self.uut.process_output = lambda output, filename, file: output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             out = self.uut.lint(file=lines) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             # Some implementations of `more` add an extra newline at the end. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             self.assertTrue(("abcd\n", "efgh\n") == out or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                             ("abcd\n", "efgh\n", "\n") == out) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     def test_stderr_output(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         self.uut.executable = "echo" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         self.uut.arguments = "hello" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         self.uut.use_stdin = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         self.uut.use_stderr = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         self.uut.process_output = lambda output, filename, file: output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         out = self.uut.lint("unused_filename") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         self.assertEqual((), out)  # stderr is used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         self.uut.use_stderr = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         out = self.uut.lint("unused_filename") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         self.assertEqual(('hello\n',), out)  # stdout is used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         def assert_warn(line): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             assert line == "hello" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         old_warn = self.uut.warn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         self.uut.warn = assert_warn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         self.uut._print_errors(["hello", "\n"]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         self.uut.warn = old_warn | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     def test_gives_corrected(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         self.uut.gives_corrected = True | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         out = tuple(self.uut.process_output(["a", "b"], "filename", ["a", "b"])) | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         self.assertEqual((), out) | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         out = tuple(self.uut.process_output(["a", "b"], "filename", ["a"])) | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         self.assertEqual(len(out), 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     def test_missing_binary(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         old_binary = Lint.executable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         invalid_binary = "invalid_binary_which_doesnt_exist" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         Lint.executable = invalid_binary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         self.assertEqual(Lint.check_prerequisites(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                          "'{}' is not installed.".format(invalid_binary)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         # "echo" is existent on nearly all platforms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         Lint.executable = "echo" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         self.assertTrue(Lint.check_prerequisites()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         del Lint.executable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         self.assertTrue(Lint.check_prerequisites()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         Lint.executable = old_binary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     def test_config_file_generator(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         self.uut.executable = "echo" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         self.uut.arguments = "-c {config_file}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         self.assertEqual( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             self.uut._create_command(config_file="configfile").strip(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             "echo -c " + escape_path_argument("configfile")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     def test_config_file_generator(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         self.uut.executable = "echo" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         self.uut.config_file = lambda: ["config line1"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         config_filename = self.uut.generate_config_file() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         self.assertTrue(os.path.isfile(config_filename)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         os.remove(config_filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         # To complete coverage of closing the config file and check if any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         # errors are thrown there. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |         self.uut.lint("filename") | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  |