Skip to content

Commit

Permalink
Handle when stream.length throws not supported stream (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Oct 16, 2023
1 parent 4b72b8b commit c07ac88
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Task StreamWithExtension()
return Verify(stream, "png");
}
```
<sup><a href='/src/Verify.Tests/StreamTests.cs#L81-L90' title='Snippet source file'>snippet source</a> | <a href='#snippet-streamwithextension' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/StreamTests.cs#L98-L107' title='Snippet source file'>snippet source</a> | <a href='#snippet-streamwithextension' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

For a `FileStream` the extension is not required:
Expand All @@ -33,7 +33,7 @@ For a `FileStream` the extension is not required:
public Task FileStream() =>
Verify(File.OpenRead("sample.txt"));
```
<sup><a href='/src/Verify.Tests/StreamTests.cs#L92-L98' title='Snippet source file'>snippet source</a> | <a href='#snippet-filestream' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/StreamTests.cs#L109-L115' title='Snippet source file'>snippet source</a> | <a href='#snippet-filestream' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion docs/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ public static string NameWithParent(this Type type)
return type.Name;
}
```
<sup><a href='/src/Verify/Extensions.cs#L85-L97' title='Snippet source file'>snippet source</a> | <a href='#snippet-namewithparent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify/Extensions.cs#L89-L101' title='Snippet source file'>snippet source</a> | <a href='#snippet-namewithparent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
4 changes: 2 additions & 2 deletions docs/verify-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Verifies the contents of a file.
public Task VerifyFilePath() =>
VerifyFile("sample.txt");
```
<sup><a href='/src/Verify.Tests/StreamTests.cs#L176-L182' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfile' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/StreamTests.cs#L193-L199' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfile' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -33,7 +33,7 @@ public Task VerifyFileWithInfo() =>
"sample.txt",
info: "the info");
```
<sup><a href='/src/Verify.Tests/StreamTests.cs#L195-L203' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfilewithinfo' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/StreamTests.cs#L212-L220' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfilewithinfo' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion docs/verify-xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Verifies Xml:
public Task VerifyFilePath() =>
VerifyFile("sample.txt");
```
<sup><a href='/src/Verify.Tests/StreamTests.cs#L176-L182' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfile' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/StreamTests.cs#L193-L199' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfile' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
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>22.1.3</Version>
<Version>22.1.4</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions src/Verify.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ public Task Stream() =>
1
}));

class LengthNotSupportedStream(byte[] bytes) : MemoryStream(bytes)
{
public override long Length =>
throw new NotSupportedException();
}

[Fact]
public Task LengthNotSupportedException() =>
Verify(
new LengthNotSupportedStream(
new byte[]
{
1
}));

[Fact]
public Task StreamTask() =>
Verify(
Expand Down
4 changes: 4 additions & 0 deletions src/Verify/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ static bool CanReadLength(this Stream stream)
{
return false;
}
catch (NotSupportedException)
{
return false;
}

return true;
}
Expand Down

0 comments on commit c07ac88

Please sign in to comment.