// DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class\r
//\r
-// Ville Palo (vi64pa@koti.soon.fi)\r
+// Authors\r
+// Ville Palo (vi64pa@koti.soon.fi)\r
+// Sebastien Pouliot <sebastien@ximian.com>\r
// \r
// (C) 2003 Ville Palo\r
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)\r
// \r
+\r
using NUnit.Framework;\r
using System;\r
+using System.Collections;\r
using System.IO;\r
+\r
namespace MonoTests.System.IO\r
{\r
[TestFixture]\r
string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");\r
\r
static readonly char DSC = Path.DirectorySeparatorChar;\r
+ string current;\r
\r
[SetUp]\r
- protected void SetUp() {\r
+ protected void SetUp ()\r
+ {\r
+ current = Directory.GetCurrentDirectory ();\r
if (Directory.Exists (TempFolder))\r
Directory.Delete (TempFolder, true);\r
Directory.CreateDirectory (TempFolder);\r
}\r
\r
[TearDown]\r
- protected void TearDown() {\r
+ protected void TearDown ()\r
+ {\r
if (Directory.Exists (TempFolder))\r
Directory.Delete (TempFolder, true);\r
+ Directory.SetCurrentDirectory (current);\r
}\r
\r
[Test]\r
DeleteDir (Path.Combine (TempFolder, sub_path));\r
}\r
\r
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentException))]
+ public void CreateSubdirectoryEmptyString ()
+ {
+ new DirectoryInfo (".").CreateSubdirectory ("");
}\r
\r
[Test]\r
} finally {\r
DeleteDir (path);\r
}\r
+ }
+
+ [Test]
+ // from bug #75443
+ public void FullName ()
+ {
+ DirectoryInfo di = new DirectoryInfo ("something");
+ Assert ("Exists", !di.Exists);
+ Assert ("FullName", di.FullName.EndsWith ("something"));
+
+ di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
+ AssertEquals ("DirectorySeparatorChar", Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1]);
+
+ di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
+ AssertEquals ("AltDirectorySeparatorChar", Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1]);
+ }\r
+
+ [Test]
+ public void FullName_RootDirectory ()
+ {
+ DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
+ if (Path.DirectorySeparatorChar == '/') {
+ // can't be sure of the root drive under windows
+ AssertEquals ("FullName", "/", di.FullName);
+ }
+ AssertNull ("Parent", di.Parent);
+
+ di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
+ if (Path.DirectorySeparatorChar == '/') {
+ // can't be sure of the root drive under windows
+ AssertEquals ("FullName-Alt", "/", di.FullName);
+ }
+ AssertNull ("Parent-Alt", di.Parent);
}\r
\r
[Test]\r
if (Directory.Exists (path))\r
Directory.Delete (path, true);\r
}\r
- \r
- }\r
+
+ [Test]
+ public void DirectoryNameWithSpace ()
+ {
+ // check for Unix platforms - see FAQ for more details
+ // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
+ int platform = (int) Environment.OSVersion.Platform;
+ if ((platform == 4) || (platform == 128)) {
+ DeleteDir ("this has a space at the end ");
+ string path = Path.Combine (TempFolder, "this has a space at the end ");
+ Directory.CreateDirectory (path);
+ DirectoryInfo i = new DirectoryInfo (path);
+ string dummy = null;
+ foreach (FileInfo f in i.GetFiles ()) // This used to throw
+ dummy = f.Name;
+ }
+ }\r
+\r
+ [Test]\r
+ public void LastWriteTime ()\r
+ {\r
+ DirectoryInfo info = new DirectoryInfo (TempFolder);\r
+ info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);\r
+\r
+ DateTime time = Directory.GetLastWriteTime (TempFolder);\r
+ AssertEquals ("test#01", 2003, time.Year);\r
+ AssertEquals ("test#02", 6, time.Month);\r
+ AssertEquals ("test#03", 4, time.Day);\r
+ AssertEquals ("test#04", 6, time.Hour);\r
+ AssertEquals ("test#05", 4, time.Minute);\r
+ AssertEquals ("test#06", 0, time.Second);\r
+\r
+ time = TimeZone.CurrentTimeZone.ToLocalTime (\r
+ Directory.GetLastWriteTimeUtc (TempFolder));\r
+ AssertEquals ("test#07", 2003, time.Year);\r
+ AssertEquals ("test#08", 6, time.Month);\r
+ AssertEquals ("test#09", 4, time.Day);\r
+ AssertEquals ("test#10", 6, time.Hour);\r
+ AssertEquals ("test#11", 4, time.Minute);\r
+ AssertEquals ("test#12", 0, time.Second);\r
+ }\r
+\r
+ [Test]\r
+ public void LastWriteTimeUtc ()\r
+ {\r
+ DirectoryInfo info = new DirectoryInfo (TempFolder);\r
+ info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);\r
+\r
+ DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (\r
+ Directory.GetLastWriteTime (TempFolder));\r
+ AssertEquals ("test#1", 2003, time.Year);\r
+ AssertEquals ("test#2", 6, time.Month);\r
+ AssertEquals ("test#3", 4, time.Day);\r
+ AssertEquals ("test#4", 6, time.Hour);\r
+ AssertEquals ("test#5", 4, time.Minute);\r
+ AssertEquals ("test#6", 0, time.Second);\r
+\r
+ time = Directory.GetLastWriteTimeUtc (TempFolder);\r
+ AssertEquals ("test#7", 2003, time.Year);\r
+ AssertEquals ("test#8", 6, time.Month);\r
+ AssertEquals ("test#9", 4, time.Day);\r
+ AssertEquals ("test#10", 6, time.Hour);\r
+ AssertEquals ("test#11", 4, time.Minute);\r
+ AssertEquals ("test#12", 0, time.Second);\r
+ }\r
+\r
+ [Test]\r
+ public void LastAccessTime ()\r
+ {\r
+ DirectoryInfo info = new DirectoryInfo (TempFolder);\r
+ info.LastAccessTime = DateTime.Now;\r
+ }\r
+\r
+ [Test]\r
+ public void LastAccessTimeUtc ()\r
+ {\r
+ DirectoryInfo info = new DirectoryInfo (TempFolder);\r
+ info.LastAccessTimeUtc = DateTime.Now;\r
+ }\r
+\r
+ [Test]\r
+ public void CreationTime ()\r
+ {\r
+ DirectoryInfo info = new DirectoryInfo (TempFolder);\r
+ info.CreationTime = DateTime.Now;\r
+ }\r
+\r
+ [Test]\r
+ public void CreationTimeUtc ()\r
+ {\r
+ DirectoryInfo info = new DirectoryInfo (TempFolder);\r
+ info.CreationTimeUtc = DateTime.Now;\r
+ }\r
+\r
+\r
+ private void CheckName (string name)\r
+ {\r
+ DirectoryInfo di = new DirectoryInfo (name);\r
+ AssertEquals (name + ".Name", "share", di.Name);\r
+ AssertEquals (name + ".Parent.Name", "usr", di.Parent.Name);\r
+ }\r
+\r
+ [Test]\r
+ public void Name_Bug76903 ()\r
+ {\r
+ CheckName ("/usr/share");\r
+ CheckName ("/usr/share/");\r
+ CheckName ("/usr/share/.");\r
+ CheckName ("/usr/share/./");\r
+ CheckName ("/usr/share/blabla/../");\r
+ CheckName ("/usr/lib/../share/.");\r
+ }\r
+\r
+ [Test]\r
+ public void Hang_76191 ()\r
+ {\r
+ // from bug #76191 (hangs on Windows)\r
+ DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);\r
+ Stack s = new Stack ();\r
+ s.Push (di);\r
+ while (di.Parent != null) {\r
+ di = di.Parent;\r
+ s.Push (di);\r
+ }\r
+ while (s.Count > 0) {\r
+ di = (DirectoryInfo) s.Pop ();\r
+ Assert (di.Name, di.Exists);\r
+ }\r
+ }\r
+\r
+ private void WindowsParentFullName (string name, string expected)\r
+ {\r
+ DirectoryInfo di = new DirectoryInfo (name);\r
+ if (di.Parent == null)\r
+ AssertNull (name, expected);\r
+ else\r
+ AssertEquals (name, expected, di.Parent.FullName);\r
+ }\r
+\r
+ [Test]\r
+ public void WindowsSystem32_76191 ()\r
+ {\r
+ // check for Unix platforms - see FAQ for more details\r
+ // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F\r
+ int platform = (int) Environment.OSVersion.Platform;\r
+ if ((platform == 4) || (platform == 128))\r
+ return;\r
+\r
+ Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");\r
+ WindowsParentFullName ("C:", "C:\\WINDOWS");\r
+ WindowsParentFullName ("C:\\", null);\r
+ WindowsParentFullName ("C:\\dir", "C:\\");\r
+ WindowsParentFullName ("C:\\dir\\", "C:\\");\r
+ WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");\r
+ WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");\r
+ }
+
+ [Test]\r
+ public void Parent_Bug77090 ()\r
+ {
+ DirectoryInfo di = new DirectoryInfo ("/home");\r
+ if (Path.DirectorySeparatorChar == '\\') {\r
+ Assert ("/home parent (Windows path)", di.Parent.Name.EndsWith (":\\"));\r
+ }\r
+ else\r
+ AssertEquals ("/home parent", "/", di.Parent.Name);\r
+ AssertNull ("/home parent parent", di.Parent.Parent);\r
+ }\r
+ }\r
}\r