mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-05-20 13:21:28 +00:00
Update pylint and flake8 unittests
Import improvements from https://github.com/bdrung/snippets
This commit is contained in:
parent
468dbc7746
commit
74df5b3869
@ -1,6 +1,5 @@
|
|||||||
# Test suite for ubuntutools
|
|
||||||
#
|
|
||||||
# Copyright (C) 2010, Stefano Rivera <stefanor@ubuntu.com>
|
# Copyright (C) 2010, Stefano Rivera <stefanor@ubuntu.com>
|
||||||
|
# Copyright (C) 2017, Benjamin Drung <benjamin.drung@profitbricks.com>
|
||||||
#
|
#
|
||||||
# Permission to use, copy, modify, and/or distribute this software for any
|
# Permission to use, copy, modify, and/or distribute this software for any
|
||||||
# purpose with or without fee is hereby granted, provided that the above
|
# purpose with or without fee is hereby granted, provided that the above
|
||||||
@ -14,6 +13,9 @@
|
|||||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
# PERFORMANCE OF THIS SOFTWARE.
|
# PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
"""Test suite for ubuntutools"""
|
||||||
|
|
||||||
|
import inspect
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -53,3 +55,16 @@ def get_source_files():
|
|||||||
else:
|
else:
|
||||||
files.append(code_file)
|
files.append(code_file)
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
|
def unittest_verbosity():
|
||||||
|
"""Return the verbosity setting of the currently running unittest
|
||||||
|
program, or None if none is running.
|
||||||
|
"""
|
||||||
|
frame = inspect.currentframe()
|
||||||
|
while frame:
|
||||||
|
self = frame.f_locals.get("self")
|
||||||
|
if isinstance(self, unittest.TestProgram):
|
||||||
|
return self.verbosity
|
||||||
|
frame = frame.f_back
|
||||||
|
return None # pragma: no cover
|
||||||
|
@ -1,3 +1,33 @@
|
|||||||
|
[MASTER]
|
||||||
|
|
||||||
|
# Pickle collected data for later comparisons.
|
||||||
|
persistent=no
|
||||||
|
|
||||||
|
|
||||||
|
[MESSAGES CONTROL]
|
||||||
|
|
||||||
|
# Only show warnings with the listed confidence levels. Leave empty to show
|
||||||
|
# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED
|
||||||
|
confidence=HIGH
|
||||||
|
|
||||||
|
# Disable the message, report, category or checker with the given id(s). You
|
||||||
|
# can either give multiple identifiers separated by comma (,) or put this
|
||||||
|
# option multiple times (only on the command line, not in the configuration
|
||||||
|
# file where it should appear only once).You can also use "--disable=all" to
|
||||||
|
# disable everything first and then reenable specific checks. For example, if
|
||||||
|
# you want to run only the similarities checker, you can use "--disable=all
|
||||||
|
# --enable=similarities". If you want to run only the classes checker, but have
|
||||||
|
# no Warning level messages displayed, use"--disable=all --enable=classes
|
||||||
|
# --disable=W"
|
||||||
|
disable=locally-disabled
|
||||||
|
|
||||||
|
|
||||||
|
[REPORTS]
|
||||||
|
|
||||||
|
# Tells whether to display a full report or only the messages
|
||||||
|
reports=no
|
||||||
|
|
||||||
|
|
||||||
[TYPECHECK]
|
[TYPECHECK]
|
||||||
|
|
||||||
# List of classes names for which member attributes should not be checked
|
# List of classes names for which member attributes should not be checked
|
||||||
@ -5,6 +35,7 @@
|
|||||||
# lpapicache classes, urlparse
|
# lpapicache classes, urlparse
|
||||||
ignored-classes=Launchpad,BaseWrapper,PersonTeam,Distribution,Consumer,Credentials,ParseResult,apt_pkg,apt_pkg.Dependency,apt_pkg.BaseDependency
|
ignored-classes=Launchpad,BaseWrapper,PersonTeam,Distribution,Consumer,Credentials,ParseResult,apt_pkg,apt_pkg.Dependency,apt_pkg.BaseDependency
|
||||||
|
|
||||||
|
|
||||||
[FORMAT]
|
[FORMAT]
|
||||||
|
|
||||||
# Maximum number of characters on a single line.
|
# Maximum number of characters on a single line.
|
||||||
@ -14,6 +45,7 @@ max-line-length=99
|
|||||||
# tab).
|
# tab).
|
||||||
indent-string=' '
|
indent-string=' '
|
||||||
|
|
||||||
|
|
||||||
[BASIC]
|
[BASIC]
|
||||||
|
|
||||||
# Allow variables called e, f, lp
|
# Allow variables called e, f, lp
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Copyright (C) 2017, Benjamin Drung <bdrung@debian.org>
|
# Copyright (C) 2017-2018, Benjamin Drung <bdrung@debian.org>
|
||||||
#
|
#
|
||||||
# Permission to use, copy, modify, and/or distribute this software for any
|
# Permission to use, copy, modify, and/or distribute this software for any
|
||||||
# purpose with or without fee is hereby granted, provided that the above
|
# purpose with or without fee is hereby granted, provided that the above
|
||||||
@ -17,19 +17,34 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from ubuntutools.test import get_source_files, unittest
|
from ubuntutools.test import get_source_files, unittest, unittest_verbosity
|
||||||
|
|
||||||
|
|
||||||
class Flake8TestCase(unittest.TestCase):
|
class Flake8TestCase(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
This unittest class provides a test that runs the flake8 code
|
||||||
|
checker (which combines pycodestyle and pyflakes) on the Python
|
||||||
|
source code. The list of source files is provided by the
|
||||||
|
get_source_files() function.
|
||||||
|
"""
|
||||||
|
|
||||||
def test_flake8(self):
|
def test_flake8(self):
|
||||||
"Test: Run flake8 on Python source code"
|
"""Test: Run flake8 on Python source code"""
|
||||||
with open('/proc/self/cmdline', 'r') as cmdline_file:
|
cmd = [sys.executable, "-m", "flake8", "--max-line-length=99"] + get_source_files()
|
||||||
python_binary = cmdline_file.read().split('\0')[0]
|
if unittest_verbosity() >= 2:
|
||||||
cmd = [python_binary, '-m', 'flake8', '--max-line-length=99'] + get_source_files()
|
|
||||||
sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd)))
|
sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd)))
|
||||||
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE, close_fds=True)
|
stderr=subprocess.PIPE, close_fds=True)
|
||||||
|
|
||||||
out, err = process.communicate()
|
out, err = process.communicate()
|
||||||
self.assertFalse(err, "Unexpected standard error from flake8 run:\n" + err.decode())
|
if process.returncode != 0: # pragma: no cover
|
||||||
self.assertFalse(out, "flake8 found issues:\n" + out.decode())
|
msgs = []
|
||||||
|
if err:
|
||||||
|
msgs.append("flake8 exited with code {} and has unexpected output on stderr:\n{}"
|
||||||
|
.format(process.returncode, err.decode().rstrip()))
|
||||||
|
if out:
|
||||||
|
msgs.append("flake8 found issues:\n{}".format(out.decode().rstrip()))
|
||||||
|
if not msgs:
|
||||||
|
msgs.append("flake8 exited with code {} and has no output on stdout or stderr."
|
||||||
|
.format(process.returncode))
|
||||||
|
self.fail("\n".join(msgs))
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
# test_pylint.py - Run pylint in errors-only mode.
|
|
||||||
#
|
|
||||||
# Copyright (C) 2010, Stefano Rivera <stefanor@ubuntu.com>
|
# Copyright (C) 2010, Stefano Rivera <stefanor@ubuntu.com>
|
||||||
# Copyright (C) 2017, Benjamin Drung <bdrung@ubuntu.com>
|
# Copyright (C) 2017-2018, Benjamin Drung <bdrung@ubuntu.com>
|
||||||
#
|
#
|
||||||
# Permission to use, copy, modify, and/or distribute this software for any
|
# Permission to use, copy, modify, and/or distribute this software for any
|
||||||
# purpose with or without fee is hereby granted, provided that the above
|
# purpose with or without fee is hereby granted, provided that the above
|
||||||
@ -15,26 +13,56 @@
|
|||||||
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
# PERFORMANCE OF THIS SOFTWARE.
|
# PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
"""test_pylint.py - Run pylint in errors-only mode."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from ubuntutools.test import get_source_files, unittest
|
from ubuntutools.test import get_source_files, unittest, unittest_verbosity
|
||||||
from ubuntutools import subprocess
|
from ubuntutools import subprocess
|
||||||
|
|
||||||
|
CONFIG = os.path.join(os.path.dirname(__file__), "pylint.conf")
|
||||||
|
|
||||||
|
|
||||||
class PylintTestCase(unittest.TestCase):
|
class PylintTestCase(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
This unittest class provides a test that runs the pylint code check
|
||||||
|
on the Python source code. The list of source files is provided by
|
||||||
|
the get_source_files() function and pylint is configured via a
|
||||||
|
config file.
|
||||||
|
"""
|
||||||
|
|
||||||
def test_pylint(self):
|
def test_pylint(self):
|
||||||
"Test: Run pylint on Python source code"
|
"""Test: Run pylint on Python source code"""
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
pylint_binary = 'pylint3'
|
cmd = [sys.executable, "-m", "pylint", "--rcfile=" + CONFIG,
|
||||||
else:
|
"-E", "--"] + get_source_files()
|
||||||
pylint_binary = 'pylint'
|
if unittest_verbosity() >= 2:
|
||||||
cmd = [pylint_binary, '--rcfile=ubuntutools/test/pylint.conf', '-E',
|
|
||||||
'--reports=n', '--confidence=HIGH', '--'] + get_source_files()
|
|
||||||
sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd)))
|
sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd)))
|
||||||
try:
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
|
close_fds=True)
|
||||||
except subprocess.CalledProcessError as e:
|
out, err = process.communicate()
|
||||||
self.fail(
|
|
||||||
'%s crashed (%d). Error output:\n%s' %
|
if process.returncode != 0: # pragma: no cover
|
||||||
(pylint_binary, e.returncode, e.output.decode())
|
# Strip trailing summary (introduced in pylint 1.7). This summary might look like:
|
||||||
)
|
#
|
||||||
|
# ------------------------------------
|
||||||
|
# Your code has been rated at 10.00/10
|
||||||
|
#
|
||||||
|
out = re.sub("^(-+|Your code has been rated at .*)$", "", out.decode(),
|
||||||
|
flags=re.MULTILINE).rstrip()
|
||||||
|
|
||||||
|
# Strip logging of used config file (introduced in pylint 1.8)
|
||||||
|
err = re.sub("^Using config file .*\n", "", err.decode()).rstrip()
|
||||||
|
|
||||||
|
msgs = []
|
||||||
|
if err:
|
||||||
|
msgs.append("pylint exited with code {} and has unexpected output on stderr:\n{}"
|
||||||
|
.format(process.returncode, err))
|
||||||
|
if out:
|
||||||
|
msgs.append("pylint found issues:\n{}".format(out))
|
||||||
|
if not msgs:
|
||||||
|
msgs.append("pylint exited with code {} and has no output on stdout or stderr."
|
||||||
|
.format(process.returncode))
|
||||||
|
self.fail("\n".join(msgs))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user