Fix: Add proper test isolation for CLI stdout output test (#298)

This commit is contained in:
Romain Courtois 2025-06-22 18:42:02 +02:00 committed by GitHub
parent 3c5384322c
commit db7ee0cc07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -54,21 +54,32 @@ def test_cli_writes_file(tmp_path: Path, monkeypatch: MonkeyPatch, cli_args: Lis
def test_cli_with_stdout_output() -> None:
"""Test CLI invocation with output directed to STDOUT."""
result = _invoke_isolated_cli_runner(["./", "--output", "-", "--exclude-pattern", "tests/"])
# Clean up any existing digest.txt file before test
if os.path.exists(OUTPUT_FILE_NAME):
os.remove(OUTPUT_FILE_NAME)
# ─── core expectations (stdout) ────────────────────────────────────-
assert result.exit_code == 0, f"CLI exited with code {result.exit_code}, stderr: {result.stderr}"
assert "---" in result.stdout, "Expected file separator '---' not found in STDOUT"
assert "src/gitingest/cli.py" in result.stdout, "Expected content (e.g., src/gitingest/cli.py) not found in STDOUT"
assert not os.path.exists(OUTPUT_FILE_NAME), f"Output file {OUTPUT_FILE_NAME} was unexpectedly created."
try:
result = _invoke_isolated_cli_runner(["./", "--output", "-", "--exclude-pattern", "tests/"])
# ─── the summary must *not* pollute STDOUT, must appear on STDERR ───
summary = "Analysis complete! Output sent to stdout."
stdout_lines = result.stdout.splitlines()
stderr_lines = result.stderr.splitlines()
assert summary not in stdout_lines, "Unexpected summary message found in STDOUT"
assert summary in stderr_lines, "Expected summary message not found in STDERR"
assert f"Output written to: {OUTPUT_FILE_NAME}" not in stderr_lines
# ─── core expectations (stdout) ────────────────────────────────────-
assert result.exit_code == 0, f"CLI exited with code {result.exit_code}, stderr: {result.stderr}"
assert "---" in result.stdout, "Expected file separator '---' not found in STDOUT"
assert (
"src/gitingest/cli.py" in result.stdout
), "Expected content (e.g., src/gitingest/cli.py) not found in STDOUT"
assert not os.path.exists(OUTPUT_FILE_NAME), f"Output file {OUTPUT_FILE_NAME} was unexpectedly created."
# ─── the summary must *not* pollute STDOUT, must appear on STDERR ───
summary = "Analysis complete! Output sent to stdout."
stdout_lines = result.stdout.splitlines()
stderr_lines = result.stderr.splitlines()
assert summary not in stdout_lines, "Unexpected summary message found in STDOUT"
assert summary in stderr_lines, "Expected summary message not found in STDERR"
assert f"Output written to: {OUTPUT_FILE_NAME}" not in stderr_lines
finally:
# Clean up any digest.txt file that might have been created during test
if os.path.exists(OUTPUT_FILE_NAME):
os.remove(OUTPUT_FILE_NAME)
def _invoke_isolated_cli_runner(args: List[str]) -> Result: