Skip to content

Commit

Permalink
Fix bug with non scoped stream for received file (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 19, 2023
1 parent 3f22b07 commit 2f4c772
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;msb3277;CS0436;CS1573;NU1901;NU1902;NU1903</NoWarn>
<Version>21.1.3</Version>
<Version>21.1.4</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
Expand Down
2 changes: 1 addition & 1 deletion src/Verify.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Task VerifyBytes() =>
public async Task EmptyBinary()
{
var exception = await Assert.ThrowsAsync<Exception>(() => Verify(Array.Empty<byte>(), "bin"));
Assert.Equal("Empty data is not allowed.", exception.Message);
Assert.StartsWith("Empty data is not allowed. Path: ", exception.Message);
}

[Fact]
Expand Down
18 changes: 13 additions & 5 deletions src/Verify/IoHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,12 @@ public static async Task WriteStream(string path, Stream stream)
CreateDirectory(Path.GetDirectoryName(path)!);
if (!TryCopyFileStream(path, stream))
{
await using var targetStream = OpenWrite(path);
await stream.SafeCopy(targetStream);
// keep using scope to stream is flushed
await using (var targetStream = OpenWrite(path))
{
await stream.SafeCopy(targetStream);
}

HandleEmptyFile(path);
}
}
Expand All @@ -236,8 +240,12 @@ public static async Task WriteStream(string path, Stream stream)
CreateDirectory(Path.GetDirectoryName(path));
if (!TryCopyFileStream(path, stream))
{
using var targetStream = OpenWrite(path);
await stream.SafeCopy(targetStream);
// keep using scope to stream is flushed
using (var targetStream = OpenWrite(path))
{
await stream.SafeCopy(targetStream);
}

HandleEmptyFile(path);
}
}
Expand All @@ -248,7 +256,7 @@ static void HandleEmptyFile(string path)
{
if (new FileInfo(path).Length == 0)
{
throw new("Empty data is not allowed.");
throw new($"Empty data is not allowed. Path: {path}");
}
}
}

0 comments on commit 2f4c772

Please sign in to comment.