Add --output-file option to usethis show commands#1565
Add --output-file option to usethis show commands#1565nathanjmcdougall merged 7 commits intomainfrom
--output-file option to usethis show commands#1565Conversation
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>
--output-file option to usethis show commands--output-file option to usethis show commands
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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>
There was a problem hiding this comment.
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_optoption and wires it into allusethis showsubcommands. - Updates core
show_*functions to optionally write output content to a provided path. - Adds CLI tests for
--output-fileand 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.
| 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") |
There was a problem hiding this comment.
_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.
| Supported options: | ||
|
|
||
| - `--output-file` to write the output to a file instead of stdout. |
There was a problem hiding this comment.
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.
| 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. |
| 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") | ||
|
|
There was a problem hiding this comment.
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).
| 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" |
There was a problem hiding this comment.
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.
--output-fileoption to allusethis showsubcommands (backend,name,sonarqube)src/usethis/_core/show.pyto acceptoutput_fileparameter and write to file instead of stdout--output-fileoption on all show subcommandsdocs/cli/reference.md)output_file_optto centralized_ui/options.pymodule--output-filedocs✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.