Skip to content

Commit 42b9db2

Browse files
authored
Build appended file targets per verification (#1885)
Appended files were stored as Targets, and a binary one holds a live stream. The engine disposes the stream of every target it writes, so a second Verify with the same settings instance failed with ObjectDisposedException, even though the caller had handed over re-readable bytes or a file path. Settings are reused routinely, since SettingsTask copies them per Verify call. They are now stored as factories and built once per verification: AppendContentAsFile(byte[]) opens a fresh MemoryStream over the same array, and AppendFile(string) opens the file per verification instead of holding a handle from the moment it was appended. Existence is still checked eagerly, so a missing path is still reported where it was passed. AppendFile(Stream) is unchanged and still single use: the stream is owned by the caller and cannot be replayed. That is now documented on the overload.
1 parent ff42d46 commit 42b9db2

11 files changed

Lines changed: 95 additions & 11 deletions
4.51 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
First
4.51 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Second
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
First
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Second

src/Verify.Tests/Converters/InstanceFileAppenderTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,31 @@ public Task TextBytesFluent() =>
5555
public Task TextStreamFluent() =>
5656
Verify("Foo")
5757
.AppendFile(new MemoryStream("appendedFile"u8.ToArray()));
58+
59+
// The engine disposes the stream of every target it writes, so an appended binary file
60+
// held as a live stream was dead after the first verification. Both of these are backed
61+
// by something re-readable, so reusing the settings has to work.
62+
[Fact]
63+
public async Task BinaryBytesSettingsReuse()
64+
{
65+
var reused = new VerifySettings();
66+
reused.AppendContentAsFile(new byte[] {1, 2, 3}, "bin", "appendedBytes");
67+
68+
await Verify("First", reused)
69+
.UseMethodName("BinaryBytesSettingsReuse_first");
70+
await Verify("Second", reused)
71+
.UseMethodName("BinaryBytesSettingsReuse_second");
72+
}
73+
74+
[Fact]
75+
public async Task AppendFileSettingsReuse()
76+
{
77+
var reused = new VerifySettings();
78+
reused.AppendFile("sample.png");
79+
80+
await Verify("First", reused)
81+
.UseMethodName("AppendFileSettingsReuse_first");
82+
await Verify("Second", reused)
83+
.UseMethodName("AppendFileSettingsReuse_second");
84+
}
5885
}

src/Verify/Guards.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ public static void BadParametersText(string value, [CallerArgumentExpression(nam
2828
}
2929
}
3030

31+
/// <summary>
32+
/// Kept as a fail fast for APIs that record a path and read it later, so a missing
33+
/// file is reported where the caller passed it rather than at verification time.
34+
/// </summary>
35+
public static void FileExists(string path, [CallerArgumentExpression(nameof(path))] string argumentName = "")
36+
{
37+
Ensure.NotNullOrEmpty(path, argumentName);
38+
if (!File.Exists(path))
39+
{
40+
throw new FileNotFoundException($"File not found. Path: {path}", path);
41+
}
42+
}
43+
3144
static char[] invalidPathChars = Path
3245
.GetInvalidPathChars()
3346
.Concat(invalidFileChars.Except(['/', '\\', ':']))

0 commit comments

Comments
 (0)