From 468dbc774680b576ab2099bd9752ee0fdda24fe3 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Sat, 6 Oct 2018 17:16:12 +0200 Subject: [PATCH] ubuntutools/test: Introduce get_source_files function The flake8 and pylint unittest use the same logic to determine the source files. Therefore put this logic in one function. --- ubuntutools/test/__init__.py | 25 +++++++++++++++++++++++++ ubuntutools/test/test_flake8.py | 13 ++----------- ubuntutools/test/test_pylint.py | 13 ++----------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/ubuntutools/test/__init__.py b/ubuntutools/test/__init__.py index c52a089..25589eb 100644 --- a/ubuntutools/test/__init__.py +++ b/ubuntutools/test/__init__.py @@ -17,6 +17,8 @@ import os import sys +import setup + if sys.version_info < (2, 7): import unittest2 as unittest else: @@ -28,3 +30,26 @@ def discover(): __main__ = sys.modules['__main__'] setupDir = os.path.abspath(os.path.dirname(__main__.__file__)) return unittest.defaultTestLoader.discover(setupDir) + + +def get_source_files(): + """Return a list of sources files/directories (to check with flake8/pylint)""" + modules = ["ubuntutools"] + py_files = ["setup.py"] + + files = [] + for code_file in setup.scripts + modules + py_files: + is_script = code_file in setup.scripts + if not os.path.exists(code_file): # pragma: no cover + # The alternative path is needed for Debian's pybuild + alternative = os.path.join(os.environ.get("OLDPWD", ""), code_file) + code_file = alternative if os.path.exists(alternative) else code_file + if is_script: + with open(code_file, "rb") as script_file: + shebang = script_file.readline().decode("utf-8") + if ((sys.version_info[0] == 3 and "python3" in shebang) + or ("python" in shebang and "python3" not in shebang)): + files.append(code_file) + else: + files.append(code_file) + return files diff --git a/ubuntutools/test/test_flake8.py b/ubuntutools/test/test_flake8.py index 3289638..d51f9ec 100644 --- a/ubuntutools/test/test_flake8.py +++ b/ubuntutools/test/test_flake8.py @@ -17,24 +17,15 @@ import subprocess import sys -import setup -from ubuntutools.test import unittest +from ubuntutools.test import get_source_files, unittest class Flake8TestCase(unittest.TestCase): def test_flake8(self): "Test: Run flake8 on Python source code" - files = ['ubuntutools', 'setup.py'] - for script in setup.scripts: - with open(script, 'r') as script_file: - shebang = script_file.readline() - if ((sys.version_info[0] == 3 and 'python3' in shebang) or - ('python' in shebang and 'python3' not in shebang)): - files.append(script) - with open('/proc/self/cmdline', 'r') as cmdline_file: python_binary = cmdline_file.read().split('\0')[0] - cmd = [python_binary, '-m', 'flake8', '--max-line-length=99'] + files + cmd = [python_binary, '-m', 'flake8', '--max-line-length=99'] + get_source_files() sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd))) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) diff --git a/ubuntutools/test/test_pylint.py b/ubuntutools/test/test_pylint.py index 4684fd3..5e5e85f 100644 --- a/ubuntutools/test/test_pylint.py +++ b/ubuntutools/test/test_pylint.py @@ -17,28 +17,19 @@ import sys -import setup -from ubuntutools.test import unittest +from ubuntutools.test import get_source_files, unittest from ubuntutools import subprocess class PylintTestCase(unittest.TestCase): def test_pylint(self): "Test: Run pylint on Python source code" - files = ['ubuntutools', 'setup.py'] - for script in setup.scripts: - with open(script, 'r') as script_file: - shebang = script_file.readline() - if ((sys.version_info[0] == 3 and 'python3' in shebang) or - ('python' in shebang and 'python3' not in shebang)): - files.append(script) - if sys.version_info[0] == 3: pylint_binary = 'pylint3' else: pylint_binary = 'pylint' cmd = [pylint_binary, '--rcfile=ubuntutools/test/pylint.conf', '-E', - '--reports=n', '--confidence=HIGH', '--'] + files + '--reports=n', '--confidence=HIGH', '--'] + get_source_files() sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd))) try: subprocess.check_output(cmd, stderr=subprocess.STDOUT)