Skip to content

Commit 648f9df

Browse files
committed
Run the NewLineTests line ending tests on all target frameworks
StringWithDifferingNewline and TrailingNewlinesRaw were each pinned to a single target framework via #if. They write fixed, unsuffixed verified files into the source directory and mutate them repeatedly, so every target framework's test run would otherwise contend over the same paths. Give each run its own paths via UniqueForRuntimeAndVersion, so all target frameworks can run both tests concurrently. Received paths are now deterministic, so the glob based cleanup collapses to direct deletes. Also disable the diff tool, since these verifies are expected to fail and were launching it, and move cleanup into a finally. The temp verified files deliberately contain \r, so a leftover one that got committed would be normalized to \n by .gitattributes and silently void the test.
1 parent fbd8306 commit 648f9df

2 files changed

Lines changed: 33 additions & 50 deletions

File tree

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ src/Verify.Tests/Tests.TextNegative.verified.tmp
1616
*.ncrunchproject
1717
*nCrunchTemp*
1818
*.cache
19-
src/Verify.Tests/NewLineTests.StringWithDifferingNewline.verified.txt
20-
src/Verify.Tests/NewLineTests.StringWithDifferingNewline.verified.json
21-
src/Verify.Tests/NewLineTests.TrailingNewlinesRaw.verified.txt
19+
src/Verify.Tests/NewLineTests.StringWithDifferingNewline.*
20+
src/Verify.Tests/NewLineTests.TrailingNewlinesRaw.*
2221
src/Verify.MSTest.Tests/Tests.AutoVerifyHasAttachment.verified.txt
2322
src/Verify.NUnit.Tests/Tests.AutoVerifyHasAttachment.verified.txt
2423
src/Verify.TUnit.Tests/Tests.AutoVerifyHasAttachment.verified.txt

src/Verify.Tests/NewLineTests.cs

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,66 +38,60 @@ public async Task WithNestedNewlineAddedByScrubber()
3838
public Task Newlines() =>
3939
Verify("a\r\nb\nc\rd\r\n");
4040

