summaryrefslogtreecommitdiffstats
path: root/tests/staged_files_only_test.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-27 10:32:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-27 10:32:22 +0000
commitd7784aa0c412c80cfcb33a29fd1b2ea69dbe8ab8 (patch)
treef7d340787b36afcb3b78f2a0875c2c9c8419cdec /tests/staged_files_only_test.py
parentReleasing debian version 3.0.4-1. (diff)
downloadpre-commit-d7784aa0c412c80cfcb33a29fd1b2ea69dbe8ab8.tar.xz
pre-commit-d7784aa0c412c80cfcb33a29fd1b2ea69dbe8ab8.zip
Merging upstream version 3.1.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/staged_files_only_test.py')
-rw-r--r--tests/staged_files_only_test.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py
index a91f315..58dbe5a 100644
--- a/tests/staged_files_only_test.py
+++ b/tests/staged_files_only_test.py
@@ -1,12 +1,15 @@
from __future__ import annotations
+import contextlib
import itertools
import os.path
import shutil
import pytest
+import re_assert
from pre_commit import git
+from pre_commit.errors import FatalError
from pre_commit.staged_files_only import staged_files_only
from pre_commit.util import cmd_output
from testing.auto_namedtuple import auto_namedtuple
@@ -14,6 +17,7 @@ from testing.fixtures import git_dir
from testing.util import cwd
from testing.util import get_resource_path
from testing.util import git_commit
+from testing.util import xfailif_windows
FOO_CONTENTS = '\n'.join(('1', '2', '3', '4', '5', '6', '7', '8', ''))
@@ -382,3 +386,49 @@ def test_intent_to_add(in_git_dir, patch_dir):
with staged_files_only(patch_dir):
assert_no_diff()
assert git.intent_to_add_files() == ['foo']
+
+
+@contextlib.contextmanager
+def _unreadable(f):
+ orig = os.stat(f).st_mode
+ os.chmod(f, 0o000)
+ try:
+ yield
+ finally:
+ os.chmod(f, orig)
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_failed_diff_does_not_discard_changes(in_git_dir, patch_dir):
+ # stage 3 files
+ for i in range(3):
+ with open(str(i), 'w') as f:
+ f.write(str(i))
+ cmd_output('git', 'add', '0', '1', '2')
+
+ # modify all of their contents
+ for i in range(3):
+ with open(str(i), 'w') as f:
+ f.write('new contents')
+
+ with _unreadable('1'):
+ with pytest.raises(FatalError) as excinfo:
+ with staged_files_only(patch_dir):
+ raise AssertionError('should have errored on enter')
+
+ # the diff command failed to produce a diff of `1`
+ msg, = excinfo.value.args
+ re_assert.Matches(
+ r'^pre-commit failed to diff -- perhaps due to permissions\?\n\n'
+ r'command: .*\n'
+ r'return code: 128\n'
+ r'stdout: \(none\)\n'
+ r'stderr:\n'
+ r' error: open\("1"\): Permission denied\n'
+ r' fatal: cannot hash 1$',
+ ).assert_matches(msg)
+
+ # even though it errored, the unstaged changes should still be present
+ for i in range(3):
+ with open(str(i)) as f:
+ assert f.read() == 'new contents'