-
Notifications
You must be signed in to change notification settings - Fork 848
Add option to output digest to stdout #264
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
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 |
---|---|---|
@@ -1,41 +1,80 @@ | ||
"""Tests for the gitingest cli.""" | ||
"""Tests for the Gitingest CLI.""" | ||
|
||
import os | ||
from inspect import signature | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from click.testing import CliRunner | ||
import pytest | ||
from _pytest.monkeypatch import MonkeyPatch | ||
from click.testing import CliRunner, Result | ||
|
||
from gitingest.cli import main | ||
from gitingest.config import MAX_FILE_SIZE, OUTPUT_FILE_NAME | ||
|
||
|
||
def test_cli_with_default_options(): | ||
runner = CliRunner() | ||
result = runner.invoke(main, ["./"]) | ||
output_lines = result.output.strip().split("\n") | ||
assert f"Analysis complete! Output written to: {OUTPUT_FILE_NAME}" in output_lines | ||
assert os.path.exists(OUTPUT_FILE_NAME), f"Output file was not created at {OUTPUT_FILE_NAME}" | ||
|
||
os.remove(OUTPUT_FILE_NAME) | ||
|
||
|
||
def test_cli_with_options(): | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
main, | ||
[ | ||
"./", | ||
"--output", | ||
str(OUTPUT_FILE_NAME), | ||
"--max-size", | ||
str(MAX_FILE_SIZE), | ||
"--exclude-pattern", | ||
"tests/", | ||
"--include-pattern", | ||
"src/", | ||
], | ||
) | ||
output_lines = result.output.strip().split("\n") | ||
assert f"Analysis complete! Output written to: {OUTPUT_FILE_NAME}" in output_lines | ||
assert os.path.exists(OUTPUT_FILE_NAME), f"Output file was not created at {OUTPUT_FILE_NAME}" | ||
|
||
os.remove(OUTPUT_FILE_NAME) | ||
@pytest.mark.parametrize( | ||
"cli_args, expect_file", | ||
[ | ||
pytest.param(["./"], True, id="default-options"), | ||
pytest.param( | ||
[ | ||
"./", | ||
"--output", | ||
str(OUTPUT_FILE_NAME), | ||
"--max-size", | ||
str(MAX_FILE_SIZE), | ||
"--exclude-pattern", | ||
"tests/", | ||
"--include-pattern", | ||
"src/", | ||
], | ||
True, | ||
id="custom-options", | ||
), | ||
], | ||
) | ||
def test_cli_writes_file(tmp_path: Path, monkeypatch: MonkeyPatch, cli_args: List[str], expect_file: bool) -> None: | ||
"""Run the CLI and verify that the SARIF file is created (or not).""" | ||
# Work inside an isolated temp directory | ||
monkeypatch.chdir(tmp_path) | ||
|
||
result = _invoke_isolated_cli_runner(cli_args) | ||
|
||
assert result.exit_code == 0, result.stderr | ||
|
||
# Summary line should be on STDOUT | ||
stdout_lines = result.stdout.splitlines() | ||
assert f"Analysis complete! Output written to: {OUTPUT_FILE_NAME}" in stdout_lines | ||
|
||
# File side-effect | ||
sarif_file = tmp_path / OUTPUT_FILE_NAME | ||
assert sarif_file.exists() is expect_file, f"{OUTPUT_FILE_NAME} existence did not match expectation" | ||
|
||
filipchristiansen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_cli_with_stdout_output() -> None: | ||
"""Test CLI invocation with output directed to STDOUT.""" | ||
result = _invoke_isolated_cli_runner(["./", "--output", "-", "--exclude-pattern", "tests/"]) | ||
|
||
# ─── 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." | ||
|
||
filipchristiansen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# ─── 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 | ||
|
||
|
||
def _invoke_isolated_cli_runner(args: List[str]) -> Result: | ||
"""Return a CliRunner that keeps stderr apart on Click 8.0-8.1.""" | ||
kwargs = {} | ||
if "mix_stderr" in signature(CliRunner.__init__).parameters: | ||
kwargs["mix_stderr"] = False # Click 8.0–8.1 | ||
runner = CliRunner(**kwargs) | ||
return runner.invoke(main, args) |
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.