def test_should_render_mode_poetry(self): path = FakeTemplatePath("$SHEBANG") template = PreCommitTemplate(path) self.assertEqual( template.render(mode=Mode.POETRY), "/usr/bin/env -S poetry run python3", )
def write(self, *, mode: Mode): template = PreCommitTemplate() pre_commit_hook = template.render(mode=mode) self.pre_commit_hook_path.write_text(pre_commit_hook) self.pre_commit_hook_path.chmod(0o775) self._pre_commit_hook = None
def test_modified_pre_commit_template(self): template = PreCommitTemplate() rendered = template.render(mode=Mode.PIPENV) lines = rendered.split('\n') lines[1] = "" path = FakeReadPath("\n".join(lines)) pre_commit_hook = PreCommitHook(path) self.assertFalse( pre_commit_hook.is_current_autohooks_pre_commit_hook())
def test_should_render_mode_poetry_multiline(self): path = FakeTemplatePath("$SHEBANG") template = PreCommitTemplate(path) self.assertEqual( template.render(mode=Mode.POETRY_MULTILINE), ("/bin/sh\n" "\"true\" ''':'\n" "poetry run python3 \"$0\" \"$@\"\n" "exit \"$?\"\n" "'''"), )
def test_read_version(self): template = PreCommitTemplate() with tempfile.TemporaryDirectory() as tempdir: tmp_hook_path = Path(tempdir) / 'pre-commit-test' # Find version using all shebang modes for mode in [m for m in Mode if m.value > 0]: with open(str(tmp_hook_path), 'w', encoding='utf-8') as tmpfile: tmpfile.write(template.render(mode=mode)) pre_commit_hook = PreCommitHook(tmp_hook_path) self.assertEqual(TEMPLATE_VERSION, pre_commit_hook.read_version())
def test_pre_commit_template(self): template = PreCommitTemplate() path = FakeReadPath(template.render(mode=Mode.PIPENV)) pre_commit_hook = PreCommitHook(path) self.assertTrue(pre_commit_hook.is_autohooks_pre_commit_hook())
def test_should_render_mode_unknown(self): path = FakeTemplatePath("$SHEBANG") template = PreCommitTemplate(path) self.assertEqual(template.render(mode=Mode.UNDEFINED), "/usr/bin/env python3")
def test_should_use_default_template(self): template = PreCommitTemplate() self.assertEqual(template.render(mode=Mode.PYTHONPATH), DEFAULT_TEMPLATE)