Skip to content

Commit ededf9d

Browse files
authored
Strip solution dir prefix from XunitV3 attachment names (#1723)
Previously attachment names were "Verify snapshot mismatch: {fullPath}" which contains a colon - invalid for GitHub Actions artifact upload on Windows-compatible file systems. Now emits the received file path relative to the solution directory, falling back to the full path when the solution dir is unknown.
1 parent 35440f8 commit ededf9d

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/Verify.XunitV3.Tests/AttachmentTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public class AttachmentTests
22
{
3-
#if NET10_0
3+
#if NET11_0
44
[Fact]
55
public async Task Simple()
66
{
@@ -10,10 +10,10 @@ public async Task Simple()
1010
File.Delete(fullPath);
1111
await Verify("Foo").AutoVerify();
1212
File.Delete(fullPath);
13-
var attachments = TestContext.Current.Attachments!;
14-
Assert.Contains(attachments.Keys,
15-
_ => _.Contains("Verify snapshot mismatch") &&
16-
_.Contains("AttachmentTests.Simple."));
13+
var key = Assert.Single(TestContext.Current.Attachments!).Key;
14+
Assert.StartsWith("Verify.XunitV3.Tests/AttachmentTests.Simple.", key);
15+
Assert.EndsWith(".received.txt", key);
16+
Assert.DoesNotContain(':', key);
1717
}
1818

1919
[Fact]
@@ -37,10 +37,10 @@ void Delete()
3737
}
3838
}
3939

40-
var attachments = TestContext.Current.Attachments!;
41-
Assert.Contains(attachments.Keys,
42-
_ => _.Contains("Verify snapshot mismatch") &&
43-
_.Contains("AttachmentTests.Simple."));
40+
var key = Assert.Single(TestContext.Current.Attachments!).Key;
41+
Assert.StartsWith("Verify.XunitV3.Tests/AttachmentTests/AttachmentTests.Simple.", key);
42+
Assert.EndsWith(".received.txt", key);
43+
Assert.DoesNotContain(':', key);
4444
}
4545
#endif
4646
}

src/Verify.XunitV3/Verifier.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,25 @@ public static partial class Verifier
55
{
66
static async Task AddFile(string path) =>
77
TestContext.Current.AddAttachment(
8-
$"Verify snapshot mismatch: {path}",
8+
GetAttachmentName(path),
99
await File.ReadAllBytesAsync(path));
1010

11+
internal static string GetAttachmentName(string path)
12+
{
13+
var fullPath = Path.GetFullPath(path).Replace('\\', '/');
14+
var solutionDir = VerifierSettings.SolutionDir;
15+
if (solutionDir is not null)
16+
{
17+
var fullSolutionDir = Path.GetFullPath(solutionDir).Replace('\\', '/').TrimEnd('/') + '/';
18+
if (fullPath.StartsWith(fullSolutionDir, StringComparison.OrdinalIgnoreCase))
19+
{
20+
return fullPath[fullSolutionDir.Length..];
21+
}
22+
}
23+
24+
return fullPath;
25+
}
26+
1127
[ModuleInitializer]
1228
[EditorBrowsable(EditorBrowsableState.Never)]
1329
public static void AddAttachmentEvents() =>

0 commit comments

Comments
 (0)