Some basic Python static analysis and style checks.
| 34 | |
| 35 | |
| 36 | class PythonTests(unittest.TestCase): |
| 37 | ''' |
| 38 | Some basic Python static analysis and style checks. |
| 39 | ''' |
| 40 | |
| 41 | def test_pylint(self): |
| 42 | ''' |
| 43 | Run pylint over the codebase. |
| 44 | ''' |
| 45 | # Run Pylint |
| 46 | stream = io.StringIO() |
| 47 | reporter = TextReporter(stream) |
| 48 | pylint.lint.Run(['./Test'], reporter, False) |
| 49 | output = stream.getvalue() |
| 50 | |
| 51 | # Write the Pylint log |
| 52 | with open('pylint.log', 'w', encoding='utf-8') as handle: |
| 53 | handle.write(output) |
| 54 | |
| 55 | # Analyze the results |
| 56 | pattern = re.compile(r'Your code has been rated at (.*?)/10') |
| 57 | match = pattern.search(output) |
| 58 | self.assertIsNotNone(match) |
| 59 | score = float(match.group(1)) |
| 60 | |
| 61 | # This target is currently low but we will increase over time |
| 62 | self.assertGreaterEqual(score, 9.95, 'Found Pylint score regression') |
| 63 | |
| 64 | def test_pycodestyle(self): |
| 65 | ''' |
| 66 | Run pycodestyle over the codebase. |
| 67 | ''' |
| 68 | style = pycodestyle.StyleGuide() |
| 69 | |
| 70 | # Write the Pycodestyle log |
| 71 | with open('pycodestyle.log', 'w', encoding='utf-8') as handle: |
| 72 | # Redirect stdout to file so we can capture output |
| 73 | old_stdout = sys.stdout |
| 74 | sys.stdout = handle |
| 75 | |
| 76 | result = style.check_files(['./Test']) |
| 77 | |
| 78 | # Restore stdout |
| 79 | sys.stdout = old_stdout |
| 80 | |
| 81 | self.assertEqual(result.total_errors, 0, |
| 82 | 'Found pycodestyle warnings or errors.') |
| 83 | |
| 84 | def test_mypy(self): |
| 85 | ''' |
| 86 | Run mypy over the codebase. |
| 87 | ''' |
| 88 | # pylint: disable=c-extension-no-member |
| 89 | result = mypy.api.run(['./Test']) |
| 90 | |
| 91 | # Write the mypy log |
| 92 | if result[0]: |
| 93 | with open('mypy.log', 'w', encoding='utf-8') as handle: |
nothing calls this directly
no outgoing calls
no test coverage detected