Skip to content

Commit 56170ee

Browse files
authored
Missing UTF8 bom preamble on some runtimes (#573)
1 parent a1c2a6e commit 56170ee

10 files changed

+34
-6
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ var result = await Verify(
415415
});
416416
Assert.Contains("Value To Check", result.Text);
417417
```
418-
<sup><a href='/src/Verify.Tests/Tests.cs#L564-L573' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyresult' title='Start of snippet'>anchor</a></sup>
418+
<sup><a href='/src/Verify.Tests/Tests.cs#L585-L594' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyresult' title='Start of snippet'>anchor</a></sup>
419419
<!-- endSnippet -->
420420

421421
If using `Verifier.Throws`, the resulting `Exception` will also be accessible
@@ -426,7 +426,7 @@ If using `Verifier.Throws`, the resulting `Exception` will also be accessible
426426
var result = await Verifier.Throws(MethodThatThrows);
427427
Assert.NotNull(result.Exception);
428428
```
429-
<sup><a href='/src/Verify.Tests/Tests.cs#L609-L614' title='Snippet source file'>snippet source</a> | <a href='#snippet-exceptionresult' title='Start of snippet'>anchor</a></sup>
429+
<sup><a href='/src/Verify.Tests/Tests.cs#L630-L635' title='Snippet source file'>snippet source</a> | <a href='#snippet-exceptionresult' title='Start of snippet'>anchor</a></sup>
430430
<!-- endSnippet -->
431431

432432

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value

src/Verify.Tests/Tests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ public Task ShouldThrowForExtensionOnSerialization()
505505
return Verifier.ThrowsTask(() => Verify(element, settings))
506506
.IgnoreStackTrace();
507507
}
508+
508509
#if NET6_0
510+
509511
[Fact]
510512
public async Task StringWithUtf8Bom()
511513
{
@@ -514,8 +516,27 @@ public async Task StringWithUtf8Bom()
514516
await Verify($"{preamble}a").AutoVerify();
515517
await Verify("a").DisableRequireUniquePrefix();
516518
}
519+
517520
#endif
518521

522+
[Fact]
523+
public async Task EnsureUtf8BomPreamble()
524+
{
525+
if (BuildServerDetector.Detected)
526+
{
527+
return;
528+
}
529+
var projectDirectory = AttributeReader.GetProjectDirectory();
530+
var file = Path.Combine(projectDirectory, $"Tests.EnsureUtf8BomPreamble.{Namer.RuntimeAndVersion}.verified.txt");
531+
File.Delete(file);
532+
await Verify("value")
533+
.UniqueForRuntimeAndVersion()
534+
.AutoVerify();
535+
var fileBytes = File.ReadAllBytes(file).Take(3);
536+
var preambleBytes = Encoding.UTF8.GetPreamble();
537+
Assert.Equal(preambleBytes, fileBytes);
538+
}
539+
519540
[Fact]
520541
public Task StringExtension()
521542
{

src/Verify.Tests/Verify.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<Using Include="Newtonsoft.Json.Serialization" />
1313
<Using Include="Newtonsoft.Json.Linq" />
1414
<Using Include="Newtonsoft.Json.Converters" />
15+
<Using Include="System.Runtime.InteropServices" />
16+
<Using Include="DiffEngine" />
1517
<PackageReference Include="InfoOf.Fody" Version="2.1.1" />
1618
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
1719
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />

src/Verify/IoHelpers.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ static bool TryCopyFileStream(string path, Stream stream)
5757

5858
#if NET461 || NET472 || NET48 || NETSTANDARD2_0
5959

60-
public static async Task WriteText(string path, string text)
60+
public static Task WriteText(string path, string text)
6161
{
62-
var encodedText = Utf8.GetBytes(text);
63-
using var stream = OpenWrite(path);
64-
await stream.WriteAsync(encodedText, 0, encodedText.Length);
62+
File.WriteAllText(path, text, Utf8);
63+
return Task.CompletedTask;
6564
}
6665

6766
#else

0 commit comments

Comments
 (0)