1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 using System.Collections;
13 using System.Collections.Generic;
15 using System.Runtime.Serialization;
16 using System.Runtime.Serialization.Formatters.Binary;
18 using NUnit.Framework;
20 namespace MonoTests.System.IO
23 public class DirectoryInfoTest
25 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
27 static readonly char DSC = Path.DirectorySeparatorChar;
31 protected void SetUp ()
33 current = Directory.GetCurrentDirectory ();
34 if (Directory.Exists (TempFolder))
35 Directory.Delete (TempFolder, true);
36 Directory.CreateDirectory (TempFolder);
40 protected void TearDown ()
42 if (Directory.Exists (TempFolder))
43 Directory.Delete (TempFolder, true);
44 Directory.SetCurrentDirectory (current);
47 [Test] // ctor (String)
48 public void Constructor1 ()
50 string path = TempFolder + DSC + "DIT.Ctr.Test";
53 DirectoryInfo info = new DirectoryInfo (path);
54 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#1");
55 Assert.IsFalse (info.Exists, "#2");
56 Assert.AreEqual (".Test", info.Extension, "#3");
57 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#4");
60 [Test] // ctor (String)
61 public void Constructor1_Path_Null ()
64 new DirectoryInfo (null);
66 } catch (ArgumentNullException ex) {
67 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
68 Assert.IsNull (ex.InnerException, "#3");
69 Assert.IsNotNull (ex.Message, "#4");
70 Assert.AreEqual ("path", ex.ParamName, "#5");
74 [Test] // ctor (String)
75 public void Constructor1_Path_Empty ()
78 new DirectoryInfo (string.Empty);
80 } catch (ArgumentException ex) {
81 // Empty file name is not legal
82 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
83 Assert.IsNull (ex.InnerException, "#3");
84 Assert.IsNotNull (ex.Message, "#4");
85 Assert.IsNull (ex.ParamName, "#5");
89 [Test] // ctor (String)
90 public void Constructor1_Path_Whitespace ()
93 new DirectoryInfo (" ");
95 } catch (ArgumentException ex) {
96 // The path is not of a legal form
97 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
98 Assert.IsNull (ex.InnerException, "#3");
99 Assert.IsNotNull (ex.Message, "#4");
100 Assert.IsNull (ex.ParamName, "#5");
104 [Test] // ctor (String)
105 public void Constructor1_Path_InvalidPathChars ()
107 string path = string.Empty;
108 foreach (char c in Path.InvalidPathChars)
111 new DirectoryInfo (path);
113 } catch (ArgumentException ex) {
114 // The path contains illegal characters
115 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
116 Assert.IsNull (ex.InnerException, "#3");
117 Assert.IsNotNull (ex.Message, "#4");
118 Assert.IsNull (ex.ParamName, "#5");
123 public void Exists ()
125 string path = TempFolder + DSC + "DIT.Exists.Test";
129 DirectoryInfo info = new DirectoryInfo (path);
130 Assert.IsFalse (info.Exists, "#1");
132 Directory.CreateDirectory (path);
133 Assert.IsFalse (info.Exists, "#2");
134 info = new DirectoryInfo (path);
135 Assert.IsTrue (info.Exists, "#3");
142 [Category ("MobileNotWorking")]
145 string path = TempFolder + DSC + "DIT.Name.Test";
149 DirectoryInfo info = new DirectoryInfo (path);
150 Assert.AreEqual ("DIT.Name.Test", info.Name, "#1");
152 info = Directory.CreateDirectory (path);
153 Assert.AreEqual ("DIT.Name.Test", info.Name, "#2");
155 info = Directory.CreateDirectory ("whatever");
156 Assert.AreEqual ("whatever", info.Name, "#3");
159 info = new DirectoryInfo ("/");
160 Assert.AreEqual ("/", info.Name, "#4");
162 info = new DirectoryInfo ("test/");
163 Assert.AreEqual ("test", info.Name, "#5");
165 info = new DirectoryInfo ("/test");
166 Assert.AreEqual ("test", info.Name, "#4");
168 info = new DirectoryInfo ("/test/");
169 Assert.AreEqual ("test", info.Name, "#4");
171 Directory.SetCurrentDirectory (@"C:\");
173 info = new DirectoryInfo (@"c:");
174 Assert.AreEqual (@"C:\", info.Name, "#4");
176 info = new DirectoryInfo (@"c:\");
177 Assert.AreEqual (@"c:\", info.Name, "#5");
179 info = new DirectoryInfo (@"c:\test");
180 Assert.AreEqual ("test", info.Name, "#6");
182 info = new DirectoryInfo (@"c:\test\");
183 Assert.AreEqual ("test", info.Name, "#7");
191 public void Parent ()
193 string path = TempFolder + DSC + "DIT.Parent.Test";
197 DirectoryInfo info = new DirectoryInfo (path);
198 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#1");
200 info = Directory.CreateDirectory (path);
201 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#2");
203 info = new DirectoryInfo ("test");
204 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#3");
207 info = new DirectoryInfo ("/");
208 Assert.IsNull (info.Parent, "#4");
210 info = new DirectoryInfo ("test/");
211 Assert.IsNotNull (info.Parent, "#5a");
212 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#5b");
214 info = new DirectoryInfo ("/test");
215 Assert.IsNotNull (info.Parent, "#6a");
216 Assert.AreEqual ("/", info.Parent.FullName, "#6b");
218 info = new DirectoryInfo ("/test/");
219 Assert.IsNotNull (info.Parent, "#7a");
220 Assert.AreEqual ("/", info.Parent.FullName, "#7b");
222 Directory.SetCurrentDirectory (@"C:\");
224 info = new DirectoryInfo (@"c:");
225 Assert.IsNull (info.Parent, "#4");
227 info = new DirectoryInfo (@"c:\");
228 Assert.IsNull (info.Parent, "#5");
230 info = new DirectoryInfo (@"c:\test");
231 Assert.IsNotNull (info.Parent, "#6a");
232 Assert.AreEqual (@"c:\", info.Parent.FullName, "#6b");
234 info = new DirectoryInfo (@"c:\test\");
235 Assert.IsNotNull (info.Parent, "#7a");
236 Assert.AreEqual (@"c:\", info.Parent.FullName, "#7b");
244 public void Create ()
246 string path = TempFolder + DSC + "DIT.Create.Test";
250 DirectoryInfo info = new DirectoryInfo (path);
251 Assert.IsFalse (info.Exists, "#1");
253 Assert.IsFalse (info.Exists, "#2");
254 info = new DirectoryInfo (path);
255 Assert.IsTrue (info.Exists, "#3");
262 public void CreateSubdirectory ()
264 string sub_path = Path.Combine ("test01", "test02");
266 DirectoryInfo info = new DirectoryInfo (TempFolder);
267 DirectoryInfo sub = info.CreateSubdirectory (sub_path);
268 Assert.IsNotNull (sub, "#1");
269 Assert.AreEqual (Path.Combine (TempFolder, sub_path), sub.FullName, "#2");
270 Assert.IsTrue (Directory.Exists (sub.FullName), "#3");
272 DeleteDir (Path.Combine (TempFolder, sub_path));
277 public void CreateSubdirectory_Path_Null ()
279 DirectoryInfo info = new DirectoryInfo (TempFolder);
281 info.CreateSubdirectory (null);
283 } catch (ArgumentNullException ex) {
284 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
285 Assert.IsNull (ex.InnerException, "#3");
286 Assert.IsNotNull (ex.Message, "#4");
287 Assert.AreEqual ("path", ex.ParamName, "#5");
292 public void CreateSubdirectory_Path_Empty ()
294 DirectoryInfo info = new DirectoryInfo (TempFolder);
296 info.CreateSubdirectory (string.Empty);
298 } catch (ArgumentException ex) {
299 // Empty file name is not legal
300 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
301 Assert.IsNull (ex.InnerException, "#3");
302 Assert.IsNotNull (ex.Message, "#4");
307 public void Delete1 ()
309 string path = TempFolder + DSC + "DIT.Delete1.Test";
313 Directory.CreateDirectory (path);
314 DirectoryInfo info = new DirectoryInfo (path);
315 Assert.IsTrue (info.Exists, "#1");
318 Assert.IsTrue (info.Exists, "#2");
320 info = new DirectoryInfo (path);
321 Assert.IsFalse (info.Exists, "#3");
328 public void Delete1_DirectoryNotEmpty ()
330 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
334 Directory.CreateDirectory (path);
335 File.Create (path + DSC + "test").Close ();
336 DirectoryInfo info = new DirectoryInfo (path);
340 } catch (IOException ex) {
341 // The directory is not empty
342 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
343 Assert.IsNull (ex.InnerException, "#3");
344 Assert.IsNotNull (ex.Message, "#4");
351 [Test] // Delete (Boolean)
352 public void Delete2 ()
354 string path = TempFolder + DSC + "DIT.Delete2.Test";
358 Directory.CreateDirectory (path);
359 File.Create (path + DSC + "test").Close ();
360 DirectoryInfo info = new DirectoryInfo (path);
361 Assert.IsTrue (info.Exists, "#1");
364 Assert.IsTrue (info.Exists, "#2");
366 info = new DirectoryInfo (path);
367 Assert.IsFalse (info.Exists, "#3");
373 [Test] // Delete (Boolean)
374 public void Delete2_DirectoryNotEmpty ()
376 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
380 Directory.CreateDirectory (path);
381 File.Create (path + DSC + "test").Close ();
382 DirectoryInfo info = new DirectoryInfo (path);
386 } catch (IOException ex) {
387 // The directory is not empty
388 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
389 Assert.IsNull (ex.InnerException, "#3");
390 Assert.IsNotNull (ex.Message, "#4");
398 public void FullName ()
400 DirectoryInfo di = new DirectoryInfo ("something");
401 Assert.IsFalse (di.Exists, "#A1");
402 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "something"), di.FullName, "#A2");
404 di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
405 Assert.IsFalse (di.Exists, "#B1");
406 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#B2");
408 di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
409 Assert.IsFalse (di.Exists, "#C1");
410 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#C2");
413 di = new DirectoryInfo ("/");
414 Assert.AreEqual ("/", di.FullName, "#D1");
416 di = new DirectoryInfo ("test/");
417 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "test/"), di.FullName, "#D2");
419 di = new DirectoryInfo ("/test");
420 Assert.AreEqual ("/test", di.FullName, "#D3");
422 di = new DirectoryInfo ("/test/");
423 Assert.AreEqual ("/test/", di.FullName, "#D4");
425 Directory.SetCurrentDirectory (@"C:\");
427 di = new DirectoryInfo (@"c:");
428 Assert.AreEqual (@"C:\", di.FullName, "#D1");
430 di = new DirectoryInfo (@"c:\");
431 Assert.AreEqual (@"c:\", di.FullName, "#D2");
433 di = new DirectoryInfo (@"c:\test");
434 Assert.AreEqual (@"c:\test", di.FullName, "#D3");
436 di = new DirectoryInfo (@"c:\test\");
437 Assert.AreEqual (@"c:\test\", di.FullName, "#D4");
442 public void FullName_RootDirectory ()
444 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
446 // can't be sure of the root drive under windows
447 Assert.AreEqual ("/", di.FullName, "#1");
449 Assert.IsNull (di.Parent, "#2");
451 di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
453 // can't be sure of the root drive under windows
454 Assert.AreEqual ("/", di.FullName, "#3");
456 Assert.IsNull (di.Parent, "#4");
459 [Test] // GetDirectories ()
460 public void GetDirectories1 ()
462 string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
465 DirectoryInfo info = Directory.CreateDirectory (path);
466 Assert.AreEqual (0, info.GetDirectories ().Length, "#1");
468 Directory.CreateDirectory (path + DSC + "1");
469 Directory.CreateDirectory (path + DSC + "2");
470 File.Create (path + DSC + "filetest").Close ();
471 Assert.AreEqual (2, info.GetDirectories ().Length, "#2");
473 Directory.Delete (path + DSC + 2);
474 Assert.AreEqual (1, info.GetDirectories ().Length, "#3");
480 [Test] // GetDirectories ()
481 public void GetDirectories1_DirectoryDoesNotExist ()
483 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
487 DirectoryInfo info = new DirectoryInfo (path);
488 info.GetDirectories ();
490 } catch (DirectoryNotFoundException ex) {
491 // Could not find a part of '...'
492 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
493 Assert.IsNull (ex.InnerException, "#3");
494 Assert.IsNotNull (ex.Message, "#4");
495 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
501 [Test] // GetDirectories (String)
502 public void GetDirectories2 ()
504 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
507 DirectoryInfo info = Directory.CreateDirectory (path);
508 Assert.AreEqual (0, info.GetDirectories ("*").Length, "#1");
510 Directory.CreateDirectory (path + DSC + "test120");
511 Directory.CreateDirectory (path + DSC + "test210");
512 Directory.CreateDirectory (path + DSC + "atest330");
513 Directory.CreateDirectory (path + DSC + "test220");
514 Directory.CreateDirectory (path + DSC + "rest");
515 Directory.CreateDirectory (path + DSC + "rest" + DSC + "subdir");
516 File.Create (path + DSC + "filetest").Close ();
518 Assert.AreEqual (5, info.GetDirectories ("*").Length, "#2");
519 Assert.AreEqual (3, info.GetDirectories ("test*").Length, "#3");
520 Assert.AreEqual (2, info.GetDirectories ("test?20").Length, "#4");
521 Assert.AreEqual (0, info.GetDirectories ("test?").Length, "#5");
522 Assert.AreEqual (0, info.GetDirectories ("test[12]*").Length, "#6");
523 Assert.AreEqual (2, info.GetDirectories ("test2*0").Length, "#7");
524 Assert.AreEqual (4, info.GetDirectories ("*test*").Length, "#8");
525 Assert.AreEqual (6, info.GetDirectories ("*", SearchOption.AllDirectories).Length, "#9");
531 [Test] // GetDirectories (String)
532 public void GetDirectories2_DirectoryDoesNotExist ()
534 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
538 DirectoryInfo info = new DirectoryInfo (path);
539 info.GetDirectories ("*");
541 } catch (DirectoryNotFoundException ex) {
542 // Could not find a part of '...'
543 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
544 Assert.IsNull (ex.InnerException, "#3");
545 Assert.IsNotNull (ex.Message, "#4");
546 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
552 [Test] // GetDirectories (String)
553 public void GetDirectories2_SearchPattern_Null ()
555 DirectoryInfo info = new DirectoryInfo (TempFolder);
557 info.GetDirectories (null);
559 } catch (ArgumentNullException ex) {
560 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
561 Assert.IsNull (ex.InnerException, "#3");
562 Assert.IsNotNull (ex.Message, "#4");
563 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
567 [Test] //GetDirectories (String, SearchOptions)
568 public void GetDirectiories_SearchOptionAllDirectories ()
570 string directoryToBeLookedFor = "lookforme";
571 DirectoryInfo baseDir = Directory.CreateDirectory(Path.Combine(TempFolder, "GetDirectiories_SearchOptionAllDirectories"));
572 DirectoryInfo subdir = baseDir.CreateSubdirectory("subdir");
573 DirectoryInfo subsubdir = subdir.CreateSubdirectory(directoryToBeLookedFor);
574 DirectoryInfo[] directoriesFound = baseDir.GetDirectories(directoryToBeLookedFor, SearchOption.AllDirectories);
575 Assert.AreEqual(1, directoriesFound.Length, "There should be exactly one directory with the specified name.");
576 Assert.AreEqual(directoryToBeLookedFor, directoriesFound[0].Name, "The name of the directory found should match the expected one.");
579 [Test] // GetDirectories (String, SearchOption)
580 public void GetDirectories3_SearchPattern_Null ()
582 DirectoryInfo info = new DirectoryInfo (TempFolder);
584 info.GetDirectories (null, SearchOption.AllDirectories);
586 } catch (ArgumentNullException ex) {
587 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
588 Assert.IsNull (ex.InnerException, "#3");
589 Assert.IsNotNull (ex.Message, "#4");
590 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
594 [Test] // GetFiles ()
595 public void GetFiles1 ()
597 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
601 DirectoryInfo info = Directory.CreateDirectory (path);
602 Assert.AreEqual (0, info.GetFiles ().Length, "#1");
603 File.Create (path + DSC + "file1").Close ();
604 File.Create (path + DSC + "file2").Close ();
605 Directory.CreateDirectory (path + DSC + "directory1");
606 Assert.AreEqual (2, info.GetFiles ().Length, "#2");
612 [Test] // GetFiles ()
613 public void GetFiles1_DirectoryDoesNotExist ()
615 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
619 DirectoryInfo info = new DirectoryInfo (path);
622 } catch (DirectoryNotFoundException ex) {
623 // Could not find a part of '...'
624 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
625 Assert.IsNull (ex.InnerException, "#3");
626 Assert.IsNotNull (ex.Message, "#4");
627 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
633 [Test] // GetFiles (String)
634 public void GetFiles2 ()
636 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
640 DirectoryInfo info = Directory.CreateDirectory (path);
641 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1");
642 File.Create (path + DSC + "file120file").Close ();
643 File.Create (path + DSC + "file220file").Close ();
644 File.Create (path + DSC + "afile330file").Close ();
645 File.Create (path + DSC + "test.abc").Close ();
646 File.Create (path + DSC + "test.abcd").Close ();
647 File.Create (path + DSC + "test.abcdef").Close ();
648 Directory.CreateDirectory (path + DSC + "dir");
650 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2");
651 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3");
652 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4");
653 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5");
654 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6");
655 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7");
661 [Test] // GetFiles (String)
662 public void GetFiles2_DirectoryDoesNotExist ()
664 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
668 DirectoryInfo info = new DirectoryInfo (path);
671 } catch (DirectoryNotFoundException ex) {
672 // Could not find a part of '...'
673 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
674 Assert.IsNull (ex.InnerException, "#3");
675 Assert.IsNotNull (ex.Message, "#4");
676 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
682 [Test] // GetFiles (String)
683 public void GetFiles2_SearchPattern_Null ()
685 DirectoryInfo info = new DirectoryInfo (TempFolder);
687 info.GetFiles (null);
689 } catch (ArgumentNullException ex) {
690 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
691 Assert.IsNull (ex.InnerException, "#3");
692 Assert.IsNotNull (ex.Message, "#4");
693 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
697 [Test] // GetFiles (String, SearchOption)
698 public void GetFiles3_SearchPattern_Null ()
700 DirectoryInfo info = new DirectoryInfo (TempFolder);
702 info.GetFiles (null, SearchOption.TopDirectoryOnly);
704 } catch (ArgumentNullException ex) {
705 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
706 Assert.IsNull (ex.InnerException, "#3");
707 Assert.IsNotNull (ex.Message, "#4");
708 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
713 public void GetFileSystemInfos2_SearchPattern_Null ()
715 DirectoryInfo info = new DirectoryInfo (TempFolder);
717 info.GetFileSystemInfos (null);
719 } catch (ArgumentNullException ex) {
720 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
721 Assert.IsNull (ex.InnerException, "#3");
722 Assert.IsNotNull (ex.Message, "#4");
723 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
728 public void MoveTo ()
730 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
731 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
736 DirectoryInfo info1 = Directory.CreateDirectory (path1);
737 DirectoryInfo info2 = new DirectoryInfo (path2);
739 Assert.IsTrue (info1.Exists, "#A1");
740 Assert.IsFalse (info2.Exists, "#A2");
742 info1.MoveTo (path2);
743 Assert.IsTrue (info1.Exists, "#B1");
744 Assert.IsFalse (info2.Exists, "#B2");
746 info1 = new DirectoryInfo (path1);
747 info2 = new DirectoryInfo (path2);
748 Assert.IsFalse (info1.Exists, "#C1");
749 Assert.IsTrue (info2.Exists, "#C2");
757 public void MoveTo_DestDirName_Empty ()
759 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
763 DirectoryInfo info = Directory.CreateDirectory (path);
765 info.MoveTo (string.Empty);
767 } catch (ArgumentException ex) {
768 // Empty file name is not legal
769 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
770 Assert.IsNull (ex.InnerException, "#3");
771 Assert.IsNotNull (ex.Message, "#4");
772 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
780 public void MoveTo_DestDirName_Null ()
782 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
786 DirectoryInfo info = Directory.CreateDirectory (path);
790 } catch (ArgumentNullException ex) {
791 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
792 Assert.IsNull (ex.InnerException, "#3");
793 Assert.IsNotNull (ex.Message, "#4");
794 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
802 public void MoveTo_DestDirName_InvalidPathChars ()
804 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
808 DirectoryInfo info = Directory.CreateDirectory (path);
810 info.MoveTo (Path.InvalidPathChars [0].ToString ());
812 } catch (ArgumentException ex) {
813 // The path contains illegal characters
814 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
815 Assert.IsNull (ex.InnerException, "#3");
816 Assert.IsNotNull (ex.Message, "#4");
817 Assert.IsNull (ex.ParamName, "#5");
825 public void MoveTo_DestDirName_Whitespace ()
827 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
831 DirectoryInfo info = Directory.CreateDirectory (path);
835 } catch (ArgumentException ex) {
836 // The path is not of a legal form
837 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
838 Assert.IsNull (ex.InnerException, "#3");
839 Assert.IsNotNull (ex.Message, "#4");
840 Assert.IsNull (ex.ParamName, "#5");
848 public void MoveTo_SourceDest_NotDifferent ()
850 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
854 DirectoryInfo info = Directory.CreateDirectory (path);
858 } catch (IOException ex) {
859 // Source and destination path must be different
860 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
861 Assert.IsNull (ex.InnerException, "#A3");
862 Assert.IsNotNull (ex.Message, "#A4");
869 DirectoryInfo info = new DirectoryInfo (path);
873 } catch (IOException ex) {
874 // Source and destination path must be different
875 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
876 Assert.IsNull (ex.InnerException, "#B3");
877 Assert.IsNotNull (ex.Message, "#B4");
885 public void MoveTo_UpdateProperties ()
887 string path = TempFolder + DSC + "DIT.MoveToUpdateProperties.Test";
888 string path2 = TempFolder + DSC + "DIT.MoveToUpdateProperties2.Test";
889 string path3 = path2 + DSC + "DIT.MoveToUpdateProperties3.Test";
891 Directory.CreateDirectory (path);
892 Directory.CreateDirectory (path2);
894 DirectoryInfo info = new DirectoryInfo(path);
896 Assert.IsTrue (Directory.Exists(info.FullName));
897 Assert.AreEqual (path, info.FullName);
898 Assert.AreEqual ("DIT.MoveToUpdateProperties.Test", info.Name);
899 Assert.AreEqual (TempFolder, info.Parent.FullName);
900 Assert.AreEqual (path, info.ToString ());
903 Assert.IsTrue (Directory.Exists(info.FullName));
904 Assert.AreEqual (path3, info.FullName);
905 Assert.AreEqual ("DIT.MoveToUpdateProperties3.Test", info.Name);
906 Assert.AreEqual (path2, info.Parent.FullName);
907 Assert.AreEqual (path3, info.ToString ());
911 public void DirectoryNameWithSpace ()
913 DeleteDir ("this has a space at the end ");
914 string path = Path.Combine (TempFolder, "this has a space at the end ");
915 Directory.CreateDirectory (path);
916 DirectoryInfo i = new DirectoryInfo (path);
918 foreach (FileInfo f in i.GetFiles ())
923 public void LastWriteTime ()
925 DirectoryInfo info = new DirectoryInfo (TempFolder);
926 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
928 DateTime time = Directory.GetLastWriteTime (TempFolder);
929 Assert.AreEqual (2003, time.Year, "#A1");
930 Assert.AreEqual (6, time.Month, "#A2");
931 Assert.AreEqual (4, time.Day, "#A3");
932 Assert.AreEqual (6, time.Hour, "#A4");
933 Assert.AreEqual (4, time.Minute, "#A5");
934 Assert.AreEqual (0, time.Second, "#A6");
936 time = TimeZone.CurrentTimeZone.ToLocalTime (
937 Directory.GetLastWriteTimeUtc (TempFolder));
938 Assert.AreEqual (2003, time.Year, "#B1");
939 Assert.AreEqual (6, time.Month, "#B2");
940 Assert.AreEqual (4, time.Day, "#B3");
941 Assert.AreEqual (6, time.Hour, "#B4");
942 Assert.AreEqual (4, time.Minute, "#B5");
943 Assert.AreEqual (0, time.Second, "#B6");
947 public void LastWriteTimeUtc ()
949 DirectoryInfo info = new DirectoryInfo (TempFolder);
950 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
952 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
953 Directory.GetLastWriteTime (TempFolder));
954 Assert.AreEqual (2003, time.Year, "#A1");
955 Assert.AreEqual (6, time.Month, "#A2");
956 Assert.AreEqual (4, time.Day, "#A3");
957 Assert.AreEqual (6, time.Hour, "#A4");
958 Assert.AreEqual (4, time.Minute, "#A5");
959 Assert.AreEqual (0, time.Second, "#A6");
961 time = Directory.GetLastWriteTimeUtc (TempFolder);
962 Assert.AreEqual (2003, time.Year, "#B1");
963 Assert.AreEqual (6, time.Month, "#B2");
964 Assert.AreEqual (4, time.Day, "#B3");
965 Assert.AreEqual (6, time.Hour, "#B4");
966 Assert.AreEqual (4, time.Minute, "#B5");
967 Assert.AreEqual (0, time.Second, "#B6");
971 public void LastAccessTime ()
973 DirectoryInfo info = new DirectoryInfo (TempFolder);
974 info.LastAccessTime = DateTime.Now;
978 public void LastAccessTimeUtc ()
980 DirectoryInfo info = new DirectoryInfo (TempFolder);
981 info.LastAccessTimeUtc = DateTime.Now;
985 public void CreationTime ()
987 DirectoryInfo info = new DirectoryInfo (TempFolder);
988 info.CreationTime = DateTime.Now;
992 public void CreationTimeUtc ()
994 DirectoryInfo info = new DirectoryInfo (TempFolder);
995 info.CreationTimeUtc = DateTime.Now;
999 public void Name_Bug76903 ()
1001 CheckName ("/usr/share");
1002 CheckName ("/usr/share/");
1003 CheckName ("/usr/share/.");
1004 CheckName ("/usr/share/./");
1005 CheckName ("/usr/share/blabla/../");
1006 CheckName ("/usr/lib/../share/.");
1010 public void Hang_76191 ()
1012 // from bug #76191 (hangs on Windows)
1013 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
1014 Stack s = new Stack ();
1016 while (di.Parent != null) {
1020 while (s.Count > 0) {
1021 di = (DirectoryInfo) s.Pop ();
1022 Assert.IsTrue (di.Exists, di.Name);
1027 public void WindowsSystem32_76191 ()
1030 Assert.Ignore ("Running on Unix.");
1032 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
1033 WindowsParentFullName ("C:", "C:\\WINDOWS");
1034 WindowsParentFullName ("C:\\", null);
1035 WindowsParentFullName ("C:\\dir", "C:\\");
1036 WindowsParentFullName ("C:\\dir\\", "C:\\");
1037 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1038 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1042 public void Parent_Bug77090 ()
1044 DirectoryInfo di = new DirectoryInfo ("/home");
1045 if (Path.DirectorySeparatorChar == '\\') {
1046 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1048 Assert.AreEqual ("/", di.Parent.Name, "#1");
1049 Assert.IsNull (di.Parent.Parent, "#2");
1053 public void ToStringTest ()
1057 info = new DirectoryInfo ("Test");
1058 Assert.AreEqual ("Test", info.ToString (), "#1");
1059 info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1060 Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1064 public void EnumerateFileSystemInfosTest ()
1066 var dirInfo = new DirectoryInfo (TempFolder);
1067 dirInfo.CreateSubdirectory ("1").CreateSubdirectory ("a");
1068 dirInfo.CreateSubdirectory ("2").CreateSubdirectory ("b");
1070 var l = new List<string> ();
1071 foreach (var info in dirInfo.EnumerateFileSystemInfos ("*", SearchOption.AllDirectories))
1075 Assert.AreEqual ("1,2,a,b", string.Join (",", l), "#1");
1080 public void Serialization ()
1083 SerializationInfo si;
1085 info = new DirectoryInfo ("Test");
1086 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1087 info.GetObjectData (si, new StreamingContext ());
1089 Assert.AreEqual (2, si.MemberCount, "#A1");
1090 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1091 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1093 info = new DirectoryInfo (TempFolder);
1094 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1095 info.GetObjectData (si, new StreamingContext ());
1097 Assert.AreEqual (2, si.MemberCount, "#B1");
1098 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1099 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1103 public void Deserialization ()
1105 DirectoryInfo info = new DirectoryInfo ("Test");
1107 MemoryStream ms = new MemoryStream ();
1108 BinaryFormatter bf = new BinaryFormatter ();
1109 bf.Serialize (ms, info);
1112 DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1113 Assert.AreEqual (info.Name, clone.Name, "#1");
1114 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1117 // Needed so that UnixSymbolicLinkInfo doesn't have to
1118 // be JITted on windows
1119 private void Symlink_helper ()
1121 string path = TempFolder + DSC + "DIT.Symlink";
1122 string dir = path + DSC + "dir";
1123 string link = path + DSC + "link";
1128 Directory.CreateDirectory (path);
1129 Directory.CreateDirectory (dir);
1130 global::Mono.Unix.UnixSymbolicLinkInfo li = new global::Mono.Unix.UnixSymbolicLinkInfo (link);
1131 li.CreateSymbolicLinkTo (dir);
1133 DirectoryInfo info = new DirectoryInfo (path);
1134 DirectoryInfo[] dirs = info.GetDirectories ();
1135 Assert.AreEqual (2, dirs.Length, "#1");
1142 [Category ("NotDotNet")]
1143 public void Symlink ()
1145 // This test only applies to Linux and
1146 // Linux-like platforms but mono-on-windows
1147 // doesn't set the NotDotNet category
1148 if (!RunningOnUnix) {
1149 Assert.Ignore ("Not running on Unix.");
1155 static bool RunningOnUnix {
1157 int p = (int) Environment.OSVersion.Platform;
1158 return ((p == 4) || (p == 128) || (p == 6));
1162 void WindowsParentFullName (string name, string expected)
1164 DirectoryInfo di = new DirectoryInfo (name);
1165 if (di.Parent == null)
1166 Assert.IsNull (expected, name);
1168 Assert.AreEqual (expected, di.Parent.FullName, name);
1171 void CheckName (string name)
1173 DirectoryInfo di = new DirectoryInfo (name);
1174 Assert.AreEqual ("share", di.Name, name + ".Name");
1175 Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1178 void DeleteDir (string path)
1180 if (Directory.Exists (path))
1181 Directory.Delete (path, true);