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.
This commit is contained in:
Benjamin Drung 2018-10-06 17:16:12 +02:00
parent 5ebd1eaa8d
commit 468dbc7746
3 changed files with 29 additions and 22 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)