Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 44 additions & 0 deletions PolyShim.Tests/NetCore30/PathTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.IO;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.NetCore30;

public class PathTests
{
[Fact]
public void EndsInDirectorySeparator_Test()
{
// Act & assert
Path.EndsInDirectorySeparator("C:\\Folder\\Subfolder\\").Should().BeTrue();
Path.EndsInDirectorySeparator("C:/Folder/Subfolder/").Should().BeTrue();
Path.EndsInDirectorySeparator("C:\\Folder\\Subfolder").Should().BeFalse();
Path.EndsInDirectorySeparator("C:/Folder/Subfolder").Should().BeFalse();
Path.EndsInDirectorySeparator("/a/b/c/").Should().BeTrue();
Path.EndsInDirectorySeparator("/a/b/c").Should().BeFalse();
Path.EndsInDirectorySeparator(string.Empty).Should().BeFalse();
}

[Fact]
public void TrimEndingDirectorySeparator_Test()
{
// Act & assert
Path.TrimEndingDirectorySeparator("C:\\Folder\\Subfolder\\")
.Should()
.Be("C:\\Folder\\Subfolder");
Path.TrimEndingDirectorySeparator("C:\\Folder\\Subfolder\\\\")
.Should()
.Be("C:\\Folder\\Subfolder\\");
Path.TrimEndingDirectorySeparator("C:/Folder/Subfolder/")
.Should()
.Be("C:/Folder/Subfolder");
Path.TrimEndingDirectorySeparator("C:\\Folder\\Subfolder")
.Should()
.Be("C:\\Folder\\Subfolder");
Path.TrimEndingDirectorySeparator("C:/Folder/Subfolder").Should().Be("C:/Folder/Subfolder");
Path.TrimEndingDirectorySeparator("/a/b/c/").Should().Be("/a/b/c");
Path.TrimEndingDirectorySeparator("/a/b/c//").Should().Be("/a/b/c/");
Path.TrimEndingDirectorySeparator("/a/b/c").Should().Be("/a/b/c");
Path.TrimEndingDirectorySeparator(string.Empty).Should().Be(string.Empty);
}
}
4 changes: 2 additions & 2 deletions PolyShim/Net70/Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public int ReadAtLeast(
var totalBytesRead = 0;
while (totalBytesRead < buffer.Length)
{
var bytesRead = stream.Read(buffer.Slice(totalBytesRead));
var bytesRead = stream.Read(buffer[totalBytesRead..]);
if (bytesRead <= 0)
break;

Expand Down Expand Up @@ -181,7 +181,7 @@ public async Task<int> ReadAtLeastAsync(
while (totalBytesRead < buffer.Length)
{
var bytesRead = await stream
.ReadAsync(buffer.Slice(totalBytesRead), cancellationToken)
.ReadAsync(buffer[totalBytesRead..], cancellationToken)
.ConfigureAwait(false);

if (bytesRead <= 0)
Expand Down
6 changes: 3 additions & 3 deletions PolyShim/NetCore10/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// <auto-generated/>

#if !FEATURE_RUNTIMEINFORMATION
// No way to detect on .NET Standard lower than 1.3
#if !(NETSTANDARD && !NETSTANDARD1_3_OR_GREATER)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
Expand All @@ -19,6 +17,8 @@ namespace System.Runtime.InteropServices;
[ExcludeFromCodeCoverage]
internal static class RuntimeInformation
{
// No way to detect on .NET Standard lower than 1.3
#if !(NETSTANDARD && !NETSTANDARD1_3_OR_GREATER)
public static bool IsOSPlatform(OSPlatform osPlatform)
{
if (osPlatform == OSPlatform.FreeBSD)
Expand Down Expand Up @@ -49,6 +49,6 @@ or PlatformID.Win32S

return false;
}
}
#endif
}
#endif
39 changes: 39 additions & 0 deletions PolyShim/NetCore30/Path.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The following comment is required to instruct analyzers to skip this file
// <auto-generated/>

#if (NETCOREAPP && !NETCOREAPP3_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System.IO;

internal static partial class PolyfillExtensions
{
extension(Path)
{
// https://learn.microsoft.com/dotnet/api/system.io.path.endsindirectoryseparator#system-io-path-endsindirectoryseparator(system-string)
public static bool EndsInDirectorySeparator(string? path)
{
if (string.IsNullOrEmpty(path))
return false;

var lastChar = path![^1];

#if !NETSTANDARD || NETSTANDARD1_3_OR_GREATER
return lastChar == Path.DirectorySeparatorChar || lastChar == Path.AltDirectorySeparatorChar;
#else
return lastChar is '\\' or '/';
#endif
}

// https://learn.microsoft.com/dotnet/api/system.io.path.trimendingdirectoryseparator#system-io-path-trimendingdirectoryseparator(system-string)
public static string TrimEndingDirectorySeparator(string path) =>
EndsInDirectorySeparator(path) && !Path.GetPathRoot(path).Equals(path, System.StringComparison.Ordinal)
? path[..^1]
: path;
}
}
#endif
Loading