-
Notifications
You must be signed in to change notification settings - Fork 3
[Breaking Change] Add usethis hook command and --hook/--no-hook to usethis init
#1454
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
4 commits
Select commit
Hold shift + click to select a range
3a68cd8
Initial plan
Copilot ab8532a
Add `usethis hook` command and `--hook/--no-hook` to `usethis init`
Copilot 624d5f7
Merge branch 'main' into copilot/add-pre-commit-hook-option
nathanjmcdougall ebd433b
Update docs/cli/reference.md: add `usethis hook` section and rename -…
Copilot 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,5 @@ | ||
| from usethis._core.tool import use_pre_commit | ||
|
|
||
|
|
||
| def use_hook_framework(remove: bool = False, how: bool = False): | ||
| use_pre_commit(remove=remove, how=how) |
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,41 @@ | ||
| import typer | ||
|
|
||
| from usethis._config import usethis_config | ||
| from usethis._types.backend import BackendEnum | ||
| from usethis._ui.options import ( | ||
| backend_opt, | ||
| frozen_opt, | ||
| how_opt, | ||
| offline_opt, | ||
| quiet_opt, | ||
| remove_opt, | ||
| ) | ||
|
|
||
|
|
||
| def hook( | ||
| remove: bool = remove_opt, | ||
| how: bool = how_opt, | ||
| offline: bool = offline_opt, | ||
| quiet: bool = quiet_opt, | ||
| frozen: bool = frozen_opt, | ||
| backend: BackendEnum = backend_opt, | ||
| ) -> None: | ||
| """Add a recommended git hook framework to the project.""" | ||
| from usethis._config_file import files_manager | ||
| from usethis._console import err_print | ||
| from usethis._toolset.hook import use_hook_framework | ||
| from usethis.errors import UsethisError | ||
|
|
||
| assert isinstance(backend, BackendEnum) | ||
|
|
||
| with ( | ||
| usethis_config.set( | ||
| offline=offline, quiet=quiet, frozen=frozen, backend=backend | ||
| ), | ||
| files_manager(), | ||
| ): | ||
| try: | ||
| use_hook_framework(remove=remove, how=how) | ||
| except UsethisError as err: | ||
| err_print(err) | ||
| raise typer.Exit(code=1) from None |
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,57 @@ | ||
| from pathlib import Path | ||
|
|
||
| from usethis._config_file import files_manager | ||
| from usethis._deps import get_deps_from_group | ||
| from usethis._test import CliRunner, change_cwd | ||
| from usethis._types.deps import Dependency | ||
| from usethis._ui.app import app | ||
|
|
||
|
|
||
| class TestHook: | ||
| def test_dependencies_added(self, tmp_path: Path): | ||
| # Act | ||
| runner = CliRunner() | ||
| with change_cwd(tmp_path): | ||
| result = runner.invoke_safe(app, ["hook"]) | ||
|
|
||
| # Assert | ||
| assert result.exit_code == 0, result.output | ||
| with change_cwd(tmp_path), files_manager(): | ||
| assert Dependency(name="pre-commit") in get_deps_from_group("dev") | ||
|
|
||
| def test_how_option(self, tmp_path: Path): | ||
| # Act | ||
| runner = CliRunner() | ||
| with change_cwd(tmp_path): | ||
| result = runner.invoke_safe(app, ["hook", "--how"]) | ||
|
|
||
| # Assert | ||
| assert result.exit_code == 0, result.output | ||
| assert "pre-commit" in result.output | ||
|
|
||
| def test_add_then_remove(self, tmp_path: Path): | ||
| # Arrange | ||
| runner = CliRunner() | ||
|
|
||
| with change_cwd(tmp_path): | ||
| # Act: Add hook framework | ||
| result = runner.invoke_safe(app, ["hook"]) | ||
| assert result.exit_code == 0, result.output | ||
|
|
||
| # Act: Remove hook framework | ||
| result = runner.invoke_safe(app, ["hook", "--remove"]) | ||
|
|
||
| # Assert | ||
| assert result.exit_code == 0, result.output | ||
| with change_cwd(tmp_path), files_manager(): | ||
| assert Dependency(name="pre-commit") not in get_deps_from_group("dev") | ||
|
|
||
| def test_none_backend(self, tmp_path: Path): | ||
| # Act | ||
| runner = CliRunner() | ||
| with change_cwd(tmp_path): | ||
| result = runner.invoke_safe(app, ["hook", "--backend", "none"]) | ||
nathanjmcdougall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Assert | ||
| assert result.exit_code == 0, result.output | ||
| assert "pre-commit" in result.output | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
README links to
.../cli/reference#usethis-hook, but the in-repo CLI reference doc (docs/cli/reference.md) currently has nousethis hooksection/anchor and still documents--pre-commit/--no-pre-commitforusethis init. Until the reference is updated, this README link is likely to 404/mislead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot likewise, please update reference.md to ensure this hyperlink is valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
docs/cli/reference.mdin ebd433b — added a new## usethis hooksection (with anchor#usethis-hook) and renamed--pre-committo--hookunderusethis init.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in ebd433b —
reference.mdnow has the#usethis-hookanchor.