Skip to content

Commit

Permalink
Fixed adding the captured output to CalledProcessError object when a …
Browse files Browse the repository at this point in the history
…component execution fails. (#10)
  • Loading branch information
DriesSchaumont authored Jun 6, 2023
1 parent 42e2d2c commit 87abda5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
*********

0.3.2 (6/06/2023)
=================
* `run_component`: fixed adding the captured output to `CalledProcessError` object when a component execution fails.

0.3.1 (6/06/2023)
================
* `run_component`: fix a bug where `pytest.fail` was used when running a component failed instead of using `CalledProcessError`.
Expand Down
56 changes: 56 additions & 0 deletions tests/unittests/fixtures/test_run_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,59 @@ def test_loading_run_component(mocker, run_component):
result.stdout.no_fnmatch_line("*def wrapper*")
result.stdout.no_fnmatch_line("*def run_component*")
assert result.ret == 1


@pytest.mark.parametrize(
"message_to_check, expected_outcome, expected_exitcode",
[
(
"RuntimeError: This script should fail",
"*test_run_component_fails_capturing.py::test_loading_run_component PASSED*",
0,
),
(
"something_something_this_will_not_work",
"*test_run_component_fails_capturing.py::test_loading_run_component FAILED*",
1,
),
],
)
def test_run_component_fails_capturing(
pytester,
makepyfile_and_add_meta,
dummy_config_with_info,
message_to_check,
expected_outcome,
expected_exitcode,
):
executable = pytester.makefile(
"",
foo="#!/bin/sh\npython -c 'import sys; raise RuntimeError(\"This script should fail\")'",
)
executable.chmod(executable.stat().st_mode | stat.S_IEXEC)

makepyfile_and_add_meta(
f"""
import subprocess
import pytest
import re
from pathlib import Path
def test_loading_run_component(mocker, run_component):
mocked_path = mocker.patch('viashpy.testing.Path.is_file', return_value=True)
with pytest.raises(subprocess.CalledProcessError) as e:
run_component(["bar"])
assert re.search(r"{message_to_check}", e.value.stdout.decode('utf-8'))
""",
dummy_config_with_info,
executable,
)
result = pytester.runpytest("-v")
# Check if output from component is shown on error
result.stdout.fnmatch_lines([expected_outcome])
if expected_exitcode == 0:
result.stdout.no_fnmatch_line("*This script should fail*")
# Check if stack traces are hidden
result.stdout.no_fnmatch_line("*def wrapper*")
result.stdout.no_fnmatch_line("*def run_component*")
assert result.ret == expected_exitcode
4 changes: 1 addition & 3 deletions viashpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ def wrapper(*args, **kwargs):
f"Captured component output was:\n{e.stdout.decode('utf-8')}"
)
# Create a new CalledProcessError object. This removes verbosity from the original object
raise CalledProcessError(
e.returncode, e.cmd, output=None, stderr=None
) from None
raise e.with_traceback(None) from None

return wrapper

Expand Down

0 comments on commit 87abda5

Please sign in to comment.