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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="21.0.29" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="21.1.3" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ public override IAsyncResult BeginWrite(byte[] buffer,
return base.BeginWrite(buffer, offset, count, callback, state);
}

/// <inheritdoc cref="FileSystemStream.Close()" />
public override void Close()
{
using IDisposable registration = _fileSystem.StatisticsRegistration
.FileStream.RegisterPathMethod(_location.FullPath, nameof(Close));

base.Close();
}

/// <inheritdoc cref="FileSystemStream.CopyTo(Stream, int)" />
public override void CopyTo(Stream destination, int bufferSize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
201 changes: 201 additions & 0 deletions Tests/Testably.Abstractions.Tests/FileSystem/FileStream/CopyToTests.cs
Original file line number Diff line number Diff line change
@@ -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<TFileSystem>
: FileSystemTestBase<TFileSystem>
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<ArgumentOutOfRangeException>(
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<ArgumentOutOfRangeException>(
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<ArgumentOutOfRangeException>();
}
#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<ObjectDisposedException>()
.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<ArgumentNullException>()
.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<NotSupportedException>()
.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<ObjectDisposedException>()
.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<NotSupportedException>()
.Which.Message.Should().Match("Stream does not support reading*");
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public void Operations_ShouldThrowAfterStreamIsDisposed(

exception.Should()
.BeOfType<ObjectDisposedException>(
$"\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
Expand Down
74 changes: 0 additions & 74 deletions Tests/Testably.Abstractions.Tests/FileSystem/FileStream/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentOutOfRangeException>(
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<ArgumentOutOfRangeException>(
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(
Expand Down
Loading