Skip to content

Commit

Permalink
Change: autohooks activate CLI will now write the settings too
Browse files Browse the repository at this point in the history
Make the installation and usage of autohooks much easier by not having
to edit the pyproject toml config file manually.

If there is no settings for autohooks or changes are forced autohooks
activate will write the settings to the pyproject.toml file.
  • Loading branch information
bjoernricks committed Aug 9, 2022
1 parent e28d990 commit 4cbe779
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
27 changes: 17 additions & 10 deletions autohooks/cli/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
load_config_from_pyproject_toml,
)
from autohooks.hooks import PreCommitHook
from autohooks.settings import Mode
from autohooks.settings import AutohooksSettings, Mode
from autohooks.terminal import Terminal


Expand All @@ -47,20 +47,27 @@ def install_hooks(term: Terminal, args: Namespace) -> None:
"the installed pre-commit hook."
)
else:
if not config.has_autohooks_config():
term.warning(
f"autohooks is not enabled in your {str(pyproject_toml)} "
"file. Run 'autohooks check' for more details."
)

if args.mode:
mode = Mode.from_string(args.mode)
else:
mode = config.get_mode()
mode = config.get_mode().get_effective_mode()

if not config.has_autohooks_config():
settings = AutohooksSettings(mode=mode)
config.settings = settings
settings.write(pyproject_toml)

term.ok(f"autohooks settings written to {pyproject_toml}.")
elif args.force:
settings = config.settings
settings.mode = mode
settings.write(pyproject_toml)

term.ok(f"autohooks settings written to {pyproject_toml}.")

pre_commit_hook.write(mode=mode)

term.ok(
f"autohooks pre-commit hook installed at {str(pre_commit_hook)}"
f" using {str(mode.get_effective_mode())} mode."
f"autohooks pre-commit hook installed at {pre_commit_hook}"
f" using {mode} mode."
)
32 changes: 22 additions & 10 deletions tests/cli/test_activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,39 @@ def test_install_hooks_force(self):

term = MagicMock()
args = Namespace(force=True, mode=None)

install_hooks(term, args)

term.warning.assert_not_called()
term.ok.assert_called_once_with(
f"autohooks pre-commit hook installed at {tmpdir}/"
".git/hooks/pre-commit using poetry mode."
term.ok.assert_has_calls(
(
call(f"autohooks settings written to {pyproject_toml}."),
call(
f"autohooks pre-commit hook installed at {pre_commit}"
" using poetry mode."
),
)
)

def test_install_no_config(self):
with tempgitdir() as tmpdir:
term = MagicMock()
args = Namespace(force=False, mode=None)

install_hooks(term, args)

term.warning.assert_called_once_with(
f"autohooks is not enabled in your {tmpdir}/pyproject.toml "
"file. Run 'autohooks check' for more details."
)
term.ok.assert_called_once_with(
f"autohooks pre-commit hook installed at {tmpdir}/"
".git/hooks/pre-commit using pythonpath mode."
term.warning.assert_not_called()
term.ok.assert_has_calls(
(
call(
"autohooks settings written to "
f"{tmpdir}/pyproject.toml."
),
call(
f"autohooks pre-commit hook installed at {tmpdir}/"
".git/hooks/pre-commit using pythonpath mode."
),
)
)

def test_install_hooks_with_mode(self):
Expand Down

0 comments on commit 4cbe779

Please sign in to comment.