From 6a11b801efbe061cbf9223b5687593503a82762d Mon Sep 17 00:00:00 2001 From: SitiSchu Date: Thu, 29 Sep 2022 00:36:01 +0200 Subject: [PATCH 1/2] Add error handling for relpath ValueError, fixes #2530 --- pre_commit/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index 3915993ff..4ecee41b3 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -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) + try: + args.config = os.path.relpath(args.config) + # https://github.com/pre-commit/pre-commit/issues/2530 + # os.relpath will fail with a ValueError if the two directories are on + # two different drives on Windows and since the path is made relative + # for display purposes only we can ignore the error + 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: From 3a3fe94760488e3e5d9f2f873194fa54c613eaf0 Mon Sep 17 00:00:00 2001 From: marsha <46257533+m-rsha@users.noreply.github.com> Date: Sun, 30 Oct 2022 18:23:55 -0500 Subject: [PATCH 2/2] Add `relpath` test for Windows error coverage --- pre_commit/main.py | 8 ++++---- tests/main_test.py | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index 4ecee41b3..70f3d43a1 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -165,14 +165,14 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None: toplevel = git.get_root() os.chdir(toplevel) + # 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) - # https://github.com/pre-commit/pre-commit/issues/2530 - # os.relpath will fail with a ValueError if the two directories are on - # two different drives on Windows and since the path is made relative - # for display purposes only we can ignore the error 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: diff --git a/tests/main_test.py b/tests/main_test.py index 511592622..4daa06908 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -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) 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