// 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
public void FullName ()
{
DirectoryInfo di = new DirectoryInfo ("something");
+ Assert ("Exists", !di.Exists);
Assert ("FullName", di.FullName.EndsWith ("something"));
di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
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
public void GetDirectories1 ()\r
{\r
DirectoryInfo info = new DirectoryInfo (TempFolder);\r
info.CreationTimeUtc = DateTime.Now;\r
- }
- }\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