Skip to content

Commit

Permalink
Refactor implementation (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Apr 20, 2024
1 parent c7c20f7 commit ebe88ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
25 changes: 15 additions & 10 deletions Source/Testably.Abstractions.Testing/Helpers/Execute.LinuxPath.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Text;
using System.Text;

namespace Testably.Abstractions.Testing.Helpers;

Expand Down Expand Up @@ -69,18 +68,23 @@ protected override bool IsEffectivelyEmpty(string path)
/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Unix.cs#L39
/// </summary>
protected override string? NormalizeDirectorySeparators(string? path)
protected override string NormalizeDirectorySeparators(string path)
{
if (string.IsNullOrEmpty(path))
bool IsAlreadyNormalized()
{
return path;
for (int i = 0; i < path.Length - 1; i++)
{
if (IsDirectorySeparator(path[i]) &&
IsDirectorySeparator(path[i + 1]))
{
return false;
}
}

return true;
}

bool isAlreadyNormalized = Enumerable
.Range(0, path.Length - 1)
.All(i => !IsDirectorySeparator(path[i]) ||
!IsDirectorySeparator(path[i + 1]));
if (isAlreadyNormalized)
if (IsAlreadyNormalized())
{
return path;
}
Expand All @@ -100,6 +104,7 @@ protected override bool IsEffectivelyEmpty(string path)
builder.Append(current);
}

builder.Append(path[path.Length - 1]);
return builder.ToString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,7 @@ private string JoinInternal(string?[] paths)
}
#endif

[return: NotNullIfNotNull(nameof(path))]
protected abstract string? NormalizeDirectorySeparators(string? path);
protected abstract string NormalizeDirectorySeparators(string path);

protected string RandomString(int length)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -201,8 +200,7 @@ private static bool IsValidDriveChar(char value)
/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L318
/// </summary>
[return: NotNullIfNotNull(nameof(path))]
protected override string? NormalizeDirectorySeparators(string? path)
protected override string NormalizeDirectorySeparators(string path)
{
bool IsAlreadyNormalized()
{
Expand All @@ -220,7 +218,7 @@ bool IsAlreadyNormalized()
return true;
}

if (string.IsNullOrEmpty(path) || IsAlreadyNormalized())
if (IsAlreadyNormalized())
{
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public void GetDirectoryName_Spaces_ShouldReturnNullOnWindowsOtherwiseEmpty(stri
[SkippableTheory]
[InlineData("\t")]
[InlineData("\n")]
[InlineData(" \t")]
[InlineData("\n ")]
public void GetDirectoryName_TabOrNewline_ShouldReturnEmptyString(string? path)
{
string? result = FileSystem.Path.GetDirectoryName(path);
Expand All @@ -81,7 +83,7 @@ public void GetDirectoryName_ShouldReturnDirectory(

[SkippableTheory]
[AutoData]
public void GetDirectoryName_ShouldNormalizeDirectorySeparators(
public void GetDirectoryName_ShouldReplaceAltDirectorySeparator(
string parentDirectory, string directory, string filename)
{
string path = parentDirectory + FileSystem.Path.AltDirectorySeparatorChar + directory +
Expand All @@ -93,6 +95,23 @@ public void GetDirectoryName_ShouldNormalizeDirectorySeparators(
result.Should().Be(expected);
}

[SkippableTheory]
[InlineData("foo//bar/file", "foo/bar", TestOS.All)]
[InlineData("foo///bar/file", "foo/bar", TestOS.All)]
[InlineData(@"foo\\bar/file", "foo/bar", TestOS.Windows)]
[InlineData(@"foo\\\bar/file", "foo/bar", TestOS.Windows)]
public void GetDirectoryName_ShouldNormalizeDirectorySeparators(
string path, string expected, TestOS operatingSystem)
{
Skip.IfNot(Test.RunsOn(operatingSystem));

expected = expected.Replace('/', FileSystem.Path.DirectorySeparatorChar);

string? result = FileSystem.Path.GetDirectoryName(path);

result.Should().Be(expected);
}

#if FEATURE_SPAN
[SkippableTheory]
[AutoData]
Expand Down

0 comments on commit ebe88ad

Please sign in to comment.