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
61 changes: 61 additions & 0 deletions src/StaticSettingsTests/ResultTextEncodingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Lives here, rather than in Verify.Tests, since UseEncoding is a static setting, and this
// project runs serially with BaseTest resetting it between tests.
public class ResultTextEncodingTests :
BaseTest
{
// Latin1 writes no preamble, so nothing on disk tells a reader the file is not UTF8. Reading
// it as UTF8 turns the 0xE9 byte into U+FFFD, which is what makes this the case that catches
// VerifyResult.Text ignoring the configured encoding.
const string value = "café";

[Fact]
public async Task ReadsExistingVerifiedWithConfiguredEncoding()
{
VerifierSettings.UseEncoding(Encoding.Latin1);

using var temp = new TempDirectory();
await File.WriteAllTextAsync(VerifiedPath(temp), value, Encoding.Latin1);

var result = await Verify(value, Settings(temp));

Assert.Equal(value, result.Text);
}

[Fact]
public async Task RoundTripsWhatVerifyWrote()
{
VerifierSettings.UseEncoding(Encoding.Latin1);

using var temp = new TempDirectory();
var settings = Settings(temp);
settings.AutoVerify();

var result = await Verify(value, settings);

var file = result.Files.Single();
Assert.Equal(Encoding.Latin1.GetBytes(value), await File.ReadAllBytesAsync(file));
Assert.Equal(value, result.Text);
}

[Fact]
public async Task DefaultEncodingIsUnaffected()
{
using var temp = new TempDirectory();
await File.WriteAllTextAsync(VerifiedPath(temp), value, new UTF8Encoding(true));

var result = await Verify(value, Settings(temp));

Assert.Equal(value, result.Text);
}

static VerifySettings Settings(TempDirectory temp)
{
var settings = new VerifySettings();
settings.UseDirectory(temp);
settings.DisableDiff();
return settings;
}

static string VerifiedPath(TempDirectory temp, [CallerMemberName] string name = "") =>
temp.BuildPath($"{nameof(ResultTextEncodingTests)}.{name}.verified.txt");
}
4 changes: 3 additions & 1 deletion src/Verify/VerifyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public string Text
throw new("More than one text file in results");
}

return File.ReadAllText(textFiles[0]);
// Read with the configured encoding, the same as the compare path, so a
// BOM-less non-UTF8 UseEncoding round-trips instead of decoding to U+FFFD.
return File.ReadAllText(textFiles[0], VerifierSettings.Encoding);
}
}

Expand Down
Loading