From f664a0b85dd6be4bb8fd16d7283fd1be0f81c8c8 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Thu, 21 Dec 2023 00:58:33 -0500 Subject: [PATCH 1/2] Add xfail marks to hook tests for WinBashStatus.Absent Precise xfail marks were added to commit hook tests in #1745, but in a few tests I didn't get it right for WinBashStatus.Absent. That is, on a Windows system that has no bash.exe at all: - More of the tests are unable to pass than have xfail marks. - One of the tests, test_commit_msg_hook_success, which does have an xfail mark for that, wrongly combines it with the xfail mark for WinBashStatus.Wsl. That test is the only one where the WSL bash.exe, even when a working WSL distribution is installed, is unable to pass. But using a single mark there is wrong, in part because the "reason" is not correct for both, but even more so because the exceptions they raise are different: AssertionError is raised when the WSL bash.exe is used in that test, but when bash.exe is altogether absent, HookExecutionError is raised. This fixes that by adding xfail marks for WinBashStatus.Absent where missing, and splitting test_commit_msg_hook_success's xfail mark that unsuccessfully tried to cover two conditions into two separate marks, each of which gives a correct reason and exception. This commit also rewords the xfail reason given for WslNoDistro, which was somewhat unclear and potentially ambiguous, to make it clearer. --- test/test_index.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/test_index.py b/test/test_index.py index 2f97f0af8..3f0990ecc 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -991,9 +991,14 @@ 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 for WSL even with no WSL distro installed", + reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", raises=HookExecutionError, ) @with_rw_repo("HEAD", bare=True) @@ -1004,7 +1009,7 @@ def test_pre_commit_hook_success(self, rw_repo): @pytest.mark.xfail( type(_win_bash_status) is WinBashStatus.WslNoDistro, - reason="Currently uses the bash.exe for WSL even with no WSL distro installed", + reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", raises=AssertionError, ) @with_rw_repo("HEAD", bare=True) @@ -1030,13 +1035,18 @@ def test_pre_commit_hook_fail(self, rw_repo): raise AssertionError("Should have caught a HookExecutionError") @pytest.mark.xfail( - type(_win_bash_status) in {WinBashStatus.Absent, WinBashStatus.Wsl}, + 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.Wsl, reason="Specifically seems to fail on WSL bash (in spite of #1399)", raises=AssertionError, ) @pytest.mark.xfail( type(_win_bash_status) is WinBashStatus.WslNoDistro, - reason="Currently uses the bash.exe for WSL even with no WSL distro installed", + reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", raises=HookExecutionError, ) @with_rw_repo("HEAD", bare=True) @@ -1054,7 +1064,7 @@ def test_commit_msg_hook_success(self, rw_repo): @pytest.mark.xfail( type(_win_bash_status) is WinBashStatus.WslNoDistro, - reason="Currently uses the bash.exe for WSL even with no WSL distro installed", + reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", raises=AssertionError, ) @with_rw_repo("HEAD", bare=True) From e1486474a2381763e11932b3bb15e08f6e9faf53 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Thu, 21 Dec 2023 01:31:54 -0500 Subject: [PATCH 2/2] 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. --- test/test_index.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/test_index.py b/test/test_index.py index 3f0990ecc..afceda131 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -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 @@ -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.",