mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Add relpath test for Windows error coverage
This commit is contained in:
parent
6a11b801ef
commit
3a3fe94760
2 changed files with 11 additions and 4 deletions
|
|
@ -165,14 +165,14 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
||||||
toplevel = git.get_root()
|
toplevel = git.get_root()
|
||||||
os.chdir(toplevel)
|
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:
|
try:
|
||||||
args.config = os.path.relpath(args.config)
|
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:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if args.command in {'run', 'try-repo'}:
|
if args.command in {'run', 'try-repo'}:
|
||||||
args.files = [os.path.relpath(filename) for filename in args.files]
|
args.files = [os.path.relpath(filename) for filename in args.files]
|
||||||
if args.commit_msg_filename is not None:
|
if args.commit_msg_filename is not None:
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,13 @@ def test_adjust_args_and_chdir_not_in_git_dir(in_tmpdir):
|
||||||
main._adjust_args_and_chdir(_args())
|
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):
|
def test_adjust_args_and_chdir_noop(in_git_dir):
|
||||||
args = _args(command='run', files=['f1', 'f2'])
|
args = _args(command='run', files=['f1', 'f2'])
|
||||||
main._adjust_args_and_chdir(args)
|
main._adjust_args_and_chdir(args)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue