mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-05-12 01:11:29 +00:00
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:
parent
5ebd1eaa8d
commit
468dbc7746
@ -17,6 +17,8 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import setup
|
||||||
|
|
||||||
if sys.version_info < (2, 7):
|
if sys.version_info < (2, 7):
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
else:
|
else:
|
||||||
@ -28,3 +30,26 @@ def discover():
|
|||||||
__main__ = sys.modules['__main__']
|
__main__ = sys.modules['__main__']
|
||||||
setupDir = os.path.abspath(os.path.dirname(__main__.__file__))
|
setupDir = os.path.abspath(os.path.dirname(__main__.__file__))
|
||||||
return unittest.defaultTestLoader.discover(setupDir)
|
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
|
||||||
|
@ -17,24 +17,15 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import setup
|
from ubuntutools.test import get_source_files, unittest
|
||||||
from ubuntutools.test import unittest
|
|
||||||
|
|
||||||
|
|
||||||
class Flake8TestCase(unittest.TestCase):
|
class Flake8TestCase(unittest.TestCase):
|
||||||
def test_flake8(self):
|
def test_flake8(self):
|
||||||
"Test: Run flake8 on Python source code"
|
"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:
|
with open('/proc/self/cmdline', 'r') as cmdline_file:
|
||||||
python_binary = cmdline_file.read().split('\0')[0]
|
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)))
|
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)
|
||||||
|
@ -17,28 +17,19 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import setup
|
from ubuntutools.test import get_source_files, unittest
|
||||||
from ubuntutools.test import unittest
|
|
||||||
from ubuntutools import subprocess
|
from ubuntutools import subprocess
|
||||||
|
|
||||||
|
|
||||||
class PylintTestCase(unittest.TestCase):
|
class PylintTestCase(unittest.TestCase):
|
||||||
def test_pylint(self):
|
def test_pylint(self):
|
||||||
"Test: Run pylint on Python source code"
|
"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:
|
if sys.version_info[0] == 3:
|
||||||
pylint_binary = 'pylint3'
|
pylint_binary = 'pylint3'
|
||||||
else:
|
else:
|
||||||
pylint_binary = 'pylint'
|
pylint_binary = 'pylint'
|
||||||
cmd = [pylint_binary, '--rcfile=ubuntutools/test/pylint.conf', '-E',
|
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)))
|
sys.stderr.write("Running following command:\n{}\n".format(" ".join(cmd)))
|
||||||
try:
|
try:
|
||||||
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
|
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user