-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add polyfills for File.ReadLinesAsync(...), TextReader.ReadLineAsync(CancellationToken), TextReader.ReadToEndAsync(CancellationToken)
#68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net70; | ||
|
|
||
| public class FileTests | ||
| { | ||
| [Fact] | ||
| public async Task ReadLinesAsync_Test() | ||
| { | ||
| // Arrange | ||
| var linesToWrite = new[] { "Line 1", "Line 2", "Line 3" }; | ||
| var tempFilePath = Path.GetTempFileName(); | ||
| await File.WriteAllLinesAsync(tempFilePath, linesToWrite); | ||
|
|
||
| try | ||
| { | ||
| // Act | ||
| var readLines = new List<string>(); | ||
| await foreach (var line in File.ReadLinesAsync(tempFilePath)) | ||
| readLines.Add(line); | ||
|
|
||
| // Assert | ||
| readLines.Should().Equal(linesToWrite); | ||
| } | ||
| finally | ||
| { | ||
| try | ||
| { | ||
| File.Delete(tempFilePath); | ||
| } | ||
| catch | ||
| { | ||
| // Ignore | ||
| } | ||
| } | ||
| } | ||
| } | ||
Tyrrrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net70; | ||
|
|
||
| public class TextReaderTests | ||
| { | ||
| [Fact] | ||
| public async Task ReadLineAsync_Test() | ||
| { | ||
| // Arrange | ||
| using var stream = new MemoryStream("Line 1\nLine 2\nLine 3\n"u8.ToArray()); | ||
| using var reader = new StreamReader(stream); | ||
|
|
||
| // Act | ||
| var line1 = await reader.ReadLineAsync(CancellationToken.None); | ||
| var line2 = await reader.ReadLineAsync(CancellationToken.None); | ||
| var line3 = await reader.ReadLineAsync(CancellationToken.None); | ||
| var line4 = await reader.ReadLineAsync(CancellationToken.None); | ||
|
|
||
| // Assert | ||
| line1.Should().Be("Line 1"); | ||
| line2.Should().Be("Line 2"); | ||
| line3.Should().Be("Line 3"); | ||
| line4.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadLineAsync_Cancellation_Test() | ||
| { | ||
| // Arrange | ||
| using var stream = new MemoryStream("Line 1\nLine 2\nLine 3\n"u8.ToArray()); | ||
| using var reader = new StreamReader(stream); | ||
| using var cts = new CancellationTokenSource(); | ||
| await cts.CancelAsync(); | ||
|
|
||
| // Act & Assert | ||
| await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => | ||
| { | ||
| await reader.ReadLineAsync(cts.Token); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadToEndAsync_Test() | ||
| { | ||
| // Arrange | ||
| using var stream = new MemoryStream("Hello, World!"u8.ToArray()); | ||
| using var reader = new StreamReader(stream); | ||
|
|
||
| // Act | ||
| var content = await reader.ReadToEndAsync(CancellationToken.None); | ||
|
|
||
| // Assert | ||
| content.Should().Be("Hello, World!"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadToEndAsync_Cancellation_Test() | ||
| { | ||
| // Arrange | ||
| using var stream = new MemoryStream("Hello, World!"u8.ToArray()); | ||
| using var reader = new StreamReader(stream); | ||
| using var cts = new CancellationTokenSource(); | ||
| await cts.CancelAsync(); | ||
|
|
||
| // Act & Assert | ||
| await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => | ||
| { | ||
| await reader.ReadToEndAsync(cts.Token); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #if (NETCOREAPP && !NET7_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Threading; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| // No file I/O on .NET Standard prior to 1.3 | ||
| #if !NETSTANDARD || NETSTANDARD1_3_OR_GREATER | ||
| extension(File) | ||
| { | ||
| #if FEATURE_TASK && FEATURE_ASYNCINTERFACES | ||
| // https://learn.microsoft.com/dotnet/api/system.io.file.readlinesasync#system-io-file-readlinesasync(system-string-system-text-encoding-system-threading-cancellationtoken) | ||
| public static async IAsyncEnumerable<string> ReadLinesAsync( | ||
| string path, | ||
| Encoding encoding, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default | ||
| ) | ||
| { | ||
| using var stream = new FileStream( | ||
| path, | ||
| FileMode.Open, | ||
| FileAccess.Read, | ||
| FileShare.Read, | ||
| 4096, | ||
| FileOptions.Asynchronous | FileOptions.SequentialScan | ||
| ); | ||
|
|
||
| using var reader = new StreamReader(stream, encoding); | ||
|
|
||
| while (!reader.EndOfStream) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false); | ||
| if (line is not null) | ||
| yield return line; | ||
| } | ||
| } | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.io.file.readlinesasync#system-io-file-readlinesasync(system-string-system-threading-cancellationtoken) | ||
| public static async IAsyncEnumerable<string> ReadLinesAsync( | ||
| string path, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default | ||
| ) | ||
| { | ||
| using var stream = new FileStream( | ||
| path, | ||
| FileMode.Open, | ||
| FileAccess.Read, | ||
| FileShare.Read, | ||
| 4096, | ||
| FileOptions.Asynchronous | FileOptions.SequentialScan | ||
| ); | ||
|
|
||
| using var reader = new StreamReader(stream); | ||
|
|
||
| while (!reader.EndOfStream) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false); | ||
| if (line is not null) | ||
| yield return line; | ||
| } | ||
| } | ||
| #endif | ||
| } | ||
| #endif | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #if (NETCOREAPP && !NET7_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(TextReader reader) | ||
| { | ||
| #if FEATURE_TASK | ||
| // https://learn.microsoft.com/dotnet/api/system.io.textreader.readlineasync#system-io-textreader-readlineasync(system-threading-cancellationtoken) | ||
| public Task<string?> ReadLineAsync(CancellationToken cancellationToken) | ||
| { | ||
| // Impossible to polyfill this properly as it requires to track the buffer's state | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| return reader.ReadLineAsync(); | ||
| } | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.io.textreader.readtoendasync#system-io-textreader-readtoendasync(system-threading-cancellationtoken) | ||
| public async Task<string> ReadToEndAsync(CancellationToken cancellationToken) | ||
| { | ||
| var result = new StringBuilder(); | ||
| var buffer = new char[4096]; | ||
|
|
||
| while (true) | ||
| { | ||
| var charsRead = await reader | ||
| .ReadAsync(buffer, cancellationToken) | ||
Tyrrrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .ConfigureAwait(false); | ||
|
|
||
| if (charsRead <= 0) | ||
| break; | ||
|
|
||
| result.Append(buffer, 0, charsRead); | ||
| } | ||
|
|
||
| return result.ToString(); | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.