Skip to content

Commit

Permalink
feat(tests): Use subprocess.run
Browse files Browse the repository at this point in the history
Simplify code by using `subprocess.run`.
  • Loading branch information
bdrung committed Aug 1, 2024
1 parent 00a3167 commit dd14b55
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
9 changes: 3 additions & 6 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ def test_black(self):
cmd = ["black", "--check", "--diff"] + get_source_files()
if unittest_verbosity() >= 2:
sys.stderr.write(f"Running following command:\n{' '.join(cmd)}\n")
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True
) as process:
output = process.communicate()[0].decode()
process = subprocess.run(cmd, capture_output=True, check=False, text=True)

if process.returncode == 1: # pragma: no cover
self.fail(f"black found code that needs reformatting:\n{output.strip()}")
self.fail(f"black found code that needs reformatting:\n{process.stdout.strip()}")
if process.returncode != 0: # pragma: no cover
self.fail(f"black exited with code {process.returncode}:\n{output.strip()}")
self.fail(f"black exited with code {process.returncode}:\n{process.stdout.strip()}")
15 changes: 6 additions & 9 deletions tests/test_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@ def test_flake8(self):
cmd = [sys.executable, "-m", "flake8", "--max-line-length=99"] + get_source_files()
if unittest_verbosity() >= 2:
sys.stderr.write(f"Running following command:\n{' '.join(cmd)}\n")
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True
) as process:
out, err = process.communicate()
process = subprocess.run(cmd, capture_output=True, check=False, text=True)

if process.returncode != 0: # pragma: no cover
msgs = []
if err:
if process.stderr:
msgs.append(
f"flake8 exited with code {process.returncode} "
f"and has unexpected output on stderr:\n{err.decode().rstrip()}"
f"flake8 exited with code {process.returncode} and has"
f" unexpected output on stderr:\n{process.stderr.rstrip()}"
)
if out:
msgs.append(f"flake8 found issues:\n{out.decode().rstrip()}")
if process.stdout:
msgs.append(f"flake8 found issues:\n{process.stdout.rstrip()}")
if not msgs:
msgs.append(
f"flake8 exited with code {process.returncode} "
Expand Down
7 changes: 2 additions & 5 deletions tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def test_isort(self):
cmd = ["isort", "--check-only", "--diff"] + get_source_files()
if unittest_verbosity() >= 2:
sys.stderr.write(f"Running following command:\n{' '.join(cmd)}\n")
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True
) as process:
output = process.communicate()[0].decode()
process = subprocess.run(cmd, capture_output=True, check=False, text=True)

if process.returncode != 0: # pragma: no cover
self.fail(f"isort found unsorted Python import definitions:\n{output.strip()}")
self.fail(f"isort found unsorted Python import definitions:\n{process.stdout.strip()}")
9 changes: 3 additions & 6 deletions tests/test_pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def test_pylint(self):
cmd.insert(2, "--errors-only")
if unittest_verbosity() >= 2:
sys.stderr.write(f"Running following command:\n{' '.join(cmd)}\n")
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True
) as process:
out, err = process.communicate()
process = subprocess.run(cmd, capture_output=True, check=False, text=True)

if process.returncode != 0: # pragma: no cover
# Strip trailing summary (introduced in pylint 1.7). This summary might look like:
Expand All @@ -54,11 +51,11 @@ def test_pylint(self):
# Your code has been rated at 10.00/10
#
out = re.sub(
"^(-+|Your code has been rated at .*)$", "", out.decode(), flags=re.MULTILINE
"^(-+|Your code has been rated at .*)$", "", process.stdout, 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()
err = re.sub("^Using config file .*\n", "", process.stderr.rstrip())

msgs = []
if err:
Expand Down

0 comments on commit dd14b55

Please sign in to comment.