Skip to content

Improve hooks tests #1777

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 2 commits into from
Dec 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add a direct test of run_commit_hook
This has three benefits:

- run_commit_hook is public, being listed in git.index.fun.__all__,
  so it makes sense for it to have its own test.

- When investigating (future, or current xfail-covered) failure of
  functions that use run_commit_hook, it will be useful to compare
  the results of other tests that already do exist to that of a
  direct test of run_commit_hook.

- When changing which bash.exe run_commit_hook selects to use on
  Windows (including to ameliorate the limitation covered by the
  WinBashStatus.WslNoDistro xfail marks, or to attempt other
  possible changes suggested in #1745), or even just to investigate
  the possibility of doing so, it will make sense to add tests like
  this test but for more specific conditions or edge cases. Having
  this typical-case test to compare to should be helpful both for
  writing such tests and for efficiently verifying that the
  conditions they test are really the triggers for any failures.
  • Loading branch information
EliahKagan committed Dec 21, 2023
commit e1486474a2381763e11932b3bb15e08f6e9faf53
20 changes: 19 additions & 1 deletion test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
InvalidGitRepositoryError,
UnmergedEntriesError,
)
from git.index.fun import hook_path
from git.index.fun import hook_path, run_commit_hook
from git.index.typ import BaseIndexEntry, IndexEntry
from git.objects import Blob
from test.lib import TestBase, fixture, fixture_path, with_rw_directory, with_rw_repo
Expand Down Expand Up @@ -991,6 +991,24 @@ class Mocked:
rel = index._to_relative_path(path)
self.assertEqual(rel, os.path.relpath(path, root))

@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.Absent,
reason="Can't run a hook on Windows without bash.exe.",
rasies=HookExecutionError,
)
@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.WslNoDistro,
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed",
raises=HookExecutionError,
)
@with_rw_repo("HEAD", bare=True)
def test_run_commit_hook(self, rw_repo):
index = rw_repo.index
_make_hook(index.repo.git_dir, "fake-hook", "echo 'ran fake hook' >output.txt")
run_commit_hook("fake-hook", index)
output = Path(rw_repo.git_dir, "output.txt").read_text(encoding="utf-8")
self.assertEqual(output, "ran fake hook\n")

@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.Absent,
reason="Can't run a hook on Windows without bash.exe.",
Expand Down