Skip to content

Commit 56e783c

Browse files
committed
Reject carriage returns in verified files instead of normalizing
The text comparison previously read the verified file via ReadStringBuilderWithFixedLines, silently converting \r\n and \r to \n. Now it reads the file directly and throws if it contains a \r, linking to the text-file-settings docs. Verified files are LF-only (enforced by .gitattributes), so this surfaces misconfigured line endings rather than masking them. Drop the now-unused ReadStringBuilderWithFixedLines(string) overload and rewrite the two net9 NewLineTests to assert the throw.
1 parent cfcd770 commit 56e783c

3 files changed

Lines changed: 26 additions & 39 deletions

File tree

src/Verify.Tests/NewLineTests.cs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,27 @@ public async Task StringWithDifferingNewline()
4444
{
4545
var fullPath = CurrentFile.Relative("NewLineTests.StringWithDifferingNewline.verified.txt");
4646
File.Delete(fullPath);
47+
var settings = new VerifySettings();
48+
settings.DisableRequireUniquePrefix();
49+
50+
// A verified file containing \r is rejected rather than silently normalized
4751
await File.WriteAllTextAsync(fullPath, "a\r\nb");
48-
await Verify("a\r\nb");
49-
PrefixUnique.Clear();
50-
await Verify("a\rb");
51-
PrefixUnique.Clear();
52-
await Verify("a\nb");
53-
PrefixUnique.Clear();
52+
var crlf = await Assert.ThrowsAnyAsync<Exception>(() => Verify("a\nb", settings));
53+
Assert.Contains("carriage return", crlf.ToString());
5454

55-
File.Delete(fullPath);
55+
await File.WriteAllTextAsync(fullPath, "a\rb");
56+
var cr = await Assert.ThrowsAnyAsync<Exception>(() => Verify("a\nb", settings));
57+
Assert.Contains("carriage return", cr.ToString());
58+
59+
// A verified file using \n still matches received content normalized to \n
5660
await File.WriteAllTextAsync(fullPath, "a\nb");
57-
await Verify("a\r\nb");
58-
PrefixUnique.Clear();
59-
await Verify("a\rb");
60-
PrefixUnique.Clear();
61-
await Verify("a\nb");
62-
PrefixUnique.Clear();
61+
await Verify("a\r\nb", settings);
62+
await Verify("a\rb", settings);
63+
await Verify("a\nb", settings);
6364

6465
File.Delete(fullPath);
65-
await File.WriteAllTextAsync(fullPath, "a\rb");
66-
await Verify("a\r\nb");
67-
PrefixUnique.Clear();
68-
await Verify("a\rb");
69-
PrefixUnique.Clear();
70-
await Verify("a\nb");
71-
File.Delete(fullPath);
7266
}
7367

74-
#if NET9_0
75-
7668
[Fact]
7769
public async Task TrailingNewlinesRaw()
7870
{
@@ -81,16 +73,12 @@ public async Task TrailingNewlinesRaw()
8173
var settings = new VerifySettings();
8274
settings.DisableRequireUniquePrefix();
8375

76+
// A verified file containing \r is rejected
8477
await File.WriteAllTextAsync(file, "a\r\n");
85-
await Verify("a\r\n", settings);
86-
await Verify("a\n", settings);
87-
await Verify("a", settings);
88-
89-
await File.WriteAllTextAsync(file, "a\r\n\r\n");
90-
await Verify("a\r\n\r\n", settings);
91-
await Verify("a\n\n", settings);
92-
await Verify("a\n", settings);
78+
var exception = await Assert.ThrowsAnyAsync<Exception>(() => Verify("a\n", settings));
79+
Assert.Contains("carriage return", exception.ToString());
9380

81+
// Trailing \n tolerance still applies for \n-only verified files
9482
await File.WriteAllTextAsync(file, "a\n");
9583
await Verify("a\n", settings);
9684
await Verify("a", settings);
@@ -100,7 +88,6 @@ public async Task TrailingNewlinesRaw()
10088
await Verify("a\n", settings);
10189
File.Delete(file);
10290
}
103-
#endif
10491

10592
//TODO: add test for trailing newlines
10693
// [Fact]

src/Verify/Compare/Comparer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ public static async Task<EqualityResult> Text(FilePair filePair, StringBuilder r
99
return new(Equality.New, null, received, null);
1010
}
1111

12-
var verified = await IoHelpers.ReadStringBuilderWithFixedLines(filePair.VerifiedPath);
12+
var verifiedText = await File.ReadAllTextAsync(filePair.VerifiedPath);
13+
if (verifiedText.Contains('\r'))
14+
{
15+
throw new($@"Verified file must use \n line endings, but it contains a \r (carriage return). Path: {filePair.VerifiedPath}. See https://github.com/verifytests/verify#text-file-settings");
16+
}
17+
18+
var verified = new StringBuilder(verifiedText);
1319
var result = await CompareStrings(filePair.Extension, received, verified, settings, bypassComparer);
1420
if (result.IsEqual)
1521
{
@@ -52,4 +58,4 @@ static Task<CompareResult> CompareStrings(string extension, StringBuilder receiv
5258

5359
return Task.FromResult(new CompareResult(isEqual));
5460
}
55-
}
61+
}

src/Verify/IoHelpers.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,6 @@ internal static string ResolveDirectoryFromSourceFile(string sourceFile)
208208
throw new($"Unable to resolve directory. sourceFile: {sourceFile}");
209209
}
210210

211-
public static async Task<StringBuilder> ReadStringBuilderWithFixedLines(string path)
212-
{
213-
using var stream = OpenRead(path);
214-
return await stream.ReadStringBuilderWithFixedLines();
215-
}
216-
217211
public static async Task WriteStream(string path, Stream stream)
218212
{
219213
Directory.CreateDirectory(Path.GetDirectoryName(path)!);

0 commit comments

Comments
 (0)