Skip to content

Commit 7454d6c

Browse files
committed
Add relpath test for Windows error coverage
1 parent 6a11b80 commit 7454d6c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pre_commit/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,15 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
165165
toplevel = git.get_root()
166166
os.chdir(toplevel)
167167

168-
try:
169-
args.config = os.path.relpath(args.config)
170168
# https://github.com/pre-commit/pre-commit/issues/2530
171169
# os.relpath will fail with a ValueError if the two directories are on
172170
# two different drives on Windows and since the path is made relative
173-
# for display purposes only we can ignore the error
171+
# for display purposes only, we can ignore the error
172+
try:
173+
args.config = os.path.relpath(args.config)
174174
except ValueError:
175175
pass
176+
176177
if args.command in {'run', 'try-repo'}:
177178
args.files = [os.path.relpath(filename) for filename in args.files]
178179
if args.commit_msg_filename is not None:

tests/main_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def test_adjust_args_and_chdir_not_in_git_dir(in_tmpdir):
2727
main._adjust_args_and_chdir(_args())
2828

2929

30+
def test_adjust_args_and_chdir_relpath_to_different_drive():
31+
args = _args(command='run', files=['f1', 'f2'])
32+
with mock.patch.object(os.path, 'relpath', side_effect=ValueError):
33+
with pytest.raises(ValueError):
34+
main._adjust_args_and_chdir(args)
35+
36+
3037
def test_adjust_args_and_chdir_noop(in_git_dir):
3138
args = _args(command='run', files=['f1', 'f2'])
3239
main._adjust_args_and_chdir(args)

0 commit comments

Comments
 (0)