2 // System.IO.Path Test Cases
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Ben Maurer (bmaurer@users.sf.net)
8 // Gilles Freart (gfr@skynet.be)
9 // Atsushi Enomoto (atsushi@ximian.com)
11 // (c) Marcin Szczepanski
12 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
13 // (c) 2003 Ben Maurer
14 // (c) 2003 Gilles Freart
17 using NUnit
.Framework
;
22 namespace MonoTests
.System
.IO
32 public class PathTest
: Assertion
38 static char DSC
= Path
.DirectorySeparatorChar
;
45 path1
= "/foo/test.txt";
48 } else if ('\\' == DSC
) {
50 path1
= "c:\\foo\\test.txt";
51 path2
= Environment
.GetEnvironmentVariable ("SYSTEMROOT");
55 //FIXME: For Mac. figure this out when we need it
56 path1
= "foo:test.txt";
65 return OS
== OsType
.Windows
;
72 return OS
== OsType
.Unix
;
79 return OS
== OsType
.Mac
;
83 public void TestChangeExtension ()
85 string [] files
= new string [3];
86 files
[(int) OsType
.Unix
] = "/foo/test.doc";
87 files
[(int) OsType
.Windows
] = "c:\\foo\\test.doc";
88 files
[(int) OsType
.Mac
] = "foo:test.doc";
90 string testPath
= Path
.ChangeExtension (path1
, "doc");
91 AssertEquals ("ChangeExtension #01", files
[(int) OS
], testPath
);
93 testPath
= Path
.ChangeExtension ("", ".extension");
94 AssertEquals ("ChangeExtension #02", String
.Empty
, testPath
);
96 testPath
= Path
.ChangeExtension (null, ".extension");
97 AssertEquals ("ChangeExtension #03", null, testPath
);
99 testPath
= Path
.ChangeExtension ("path", null);
100 AssertEquals ("ChangeExtension #04", "path", testPath
);
102 testPath
= Path
.ChangeExtension ("path.ext", "doc");
103 AssertEquals ("ChangeExtension #05", "path.doc", testPath
);
105 testPath
= Path
.ChangeExtension ("path.ext1.ext2", "doc");
106 AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath
);
108 testPath
= Path
.ChangeExtension ("hogehoge.xml", ".xsl");
109 AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath
);
110 testPath
= Path
.ChangeExtension ("hogehoge", ".xsl");
111 AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath
);
112 testPath
= Path
.ChangeExtension ("hogehoge.xml", "xsl");
113 AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath
);
114 testPath
= Path
.ChangeExtension ("hogehoge", "xsl");
115 AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath
);
116 testPath
= Path
.ChangeExtension ("hogehoge.xml", String
.Empty
);
117 AssertEquals ("ChangeExtension #11", "hogehoge.", testPath
);
118 testPath
= Path
.ChangeExtension ("hogehoge", String
.Empty
);
119 AssertEquals ("ChangeExtension #12", "hogehoge.", testPath
);
120 testPath
= Path
.ChangeExtension ("hogehoge.", null);
121 AssertEquals ("ChangeExtension #13", "hogehoge", testPath
);
122 testPath
= Path
.ChangeExtension ("hogehoge", null);
123 AssertEquals ("ChangeExtension #14", "hogehoge", testPath
);
124 testPath
= Path
.ChangeExtension (String
.Empty
, null);
125 AssertEquals ("ChangeExtension #15", String
.Empty
, testPath
);
126 testPath
= Path
.ChangeExtension (String
.Empty
, "bashrc");
127 AssertEquals ("ChangeExtension #16", String
.Empty
, testPath
);
128 testPath
= Path
.ChangeExtension (String
.Empty
, ".bashrc");
129 AssertEquals ("ChangeExtension #17", String
.Empty
, testPath
);
130 testPath
= Path
.ChangeExtension (null, null);
131 AssertNull ("ChangeExtension #18", testPath
);
135 [ExpectedException (typeof (ArgumentException
))]
136 public void ChangeExtension_BadPath ()
138 if (!Windows
) throw new ArgumentException ("Test Only On Windows");
139 Path
.ChangeExtension ("<", ".extension");
143 public void ChangeExtension_BadExtension ()
146 string fn
= Path
.ChangeExtension ("file.ext", "<");
147 AssertEquals ("Invalid filename", "file.<", fn
);
151 public void TestCombine ()
153 string [] files
= new string [3];
154 files
[(int) OsType
.Unix
] = "/etc/init.d";
155 files
[(int) OsType
.Windows
] = Environment
.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
156 files
[(int) OsType
.Mac
] = "foo:bar";
158 string testPath
= Path
.Combine (path2
, path3
);
159 AssertEquals ("Combine #01", files
[(int) OS
], testPath
);
161 testPath
= Path
.Combine ("one", "");
162 AssertEquals ("Combine #02", "one", testPath
);
164 testPath
= Path
.Combine ("", "one");
165 AssertEquals ("Combine #03", "one", testPath
);
167 string current
= Directory
.GetCurrentDirectory ();
168 testPath
= Path
.Combine (current
, "one");
170 string expected
= current
+ DSC
+ "one";
171 AssertEquals ("Combine #04", expected
, testPath
);
173 testPath
= Path
.Combine ("one", current
);
174 // LAMESPEC noted in Path.cs
175 AssertEquals ("Combine #05", current
, testPath
);
177 testPath
= Path
.Combine (current
, expected
);
178 AssertEquals ("Combine #06", expected
, testPath
);
180 testPath
= DSC
+ "one";
181 testPath
= Path
.Combine (testPath
, "two" + DSC
);
182 expected
= DSC
+ "one" + DSC
+ "two" + DSC
;
183 AssertEquals ("Combine #06", expected
, testPath
);
185 testPath
= "one" + DSC
;
186 testPath
= Path
.Combine (testPath
, DSC
+ "two");
187 expected
= DSC
+ "two";
188 AssertEquals ("Combine #06", expected
, testPath
);
190 testPath
= "one" + DSC
;
191 testPath
= Path
.Combine (testPath
, "two" + DSC
);
192 expected
= "one" + DSC
+ "two" + DSC
;
193 AssertEquals ("Combine #07", expected
, testPath
);
195 //TODO: Tests for UNC names
197 testPath
= Path
.Combine ("one", null);
198 Fail ("Combine Fail #01");
199 } catch (Exception e
) {
200 AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException
), e
.GetType ());
204 testPath
= Path
.Combine (null, "one");
205 Fail ("Combine Fail #02");
206 } catch (Exception e
) {
207 AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException
), e
.GetType ());
212 testPath
= Path
.Combine ("a>", "one");
213 Fail ("Combine Fail #03");
214 } catch (Exception e
) {
215 AssertEquals ("Combine Exc. #03", typeof (ArgumentException
), e
.GetType ());
219 testPath
= Path
.Combine ("one", "aaa<");
220 Fail ("Combine Fail #04");
221 } catch (Exception e
) {
222 AssertEquals ("Combine Exc. #04", typeof (ArgumentException
), e
.GetType ());
228 [ExpectedException (typeof(ArgumentException
))]
229 public void EmptyDirectoryName ()
231 string testDirName
= Path
.GetDirectoryName ("");
234 public void TestDirectoryName ()
236 string [] files
= new string [3];
237 files
[(int) OsType
.Unix
] = "/foo";
238 files
[(int) OsType
.Windows
] = "c:\\foo";
239 files
[(int) OsType
.Mac
] = "foo";
241 AssertEquals ("GetDirectoryName #01", null, Path
.GetDirectoryName (null));
242 string testDirName
= Path
.GetDirectoryName (path1
);
243 AssertEquals ("GetDirectoryName #02", files
[(int) OS
], testDirName
);
244 testDirName
= Path
.GetDirectoryName (files
[(int) OS
] + DSC
);
245 AssertEquals ("GetDirectoryName #03", files
[(int) OS
], testDirName
);
249 testDirName
= Path
.GetDirectoryName ("aaa>");
250 Fail ("GetDirectoryName Fail #02");
251 } catch (Exception e
) {
252 AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException
), e
.GetType ());
257 testDirName
= Path
.GetDirectoryName (" ");
258 Fail ("GetDirectoryName Fail #03");
259 } catch (Exception e
) {
260 AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException
), e
.GetType ());
264 public void TestGetExtension ()
266 string testExtn
= Path
.GetExtension (path1
);
268 AssertEquals ("GetExtension #01", ".txt", testExtn
);
270 testExtn
= Path
.GetExtension (path2
);
271 AssertEquals ("GetExtension #02", String
.Empty
, testExtn
);
273 testExtn
= Path
.GetExtension (String
.Empty
);
274 AssertEquals ("GetExtension #03", String
.Empty
, testExtn
);
276 testExtn
= Path
.GetExtension (null);
277 AssertEquals ("GetExtension #04", null, testExtn
);
279 testExtn
= Path
.GetExtension (" ");
280 AssertEquals ("GetExtension #05", String
.Empty
, testExtn
);
282 testExtn
= Path
.GetExtension (path1
+ ".doc");
283 AssertEquals ("GetExtension #06", ".doc", testExtn
);
285 testExtn
= Path
.GetExtension (path1
+ ".doc" + DSC
+ "a.txt");
286 AssertEquals ("GetExtension #07", ".txt", testExtn
);
288 testExtn
= Path
.GetExtension (".");
289 AssertEquals ("GetExtension #08", String
.Empty
, testExtn
);
291 testExtn
= Path
.GetExtension ("end.");
292 AssertEquals ("GetExtension #09", String
.Empty
, testExtn
);
294 testExtn
= Path
.GetExtension (".start");
295 AssertEquals ("GetExtension #10", ".start", testExtn
);
297 testExtn
= Path
.GetExtension (".a");
298 AssertEquals ("GetExtension #11", ".a", testExtn
);
300 testExtn
= Path
.GetExtension ("a.");
301 AssertEquals ("GetExtension #12", String
.Empty
, testExtn
);
303 testExtn
= Path
.GetExtension ("a");
304 AssertEquals ("GetExtension #13", String
.Empty
, testExtn
);
306 testExtn
= Path
.GetExtension ("makefile");
307 AssertEquals ("GetExtension #14", String
.Empty
, testExtn
);
311 testExtn
= Path
.GetExtension ("hi<there.txt");
312 Fail ("GetExtension Fail #01");
313 } catch (Exception e
) {
314 AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException
), e
.GetType ());
319 public void TestGetFileName ()
321 string testFileName
= Path
.GetFileName (path1
);
323 AssertEquals ("GetFileName #01", "test.txt", testFileName
);
324 testFileName
= Path
.GetFileName (null);
325 AssertEquals ("GetFileName #02", null, testFileName
);
326 testFileName
= Path
.GetFileName (String
.Empty
);
327 AssertEquals ("GetFileName #03", String
.Empty
, testFileName
);
328 testFileName
= Path
.GetFileName (" ");
329 AssertEquals ("GetFileName #04", " ", testFileName
);
333 testFileName
= Path
.GetFileName ("hi<");
334 Fail ("GetFileName Fail #01");
335 } catch (Exception e
) {
336 AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException
), e
.GetType ());
341 public void TestGetFileNameWithoutExtension ()
343 string testFileName
= Path
.GetFileNameWithoutExtension (path1
);
345 AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName
);
347 testFileName
= Path
.GetFileNameWithoutExtension (null);
348 AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName
);
350 testFileName
= Path
.GetFileNameWithoutExtension (String
.Empty
);
351 AssertEquals ("GetFileNameWithoutExtension #03", String
.Empty
, testFileName
);
354 [Ignore("This does not work under windows. See ERROR comments below.")]
355 public void TestGetFullPath ()
357 string current
= Directory
.GetCurrentDirectory ();
359 string testFullPath
= Path
.GetFullPath ("foo.txt");
360 string expected
= current
+ DSC
+ "foo.txt";
361 AssertEquals ("GetFullPath #01", expected
, testFullPath
);
363 testFullPath
= Path
.GetFullPath ("a//./.././foo.txt");
364 AssertEquals ("GetFullPath #02", expected
, testFullPath
);
365 string root
= Windows
? "C:\\" : "/";
366 string [,] test
= new string [,] {
367 {"root////././././././../root/././../root", "root"}
,
369 {"root/./", "root/"}
,
370 {"root/./", "root/"}
,
374 {"root/.hiddenfile", "root/.hiddenfile"}
,
375 {"root/. /", "root/. /"}
,
376 {"root/.. /", "root/.. /"}
,
377 {"root/..weirdname", "root/..weirdname"}
,
379 {"root/../a/b/../../..", ""}
,
383 {"root//dir", "root/dir"}
,
384 {"root/. /", "root/. /"}
,
385 {"root/.. /", "root/.. /"}
,
386 {"root/ . /", "root/ . /"}
,
387 {"root/ .. /", "root/ .. /"}
,
388 {"root/./", "root/"}
,
389 //ERROR! Paths are trimmed
390 {"root/.. /", "root/.. /"}
,
394 //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
395 //... so only the first test was being done.
396 for (int i
= 0; i
< test
.GetUpperBound (1); i
++) {
397 AssertEquals (String
.Format ("GetFullPath #{0}", i
), root
+ test
[i
, 1], Path
.GetFullPath (root
+ test
[i
, 0]));
401 string uncroot
= @"\\server\share\";
402 string [,] testunc
= new string [,] {
403 {"root////././././././../root/././../root", "root"}
,
405 {"root/./", "root/"}
,
406 {"root/./", "root/"}
,
410 {"root/.hiddenfile", "root/.hiddenfile"}
,
411 {"root/. /", "root/. /"}
,
412 {"root/.. /", "root/.. /"}
,
413 {"root/..weirdname", "root/..weirdname"}
,
415 {"root/../a/b/../../..", ""}
,
419 {"root//dir", "root/dir"}
,
420 {"root/. /", "root/. /"}
,
421 {"root/.. /", "root/.. /"}
,
422 {"root/ . /", "root/ . /"}
,
423 {"root/ .. /", "root/ .. /"}
,
424 {"root/./", "root/"}
,
425 {"root/.. /", "root/.. /"}
,
428 for (int i
= 0; i
< test
.GetUpperBound (1); i
++) {
429 AssertEquals (String
.Format ("GetFullPath UNC #{0}", i
), uncroot
+ test
[i
, 1], Path
.GetFullPath (uncroot
+ test
[i
, 0]));
434 testFullPath
= Path
.GetFullPath (null);
435 Fail ("GetFullPath Fail #01");
436 } catch (Exception e
) {
437 AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException
), e
.GetType ());
441 testFullPath
= Path
.GetFullPath (String
.Empty
);
442 Fail ("GetFullPath Fail #02");
443 } catch (Exception e
) {
444 AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException
), e
.GetType ());
448 public void TestGetFullPath2 ()
451 AssertEquals ("GetFullPath w#01", @"Z:\", Path
.GetFullPath ("Z:"));
452 AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path
.GetFullPath (@"c:\abc\def"));
453 Assert ("GetFullPath w#03", Path
.GetFullPath (@"\").EndsWith (@"\"));
454 // "\\\\" is not allowed
455 Assert ("GetFullPath w#05", Path
.GetFullPath ("/").EndsWith (@"\"));
456 // "//" is not allowed
457 Assert ("GetFullPath w#07", Path
.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
458 Assert ("GetFullPath w#08", Path
.GetFullPath ("c").EndsWith (@"\c"));
459 Assert ("GetFullPath w#09", Path
.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
460 Assert ("GetFullPath w#10", Path
.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
461 AssertEquals ("GetFullPath w#11", @"\\abc\def", Path
.GetFullPath (@"\\abc\def"));
462 AssertEquals ("GetFullPath w#12", Directory
.GetCurrentDirectory () + @"\abc\def", Path
.GetFullPath (@"abc//def"));
463 AssertEquals ("GetFullPath w#13", Directory
.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path
.GetFullPath ("/abc/def"));
464 AssertEquals ("GetFullPath w#14", @"\\abc\def", Path
.GetFullPath ("//abc/def"));
468 public void TestGetPathRoot ()
473 current
= Directory
.GetCurrentDirectory ();
474 expected
= current
[0].ToString ();
476 current
= @"J:\Some\Strange Directory\Name";
480 string pathRoot
= Path
.GetPathRoot (current
);
481 AssertEquals ("GetPathRoot #01", expected
, pathRoot
);
483 pathRoot
= Path
.GetPathRoot ("hola");
484 AssertEquals ("GetPathRoot #02", String
.Empty
, pathRoot
);
486 pathRoot
= Path
.GetPathRoot (null);
487 AssertEquals ("GetPathRoot #03", null, pathRoot
);
490 AssertEquals ("GetPathRoot w#01", "z:", Path
.GetPathRoot ("z:"));
491 AssertEquals ("GetPathRoot w#02", "c:\\", Path
.GetPathRoot ("c:\\abc\\def"));
492 AssertEquals ("GetPathRoot w#03", "\\", Path
.GetPathRoot ("\\"));
493 AssertEquals ("GetPathRoot w#04", "\\\\", Path
.GetPathRoot ("\\\\"));
494 AssertEquals ("GetPathRoot w#05", "\\", Path
.GetPathRoot ("/"));
495 AssertEquals ("GetPathRoot w#06", "\\\\", Path
.GetPathRoot ("//"));
496 AssertEquals ("GetPathRoot w#07", String
.Empty
, Path
.GetPathRoot ("readme.txt"));
497 AssertEquals ("GetPathRoot w#08", String
.Empty
, Path
.GetPathRoot ("c"));
498 AssertEquals ("GetPathRoot w#09", String
.Empty
, Path
.GetPathRoot ("abc\\def"));
499 AssertEquals ("GetPathRoot w#10", "\\", Path
.GetPathRoot ("\\abc\\def"));
500 AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path
.GetPathRoot ("\\\\abc\\def"));
501 AssertEquals ("GetPathRoot w#12", String
.Empty
, Path
.GetPathRoot ("abc//def"));
502 AssertEquals ("GetPathRoot w#13", "\\", Path
.GetPathRoot ("/abc/def"));
503 AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path
.GetPathRoot ("//abc/def"));
505 // TODO: Same tests for Unix.
509 public void TestGetTempPath ()
511 string getTempPath
= Path
.GetTempPath ();
512 Assert ("GetTempPath #01", getTempPath
!= String
.Empty
);
513 Assert ("GetTempPath #02", Path
.IsPathRooted (getTempPath
));
514 AssertEquals ("GetTempPath #03", Path
.DirectorySeparatorChar
, getTempPath
[getTempPath
.Length
- 1]);
517 public void TestGetTempFileName ()
519 string getTempFileName
= null;
521 getTempFileName
= Path
.GetTempFileName ();
522 Assert ("GetTempFileName #01", getTempFileName
!= String
.Empty
);
523 Assert ("GetTempFileName #02", File
.Exists (getTempFileName
));
525 if (getTempFileName
!= null && getTempFileName
!= String
.Empty
){
526 File
.Delete (getTempFileName
);
531 public void TestHasExtension ()
533 AssertEquals ("HasExtension #01", true, Path
.HasExtension ("foo.txt"));
534 AssertEquals ("HasExtension #02", false, Path
.HasExtension ("foo"));
535 AssertEquals ("HasExtension #03", true, Path
.HasExtension (path1
));
536 AssertEquals ("HasExtension #04", false, Path
.HasExtension (path2
));
537 AssertEquals ("HasExtension #05", false, Path
.HasExtension (null));
538 AssertEquals ("HasExtension #06", false, Path
.HasExtension (String
.Empty
));
539 AssertEquals ("HasExtension #07", false, Path
.HasExtension (" "));
540 AssertEquals ("HasExtension #08", false, Path
.HasExtension ("."));
541 AssertEquals ("HasExtension #09", false, Path
.HasExtension ("end."));
542 AssertEquals ("HasExtension #10", true, Path
.HasExtension (".start"));
543 AssertEquals ("HasExtension #11", true, Path
.HasExtension (".a"));
544 AssertEquals ("HasExtension #12", false, Path
.HasExtension ("a."));
545 AssertEquals ("HasExtension #13", false, Path
.HasExtension ("Makefile"));
548 public void TestRooted ()
550 Assert ("IsPathRooted #01", Path
.IsPathRooted (path2
));
551 Assert ("IsPathRooted #02", !Path
.IsPathRooted (path3
));
552 Assert ("IsPathRooted #03", !Path
.IsPathRooted (null));
553 Assert ("IsPathRooted #04", !Path
.IsPathRooted (String
.Empty
));
554 Assert ("IsPathRooted #05", !Path
.IsPathRooted (" "));
555 Assert ("IsPathRooted #06", Path
.IsPathRooted ("/"));
556 Assert ("IsPathRooted #07", Path
.IsPathRooted ("\\"));
557 Assert ("IsPathRooted #08", Path
.IsPathRooted ("//"));
558 Assert ("IsPathRooted #09", Path
.IsPathRooted ("\\\\"));
559 Assert ("IsPathRooted #10", !Path
.IsPathRooted (":"));
561 Assert ("IsPathRooted #11", Path
.IsPathRooted ("z:"));
563 Assert ("IsPathRooted #11", !Path
.IsPathRooted ("z:"));
566 Assert ("IsPathRooted #12", Path
.IsPathRooted ("z:\\"));
567 Assert ("IsPathRooted #13", Path
.IsPathRooted ("z:\\topdir"));
568 // This looks MS BUG. It is treated as absolute path
569 Assert ("IsPathRooted #14", Path
.IsPathRooted ("z:curdir"));
570 Assert ("IsPathRooted #15", Path
.IsPathRooted ("\\abc\\def"));
574 public void TestCanonicalizeDots ()
576 string current
= Path
.GetFullPath (".");
577 Assert ("TestCanonicalizeDotst #01", !current
.EndsWith ("."));
578 string parent
= Path
.GetFullPath ("..");
579 Assert ("TestCanonicalizeDotst #02", !current
.EndsWith (".."));
582 public void TestDirectoryNameBugs ()
585 AssertEquals ("Win #01", "C:\\foo", Path
.GetDirectoryName ("C:\\foo\\foo.txt"));
587 AssertEquals ("No win #01", "/etc", Path
.GetDirectoryName ("/etc/hostname"));
591 public void TestGetFullPathUnix ()
596 AssertEquals ("#01", "/", Path
.GetFullPath ("/"));
597 AssertEquals ("#02", "/hey", Path
.GetFullPath ("/hey"));
598 AssertEquals ("#03", Environment
.CurrentDirectory
, Path
.GetFullPath ("."));
599 AssertEquals ("#04", Path
.Combine (Environment
.CurrentDirectory
, "hey"),
600 Path
.GetFullPath ("hey"));