* Mono.Posix/Mono.Posix_test.dll.sources: Add Mono.Unix/UnixPathTest.cs.
authorJonathan Pryor <jpryor@novell.com>
Sun, 2 Jul 2006 19:48:13 +0000 (19:48 -0000)
committerJonathan Pryor <jpryor@novell.com>
Sun, 2 Jul 2006 19:48:13 +0000 (19:48 -0000)
* Mono.Posix/Mono.Unix/UnixPath.cs: Follow .NET's System.IO.Path.Combine()
  behavior, and "reset" the generated path if any argument beings with a
  path root -- UnixPath.Combine("/a", "/b") should return "/b", not "/a/b".
  Fixes #78580.
* Mono.Posix/Test/Mono.Unix/UnixPathTest.cs: Added; test UnixPath.Combine().

svn path=/trunk/mcs/; revision=62191

mcs/class/Mono.Posix/ChangeLog
mcs/class/Mono.Posix/Mono.Posix_test.dll.sources
mcs/class/Mono.Posix/Mono.Unix/ChangeLog
mcs/class/Mono.Posix/Mono.Unix/UnixPath.cs
mcs/class/Mono.Posix/Test/Mono.Unix/ChangeLog
mcs/class/Mono.Posix/Test/Mono.Unix/UnixPathTest.cs [new file with mode: 0644]

index 2d076430916f61f00d34f441a7ac5f95a28a84f4..f7b8af0ba10cc52520781d68416538110d97e905 100644 (file)
@@ -1,3 +1,7 @@
+2006-07-02  Jonathan Pryor  <jonpryor@vt.edu>
+
+       * Mono.Posix_test.dll.sources: Add Mono.Unix/UnixPathTest.cs.
+
 2006-01-02  Jonathan Pryor  <jonpryor@vt.edu>
 
        * Mono.Posix.dll.sources: Remove Mono.Unix obsolete files.
index b52b86b2738f496ea57683b4ba3ad9846dfb0596..5abb28113f2f6a52e06dadcbdb85c03ee3accd6b 100644 (file)
@@ -2,5 +2,6 @@ Mono.Unix/StdioFileStreamTest.cs
 Mono.Unix/UnixEncodingTest.cs
 Mono.Unix/UnixGroupTest.cs
 Mono.Unix/UnixMarshalTest.cs
+Mono.Unix/UnixPathTest.cs
 Mono.Unix/UnixUserTest.cs
 Mono.Unix.Native/StdlibTest.cs
index d291c29d4b56d66934f5c45939cfbd7f2cada6ea..6be319eb7398aa9b3f32595add068af1294d6afe 100644 (file)
@@ -1,3 +1,9 @@
+2006-07-02  Jonathan Pryor  <jonpryor@vt.edu>
+
+       * UnixPath.cs: Follow .NET's System.IO.Path.Combine() behavior, and "reset"
+         the generated path if any argument beings with a path root --
+         UnixPath.Combine("/a", "/b") should return "/b", not "/a/b".  Fixes #78580.
+
 2006-04-21  Jonathan Pryor  <jonpryor@vt.edu>
 
        * UnixFileSystemInfo.cs: rename Create() to GetFileSystemEntry(), and make
index a7836db0bf2528f28d40338a57ce69cbe3f4d1f3..033212b6d4091ad70b1971cdefad9bde3a737bb7 100644 (file)
@@ -58,29 +58,39 @@ namespace Mono.Unix {
                        if (path1.IndexOfAny (_InvalidPathChars) != -1)
                                throw new ArgumentException ("Illegal characters in path", "path1");
 
-                       int len = path1.Length + 1;
+                       int len = path1.Length;
+                       int start = -1;
                        for (int i = 0; i < paths.Length; ++i) {
                                if (paths [i] == null)
-                                       throw new ArgumentNullException ("paths");
+                                       throw new ArgumentNullException ("paths[" + i + "]");
+                               if (paths [i].IndexOfAny (_InvalidPathChars) != -1)
+                                       throw new ArgumentException ("Illegal characters in path", "paths[" + i + "]");
+                               if (IsPathRooted (paths [i])) {
+                                       len = 0;
+                                       start = i;
+                               }
                                len += paths [i].Length + 1;
                        }
 
                        StringBuilder sb = new StringBuilder (len);
-                       sb.Append (path1);
-                       for (int i = 0; i < paths.Length; ++i)
+                       if (start == -1) {
+                               sb.Append (path1);
+                               start = 0;
+                       }
+                       for (int i = start; i < paths.Length; ++i)
                                Combine (sb, paths [i]);
                        return sb.ToString ();
                }
 
                private static void Combine (StringBuilder path, string part)
                {
-                       if (part.IndexOfAny (_InvalidPathChars) != -1)
-                               throw new ArgumentException ("Illegal characters in path", "path1");
-                       char end = path [path.Length-1];
-                       if (end != DirectorySeparatorChar && 
-                                       end != AltDirectorySeparatorChar && 
-                                       end != VolumeSeparatorChar)
-                               path.Append (DirectorySeparatorChar);
+                       if (path.Length > 0 && part.Length > 0) {
+                               char end = path [path.Length-1];
+                               if (end != DirectorySeparatorChar && 
+                                               end != AltDirectorySeparatorChar && 
+                                               end != VolumeSeparatorChar)
+                                       path.Append (DirectorySeparatorChar);
+                       }
                        path.Append (part);
                }
 
