Skip to content

Commit

Permalink
Enable GetDirectoryName
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Apr 17, 2024
1 parent 75396c7 commit 9281a1c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Source/Testably.Abstractions.Testing/Helpers/Execute.LinuxPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,24 @@ public override string GetTempPath()
public override bool IsPathRooted(string? path)
=> path?.Length > 0 && path[0] == '/';

/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Unix.cs#L22
/// </summary>
protected override int GetRootLength(string path)
{
return path.Length > 0 && IsDirectorySeparator(path[0]) ? 1 : 0;
}

/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Unix.cs#L27
/// </summary>
protected override bool IsDirectorySeparator(char c)
=> c == DirectorySeparatorChar;

/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Unix.cs#L89
/// </summary>
protected override bool IsEffectivelyEmpty(string path)
=> string.IsNullOrEmpty(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,28 @@ public ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path)

/// <inheritdoc cref="IPath.GetDirectoryName(string)" />
public string? GetDirectoryName(string? path)
=> System.IO.Path.GetDirectoryName(path);
{
if (path == null || IsEffectivelyEmpty(path))
{
return null;
}

int rootLength = GetRootLength(path);
for (int i = path.Length - 1; i >= 0; i--)
{
char ch = path[i];

if (IsDirectorySeparator(ch))
{
if (i > rootLength)
{
return path.Substring(0, i);
}
}
}

return "";
}

#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetExtension(ReadOnlySpan{char})" />
Expand Down Expand Up @@ -450,7 +471,9 @@ public bool TryJoin(ReadOnlySpan<char> path1,
private static string CombineInternal(string[] paths)
=> System.IO.Path.Combine(paths);

protected abstract int GetRootLength(string path);
protected abstract bool IsDirectorySeparator(char c);
protected abstract bool IsEffectivelyEmpty(string path);

#if FEATURE_PATH_ADVANCED
private static string JoinInternal(string?[] paths)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Testably.Abstractions.Testing.Helpers;

Expand Down Expand Up @@ -63,12 +64,45 @@ public override bool IsPathRooted(string? path)
(length >= 2 && IsValidDriveChar(path![0]) && path[1] == VolumeSeparatorChar);
}

/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.3/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L181
/// </summary>
protected override int GetRootLength(string path)
{
if (path.Length >= 2
&& path[1] == ':'
&& IsValidDriveChar(path[0]))
{
if (path.Length > 2 && IsDirectorySeparator(path[2]))
{
return 3;
}

return 2;
}

return 0;
}

/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L280
/// </summary>
protected override bool IsDirectorySeparator(char c)
=> c == DirectorySeparatorChar || c == AltDirectorySeparatorChar;

/// <summary>
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L381
/// </summary>
protected override bool IsEffectivelyEmpty(string path)
{
if (string.IsNullOrEmpty(path))
{
return true;
}

return path.All(c => c == ' ');
}

/// <summary>
/// Returns true if the given character is a valid drive letter
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private bool IncludeSimulatedTests(ClassModel @class)
[
"ChangeExtensionTests",
"EndsInDirectorySeparatorTests",
"GetDirectoryNameTests",
"GetExtensionTests",
"GetPathRootTests",
"GetRandomFileNameTests",
Expand Down

0 comments on commit 9281a1c

Please sign in to comment.