41-
#if NET9_0
4241
[Fact]
4342
public async Task StringWithDifferingNewline()
4443
{
45-
var fullPath = CurrentFile.Relative("NewLineTests.StringWithDifferingNewline.verified.txt");
44+
// This writes verified files into the source directory and mutates them repeatedly,
45+
// so every target framework has to own a distinct set of paths. UniqueForRuntimeAndVersion
46+
// puts the runtime in the name, which is what allows this to run on all of them at once.
47+
var prefix = $"NewLineTests.StringWithDifferingNewline.{Namer.RuntimeAndVersion}";
48+
var verifiedPath = CurrentFile.Relative($"{prefix}.verified.txt");
49+
var receivedPath = CurrentFile.Relative($"{prefix}.received.txt");
4650
// The suggested .gitattributes line uses the extension of the failing file,
4751
// which is not always txt
48-
var jsonPath = CurrentFile.Relative("NewLineTests.StringWithDifferingNewline.verified.json");
49-
var directory = Path.GetDirectoryName(fullPath)!;
50-
// Globbed because received carries the namer uniqueness suffix, while the verified
51-
// files above are the unsuffixed fallback.
52-
const string receivedPattern = "NewLineTests.StringWithDifferingNewline*.received.txt";
53-
const string receivedJsonPattern = "NewLineTests.StringWithDifferingNewline*.received.json";
54-
File.Delete(fullPath);
55-
File.Delete(jsonPath);
52+
var verifiedJsonPath = CurrentFile.Relative($"{prefix}.verified.json");
53+
var receivedJsonPath = CurrentFile.Relative($"{prefix}.received.json");
54+
File.Delete(verifiedPath);
55+
File.Delete(verifiedJsonPath);
5656
var settings = new VerifySettings();
57+
settings.UniqueForRuntimeAndVersion();
5758
settings.DisableRequireUniquePrefix();
5859
// Every verify below is expected to fail, so without this the diff tool is launched
5960
settings.DisableDiff();
6061

6162
try
6263
{
6364
// A verified file containing \r is rejected rather than silently normalized
64-
await File.WriteAllTextAsync(fullPath, "a\r\nb");
65+
await File.WriteAllTextAsync(verifiedPath, "a\r\nb");
6566
var crlf = await Assert.ThrowsAnyAsync<Exception>(() => Verify("a\nb", settings));
6667
Assert.Contains("carriage return", crlf.ToString());
6768

68-
await File.WriteAllTextAsync(fullPath, "a\rb");
69+
await File.WriteAllTextAsync(verifiedPath, "a\rb");
6970
var cr = await Assert.ThrowsAnyAsync<Exception>(() => Verify("a\nb", settings));
7071
Assert.Contains("carriage return", cr.ToString());
7172

7273
// The rejection writes received, so the run is not silent, and is not wrapped in a
7374
// generic "Failed to compare files" that hides the cause.
74-
foreach (var stale in Directory.EnumerateFiles(directory, receivedPattern))
75-
{
76-
File.Delete(stale);
77-
}
75+
File.Delete(receivedPath);
7876

79-
await File.WriteAllTextAsync(fullPath, "a\r\nb");
77+
await File.WriteAllTextAsync(verifiedPath, "a\r\nb");
8078
var rejection = await Assert.ThrowsAnyAsync<Exception>(() => Verify("a\nb", settings));
8179
Assert.DoesNotContain("Failed to compare files", rejection.Message);
8280
Assert.Contains("*.verified.txt text eol=lf", rejection.Message);
83-
var received = Directory.EnumerateFiles(directory, receivedPattern).Single();
84-
Assert.Equal("a\nb", await File.ReadAllTextAsync(received));
85-
File.Delete(received);
81+
Assert.Equal("a\nb", await File.ReadAllTextAsync(receivedPath));
82+
File.Delete(receivedPath);
8683

87-
await File.WriteAllTextAsync(jsonPath, "{\r\n}");
84+
await File.WriteAllTextAsync(verifiedJsonPath, "{\r\n}");
8885
var json = await Assert.ThrowsAnyAsync<Exception>(
8986
() => Verify("{\n}", extension: "json", settings: settings));
9087
Assert.Contains("*.verified.json text eol=lf", json.Message);
9188
// Inline, not only in the finally, since the verifies below would otherwise treat
9289
// the json as a dangling verified file for this test and fail on it
93-
File.Delete(jsonPath);
94-
foreach (var stale in Directory.EnumerateFiles(directory, receivedJsonPattern))
95-
{
96-
File.Delete(stale);
97-
}
90+
File.Delete(verifiedJsonPath);
91+
File.Delete(receivedJsonPath);
9892

9993
// A verified file using \n still matches received content normalized to \n
100-
await File.WriteAllTextAsync(fullPath, "a\nb");
94+
await File.WriteAllTextAsync(verifiedPath, "a\nb");
10195
await Verify("a\r\nb", settings);
10296
await Verify("a\rb", settings);
10397
await Verify("a\nb", settings);
@@ -106,13 +100,10 @@ public async Task StringWithDifferingNewline()
106100
{
107101
// In a finally since these deliberately contain \r. A leftover verified file is
108102
// normalized to \n by .gitattributes if committed, silently voiding this test.
109-
File.Delete(fullPath);
110-
File.Delete(jsonPath);
111-
foreach (var stale in Directory.EnumerateFiles(directory, receivedPattern)
112-
.Concat(Directory.EnumerateFiles(directory, receivedJsonPattern)))
113-
{
114-
File.Delete(stale);
115-
}
103+
File.Delete(verifiedPath);
104+
File.Delete(receivedPath);
105+
File.Delete(verifiedJsonPath);
106+
File.Delete(receivedJsonPath);
116107
}
117108
}
118109

@@ -140,19 +131,16 @@ public async Task StringWithDifferingNewline()
140131
// await Verify(target, settings);
141132
// }
142133

143-
#endif
144-
145-
#if NET10_0
146134
[Fact]
147135
public async Task TrailingNewlinesRaw()
148136
{
149-
var file = CurrentFile.Relative("NewLineTests.TrailingNewlinesRaw.verified.txt");
150-
var directory = Path.GetDirectoryName(file)!;
151-
// Globbed because received carries the namer uniqueness suffix, while the verified
152-
// file above is the unsuffixed fallback.
153-
const string receivedPattern = "NewLineTests.TrailingNewlinesRaw*.received.txt";
137+
// Per target framework paths, for the same reason as StringWithDifferingNewline above
138+
var prefix = $"NewLineTests.TrailingNewlinesRaw.{Namer.RuntimeAndVersion}";
139+
var file = CurrentFile.Relative($"{prefix}.verified.txt");
140+
var receivedPath = CurrentFile.Relative($"{prefix}.received.txt");
154141
File.Delete(file);
155142
var settings = new VerifySettings();
143+
settings.UniqueForRuntimeAndVersion();
156144
settings.DisableRequireUniquePrefix();
157145
// Several verifies below are expected to fail, so without this the diff tool is launched
158146
settings.DisableDiff();
@@ -178,11 +166,7 @@ public async Task TrailingNewlinesRaw()
178166
// Deleting verified orphans received, so no subsequent run reconciles it, and
179167
// DiffEngineTray shows it as a pending change forever.
180168
File.Delete(file);
181-
foreach (var stale in Directory.EnumerateFiles(directory, receivedPattern))
182-
{
183-
File.Delete(stale);
184-
}
169+
File.Delete(receivedPath);
185170
}
186171
}
187-
#endif
188172
}

0 commit comments

Comments
 (0)