Skip to content

Add relpath test for Windows error coverage #2571

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion pre_commit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
toplevel = git.get_root()
os.chdir(toplevel)

args.config = os.path.relpath(args.config)
# https://github.com/pre-commit/pre-commit/issues/2530
# `os.relpath` raises a ValueError on Windows when the paths given are on
# separate drives.
try:
args.config = os.path.relpath(args.config)
except ValueError:
pass

if args.command in {'run', 'try-repo'}:
args.files = [os.path.relpath(filename) for filename in args.files]
if args.commit_msg_filename is not None:
Expand Down
7 changes: 7 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def test_adjust_args_and_chdir_not_in_git_dir(in_tmpdir):
main._adjust_args_and_chdir(_args())


def test_adjust_args_and_chdir_relpath_to_different_drive():
args = _args(command='run', files=['f1', 'f2'])
with mock.patch.object(os.path, 'relpath', side_effect=ValueError):
with pytest.raises(ValueError):
main._adjust_args_and_chdir(args)


def test_adjust_args_and_chdir_noop(in_git_dir):
args = _args(command='run', files=['f1', 'f2'])
main._adjust_args_and_chdir(args)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy