-
Notifications
You must be signed in to change notification settings - Fork 0
Stop double-logging external command invocations (#104) #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b0dca3f
Stop double-logging external command invocations
leynos 95a3496
Harden subprocess logging regression test assertions
leynos f5ab030
Log cmd-mox passthrough command invocations
leynos 3da1df2
Reduce passthrough test fixture argument count
leynos 51f17f7
Add descriptive failure messages to logging regression asserts
leynos f455987
Document single-invocation logging contract (#104)
leynos e483c41
Snapshot subprocess invocation log messages
leynos ce79449
Log subprocess environment only when overrides exist
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # serializer version: 1 | ||
| # name: test_command_logged_exactly_once | ||
| 'Running external command: echo hello' | ||
| # --- | ||
| # name: test_command_logged_exactly_once_with_cwd | ||
| 'Running external command: echo hello (cwd=<tmpdir>)' | ||
| # --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """Regression tests for subprocess runner invocation logging. | ||
|
|
||
| Issue #104: every external command used to be logged twice — once at INFO | ||
| by ``subprocess_runner`` and once at DEBUG by ``invoke_via_subprocess``. | ||
| These tests pin the single-log contract. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import typing as typ | ||
|
|
||
| from lading.runtime.subprocess_runner import subprocess_runner | ||
|
|
||
| if typ.TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from syrupy.assertion import SnapshotAssertion | ||
|
|
||
| LogCaptureFixture = pytest.LogCaptureFixture | ||
| else: # pragma: no cover - typing helpers | ||
| Path = typ.Any | ||
| LogCaptureFixture = typ.Any | ||
| SnapshotAssertion = typ.Any | ||
|
|
||
| _RUNNER_LOGGER = "lading.runtime.subprocess_runner" | ||
|
|
||
|
|
||
| def _invocation_records( | ||
| caplog: LogCaptureFixture, | ||
| ) -> list[logging.LogRecord]: | ||
| """Return records that render the external command line.""" | ||
| return [ | ||
| record | ||
| for record in caplog.records | ||
| if "Running external command" in record.getMessage() | ||
|
leynos marked this conversation as resolved.
|
||
| ] | ||
|
|
||
|
|
||
| def _assert_no_spawn_record(caplog: LogCaptureFixture) -> None: | ||
| """Assert the removed DEBUG spawn log is absent (regression for #104).""" | ||
| assert all( | ||
| "Spawning subprocess:" not in record.getMessage() for record in caplog.records | ||
| ), "Unexpected 'Spawning subprocess:' log emitted" | ||
|
|
||
|
|
||
| def test_command_logged_exactly_once( | ||
| caplog: LogCaptureFixture, | ||
| snapshot: SnapshotAssertion, | ||
| ) -> None: | ||
| """A command produces a single invocation log record at INFO.""" | ||
| caplog.set_level(logging.DEBUG, logger=_RUNNER_LOGGER) | ||
|
|
||
| exit_code, stdout, stderr = subprocess_runner(("echo", "hello"), echo_stdout=False) | ||
|
|
||
| assert exit_code == 0, "expected exit code 0" | ||
| assert stdout.strip() == "hello", 'expected stdout to contain "hello"' | ||
| assert stderr == "", "expected empty stderr" | ||
| records = _invocation_records(caplog) | ||
| assert len(records) == 1, "expected exactly one invocation record" | ||
| assert records[0].levelno == logging.INFO, "expected invocation at INFO level" | ||
| assert records[0].getMessage() == snapshot(), "expected message to match snapshot" | ||
| _assert_no_spawn_record(caplog) | ||
|
|
||
|
|
||
| def test_command_logged_exactly_once_with_cwd( | ||
| caplog: LogCaptureFixture, | ||
| tmp_path: Path, | ||
| snapshot: SnapshotAssertion, | ||
| ) -> None: | ||
| """The single invocation record includes the working directory.""" | ||
| caplog.set_level(logging.DEBUG, logger=_RUNNER_LOGGER) | ||
|
|
||
| exit_code, _, _ = subprocess_runner( | ||
| ("echo", "hello"), cwd=tmp_path, echo_stdout=False | ||
| ) | ||
|
|
||
| assert exit_code == 0, "expected exit code 0" | ||
| records = _invocation_records(caplog) | ||
| assert len(records) == 1, "expected exactly one invocation record" | ||
| assert records[0].levelno == logging.INFO, "expected invocation at INFO level" | ||
| redacted = records[0].getMessage().replace(str(tmp_path), "<tmpdir>") | ||
| assert redacted == snapshot(), "expected redacted cwd message to match snapshot" | ||
| _assert_no_spawn_record(caplog) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.