@@ -103,6 +103,157 @@ def test_convert_mixed_override_path_sep(self):
103103 self .assertEqual (converted_path , expected_path )
104104
105105
106+ class TestToPosixPath (TestBase ):
107+
108+ @platform_dependent (["windows" ])
109+ def test_normal_windows_paths (self ):
110+ self .assertEqual (cygpath .to_posix_path (
111+ "C:\\ Users\\ John\\ Documents" ), "/c/Users/John/Documents"
112+ )
113+ self .assertEqual (
114+ cygpath .to_posix_path ("D:\\ Projects\\ Python" ), "/d/Projects/Python"
115+ )
116+
117+ @platform_dependent (["windows" ])
118+ def test_windows_paths_with_spaces (self ):
119+ self .assertEqual (cygpath .to_posix_path (
120+ "C:\\ Program Files\\ Python" ), "/c/Program Files/Python"
121+ )
122+ self .assertEqual (cygpath .to_posix_path (
123+ "D:\\ My Documents\\ Photos" ), "/d/My Documents/Photos"
124+ )
125+
126+ @platform_dependent (["windows" ])
127+ def test_windows_paths_with_special_characters (self ):
128+ self .assertEqual (cygpath .to_posix_path (
129+ "C:\\ Users\\ John\\ #Projects" ), "/c/Users/John/#Projects"
130+ )
131+ self .assertEqual (cygpath .to_posix_path (
132+ "D:\\ Projects\\ Python@Home" ), "/d/Projects/Python@Home"
133+ )
134+
135+ @platform_dependent (["windows" ])
136+ def test_windows_paths_with_mixed_slashes (self ):
137+ self .assertEqual (cygpath .to_posix_path (
138+ "C:\\ Users/John/Documents" ), "/c/Users/John/Documents"
139+ )
140+ self .assertEqual (
141+ cygpath .to_posix_path ("D:/Projects\\ Python" ), "/d/Projects/Python"
142+ )
143+
144+ @platform_dependent (["windows" ])
145+ def test_windows_paths_with_lowercase_drive_letters (self ):
146+ self .assertEqual (cygpath .to_posix_path (
147+ "c:\\ Users\\ John\\ Documents" ), "/c/Users/John/Documents"
148+ )
149+ self .assertEqual (
150+ cygpath .to_posix_path ("d:\\ Projects\\ Python" ), "/d/Projects/Python"
151+ )
152+
153+ @platform_dependent (["windows" ])
154+ def test_already_posix_style_paths (self ):
155+ self .assertEqual (cygpath .to_posix_path (
156+ "/c/Users/John/Documents" ), "/c/Users/John/Documents"
157+ )
158+ self .assertEqual (
159+ cygpath .to_posix_path ("/d/projects/python" ), "/d/projects/python" )
160+ self .assertRaisesRegexp (
161+ ValueError ,
162+ "Cannot convert path to posix path: '.*' "
163+ "Please ensure that the path is absolute" ,
164+ cygpath .to_posix_path ,
165+ "/home/john/documents"
166+ )
167+ self .assertRaisesRegexp (
168+ ValueError ,
169+ "Cannot convert path to posix path: '.*' "
170+ "Please ensure that the path is absolute" ,
171+ cygpath .to_posix_path ,
172+ "/projects/python"
173+ )
174+
175+ @platform_dependent (["windows" ])
176+ def test_relative_paths (self ):
177+ self .assertRaisesRegexp (
178+ ValueError ,
179+ "Cannot convert path to posix path: '.*' "
180+ "Please ensure that the path is absolute" ,
181+ cygpath .to_posix_path ,
182+ "jane/documents"
183+ )
184+
185+ self .assertRaisesRegexp (
186+ ValueError ,
187+ "Cannot convert path to posix path: '.*' "
188+ "Please ensure that the path is absolute" ,
189+ cygpath .to_posix_path ,
190+ "projects/python/file.py"
191+ )
192+
193+ @platform_dependent (["windows" ])
194+ def test_windows_unc_paths (self ):
195+ self .assertEqual (cygpath .to_posix_path (
196+ "\\ \\ Server\\ Share\\ folder" ), "//Server/Share/folder"
197+ )
198+ self .assertEqual (cygpath .to_posix_path (
199+ "\\ \\ server\\ share\\ folder\\ file.txt" ), "//server/share/folder/file.txt"
200+ )
201+
202+ @platform_dependent (["windows" ])
203+ def test_windows_long_paths (self ):
204+ self .assertEqual (cygpath .to_posix_path (
205+ "\\ \\ ?\\ C:\\ Users\\ Jane\\ Documents" ), "/c/Users/Jane/Documents"
206+ )
207+ self .assertEqual (cygpath .to_posix_path (
208+ "\\ \\ ?\\ d:\\ projects\\ python" ), "/d/projects/python"
209+ )
210+
211+ @platform_dependent (["windows" ])
212+ def test_windows_malformed_paths (self ):
213+ self .assertEqual (cygpath .to_posix_path (
214+ "C:\\ Users/Jane/\\ Documents" ), "/c/Users/Jane/Documents"
215+ )
216+ self .assertEqual (
217+ cygpath .to_posix_path ("D:/Projects\\ /Python" ), "/d/Projects/Python"
218+ )
219+ self .assertEqual (cygpath .to_posix_path (
220+ "C:/Users\\ Jane/Documents" ), "/c/Users/Jane/Documents"
221+ )
222+ self .assertEqual (
223+ cygpath .to_posix_path ("D:\\ projects/python" ), "/d/projects/python"
224+ )
225+ self .assertRaisesRegexp (
226+ ValueError ,
227+ "Cannot convert path to posix path: '.*' "
228+ "This is most likely due to a malformed path" ,
229+ cygpath .to_posix_path ,
230+ "D:\\ ..\\ Projects"
231+ )
232+ self .assertRaisesRegexp (
233+ ValueError ,
234+ "Cannot convert path to posix path: '.*' "
235+ "This is most likely due to a malformed path" ,
236+ cygpath .to_posix_path ,
237+ "/d/..\\ projects"
238+ )
239+
240+ @platform_dependent (["windows" ])
241+ def test_dotted_paths (self ):
242+ self .assertEqual (cygpath .to_posix_path (
243+ "C:\\ Users\\ John\\ ..\\ Projects" ), "/c/Users/Projects"
244+ )
245+ self .assertEqual (cygpath .to_posix_path (
246+ "/c/users/./jane" ), "/c/users/jane"
247+ )
248+ self .assertRaisesRegexp (
249+ ValueError ,
250+ "Cannot convert path to posix path: '.*' "
251+ "Please ensure that the path is absolute" ,
252+ cygpath .to_posix_path ,
253+ "./projects/python"
254+ )
255+
256+
106257class TestToCygdrive (TestBase ):
107258 """Test cygpath.to_cygdrive() function."""
108259
@@ -150,7 +301,9 @@ def test_edge_cases(self):
150301 self .assertEqual (cygpath .to_cygdrive ("C:/" ), "/c/" )
151302 self .assertEqual (cygpath .to_cygdrive ("D:\\ folder with space" ), "/d/" )
152303 # Unsupported and reserved characters
153- self .assertEqual (cygpath .to_cygdrive ("E:\\ folder!@#$%^&*()_+-={}[]|;:,.<>?" ), "/e/" )
304+ self .assertEqual (
305+ cygpath .to_cygdrive ("E:\\ folder!@#$%^&*()_+-={}[]|;:,.<>?" ), "/e/"
306+ )
154307 self .assertEqual (cygpath .to_cygdrive ("F:\\ folder_日本語" ), "/f/" )
155308 self .assertEqual (cygpath .to_cygdrive ("\\ \\ ?\\ C:\\ folder\\ file.txt" ), "/c/" )
156309
@@ -165,6 +318,15 @@ def test_normal_windows_paths(self):
165318 self .assertEqual (cygpath .to_mixed_path (
166319 'E:\\ projects\\ python\\ main.py' ), 'E:/projects/python/main.py' )
167320
321+ @platform_dependent (["windows" ])
322+ def test_already_mixed_style_paths (self ):
323+ self .assertEqual (
324+ cygpath .to_mixed_path ('C:/home/john/documents' ), 'C:/home/john/documents'
325+ )
326+ self .assertEqual (cygpath .to_mixed_path (
327+ 'Z:/projects/python' ), 'Z:/projects/python'
328+ )
329+
168330 @platform_dependent (["windows" ])
169331 def test_paths_with_escaped_backslashes (self ):
170332 self .assertEqual (cygpath .to_mixed_path ('C:\\ \\ foo\\ \\ bar' ), 'C:/foo/bar' )
@@ -187,18 +349,48 @@ def test_paths_with_mixed_slashes(self):
187349
188350 @platform_dependent (["windows" ])
189351 def test_paths_with_no_drive_letter (self ):
190- self .assertEqual (cygpath .to_mixed_path (
191- '\\ foo\\ bar' ), '/foo/bar'
352+ self .assertRaisesRegexp (
353+ ValueError ,
354+ "Cannot convert path to mixed path: '.*' "
355+ "Please ensure that the path is absolute" ,
356+ cygpath .to_mixed_path ,
357+ '\\ foo\\ bar'
192358 )
193- self .assertEqual (cygpath .to_mixed_path (
194- '\\ \\ my_folder\\ my_file.txt' ), '//my_folder/my_file.txt'
359+
360+ self .assertRaisesRegexp (
361+ ValueError ,
362+ "Cannot convert path to mixed path: '.*' "
363+ "Please ensure that the path is absolute" ,
364+ cygpath .to_mixed_path ,
365+ '\\ \\ my_folder\\ my_file.txt'
195366 )
196- self .assertEqual (cygpath .to_mixed_path (
197- '/projects/python/main.py' ), '/projects/python/main.py'
367+
368+ self .assertRaisesRegexp (
369+ ValueError ,
370+ "Cannot convert path to mixed path: '.*' "
371+ "Please ensure that the path is absolute" ,
372+ cygpath .to_mixed_path ,
373+ '/projects/python/main.py'
198374 )
199375
200376 @platform_dependent (["windows" ])
201377 def test_paths_with_only_a_drive_letter (self ):
202- self .assertEqual (cygpath .to_mixed_path ('C:' ), 'C:' )
203- self .assertEqual (cygpath .to_mixed_path ('D:' ), 'D:' )
204- self .assertEqual (cygpath .to_mixed_path ('E:' ), 'E:' )
378+ self .assertEqual (cygpath .to_mixed_path ('C:' ), 'C:/' )
379+ self .assertEqual (cygpath .to_mixed_path ('D:' ), 'D:/' )
380+ self .assertEqual (cygpath .to_mixed_path ('E:' ), 'E:/' )
381+
382+ @platform_dependent (["windows" ])
383+ def test_dotted_paths (self ):
384+ self .assertEqual (cygpath .to_mixed_path (
385+ "C:\\ Users\\ John\\ ..\\ Projects" ), "C:/Users/Projects"
386+ )
387+ self .assertEqual (cygpath .to_mixed_path (
388+ "C:/users/./jane" ), "C:/users/jane"
389+ )
390+ self .assertRaisesRegexp (
391+ ValueError ,
392+ "Cannot convert path to posix path: '.*' "
393+ "Please ensure that the path is absolute" ,
394+ cygpath .to_posix_path ,
395+ "./projects/python"
396+ )
0 commit comments