|
| 1 | +# Copyright (C) 2010, Stefano Rivera <stefanor@debian.org> |
| 2 | +# Copyright (C) 2017-2018, Benjamin Drung <bdrung@debian.org> |
| 3 | +# |
| 4 | +# Permission to use, copy, modify, and/or distribute this software for any |
| 5 | +# purpose with or without fee is hereby granted, provided that the above |
| 6 | +# copyright notice and this permission notice appear in all copies. |
| 7 | +# |
| 8 | +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 9 | +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 10 | +# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 11 | +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 12 | +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 13 | +# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 14 | +# PERFORMANCE OF THIS SOFTWARE. |
| 15 | + |
| 16 | +"""test_pylint.py - Run pylint""" |
| 17 | + |
| 18 | +import os |
| 19 | +import re |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import unittest |
| 23 | + |
| 24 | +from . import get_source_files, unittest_verbosity |
| 25 | + |
| 26 | +CONFIG = os.path.join(os.path.dirname(__file__), "pylint.conf") |
| 27 | + |
| 28 | + |
| 29 | +class PylintTestCase(unittest.TestCase): |
| 30 | + """ |
| 31 | + This unittest class provides a test that runs the pylint code check |
| 32 | + on the Python source code. The list of source files is provided by |
| 33 | + the get_source_files() function and pylint is purely configured via |
| 34 | + a config file. |
| 35 | + """ |
| 36 | + |
| 37 | + def test_pylint(self): |
| 38 | + """Test: Run pylint on Python source code""" |
| 39 | + |
| 40 | + cmd = [sys.executable, "-m", "pylint", "--rcfile=" + CONFIG, "--"] + get_source_files() |
| 41 | + if unittest_verbosity() >= 2: |
| 42 | + sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd))) |
| 43 | + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 44 | + close_fds=True) |
| 45 | + out, err = process.communicate() |
| 46 | + |
| 47 | + if process.returncode != 0: # pragma: no cover |
| 48 | + # Strip trailing summary (introduced in pylint 1.7). This summary might look like: |
| 49 | + # |
| 50 | + # ------------------------------------ |
| 51 | + # Your code has been rated at 10.00/10 |
| 52 | + # |
| 53 | + out = re.sub("^(-+|Your code has been rated at .*)$", "", out.decode(), |
| 54 | + flags=re.MULTILINE).rstrip() |
| 55 | + |
| 56 | + # Strip logging of used config file (introduced in pylint 1.8) |
| 57 | + err = re.sub("^Using config file .*\n", "", err.decode()).rstrip() |
| 58 | + |
| 59 | + msgs = [] |
| 60 | + if err: |
| 61 | + msgs.append("pylint exited with code {} and has unexpected output on stderr:\n{}" |
| 62 | + .format(process.returncode, err)) |
| 63 | + if out: |
| 64 | + msgs.append("pylint found issues:\n{}".format(out)) |
| 65 | + if not msgs: |
| 66 | + msgs.append("pylint exited with code {} and has no output on stdout or stderr." |
| 67 | + .format(process.returncode)) |
| 68 | + self.fail("\n".join(msgs)) |
0 commit comments