示例#1
0
def install_hooks(term: Terminal, args: Namespace) -> None:
    pre_commit_hook = PreCommitHook()
    pyproject_toml = get_pyproject_toml_path()
    config = load_config_from_pyproject_toml(pyproject_toml)

    if pre_commit_hook.exists() and not args.force:
        term.ok('autohooks pre-commit hook is already installed at {}.'.format(
            str(pre_commit_hook)))
        with term.indent():
            term.print()
            term.info(
                "Run 'autohooks activate --force' to override the current "
                "installed pre-commit hook.")
            term.info(
                "Run 'autohooks check' to validate the current status of "
                "the installed pre-commit hook.")
    else:
        if not config.is_autohooks_enabled():
            term.warning('autohooks is not enabled in your {} file. '
                         'Run \'autohooks check\' for more '
                         'details.'.format(str(pyproject_toml)))

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

        pre_commit_hook.write(mode=mode)

        term.ok(
            'autohooks pre-commit hook installed at {} using {} mode.'.format(
                str(pre_commit_hook), str(mode.get_effective_mode())))
示例#2
0
    def test_install(self):
        pre_commit_hook = PreCommitHook()

        self.assertFalse(pre_commit_hook.exists())

        pre_commit_hook.write(mode=Mode.PIPENV)

        self.assertTrue(pre_commit_hook.exists())
示例#3
0
 def install_git_hook(self) -> None:
     try:
         pre_commit_hook = PreCommitHook()
         if not pre_commit_hook.exists():
             config = load_config_from_pyproject_toml()
             pre_commit_hook.write(mode=config.get_mode())
     except Exception:  # pylint: disable=broad-except
         pass
示例#4
0
    def test_pythonpath_mode(self):
        write_path = Mock()
        pre_commit_hook = PreCommitHook(write_path)
        pre_commit_hook.write(mode=Mode.PYTHONPATH)

        write_path.chmod.assert_called_with(0o775)
        self.assertTrue(write_path.write_text.called)

        args, _kwargs = write_path.write_text.call_args
        text = args[0]
        self.assertRegex(text, '^#!{} *'.format(PYTHON3_SHEBANG))
示例#5
0
    def test_poetry_mode(self):
        write_path = Mock()
        pre_commit_hook = PreCommitHook(write_path)
        pre_commit_hook.write(mode=Mode.POETRY)

        write_path.chmod.assert_called_with(0o775)
        self.assertTrue(write_path.write_text.called)

        args, _kwargs = write_path.write_text.call_args
        text = args[0]
        self.assertRegex(text, f'^#!{POETRY_SHEBANG} *')