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
22 changes: 22 additions & 0 deletions src/Verify.XunitV3.Tests/AttachmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,27 @@ void Delete()
Assert.EndsWith(".received.txt", key);
Assert.DoesNotContain(':', key);
}

[Fact]
public async Task DuplicateAttachmentName()
{
DiffEngine.BuildServerDetector.Detected = true;
var verified = Path.GetFullPath(CurrentFile.Relative("AttachmentTests.DuplicateAttachmentName.verified.txt"));
File.Delete(verified);
await File.WriteAllTextAsync(verified, "expected");
var settings = new VerifySettings();
settings.DisableRequireUniquePrefix();

// Both failing verifies share one received file name, so it is added as an attachment
// twice. Without the dedup guard in AddFile the second add throws ArgumentException
// instead of the expected VerifyException.
await Assert.ThrowsAsync<VerifyException>(() => Verify("one", settings));
await Assert.ThrowsAsync<VerifyException>(() => Verify("two", settings));

File.Delete(verified);

var key = Assert.Single(TestContext.Current.Attachments!).Key;
Assert.EndsWith(".received.txt", key);
}
#endif
}
18 changes: 14 additions & 4 deletions src/Verify.XunitV3/Verifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ namespace VerifyXunit;

public static partial class Verifier
{
static async Task AddFile(string path) =>
TestContext.Current.AddAttachment(
GetAttachmentName(path),
await File.ReadAllBytesAsync(path));
static async Task AddFile(string path)
{
var name = GetAttachmentName(path);
var context = TestContext.Current;
// A single test can produce the same received file name from multiple failing
// Verify calls (e.g. when DisableRequireUniquePrefix is used). xunit throws on a
// duplicate attachment name, so skip if it has already been added.
if (context.Attachments?.ContainsKey(name) == true)
{
return;
}

context.AddAttachment(name, await File.ReadAllBytesAsync(path));
}

internal static string GetAttachmentName(string path)
{
Expand Down
Loading