Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions PowerKit.Tests/Extensions/PathExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public void GetInvalidPathChars_Test()
Path.GetInvalidPathChars(false).Should().BeEquivalentTo(Path.GetInvalidPathChars());
}

[Fact]
public void Normalize_Test()
{
// Act & assert
Path.Normalize("/foo/bar/").Should().Be(Path.GetFullPath("/foo/bar"));
Path.Normalize("/foo/./bar").Should().Be(Path.GetFullPath("/foo/bar"));
Path.Normalize("/foo/baz/../bar").Should().Be(Path.GetFullPath("/foo/bar"));
Path.Normalize("/foo/bar").Should().Be(Path.GetFullPath("/foo/bar"));
}

[Fact]
public void AreEqual_Test()
{
// Act & assert
Path.AreEqual(null, null).Should().BeTrue();
Path.AreEqual(null, "/foo").Should().BeFalse();
Path.AreEqual("/foo", null).Should().BeFalse();
Path.AreEqual("/foo/bar", "/foo/bar").Should().BeTrue();
Path.AreEqual("/foo/bar", "/foo/bar/").Should().BeTrue();
Path.AreEqual("/foo/./bar", "/foo/bar").Should().BeTrue();
Path.AreEqual("/foo/baz/../bar", "/foo/bar").Should().BeTrue();
Path.AreEqual("/foo/bar", "/foo/baz").Should().BeFalse();
Comment thread
Copilot marked this conversation as resolved.
}

[Fact]
public void EscapeFileName_Test()
{
Expand Down
28 changes: 28 additions & 0 deletions PowerKit/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -89,6 +90,33 @@ public static char[] GetInvalidFileNameChars(bool crossPlatform) =>
public static char[] GetInvalidPathChars(bool crossPlatform) =>
crossPlatform ? PathEx.CrossPlatformInvalidPathChars : Path.GetInvalidPathChars();

/// <summary>
/// Returns a normalized representation of the specified path by resolving
/// <c>.</c> and <c>..</c> segments and removing trailing directory separators.
/// </summary>
public static string Normalize(string path) =>
Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
Comment thread
Tyrrrz marked this conversation as resolved.

/// <summary>
/// Compares two paths for equality, normalizing them first and using
/// the path comparison rules of the current operating system
/// (case-insensitive on Windows, case-sensitive on other platforms).
/// </summary>
public static bool AreEqual(string? path1, string? path2)
{
if (path1 is null && path2 is null)
return true;

if (path1 is null || path2 is null)
return false;

var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;

return string.Equals(Path.Normalize(path1), Path.Normalize(path2), comparison);
}

/// <summary>
/// Replaces invalid file name characters with underscores and strips trailing dots and whitespace.
/// When <paramref name="crossPlatform" /> is <see langword="true" />, considers characters
Expand Down