@@ -1086,14 +1086,43 @@ class Mocked:
10861086 path = os .path .join (repo_root , f"directory2{ os .sep } " )
10871087 index = IndexFile (repo )
10881088
1089- expected_path = f "directory2{ os . sep } "
1089+ expected_path = "directory2/ "
10901090 actual_path = index ._to_relative_path (path )
10911091 self .assertEqual (expected_path , actual_path )
10921092
1093- with mock .patch ("git.index.base.os.path" ) as ospath_mock :
1094- ospath_mock .relpath .return_value = f"directory2{ os .sep } "
1095- actual_path = index ._to_relative_path (path )
1096- self .assertEqual (expected_path , actual_path )
1093+ @pytest .mark .skipif (sys .platform != "win32" , reason = "Specifically for Windows." )
1094+ @with_rw_directory
1095+ def test__to_relative_path_windows_path_kinds (self , rw_dir ):
1096+ repo_root = osp .join (rw_dir , "repo" )
1097+ os .makedirs (osp .join (repo_root , "nested" ))
1098+
1099+ class Mocked :
1100+ bare = False
1101+ git_dir = osp .join (repo_root , ".git" )
1102+ working_tree_dir = repo_root
1103+
1104+ index = IndexFile (Mocked ())
1105+ inside_path = osp .join (repo_root , "nested" , "file" )
1106+ _drive , rooted_inside_path = osp .splitdrive (inside_path )
1107+
1108+ self .assertEqual (index ._to_relative_path (inside_path ), "nested/file" )
1109+ self .assertEqual (index ._to_relative_path (rooted_inside_path ), "nested/file" )
1110+ self .assertEqual (index ._to_relative_path (rooted_inside_path .replace ("\\ " , "/" )), "nested/file" )
1111+ self .assertEqual (index ._to_relative_path (PathLikeMock (inside_path )), "nested/file" )
1112+ self .assertEqual (index ._to_relative_path (inside_path .upper ()), "NESTED/FILE" )
1113+ self .assertRaises (ValueError , index ._to_relative_path , osp .join (repo_root + "-other" , "file" ))
1114+ self .assertRaises (ValueError , index ._to_relative_path , osp .join (".." , "outside" ))
1115+ self .assertRaises (ValueError , index ._to_relative_path , f"{ osp .splitdrive (repo_root )[0 ]} relative" )
1116+ self .assertRaises (ValueError , index ._to_relative_path , R"Z:\outside" )
1117+ self .assertRaises (ValueError , index ._to_relative_path , R"\\server\share\outside" )
1118+ self .assertRaises (ValueError , index ._to_relative_path , R"\\?\C:\outside" )
1119+
1120+ Mocked .bare = True
1121+ bare_index = IndexFile (Mocked ())
1122+ self .assertRaises (InvalidGitRepositoryError , bare_index ._to_relative_path , rooted_inside_path )
1123+ self .assertRaises (
1124+ InvalidGitRepositoryError , bare_index ._to_relative_path , f"{ osp .splitdrive (repo_root )[0 ]} relative"
1125+ )
10971126
10981127 @pytest .mark .xfail (
10991128 type (_win_bash_status ) is WinBashStatus .Absent ,
0 commit comments