diff options
author | JensDiemer <git@jensdiemer.de> | 2020-01-18 23:26:35 +0100 |
---|---|---|
committer | JensDiemer <git@jensdiemer.de> | 2020-01-18 23:26:35 +0100 |
commit | 51295d3a7251d29b6701d9c43fae83e8082614a9 (patch) | |
tree | 9284b95798b8361ed47b0c551103e5ab8f5e9c34 /creole/tests | |
parent | 6c3b724aae476aca79e477e84f3a31f58d5b2a30 (diff) | |
download | creole-51295d3a7251d29b6701d9c43fae83e8082614a9.tar.gz |
Simplify subprocess mixin
Diffstat (limited to 'creole/tests')
-rw-r--r-- | creole/tests/test_subprocess.py | 26 | ||||
-rw-r--r-- | creole/tests/utils/unittest_subprocess.py | 57 |
2 files changed, 29 insertions, 54 deletions
diff --git a/creole/tests/test_subprocess.py b/creole/tests/test_subprocess.py index 48fcd1f..fcce3bc 100644 --- a/creole/tests/test_subprocess.py +++ b/creole/tests/test_subprocess.py @@ -4,41 +4,23 @@ unittest ~~~~~~~~ - :copyleft: 2015 by python-creole team, see AUTHORS for more details. + :copyleft: 2015-2020 by python-creole team, see AUTHORS for more details. :license: GNU GPL v3 or above, see LICENSE for more details. """ - -import unittest -import sys import os +import sys +import unittest from creole.tests.utils.unittest_subprocess import SubprocessMixin class TestSubprocessMixin(unittest.TestCase, SubprocessMixin): - def test_find_executable(self): - filepath = self.find_executable("python") - if not hasattr(self, "assertRegex"): # New in version 3.1 - self.assertRegex = self.assertRegexpMatches - self.assertRegex(filepath, ".*?python.*?") - - def test_executable_not_exists(self): - with self.assertRaisesRegexp(AssertionError, """Program "doesn't exists!" not found in:.*"""): - self.find_executable("doesn't exists!") - - def test_executable_with_path(self): - msg = "'%s' unexpectedly found in '%s'" % ( - os.sep, sys.executable - ) - with self.assertRaisesRegexp(AssertionError, msg): - self.find_executable(sys.executable) def test_subprocess(self): popen_args, retcode, stdout = self.subprocess( popen_args=[sys.executable, "-c", "import sys;sys.stdout.write('to stdout')"], - verbose=False ) self.assertEqual(stdout, "to stdout") self.assertEqual(retcode, 0) @@ -64,6 +46,4 @@ class TestSubprocessMixin(unittest.TestCase, SubprocessMixin): "to stdout 2\n" "to stderr 2\n" ), - verbose=True ) - diff --git a/creole/tests/utils/unittest_subprocess.py b/creole/tests/utils/unittest_subprocess.py index 96f271b..3fe4b3c 100644 --- a/creole/tests/utils/unittest_subprocess.py +++ b/creole/tests/utils/unittest_subprocess.py @@ -1,63 +1,60 @@ -# coding: utf-8 - """ unittest subprocess helper ~~~~~~~~~~~~~~~~~~~~~~~~~~ - :copyleft: 2015 by python-creole team, see AUTHORS for more details. + :copyleft: 2015-2020 by python-creole team, see AUTHORS for more details. :license: GNU GPL v3 or above, see LICENSE for more details. """ -import json + import os +import shutil import subprocess import sys +from pathlib import Path -class SubprocessMixin(object): - # call .../env/bin/python will not add the .../env/bin/ to the PATH - SEARCH_PATH=[os.path.dirname(sys.executable)] + os.environ.get("PATH", "").split(os.pathsep) +class SubprocessMixin: - def find_executable(self, program): - self.assertNotIn(os.sep, program) - for path in self.SEARCH_PATH: - filepath = os.path.join(path, program) - if os.path.isfile(filepath): - if not os.access(filepath, os.X_OK): - sys.stderr.write("File %r is not executable?!?\n" % filepath) - else: - return filepath + def subprocess(self, popen_args): + assert isinstance(popen_args, (tuple, list)) - self.fail("Program %s not found in:\n\t* %s" % (json.dumps(program), "\n\t* ".join(self.SEARCH_PATH))) + print("Call:", popen_args) - def subprocess(self, popen_args, verbose=True): - assert isinstance(popen_args, (tuple, list)) + # Expand PATH + bin_path = str(Path(sys.executable).parent) + if bin_path not in os.environ["PATH"]: + # .../venv/bin will be not in PATH in tests, just add it + # so that installed [tool.poetry.scripts] will be found + os.environ["PATH"] += os.pathsep + bin_path - if verbose: - print("Call:", popen_args) + # Check if executeable will be found + prog = popen_args[0] + cmd = shutil.which(prog) + assert cmd is not None, f'{prog!r} not found in PATH: {os.environ.get("PATH")!r}!' try: - process = subprocess.Popen(popen_args, + process = subprocess.Popen( + popen_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, ) except Exception as err: - self.fail("Error subprocess call with %r: %s" % (popen_args, err)) + self.fail(f"Error subprocess call with {popen_args!r}: {err}") stdout, stderr = process.communicate() retcode = process.poll() - if verbose: - print("return code: %r" % retcode) - print("stdout: %r" % stdout) - print("stderr: %r" % stderr) + print(f"return code: {retcode!r}") + print(f"stdout: {stdout!r}") + print(f"stderr: {stderr!r}") stdout = stdout.strip() return popen_args, retcode, stdout - def assertSubprocess(self, popen_args, retcode, stdout, verbose=True): - popen_args2, retcode2, stdout2 = self.subprocess(popen_args, verbose) + def assertSubprocess(self, popen_args, retcode, stdout): + popen_args2, retcode2, stdout2 = self.subprocess(popen_args) stdout = stdout.strip() try: self.assertEqual(stdout, stdout2, "stdout wrong:") @@ -76,5 +73,3 @@ class SubprocessMixin(object): stdout2, ) self.fail(msg) - - |