Skip to content

Commit

Permalink
Several fixes for Path (#99)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Apr 17, 2024
1 parent 6081b44 commit 4d587f9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
12 changes: 5 additions & 7 deletions System.IO.FileSystem.UnitTests/PathUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace System.IO.FileSystem.UnitTests
[TestClass]
internal class PathUnitTests
{
private string RootDirectory => "\\";

[TestMethod]
public void ChangeExtension_adds_extension()
{
Expand Down Expand Up @@ -318,21 +316,21 @@ public void GetFullPath0()
{
string fullPath = Path.GetFullPath(@"dir1\dir2\file.ext");

Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
Assert.AreEqual(@"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void GetFullPath1()
{
string fullPath = Path.GetFullPath(RootDirectory + @"\dir1\dir2\file.ext");
Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
string fullPath = Path.GetFullPath(@"\dir1\dir2\file.ext");
Assert.AreEqual(@"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void GetFullPath2()
{
string fullPath = Path.GetFullPath(RootDirectory + @"\dir1\..\dir2\file.ext");
Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
string fullPath = Path.GetFullPath(@"\dir1\..\dir2\file.ext");
Assert.AreEqual(@"\dir1\..\dir2\file.ext", fullPath);
}

[TestMethod]
Expand Down
40 changes: 28 additions & 12 deletions System.IO.FileSystem/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,22 @@ public static string Combine(string path1, string path2)
throw new ArgumentNullException();
}

return CombineInternal(path1, path2);
}
if (string.IsNullOrEmpty(path1))
{
return path2;
}

private static string CombineInternal(string first, string second)
{
ValidateNullOrEmpty(first);
ValidateNullOrEmpty(second);
if (string.IsNullOrEmpty(path2))
{
return path1;
}

if (IsPathRooted(second))
if (IsPathRooted(path2))
{
return second;
return path2;
}

return JoinInternal(first, second);
return JoinInternal(path1, path2);
}

/// <summary>
Expand Down Expand Up @@ -186,7 +188,10 @@ internal static int GetDirectoryNameOffset(string path)
[return: NotNullIfNotNull("path")]
public static string GetExtension(string path)
{
ValidateNullOrEmpty(path);
if (path is null)
{
return null;
}

var length = path.Length;

Expand Down Expand Up @@ -225,7 +230,10 @@ public static string GetExtension(string path)
[return: NotNullIfNotNull("path")]
public static string GetFileName(string path)
{
ValidateNullOrEmpty(path);
if (path is null)
{
return null;
}

var root = GetPathRoot(path).Length;

Expand All @@ -251,7 +259,10 @@ public static string GetFileName(string path)
[return: NotNullIfNotNull("path")]
public static string GetFileNameWithoutExtension(string path)
{
ValidateNullOrEmpty(path);
if (path is null)
{
return null;
}

var fileName = GetFileName(path);
var lastPeriod = fileName.LastIndexOf('.');
Expand Down Expand Up @@ -393,6 +404,11 @@ private static string JoinInternal(string first, string second)
var hasSeparator = PathInternal.IsDirectorySeparator(first[first.Length - 1])
|| PathInternal.IsDirectorySeparator(second[0]);

if (first.Equals(PathInternal.DirectorySeparatorCharAsString))
{
first = string.Empty;
}

return hasSeparator ?
string.Concat(first, second) :
string.Concat(first, PathInternal.DirectorySeparatorCharAsString, second);
Expand Down

0 comments on commit 4d587f9

Please sign in to comment.