index b6a2d464566f3e57bdca84c87c8b0c0a5385c68c..55a01b26ab5a6796a70c3361486539e32e74a718 100644 (file)
@@ -1,3 +1,7 @@
+2006-07-02  Jonathan Pryor  <jonpryor@vt.edu>
+
+       * UnixPathTest.cs: Added; test UnixPath.Combine().
+
 2005-12-07  Jonathan Pryor  <jonpryor@vt.edu>
 
        * UnixMarshalTest.cs: Make test public so that it's actually executed by
diff --git a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixPathTest.cs b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixPathTest.cs
new file mode 100644 (file)
index 0000000..5c288de
--- /dev/null
@@ -0,0 +1,86 @@
+//
+// Mono.Unix.UnixPath Test Cases
+//
+// Authors:
+//  Jonathan Pryor (jonpryor@vt.edu)
+//
+// (c) 2006 Jonathan Pryor
+//
+
+using NUnit.Framework;
+using System.IO;
+using System;
+using System.Text;
+using Mono.Unix;
+
+namespace MonoTests.Mono.Unix
+{
+
+       [TestFixture, Category ("NotDotNet")]
+       public class UnixPathTest {
+
+               private static readonly char DSC = UnixPath.DirectorySeparatorChar;
+
+               [Test]
+               public void Combine ()
+               {
+                       string path, expected;
+                       string current = UnixDirectoryInfo.GetCurrentDirectory ();
+
+                       path = UnixPath.Combine ("/etc", "init.d");
+                       Assert.AreEqual ("/etc/init.d", path);
+
+                       path = UnixPath.Combine ("one", "");
+                       Assert.AreEqual ("one", path);
+
+                       path = UnixPath.Combine ("", "one");
+                       Assert.AreEqual ("one", path);
+
+                       path = UnixPath.Combine (current, "one");
+                       expected = current + DSC + "one";
+                       Assert.AreEqual (expected, path);
+
+                       path = UnixPath.Combine ("one", current);
+                       Assert.AreEqual (current, path);
+
+                       path = UnixPath.Combine (current, expected);
+                       Assert.AreEqual (expected, path);
+
+                       path = DSC + "one";
+                       path = UnixPath.Combine (path, "two" + DSC);
+                       expected = DSC + "one" + DSC + "two" + DSC;
+                       Assert.AreEqual (expected, path);
+
+                       path = "one" + DSC;
+                       path = UnixPath.Combine (path, DSC + "two");
+                       expected = DSC + "two";
+                       Assert.AreEqual (expected, path);
+
+                       path = "one" + DSC;
+                       path = UnixPath.Combine (path, "two" + DSC);
+                       expected = "one" + DSC + "two" + DSC;
+                       Assert.AreEqual (expected, path);
+
+                       path = UnixPath.Combine ("/a", "b", "c", "/d", "e");
+                       expected = "/d/e";
+                       Assert.AreEqual (expected, path);
+
+                       try {
+                               path = UnixPath.Combine ("one", null);
+                               Assert.Fail ("Combine Fail #01");
+                       }
+                       catch (Exception e) {
+                               Assert.AreEqual (typeof (ArgumentNullException), e.GetType ());
+                       }
+
+                       try {
+                               path = UnixPath.Combine (null, "one");
+                               Assert.Fail ("Combine Fail #02");
+                       }
+                       catch (Exception e) {
+                               Assert.AreEqual (typeof (ArgumentNullException), e.GetType ());
+                       }
+               }
+       }
+}
+