Skip to content

Commit c21a890

Browse files
committed
🧪 Make stderr interceptable
Previously, `sys.stderr` was exposed as a module-scoped var, while `capsys` patches the `stderr` attribute on the `sys` module. So such patching did not influence the `stderr` variable in the `_cli` module as it remained linked to the non-patched object. With this change, the ability to inspect printing to stderr is recovered.
1 parent 828eb62 commit c21a890

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/pre_commit_terraform/_cli.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Outer CLI layer of the app interface."""
22

3-
from sys import stderr
3+
import sys
44
from typing import cast as cast_to
55

66
from ._cli_parsing import initialize_argument_parser
@@ -30,25 +30,25 @@ def invoke_cli_app(cli_args: list[str]) -> ReturnCodeType:
3030
try:
3131
return invoke_cli_app(parsed_cli_args)
3232
except PreCommitTerraformExit as exit_err:
33-
print(f'App exiting: {exit_err !s}', file=stderr)
33+
print(f'App exiting: {exit_err !s}', file=sys.stderr)
3434
raise
3535
except PreCommitTerraformRuntimeError as unhandled_exc:
3636
print(
3737
f'App execution took an unexpected turn: {unhandled_exc !s}. '
3838
'Exiting...',
39-
file=stderr,
39+
file=sys.stderr,
4040
)
4141
return ReturnCode.ERROR
4242
except PreCommitTerraformBaseError as unhandled_exc:
4343
print(
4444
f'A surprising exception happened: {unhandled_exc !s}. Exiting...',
45-
file=stderr,
45+
file=sys.stderr,
4646
)
4747
return ReturnCode.ERROR
4848
except KeyboardInterrupt as ctrl_c_exc:
4949
print(
5050
f'User-initiated interrupt: {ctrl_c_exc !s}. Exiting...',
51-
file=stderr,
51+
file=sys.stderr,
5252
)
5353
return ReturnCode.ERROR
5454

tests/pytest/_cli_test.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,29 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
'raised_error',
24+
('raised_error', 'expected_stderr'),
2525
(
2626
# pytest.param(PreCommitTerraformExit('sentinel'), 'App exiting: sentinel', id='app-exit'),
2727
pytest.param(
2828
PreCommitTerraformRuntimeError('sentinel'),
29+
'App execution took an unexpected turn: sentinel. Exiting...',
2930
id='app-runtime-exc',
3031
),
3132
pytest.param(
3233
PreCommitTerraformBaseError('sentinel'),
34+
'A surprising exception happened: sentinel. Exiting...',
3335
id='app-base-exc',
3436
),
3537
pytest.param(
3638
KeyboardInterrupt('sentinel'),
39+
'User-initiated interrupt: sentinel. Exiting...',
3740
id='ctrl-c',
3841
),
3942
),
4043
)
4144
def test_known_interrupts(
45+
capsys: pytest.CaptureFixture[str],
46+
expected_stderr: str,
4247
monkeypatch: pytest.MonkeyPatch,
4348
raised_error: BaseException,
4449
) -> None:
@@ -62,8 +67,14 @@ def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
6267

6368
assert ReturnCode.ERROR == invoke_cli_app(['sentinel'])
6469

70+
captured_outputs = capsys.readouterr()
71+
assert captured_outputs.err == f'{expected_stderr !s}\n'
6572

66-
def test_app_exit(monkeypatch: pytest.MonkeyPatch) -> None:
73+
74+
def test_app_exit(
75+
capsys: pytest.CaptureFixture[str],
76+
monkeypatch: pytest.MonkeyPatch,
77+
) -> None:
6778
"""Check that an exit exception is re-raised."""
6879
class CustomCmdStub:
6980
CLI_SUBCOMMAND_NAME = 'sentinel'
@@ -84,3 +95,6 @@ def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
8495

8596
with pytest.raises(PreCommitTerraformExit, match='^sentinel$'):
8697
invoke_cli_app(['sentinel'])
98+
99+
captured_outputs = capsys.readouterr()
100+
assert captured_outputs.err == 'App exiting: sentinel\n'

0 commit comments

Comments
 (0)