Skip to content

Add --output-file option to usethis show commands#1565

Merged
nathanjmcdougall merged 7 commits intomainfrom
copilot/add-output-file-option-usethis-show
Mar 27, 2026
Merged

Add --output-file option to usethis show commands#1565
nathanjmcdougall merged 7 commits intomainfrom
copilot/add-output-file-option-usethis-show

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 26, 2026

  • Add --output-file option to all usethis show subcommands (backend, name, sonarqube)
  • Modify core show functions in src/usethis/_core/show.py to accept output_file parameter and write to file instead of stdout
  • Add tests for the --output-file option on all show subcommands
  • Update CLI documentation (docs/cli/reference.md)
  • Run static checks and verify changes
  • Move output_file_opt to centralized _ui/options.py module
  • Trim excessive detail from --output-file docs

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits March 26, 2026 22:20
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/1258b6ab-bd9c-4a25-a42a-1f452a5349f3

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/1258b6ab-bd9c-4a25-a42a-1f452a5349f3

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/1258b6ab-bd9c-4a25-a42a-1f452a5349f3

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Add --output-file option to usethis show commands Add --output-file option to usethis show commands Mar 26, 2026
Copilot AI requested a review from nathanjmcdougall March 26, 2026 22:27
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 26, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/add-output-file-option-usethis-show (5ed7f37) with main (25840a9)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (07c4eaa) during the generation of this report, so 25840a9 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/5efe9934-159d-487c-8b3f-444c3d252dfc

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/cd21b7cc-e068-4f4d-b672-45567726b7e2

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI requested a review from nathanjmcdougall March 27, 2026 02:01
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review March 27, 2026 03:29
@nathanjmcdougall nathanjmcdougall merged commit 13f5208 into main Mar 27, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/add-output-file-option-usethis-show branch March 27, 2026 03:48
@nathanjmcdougall nathanjmcdougall linked an issue Mar 27, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an --output-file option to the usethis show command group so users can write show output directly to a file instead of printing to stdout.

Changes:

  • Adds a shared output_file_opt option and wires it into all usethis show subcommands.
  • Updates core show_* functions to optionally write output content to a provided path.
  • Adds CLI tests for --output-file and updates CLI reference docs.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/usethis/_ui/interface/test_show.py Adds new tests covering --output-file for show backend/name/sonarqube.
src/usethis/_ui/options.py Centralizes the --output-file Typer option for reuse.
src/usethis/_ui/interface/show.py Plumbs --output-file through the Typer interface into core show functions.
src/usethis/_core/show.py Implements shared output routing to stdout vs file.
docs/cli/reference.md Documents --output-file support (currently only under show sonarqube).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +30 to +34
def _output(content: str, *, output_file: Path | None = None) -> None:
if output_file is not None:
if not content.endswith("\n"):
content += "\n"
output_file.write_text(content, encoding="utf-8")
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_output() writes directly via Path.write_text(). If the target path is unwritable / parent dir missing / path is a directory, this will raise an OSError that won't be caught by the Typer interface (which only catches UsethisError), likely resulting in a traceback. Consider catching OSError here (or in the interface) and re-raising a UsethisError with a clear message that includes the output path.

Copilot uses AI. Check for mistakes.
Comment on lines +478 to +480
Supported options:

- `--output-file` to write the output to a file instead of stdout.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc update implies --output-file is only supported for usethis show sonarqube, but the PR adds it to all usethis show subcommands (backend, name, sonarqube). The reference should either document --output-file as a shared usethis show option or list it under each usethis show <subcommand> that supports it, to avoid misleading users.

Suggested change
Supported options:
- `--output-file` to write the output to a file instead of stdout.
Common `usethis show` options:
- `--output-file` to write the output of any `usethis show` subcommand to a file instead of stdout.

Copilot uses AI. Check for mistakes.
Comment on lines +223 to +242
def test_output_file_not_detected_as_existing(self, tmp_path: Path):
"""Using --output-file avoids the redirect problem.

When using shell redirect (`> file`), the file is created empty before
the command runs, which causes sonarqube to read that empty file.
With --output-file, the file is written after generation.
"""
# Arrange
(tmp_path / "pyproject.toml").write_text(
"""
[tool.usethis.sonarqube]
project-key = "fun"

[tool.coverage.xml.output]
"""
)
# Simulate what happens with shell redirect: an empty file pre-exists
output_file = tmp_path / "sonar-project.properties"
output_file.write_text("", encoding="utf-8")

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is described as simulating the shell redirect issue, but pre-creating an empty sonar-project.properties file makes get_sonar_project_properties() return that empty file by design. That means the test doesn't actually validate that --output-file avoids the redirect problem (i.e., writing after generation when the file did not exist at generation time), and the current assertion (content != "") can pass even if the command only writes a newline. Consider removing this test or rewriting it to cover the intended scenario (e.g., ensure the output file does not exist before invocation and assert the written file contains expected generated properties).

Copilot uses AI. Check for mistakes.
Comment on lines +41 to +49
runner = CliRunner()
with change_cwd(tmp_path):
result = runner.invoke_safe(
app, ["backend", "--output-file", str(output_file)]
)

# Assert
assert result.exit_code == 0, result.output
assert output_file.read_text(encoding="utf-8") == "uv\n"
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --output-file option is documented as writing output to a file instead of stdout, but this test only asserts the file contents. Adding an assertion that result.output is empty (and doing the same for the other test_output_file cases in this file) would better lock in the intended behavior and prevent accidental future regressions that write to both stdout and the file.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --output-file option to usethis show commands

3 participants