1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
\r
4 // Ville Palo (vi64pa@koti.soon.fi)
\r
5 // Sebastien Pouliot <sebastien@ximian.com>
\r
7 // (C) 2003 Ville Palo
\r
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
\r
11 using NUnit.Framework;
\r
13 using System.Collections;
\r
16 namespace MonoTests.System.IO
\r
19 public class DirectoryInfoTest : Assertion
\r
21 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
\r
23 static readonly char DSC = Path.DirectorySeparatorChar;
\r
27 protected void SetUp ()
\r
29 current = Directory.GetCurrentDirectory ();
\r
30 if (Directory.Exists (TempFolder))
\r
31 Directory.Delete (TempFolder, true);
\r
32 Directory.CreateDirectory (TempFolder);
\r
36 protected void TearDown ()
\r
38 if (Directory.Exists (TempFolder))
\r
39 Directory.Delete (TempFolder, true);
\r
40 Directory.SetCurrentDirectory (current);
\r
46 string path = TempFolder + DSC + "DIT.Ctr.Test";
\r
49 FileInfo info = new FileInfo (path);
\r
50 AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
\r
51 AssertEquals ("test#02", false, info.Exists);
\r
52 AssertEquals ("test#03", ".Test", info.Extension);
\r
53 AssertEquals ("test#05", "DIT.Ctr.Test", info.Name);
\r
57 [ExpectedException(typeof(ArgumentNullException))]
\r
58 public void CtorArgumentNullException ()
\r
60 DirectoryInfo info = new DirectoryInfo (null);
\r
64 [ExpectedException(typeof(ArgumentException))]
\r
65 public void CtorArgumentException1 ()
\r
67 DirectoryInfo info = new DirectoryInfo ("");
\r
71 [ExpectedException(typeof(ArgumentException))]
\r
72 public void CtorArgumentException2 ()
\r
74 DirectoryInfo info = new DirectoryInfo (" ");
\r
78 [ExpectedException(typeof(ArgumentException))]
\r
79 public void CtorArgumentException3 ()
\r
82 foreach (char c in Path.InvalidPathChars) {
\r
85 DirectoryInfo info = new DirectoryInfo (path);
\r
89 public void Exists ()
\r
91 string path = TempFolder + DSC + "DIT.Exists.Test";
\r
95 DirectoryInfo info = new DirectoryInfo (path);
\r
96 AssertEquals ("test#01", false, info.Exists);
\r
98 Directory.CreateDirectory (path);
\r
99 AssertEquals ("test#02", false, info.Exists);
\r
100 info = new DirectoryInfo (path);
\r
101 AssertEquals ("test#03", true, info.Exists);
\r
108 public void Name ()
\r
110 string path = TempFolder + DSC + "DIT.Name.Test";
\r
114 DirectoryInfo info = new DirectoryInfo (path);
\r
115 AssertEquals ("test#01", "DIT.Name.Test", info.Name);
\r
117 info = Directory.CreateDirectory (path);
\r
118 AssertEquals ("test#02", "DIT.Name.Test", info.Name);
\r
127 public void Parent ()
\r
129 string path = TempFolder + DSC + "DIT.Parent.Test";
\r
133 DirectoryInfo info = new DirectoryInfo (path);
\r
134 AssertEquals ("test#01", "MonoTests.System.IO.Tests", info.Parent.Name);
\r
136 info = Directory.CreateDirectory (path);
\r
137 AssertEquals ("test#02", "MonoTests.System.IO.Tests", info.Parent.Name);
\r
145 public void Create ()
\r
147 string path = TempFolder + DSC + "DIT.Create.Test";
\r
151 DirectoryInfo info = new DirectoryInfo (path);
\r
152 AssertEquals ("test#01", false, info.Exists);
\r
154 AssertEquals ("test#02", false, info.Exists);
\r
155 info = new DirectoryInfo (path);
\r
156 AssertEquals ("test#03", true, info.Exists);
\r
163 public void CreateSubdirectory ()
\r
165 string sub_path = Path.Combine ("test01", "test02");
\r
167 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
168 info.CreateSubdirectory (sub_path);
\r
169 Assert ("test#01", Directory.Exists (Path.Combine (TempFolder, sub_path)));
\r
171 DeleteDir (Path.Combine (TempFolder, sub_path));
\r
177 [ExpectedException(typeof(ArgumentException))]
178 public void CreateSubdirectoryEmptyString ()
180 new DirectoryInfo (".").CreateSubdirectory ("");
184 public void Delete1 ()
\r
186 string path = TempFolder + DSC + "DIT.Delete1.Test";
\r
190 Directory.CreateDirectory (path);
\r
191 DirectoryInfo info = new DirectoryInfo (path);
\r
192 AssertEquals ("test#01", true, info.Exists);
\r
195 AssertEquals ("test#02", true, info.Exists);
\r
197 info = new DirectoryInfo (path);
\r
198 AssertEquals ("test#03", false, info.Exists);
\r
205 public void Delete2 ()
\r
207 string path = TempFolder + DSC + "DIT.Delete2.Test";
\r
211 Directory.CreateDirectory (path);
\r
212 File.Create (path + DSC + "test").Close ();
\r
213 DirectoryInfo info = new DirectoryInfo (path);
\r
214 AssertEquals ("test#01", true, info.Exists);
\r
216 info.Delete (true);
\r
217 AssertEquals ("test#02", true, info.Exists);
\r
219 info = new DirectoryInfo (path);
\r
220 AssertEquals ("test#03", false, info.Exists);
\r
227 [ExpectedException (typeof (IOException))]
\r
228 public void DeleteIOException1 ()
\r
230 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
\r
234 Directory.CreateDirectory (path);
\r
235 File.Create (path + DSC + "test").Close ();
\r
236 DirectoryInfo info = new DirectoryInfo (path);
\r
244 [ExpectedException (typeof (IOException))]
\r
245 public void DeleteIOException2 ()
\r
247 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
\r
251 Directory.CreateDirectory (path);
\r
252 File.Create (path + DSC + "test").Close ();
\r
253 DirectoryInfo info = new DirectoryInfo (path);
\r
254 info.Delete (false);
\r
262 public void FullName ()
264 DirectoryInfo di = new DirectoryInfo ("something");
265 Assert ("Exists", !di.Exists);
266 Assert ("FullName", di.FullName.EndsWith ("something"));
268 di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
269 AssertEquals ("DirectorySeparatorChar", Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1]);
271 di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
272 AssertEquals ("AltDirectorySeparatorChar", Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1]);
276 public void FullName_RootDirectory ()
278 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
279 if (Path.DirectorySeparatorChar == '/') {
280 // can't be sure of the root drive under windows
281 AssertEquals ("FullName", "/", di.FullName);
283 AssertNull ("Parent", di.Parent);
285 di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
286 if (Path.DirectorySeparatorChar == '/') {
287 // can't be sure of the root drive under windows
288 AssertEquals ("FullName-Alt", "/", di.FullName);
290 AssertNull ("Parent-Alt", di.Parent);
294 public void GetDirectories1 ()
\r
296 string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
\r
299 DirectoryInfo info = Directory.CreateDirectory (path);
\r
300 AssertEquals ("test#01", 0, info.GetDirectories ().Length);
\r
302 Directory.CreateDirectory (path + DSC + "1");
\r
303 Directory.CreateDirectory (path + DSC + "2");
\r
304 File.Create (path + DSC + "filetest").Close ();
\r
305 AssertEquals ("test#02", 2, info.GetDirectories ().Length);
\r
307 Directory.Delete (path + DSC + 2);
\r
308 AssertEquals ("test#02", 1, info.GetDirectories ().Length);
\r
316 public void GetDirectories2 ()
\r
318 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
\r
321 DirectoryInfo info = Directory.CreateDirectory (path);
\r
322 AssertEquals ("test#01", 0, info.GetDirectories ("*").Length);
\r
324 Directory.CreateDirectory (path + DSC + "test120");
\r
325 Directory.CreateDirectory (path + DSC + "test210");
\r
326 Directory.CreateDirectory (path + DSC + "atest330");
\r
327 Directory.CreateDirectory (path + DSC + "test220");
\r
328 File.Create (path + DSC + "filetest").Close ();
\r
330 AssertEquals ("test#02", 4, info.GetDirectories ("*").Length);
\r
331 AssertEquals ("test#03", 3, info.GetDirectories ("test*").Length);
\r
332 AssertEquals ("test#04", 2, info.GetDirectories ("test?20").Length);
\r
333 AssertEquals ("test#05", 0, info.GetDirectories ("test?").Length);
\r
334 AssertEquals ("test#06", 0, info.GetDirectories ("test[12]*").Length);
\r
335 AssertEquals ("test#07", 2, info.GetDirectories ("test2*0").Length);
\r
336 AssertEquals ("test#08", 4, info.GetDirectories ("*test*").Length);
\r
344 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
345 public void GetDirectoriesDirectoryNotFoundException1 ()
\r
347 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
\r
351 DirectoryInfo info = new DirectoryInfo (path);
\r
352 info.GetDirectories ();
\r
359 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
360 public void GetDirectoriesDirectoryNotFoundException2 ()
\r
362 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
\r
366 DirectoryInfo info = new DirectoryInfo (path);
\r
367 info.GetDirectories ("*");
\r
374 [ExpectedException (typeof (ArgumentNullException))]
\r
375 public void GetDirectoriesArgumentNullException ()
\r
377 string path = TempFolder + DSC + "DIT.GetDirectoriesArgumentNullException.Test";
\r
381 DirectoryInfo info = new DirectoryInfo (path);
\r
382 info.GetDirectories (null);
\r
389 public void GetFiles1 ()
\r
391 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
\r
395 DirectoryInfo info = Directory.CreateDirectory (path);
\r
396 AssertEquals ("test#01", 0, info.GetFiles ().Length);
\r
397 File.Create (path + DSC + "file1").Close ();
\r
398 File.Create (path + DSC + "file2").Close ();
\r
399 Directory.CreateDirectory (path + DSC + "directory1");
\r
400 AssertEquals ("test#02", 2, info.GetFiles ().Length);
\r
408 public void GetFiles2()
\r
410 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
\r
414 DirectoryInfo info = Directory.CreateDirectory (path);
\r
415 AssertEquals ("test#01", 0, info.GetFiles ("*").Length);
\r
416 File.Create (path + DSC + "file120file").Close ();
\r
417 File.Create (path + DSC + "file220file").Close ();
\r
418 File.Create (path + DSC + "afile330file").Close ();
\r
419 File.Create (path + DSC + "test.abc").Close ();
\r
420 File.Create (path + DSC + "test.abcd").Close ();
\r
421 File.Create (path + DSC + "test.abcdef").Close ();
\r
422 Directory.CreateDirectory (path + DSC + "dir");
\r
424 AssertEquals ("test#02", 6, info.GetFiles ("*").Length);
\r
425 AssertEquals ("test#03", 2, info.GetFiles ("file*file").Length);
\r
426 AssertEquals ("test#04", 3, info.GetFiles ("*file*").Length);
\r
427 AssertEquals ("test#05", 2, info.GetFiles ("file?20file").Length);
\r
428 AssertEquals ("test#07", 1, info.GetFiles ("*.abcd").Length);
\r
429 AssertEquals ("test#08", 2, info.GetFiles ("*.abcd*").Length);
\r
436 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
437 public void GetFilesDirectoryNotFoundException1 ()
\r
439 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
\r
443 DirectoryInfo info = new DirectoryInfo (path);
\r
452 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
453 public void GetFilesDirectoryNotFoundException2 ()
\r
455 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
\r
459 DirectoryInfo info = new DirectoryInfo (path);
\r
460 info.GetFiles ("*");
\r
468 [ExpectedException (typeof (ArgumentNullException))]
\r
469 public void GetFilesArgumentNullException ()
\r
471 string path = TempFolder + DSC + "DIT.GetFilesArgumentNullException.Test";
\r
475 DirectoryInfo info = new DirectoryInfo (path);
\r
476 info.GetFiles (null);
\r
483 public void MoveTo ()
\r
485 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
\r
486 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
\r
491 DirectoryInfo info1 = Directory.CreateDirectory (path1);
\r
492 DirectoryInfo info2 = new DirectoryInfo (path2);
\r
494 AssertEquals ("test#01", true, info1.Exists);
\r
495 AssertEquals ("test#02", false, info2.Exists);
\r
497 info1.MoveTo (path2);
\r
498 AssertEquals ("test#03", true, info1.Exists);
\r
499 AssertEquals ("test#04", false, info2.Exists);
\r
501 info1 = new DirectoryInfo (path1);
\r
502 info2 = new DirectoryInfo (path2);
\r
503 AssertEquals ("test#05", false, info1.Exists);
\r
504 AssertEquals ("test#06", true, info2.Exists);
\r
513 [ExpectedException (typeof (ArgumentNullException))]
\r
514 public void MoveToArgumentNullException ()
\r
516 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
\r
520 DirectoryInfo info = Directory.CreateDirectory (path);
\r
521 info.MoveTo (null);
\r
529 [ExpectedException (typeof (IOException))]
\r
530 public void MoveToIOException1 ()
\r
532 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
\r
536 DirectoryInfo info = Directory.CreateDirectory (path);
\r
537 info.MoveTo (path);
\r
544 [ExpectedException (typeof (ArgumentException))]
\r
545 public void MoveToArgumentException1 ()
\r
547 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
\r
551 DirectoryInfo info = Directory.CreateDirectory (path);
\r
559 [ExpectedException (typeof (ArgumentException))]
\r
560 public void MoveToArgumentException2 ()
\r
562 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
\r
566 DirectoryInfo info = Directory.CreateDirectory (path);
\r
574 [ExpectedException (typeof (ArgumentException))]
\r
575 public void MoveToArgumentException3 ()
\r
577 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
\r
581 DirectoryInfo info = Directory.CreateDirectory (path);
\r
582 info.MoveTo (Path.InvalidPathChars [0].ToString ());
\r
589 [ExpectedException (typeof (IOException))]
\r
590 public void MoveToIOException2 ()
\r
592 string path = TempFolder + DSC + "DIT.MoveToIOException2.Test";
\r
596 DirectoryInfo info = new DirectoryInfo (path);
\r
597 info.MoveTo (path);
\r
603 private void DeleteDir (string path)
\r
605 if (Directory.Exists (path))
\r
606 Directory.Delete (path, true);
\r
610 public void DirectoryNameWithSpace ()
612 // check for Unix platforms - see FAQ for more details
613 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
614 int platform = (int) Environment.OSVersion.Platform;
615 if ((platform == 4) || (platform == 128)) {
616 DeleteDir ("this has a space at the end ");
617 string path = Path.Combine (TempFolder, "this has a space at the end ");
618 Directory.CreateDirectory (path);
619 DirectoryInfo i = new DirectoryInfo (path);
621 foreach (FileInfo f in i.GetFiles ()) // This used to throw
627 public void LastWriteTime ()
\r
629 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
630 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
\r
632 DateTime time = Directory.GetLastWriteTime (TempFolder);
\r
633 AssertEquals ("test#01", 2003, time.Year);
\r
634 AssertEquals ("test#02", 6, time.Month);
\r
635 AssertEquals ("test#03", 4, time.Day);
\r
636 AssertEquals ("test#04", 6, time.Hour);
\r
637 AssertEquals ("test#05", 4, time.Minute);
\r
638 AssertEquals ("test#06", 0, time.Second);
\r
640 time = TimeZone.CurrentTimeZone.ToLocalTime (
\r
641 Directory.GetLastWriteTimeUtc (TempFolder));
\r
642 AssertEquals ("test#07", 2003, time.Year);
\r
643 AssertEquals ("test#08", 6, time.Month);
\r
644 AssertEquals ("test#09", 4, time.Day);
\r
645 AssertEquals ("test#10", 6, time.Hour);
\r
646 AssertEquals ("test#11", 4, time.Minute);
\r
647 AssertEquals ("test#12", 0, time.Second);
\r
651 public void LastWriteTimeUtc ()
\r
653 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
654 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
\r
656 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
\r
657 Directory.GetLastWriteTime (TempFolder));
\r
658 AssertEquals ("test#1", 2003, time.Year);
\r
659 AssertEquals ("test#2", 6, time.Month);
\r
660 AssertEquals ("test#3", 4, time.Day);
\r
661 AssertEquals ("test#4", 6, time.Hour);
\r
662 AssertEquals ("test#5", 4, time.Minute);
\r
663 AssertEquals ("test#6", 0, time.Second);
\r
665 time = Directory.GetLastWriteTimeUtc (TempFolder);
\r
666 AssertEquals ("test#7", 2003, time.Year);
\r
667 AssertEquals ("test#8", 6, time.Month);
\r
668 AssertEquals ("test#9", 4, time.Day);
\r
669 AssertEquals ("test#10", 6, time.Hour);
\r
670 AssertEquals ("test#11", 4, time.Minute);
\r
671 AssertEquals ("test#12", 0, time.Second);
\r
675 public void LastAccessTime ()
\r
677 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
678 info.LastAccessTime = DateTime.Now;
\r
682 public void LastAccessTimeUtc ()
\r
684 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
685 info.LastAccessTimeUtc = DateTime.Now;
\r
689 public void CreationTime ()
\r
691 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
692 info.CreationTime = DateTime.Now;
\r
696 public void CreationTimeUtc ()
\r
698 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
699 info.CreationTimeUtc = DateTime.Now;
\r
703 private void CheckName (string name)
\r
705 DirectoryInfo di = new DirectoryInfo (name);
\r
706 AssertEquals (name + ".Name", "share", di.Name);
\r
707 AssertEquals (name + ".Parent.Name", "usr", di.Parent.Name);
\r
711 public void Name_Bug76903 ()
\r
713 CheckName ("/usr/share");
\r
714 CheckName ("/usr/share/");
\r
715 CheckName ("/usr/share/.");
\r
716 CheckName ("/usr/share/./");
\r
717 CheckName ("/usr/share/blabla/../");
\r
718 CheckName ("/usr/lib/../share/.");
\r
722 public void Hang_76191 ()
\r
724 // from bug #76191 (hangs on Windows)
\r
725 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
\r
726 Stack s = new Stack ();
\r
728 while (di.Parent != null) {
\r
732 while (s.Count > 0) {
\r
733 di = (DirectoryInfo) s.Pop ();
\r
734 Assert (di.Name, di.Exists);
\r
738 private void WindowsParentFullName (string name, string expected)
\r
740 DirectoryInfo di = new DirectoryInfo (name);
\r
741 if (di.Parent == null)
\r
742 AssertNull (name, expected);
\r
744 AssertEquals (name, expected, di.Parent.FullName);
\r
748 public void WindowsSystem32_76191 ()
\r
750 // check for Unix platforms - see FAQ for more details
\r
751 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
\r
752 int platform = (int) Environment.OSVersion.Platform;
\r
753 if ((platform == 4) || (platform == 128))
\r
756 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
\r
757 WindowsParentFullName ("C:", "C:\\WINDOWS");
\r
758 WindowsParentFullName ("C:\\", null);
\r
759 WindowsParentFullName ("C:\\dir", "C:\\");
\r
760 WindowsParentFullName ("C:\\dir\\", "C:\\");
\r
761 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
\r
762 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
\r
766 public void Parent_Bug77090 ()
\r
768 DirectoryInfo di = new DirectoryInfo ("/home");
\r
769 if (Path.DirectorySeparatorChar == '\\') {
\r
770 Assert ("/home parent (Windows path)", di.Parent.Name.EndsWith (":\\"));
\r
773 AssertEquals ("/home parent", "/", di.Parent.Name);
\r
774 AssertNull ("/home parent parent", di.Parent.Parent);
\r