• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# -----------------------------------------------------------------------------
4# Copyright 2020 Arm Limited
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may not
7# use this file except in compliance with the License. You may obtain a copy
8# of the License at:
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17# -----------------------------------------------------------------------------
18"""
19The python test runner is designed to run some basic tests against the Python
20test code base.
21"""
22
23import re
24import sys
25import unittest
26
27import pycodestyle
28import pylint.epylint as lint
29
30
31class PythonTests(unittest.TestCase):
32    """
33    Some basic Python static analysis and style checks.
34    """
35
36    def test_pylint(self):
37        """
38        Run pylint over the codebase.
39        """
40        pylintOut, _ = lint.py_run("./Test", True)
41        pattern = re.compile(r"Your code has been rated at (.*?)/10")
42        match = pattern.search(pylintOut.getvalue())
43        self.assertIsNotNone(match)
44        score = float(match.group(1))
45        self.assertGreaterEqual(score, 9.8)
46
47        with open("pylint.log", "w") as fileHandle:
48            fileHandle.write(pylintOut.getvalue())
49
50    def test_pycodestyle(self):
51        """
52        Test that we conform to PEP-8.
53        """
54        style = pycodestyle.StyleGuide()
55        result = style.check_files(["./Test"])
56        self.assertEqual(result.total_errors, 0,
57                         "Found code style errors (and warnings).")
58
59
60def main():
61    """
62    The main function.
63
64    Returns:
65        int: The process return code.
66    """
67    results = unittest.main(exit=False)
68    return 0 if results.result.wasSuccessful() else 1
69
70
71if __name__ == "__main__":
72    sys.exit(main())
73