From 6d308a461f6f8c1b4213bfb08472a19b5bf9bb8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Mon, 11 Nov 2024 20:45:07 +0100 Subject: [PATCH] chore(deps): update dependency System.IO.Abstractions to 21.1.3 (#664) Update version to 21.1.3 and adapt tests --- .github/workflows/build.yml | 2 +- .github/workflows/ci.yml | 2 +- Directory.Packages.props | 2 +- .../FileSystem/FileStreamMock.cs | 9 + .../FileSystem/FileStreamStatisticsTests.cs | 12 ++ .../FileSystem/FileStream/CopyToTests.cs | 201 ++++++++++++++++++ .../FileSystem/FileStream/DisposeTests.cs | 4 +- .../FileSystem/FileStream/Tests.cs | 74 ------- 8 files changed, 226 insertions(+), 80 deletions(-) create mode 100644 Tests/Testably.Abstractions.Tests/FileSystem/FileStream/CopyToTests.cs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index acc0569f0..07a7e35f0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -231,7 +231,7 @@ jobs: - name: Install .NET Stryker shell: bash run: | - dotnet tool install dotnet-stryker --tool-path ../tools --version 4.3.0 + dotnet tool install dotnet-stryker --tool-path ../tools - name: Analyze Testably.Abstractions.Testing env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00e08a5c1..64a9190f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -196,7 +196,7 @@ jobs: - name: Install .NET Stryker shell: bash run: | - dotnet tool install dotnet-stryker --tool-path ../tools --version 4.3.0 + dotnet tool install dotnet-stryker --tool-path ../tools - name: Prepare Reports directory shell: bash run: | diff --git a/Directory.Packages.props b/Directory.Packages.props index e4103ce57..f61d1f37a 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -6,7 +6,7 @@ - + diff --git a/Source/Testably.Abstractions.Testing/FileSystem/FileStreamMock.cs b/Source/Testably.Abstractions.Testing/FileSystem/FileStreamMock.cs index a52ceae43..f890bec96 100644 --- a/Source/Testably.Abstractions.Testing/FileSystem/FileStreamMock.cs +++ b/Source/Testably.Abstractions.Testing/FileSystem/FileStreamMock.cs @@ -325,6 +325,15 @@ public override IAsyncResult BeginWrite(byte[] buffer, return base.BeginWrite(buffer, offset, count, callback, state); } + /// + public override void Close() + { + using IDisposable registration = _fileSystem.StatisticsRegistration + .FileStream.RegisterPathMethod(_location.FullPath, nameof(Close)); + + base.Close(); + } + /// public override void CopyTo(Stream destination, int bufferSize) { diff --git a/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileStreamStatisticsTests.cs b/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileStreamStatisticsTests.cs index 0c8db3063..f9317f022 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileStreamStatisticsTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileStreamStatisticsTests.cs @@ -47,6 +47,18 @@ public void Method_BeginWrite_ByteArray_Int_Int_AsyncCallback_Object_ShouldRegis buffer, offset, count, callback, state); } + [SkippableFact] + public void Method_Close_ShouldRegisterCall() + { + MockFileSystem sut = new(); + using FileSystemStream fileStream = sut.FileStream.New("foo", FileMode.OpenOrCreate); + + fileStream.Close(); + + sut.Statistics.FileStream["foo"] + .ShouldOnlyContainMethodCall(nameof(FileSystemStream.Close)); + } + [SkippableFact] public void Method_CopyTo_Stream_Int_ShouldRegisterCall() { diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/CopyToTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/CopyToTests.cs new file mode 100644 index 000000000..6d05f0a24 --- /dev/null +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/CopyToTests.cs @@ -0,0 +1,201 @@ +using System.IO; +using System.Threading.Tasks; +#if FEATURE_SPAN +#endif + +namespace Testably.Abstractions.Tests.FileSystem.FileStream; + +// ReSharper disable once PartialTypeWithSinglePart +public abstract partial class CopyToTests + : FileSystemTestBase + where TFileSystem : IFileSystem +{ + [SkippableTheory] + [AutoData] + public void CopyTo_BufferSizeZero_ShouldThrowArgumentOutOfRangeException( + string path, byte[] bytes) + { + byte[] buffer = new byte[bytes.Length]; + FileSystem.File.WriteAllBytes(path, bytes); + + Exception? exception = Record.Exception(() => + { + using FileSystemStream stream = FileSystem.File.OpenRead(path); + using MemoryStream destination = new(buffer); + stream.CopyTo(destination, 0); + }); + + exception.Should().BeException( + paramName: "bufferSize"); + } + + [SkippableTheory] + [AutoData] + public void CopyTo_ShouldCopyBytes( + string path, byte[] bytes) + { + byte[] buffer = new byte[bytes.Length]; + FileSystem.File.WriteAllBytes(path, bytes); + using FileSystemStream stream = FileSystem.File.OpenRead(path); + using MemoryStream destination = new(buffer); + + stream.CopyTo(destination); + + destination.Flush(); + buffer.Should().BeEquivalentTo(bytes); + } + +#if FEATURE_FILESYSTEM_ASYNC + [SkippableTheory] + [AutoData] + public async Task CopyToAsync_BufferSizeZero_ShouldThrowArgumentOutOfRangeException( + string path, byte[] bytes) + { + byte[] buffer = new byte[bytes.Length]; + await FileSystem.File.WriteAllBytesAsync(path, bytes); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await using FileSystemStream stream = FileSystem.File.OpenRead(path); + using MemoryStream destination = new(buffer); + await stream.CopyToAsync(destination, 0); + }); + + exception.Should().BeException( + paramName: "bufferSize"); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [SkippableTheory] + [AutoData] + public async Task CopyToAsync_ShouldCopyBytes( + string path, byte[] bytes) + { + byte[] buffer = new byte[bytes.Length]; + await FileSystem.File.WriteAllBytesAsync(path, bytes); + await using FileSystemStream stream = FileSystem.File.OpenRead(path); + using MemoryStream destination = new(buffer); + + await stream.CopyToAsync(destination); + + await destination.FlushAsync(); + buffer.Should().BeEquivalentTo(bytes); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [Theory] + [InlineData(0)] + [InlineData(-1)] + public async Task CopyToAsync_WhenBufferSizeIsNotPositive_ShouldThrowArgumentNullException( + int bufferSize) + { + await FileSystem.File.WriteAllTextAsync("foo.txt", ""); + await FileSystem.File.WriteAllTextAsync("bar.txt", ""); + await using FileSystemStream source = FileSystem.FileInfo.New("foo.txt").OpenRead(); + await using FileSystemStream destination = FileSystem.FileInfo.New("bar.txt").OpenWrite(); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await source.CopyToAsync(destination, bufferSize); + }); + + exception.Should().BeOfType(); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [Fact] + public async Task CopyToAsync_WhenDestinationIsClosed_ShouldThrowObjectDisposedException() + { + await FileSystem.File.WriteAllTextAsync("foo.txt", ""); + await using FileSystemStream source = FileSystem.FileInfo.New("foo.txt").OpenRead(); + using MemoryStream destination = new(); + destination.Close(); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await source.CopyToAsync(destination); + }); + + exception.Should().BeOfType() + .Which.Message.Should().Match("Cannot access a*"); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [Fact] + public async Task CopyToAsync_WhenDestinationIsNull_ShouldThrowArgumentNullException() + { + await FileSystem.File.WriteAllTextAsync("foo.txt", ""); + await using FileSystemStream source = FileSystem.FileInfo.New("foo.txt").OpenRead(); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await source.CopyToAsync(null!); + }); + + exception.Should().BeOfType() + .Which.Message.Should().Match("*cannot be null*"); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [Fact] + public async Task CopyToAsync_WhenDestinationIsReadOnly_ShouldThrowNotSupportedException() + { + await FileSystem.File.WriteAllTextAsync("foo.txt", ""); + await FileSystem.File.WriteAllTextAsync("bar.txt", ""); + await using FileSystemStream source = FileSystem.FileInfo.New("foo.txt").OpenRead(); + await using FileSystemStream destination = FileSystem.FileInfo.New("bar.txt").OpenRead(); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await source.CopyToAsync(destination); + }); + + exception.Should().BeOfType() + .Which.Message.Should().Match("Stream does not support writing*"); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [Fact] + public async Task CopyToAsync_WhenSourceIsClosed_ShouldThrowObjectDisposedException() + { + await FileSystem.File.WriteAllTextAsync("foo.txt", ""); + await FileSystem.File.WriteAllTextAsync("bar.txt", ""); + await using FileSystemStream source = FileSystem.FileInfo.New("foo.txt").OpenRead(); + await using FileSystemStream destination = FileSystem.FileInfo.New("bar.txt").OpenWrite(); + source.Close(); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await source.CopyToAsync(destination); + }); + + exception.Should().BeOfType() + .Which.Message.Should().Match("Cannot access a*"); + } +#endif + +#if FEATURE_FILESYSTEM_ASYNC + [Fact] + public async Task CopyToAsync_WhenSourceIsWriteOnly_ShouldThrowNotSupportedException() + { + await FileSystem.File.WriteAllTextAsync("foo.txt", ""); + await FileSystem.File.WriteAllTextAsync("bar.txt", ""); + await using FileSystemStream source = FileSystem.FileInfo.New("foo.txt").OpenWrite(); + await using FileSystemStream destination = FileSystem.FileInfo.New("bar.txt").OpenWrite(); + + Exception? exception = await Record.ExceptionAsync(async () => + { + await source.CopyToAsync(destination); + }); + + exception.Should().BeOfType() + .Which.Message.Should().Match("Stream does not support reading*"); + } +#endif +} diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/DisposeTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/DisposeTests.cs index 71ee439f2..6f4260bf8 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/DisposeTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/DisposeTests.cs @@ -68,9 +68,7 @@ public void Operations_ShouldThrowAfterStreamIsDisposed( exception.Should() .BeOfType( - $"\n{callback}\n executed after Dispose() was called.") - .Which.ObjectName.Should() - .BeEmpty($"\n{callback}\n executed after Dispose() was called."); + $"\n{callback}\n executed after Dispose() was called."); } #region Helpers diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/Tests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/Tests.cs index ce874eded..e1e619ecc 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/Tests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileStream/Tests.cs @@ -55,80 +55,6 @@ public void Close_CalledMultipleTimes_ShouldNotThrow( exception.Should().BeNull(); } - [SkippableTheory] - [AutoData] - public void CopyTo_BufferSizeZero_ShouldThrowArgumentOutOfRangeException( - string path, byte[] bytes) - { - byte[] buffer = new byte[bytes.Length]; - FileSystem.File.WriteAllBytes(path, bytes); - - Exception? exception = Record.Exception(() => - { - using FileSystemStream stream = FileSystem.File.OpenRead(path); - using MemoryStream destination = new(buffer); - stream.CopyTo(destination, 0); - }); - - exception.Should().BeException( - paramName: "bufferSize"); - } - - [SkippableTheory] - [AutoData] - public void CopyTo_ShouldCopyBytes( - string path, byte[] bytes) - { - byte[] buffer = new byte[bytes.Length]; - FileSystem.File.WriteAllBytes(path, bytes); - using FileSystemStream stream = FileSystem.File.OpenRead(path); - using MemoryStream destination = new(buffer); - - stream.CopyTo(destination); - - destination.Flush(); - buffer.Should().BeEquivalentTo(bytes); - } - -#if FEATURE_FILESYSTEM_ASYNC - [SkippableTheory] - [AutoData] - public async Task CopyToAsync_BufferSizeZero_ShouldThrowArgumentOutOfRangeException( - string path, byte[] bytes) - { - byte[] buffer = new byte[bytes.Length]; - await FileSystem.File.WriteAllBytesAsync(path, bytes); - - Exception? exception = await Record.ExceptionAsync(async () => - { - using FileSystemStream stream = FileSystem.File.OpenRead(path); - using MemoryStream destination = new(buffer); - await stream.CopyToAsync(destination, 0); - }); - - exception.Should().BeException( - paramName: "bufferSize"); - } -#endif - -#if FEATURE_FILESYSTEM_ASYNC - [SkippableTheory] - [AutoData] - public async Task CopyToAsync_ShouldCopyBytes( - string path, byte[] bytes) - { - byte[] buffer = new byte[bytes.Length]; - await FileSystem.File.WriteAllBytesAsync(path, bytes); - await using FileSystemStream stream = FileSystem.File.OpenRead(path); - using MemoryStream destination = new(buffer); - - await stream.CopyToAsync(destination); - - await destination.FlushAsync(); - buffer.Should().BeEquivalentTo(bytes); - } -#endif - [SkippableTheory] [AutoData] public void Extensibility_ShouldWrapFileStreamOnRealFileSystem(