Skip to content

Commit

Permalink
Merge pull request #184 from AnakinRaW/develop
Browse files Browse the repository at this point in the history
Support ROS for IsChildOf
  • Loading branch information
AnakinRaW authored Aug 10, 2024
2 parents 21f14dd + a500761 commit 8e2e7ec
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PackageReference>
<PackageReference Include="Nerdbank.GitVersioning" Condition="!Exists('packages.config')">
<PrivateAssets>all</PrivateAssets>
<Version>3.6.139</Version>
<Version>3.6.141</Version>
</PackageReference>
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ protected override async Task<DownloadResult> DownloadAsyncCore(Uri uri, Stream
#else
using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
#endif
var contentLengthData = response.Content.Headers.ContentLength;
if (contentLengthData is null or 0L)
throw new IOException("Error: Response stream length is 0.");

var contentLength = contentLengthData.Value;
var contentLengthData = response.Content.Headers.ContentLength ?? 0;
var contentLength = contentLengthData;

var requestRegistration = cancellationToken.Register(webRequest.Dispose);
try
Expand Down Expand Up @@ -98,7 +95,7 @@ private static HttpRequestMessage CreateRequest(Uri uri)
{
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("defalte"));
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ protected override async Task<DownloadResult> DownloadAsyncCore(Uri uri, Stream
{
try
{
#if NET || NETSTANDARD2_1
await
#endif
using var responseStream = webResponse.GetResponseStream();
var contentLength = webResponse.Headers["Content-Length"];
if (string.IsNullOrEmpty(contentLength))
Expand All @@ -50,7 +53,7 @@ protected override async Task<DownloadResult> DownloadAsyncCore(Uri uri, Stream
if (totalStreamLength.Equals(0L))
throw new IOException("Error: Response stream length is 0.");

var requestRegistration = cancellationToken.Register(() => webRequest!.Abort());
var requestRegistration = cancellationToken.Register(webRequest.Abort);
try
{
summary.DownloadedSize = await StreamUtilities.CopyStreamWithProgressAsync(responseStream,
Expand All @@ -60,7 +63,11 @@ protected override async Task<DownloadResult> DownloadAsyncCore(Uri uri, Stream
}
finally
{
#if NET || NETSTANDARD2_1
await requestRegistration.DisposeAsync();
#else
requestRegistration.Dispose();
#endif
}
}
catch (WebException ex)
Expand Down Expand Up @@ -110,6 +117,9 @@ private static HttpWebRequest CreateRequest(Uri uri)
var success = false;
try
{
#if NET || NETSTANDARD2_1
await
#endif
using (cancellationToken.Register(webRequest.Abort))
httpWebResponse = (HttpWebResponse)await webRequest.GetResponseAsync().ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Testably.Abstractions" Version="3.2.1" />
<PackageReference Include="Testably.Abstractions.Testing" Version="3.2.1" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="Testably.Abstractions" Version="3.2.2" />
<PackageReference Include="Testably.Abstractions.Testing" Version="3.2.2" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public async Task Test_DownloadAsync_DownloadWithHttpClient()

var progressTriggered = false;

var summary = await manager.DownloadAsync(new Uri("http://speedtest.ftp.otenet.gr/files/test10Mb.db"), file, ProgressMethod,
var summary = await manager.DownloadAsync(new Uri("https://raw.githubusercontent.com/BitDoctor/speed-test-file/master/5mb.txt"), file, ProgressMethod,
null, CancellationToken.None);

Assert.Equal(10 * 1024 * 1024, summary.DownloadedSize);
Assert.Equal(10 * 1024 * 1024, file.Length);
Assert.Equal(5 * 1024 * 1024, summary.DownloadedSize);
Assert.Equal(5 * 1024 * 1024, file.Length);
Assert.True(progressTriggered);

void ProgressMethod(ProgressUpdateStatus status)
Expand Down Expand Up @@ -193,11 +193,11 @@ public async Task Test_DownloadAsync_DownloadWithWebClient()

var progressTriggered = false;

var summary = await manager.DownloadAsync(new Uri("http://speedtest.ftp.otenet.gr/files/test10Mb.db"), file, ProgressMethod,
var summary = await manager.DownloadAsync(new Uri("https://raw.githubusercontent.com/BitDoctor/speed-test-file/master/5mb.txt"), file, ProgressMethod,
null, CancellationToken.None);

Assert.Equal(10 * 1024 * 1024, summary.DownloadedSize);
Assert.Equal(10 * 1024 * 1024, file.Length);
Assert.Equal(5 * 1024 * 1024, summary.DownloadedSize);
Assert.Equal(5 * 1024 * 1024, file.Length);
Assert.True(progressTriggered);

void ProgressMethod(ProgressUpdateStatus status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task Test_DownloadAsync_Download()
{
var outStream = new MemoryStream();
var result = await _provider.DownloadAsync(
new Uri("http://speedtest.ftp.otenet.gr/files/test100k.db"),
new Uri("https://raw.githubusercontent.com/AnakinRaW/CommonUtilities/2ab2e6a26872974422459b0605b26222c9e126ca/README.md"),
outStream, null, CancellationToken.None);
Assert.True(result.DownloadedSize > 0);
Assert.Equal(result.DownloadedSize, outStream.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task Test_DownloadAsync_Download()
{
var outStream = new MemoryStream();
var result = await _provider.DownloadAsync(
new Uri("http://speedtest.ftp.otenet.gr/files/test100k.db"),
new Uri("https://raw.githubusercontent.com/AnakinRaW/CommonUtilities/2ab2e6a26872974422459b0605b26222c9e126ca/README.md"),
outStream, null, CancellationToken.None);
Assert.True(result.DownloadedSize > 0);
Assert.Equal(result.DownloadedSize, outStream.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IO.Abstractions" Version="21.0.2" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Analyzers" Version="2022.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
29 changes: 23 additions & 6 deletions src/CommonUtilities.FileSystem/src/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static bool AreEqual(this IPath _, string pathA, string pathB)

internal static bool PathsEqual(string path1, string path2)
{
return PathsEqual(path1, path2, Math.Max(path1.Length, path2.Length));
return PathsEqual(path1.AsSpan(), path2.AsSpan(), Math.Max(path1.Length, path2.Length));
}

private static void CombinePathsUnchecked(ref ValueStringBuilder sb, string relativePath)
Expand All @@ -318,7 +318,7 @@ private static void CombinePathsUnchecked(ref ValueStringBuilder sb, string rela
/// <summary>
/// True if the two paths are the same. (but only up to the specified length)
/// </summary>
private static bool PathsEqual(string path1, string path2, int length)
private static bool PathsEqual(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, int length)
{
if (path1.Length < length || path2.Length < length)
return false;
Expand Down Expand Up @@ -353,11 +353,28 @@ public static bool IsChildOf(this IPath _, string basePath, string candidate)
{
var fullBase = _.GetFullPath(basePath);
var fullCandidate = _.GetFullPath(candidate);
return _.IsChildOf(fullBase.AsSpan(), fullCandidate.AsSpan());
}

return fullBase.Length > 0
&& fullCandidate.Length > fullBase.Length
&& PathsEqual(fullCandidate, fullBase, fullBase.Length)
&& (IsAnyDirectorySeparator(fullBase[fullBase.Length - 1]) || IsAnyDirectorySeparator(fullCandidate[fullBase.Length]));
/// <summary>
/// Determines whether a specified candidate path is a real child path to a specified base path.
/// </summary>
/// <param name="_"></param>
/// <param name="basePath">The base path-</param>
/// <param name="candidate">The relative path candidate.</param>
/// <returns><see langword="true"/> if <paramref name="candidate"/> is a child path to <paramref name="basePath"/>; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="basePath"/> or <paramref name="candidate"/> are not fully qualified.</exception>
public static bool IsChildOf(this IPath _, ReadOnlySpan<char> basePath, ReadOnlySpan<char> candidate)
{
if (!_.IsPathFullyQualified(basePath))
throw new ArgumentException("candidate must be fully qualified", nameof(basePath));
if (!_.IsPathFullyQualified(candidate))
throw new ArgumentException("candidate must be fully qualified", nameof(candidate));

return basePath.Length > 0
&& candidate.Length > basePath.Length
&& PathsEqual(candidate, basePath, basePath.Length)
&& (IsAnyDirectorySeparator(basePath[basePath.Length - 1]) || IsAnyDirectorySeparator(candidate[basePath.Length]));
}

internal static bool IsValidDriveChar(char value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Testably.Abstractions" Version="3.2.1" />
<PackageReference Include="Testably.Abstractions.Testing" Version="3.2.1" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="Testably.Abstractions" Version="3.2.2" />
<PackageReference Include="Testably.Abstractions.Testing" Version="3.2.2" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Abstractions;
using System;
using System.IO.Abstractions;
using AnakinRaW.CommonUtilities.Testing;
using Testably.Abstractions.Testing;
using Xunit;
Expand Down Expand Up @@ -35,6 +36,10 @@ public void TestIsChild_Windows(string basePath, string candidate, bool expected
_fileSystem.Initialize().WithSubdirectory("C:\\current");
_fileSystem.Directory.SetCurrentDirectory("C:\\current");
Assert.Equal(expected, _fileSystem.Path.IsChildOf(basePath, candidate));
Assert.Equal(
expected,
_fileSystem.Path.IsChildOf(_fileSystem.Path.GetFullPath(basePath).AsSpan(),
_fileSystem.Path.GetFullPath(candidate).AsSpan()));
}

[PlatformSpecificTheory(TestPlatformIdentifier.Linux)]
Expand All @@ -49,5 +54,27 @@ public void TestIsChild_Linux(string basePath, string candidate, bool expected)
_fileSystem.Initialize().WithSubdirectory("/current");
_fileSystem.Directory.SetCurrentDirectory("/current");
Assert.Equal(expected, _fileSystem.Path.IsChildOf(basePath, candidate));
Assert.Equal(
expected,
_fileSystem.Path.IsChildOf(_fileSystem.Path.GetFullPath(basePath).AsSpan(),
_fileSystem.Path.GetFullPath(candidate).AsSpan()));
}

[PlatformSpecificTheory(TestPlatformIdentifier.Linux)]
[InlineData("test", "/")]
[InlineData("/", "test")]
[InlineData("test", "test")]
public void TestIsChild_NoFullyQualifiedPathsForROS_Linux(string basePath, string candidate)
{
Assert.Throws<ArgumentException>(() => _fileSystem.Path.IsChildOf(basePath.AsSpan(), candidate.AsSpan()));
}

[PlatformSpecificTheory(TestPlatformIdentifier.Windows)]
[InlineData("test", "test")]
[InlineData("C:/test", "test")]
[InlineData("test", "C:/test")]
public void TestIsChild_NoFullyQualifiedPathsForROS_Windows(string basePath, string candidate)
{
Assert.Throws<ArgumentException>(() => _fileSystem.Path.IsChildOf(basePath.AsSpan(), candidate.AsSpan()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.2" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.29" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion src/CommonUtilities/src/CommonUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.2" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
8 changes: 4 additions & 4 deletions src/CommonUtilities/test/CommonUtilities.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Testably.Abstractions" Version="3.2.1" />
<PackageReference Include="Testably.Abstractions.Testing" Version="3.2.1" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="Testably.Abstractions" Version="3.2.2" />
<PackageReference Include="Testably.Abstractions.Testing" Version="3.2.2" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 8e2e7ec

Please sign in to comment.