From 383ad4e44ff432f8c1ca59736d3627b449d89ed5 Mon Sep 17 00:00:00 2001 From: Xavier Solau Date: Mon, 4 May 2026 23:42:54 +0200 Subject: [PATCH 1/2] Add Encoding support to TextSnapshotStrategy and SnapshotTestBuilder. --- .../Snapshot/ISnapshotTestBuilder.cs | 16 ++++++++- .../Snapshot/Impl/TextSnapshotStrategy.cs | 9 +++-- .../Snapshot/SnapshotTestBuilder.cs | 10 ++++-- .../Snapshot/SnapshotTestBuilderTest.cs | 35 +++++++++++++++++++ 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/ISnapshotTestBuilder.cs b/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/ISnapshotTestBuilder.cs index 3469ea4..d3f2a9d 100644 --- a/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/ISnapshotTestBuilder.cs +++ b/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/ISnapshotTestBuilder.cs @@ -8,6 +8,7 @@ using System.IO; using System.Runtime.CompilerServices; +using System.Text; namespace SoloX.CodeQuality.Test.Helpers.Snapshot { @@ -57,6 +58,18 @@ public interface ISnapshotTestBuilder /// An updated snapshot test builder configured to use PNG image comparison. ISnapshotTestBuilder WithPngStrategy(double differencesThreshold = 0.0); + /// + /// Configures the snapshot test builder to use a UTF-8 encoded text-based comparison strategy. + /// + /// Use this method when the expected and actual values should be compared as plain text using UTF-8 encoding, + /// such as for verifying file contents or string outputs. + /// Indicates whether to ignore differences in whitespace when comparing text. If true, all whitespace characters + /// are treated as equivalent, and differences in whitespace will not cause the test to fail. Default is true. + /// Indicates whether to ignore case differences when comparing text. If true, uppercase and lowercase characters + /// are treated as equivalent, and differences in case will not cause the test to fail. Default is false. + /// An instance of the snapshot test builder configured to compare string content using a UTF-8 text strategy. + ISnapshotTestBuilder WithUtf8TextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false); + /// /// Configures the snapshot test builder to use a text-based comparison strategy. /// @@ -66,8 +79,9 @@ public interface ISnapshotTestBuilder /// are treated as equivalent, and differences in whitespace will not cause the test to fail. Default is true. /// Indicates whether to ignore case differences when comparing text. If true, uppercase and lowercase characters /// are treated as equivalent, and differences in case will not cause the test to fail. Default is false. + /// The text encoding to use when reading and writing text files. If null, the default encoding will be used. /// An instance of the snapshot test builder configured to compare string content using a text strategy. - ISnapshotTestBuilder WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false); + ISnapshotTestBuilder WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null); } /// diff --git a/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/Impl/TextSnapshotStrategy.cs b/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/Impl/TextSnapshotStrategy.cs index 0e66922..511f446 100644 --- a/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/Impl/TextSnapshotStrategy.cs +++ b/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/Impl/TextSnapshotStrategy.cs @@ -7,6 +7,7 @@ // ---------------------------------------------------------------------- using System.IO; +using System.Text; using System.Threading.Tasks; using DiffPlex.Renderer; @@ -23,14 +24,16 @@ public class TextSnapshotStrategy : ISnapshotStrategy { private readonly bool ignoreWhitespace; private readonly bool ignoreCase; + private readonly Encoding encoding; /// public string FileExtension => "txt"; - public TextSnapshotStrategy(bool ignoreWhitespace = true, bool ignoreCase = false) + public TextSnapshotStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null) { this.ignoreWhitespace = ignoreWhitespace; this.ignoreCase = ignoreCase; + this.encoding = encoding ?? Encoding.Default; } /// @@ -41,13 +44,13 @@ public Task SaveAsync(string snapshotFile, string snapshotData) File.Delete(snapshotFile); } - return File.WriteAllTextAsync(snapshotFile, snapshotData); + return File.WriteAllTextAsync(snapshotFile, snapshotData, this.encoding); } /// public async Task> CompareAsync(string snapshotReferenceFile, string snapshotData) { - var referenceText = await File.ReadAllTextAsync(snapshotReferenceFile).ConfigureAwait(false); + var referenceText = await File.ReadAllTextAsync(snapshotReferenceFile, this.encoding).ConfigureAwait(false); var snapshotDiffs = UnidiffRenderer.GenerateUnidiff( referenceText, snapshotData, diff --git a/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/SnapshotTestBuilder.cs b/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/SnapshotTestBuilder.cs index acd88e4..3b5fd8a 100644 --- a/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/SnapshotTestBuilder.cs +++ b/src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/SnapshotTestBuilder.cs @@ -9,6 +9,7 @@ using System; using System.IO; using System.Runtime.CompilerServices; +using System.Text; using SoloX.CodeQuality.Test.Helpers.Snapshot.Impl; namespace SoloX.CodeQuality.Test.Helpers.Snapshot @@ -76,9 +77,14 @@ public ISnapshotTestBuilder WithPngStrategy(double differencesThreshold return builder; } - public ISnapshotTestBuilder WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false) + public ISnapshotTestBuilder WithUtf8TextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false) { - var builder = new SnapshotTestBuilderInternal(this, new TextSnapshotStrategy(ignoreWhitespace, ignoreCase)); + return WithTextStrategy(ignoreWhitespace, ignoreCase, Encoding.UTF8); + } + + public ISnapshotTestBuilder WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null) + { + var builder = new SnapshotTestBuilderInternal(this, new TextSnapshotStrategy(ignoreWhitespace, ignoreCase, encoding)); return builder; } diff --git a/src/tests/SoloX.CodeQuality.Test.Helpers.UTest/Snapshot/SnapshotTestBuilderTest.cs b/src/tests/SoloX.CodeQuality.Test.Helpers.UTest/Snapshot/SnapshotTestBuilderTest.cs index a35e04c..670ab88 100644 --- a/src/tests/SoloX.CodeQuality.Test.Helpers.UTest/Snapshot/SnapshotTestBuilderTest.cs +++ b/src/tests/SoloX.CodeQuality.Test.Helpers.UTest/Snapshot/SnapshotTestBuilderTest.cs @@ -6,6 +6,7 @@ // // ---------------------------------------------------------------------- +using System.Text; using Shouldly; using SoloX.CodeQuality.Test.Helpers.Snapshot; using Xunit; @@ -48,6 +49,40 @@ public async Task ItShouldGenerateTextSnapshotAsync() } } + [Fact] + public async Task ItShouldGenerateUtf8TextSnapshotAsync() + { + var sh = SnapshotTestBuilder + .Create() + .WithLocation(".") + .WithUtf8TextStrategy() + .Build(); + + var expectedFile = @$"./Snapshots/{nameof(ItShouldGenerateUtf8TextSnapshotAsync)}.snapshot.ref.txt"; + + try + { + var someGeneratedText = "some generated utf8 text"; + + await sh.CompareSnapshotAsync(nameof(ItShouldGenerateUtf8TextSnapshotAsync), someGeneratedText); + + // Check that the snapshot reference file exists and has been generated with the same content as the generated text + File.Exists(expectedFile) + .ShouldBeTrue(); + + var generatedText = await File.ReadAllTextAsync(expectedFile, Encoding.UTF8); + + generatedText.ShouldBe(someGeneratedText); + } + finally + { + if (File.Exists(expectedFile)) + { + File.Delete(expectedFile); + } + } + } + [Fact] public async Task ItShouldReplaceTextSnapshotAsync() { From 6fef8e449562ddcb11b41806a7a4f5e095d55fd5 Mon Sep 17 00:00:00 2001 From: Xavier Solau Date: Tue, 5 May 2026 00:14:26 +0200 Subject: [PATCH 2/2] Bump to version 3.0.1-preview.2 --- README.md | 42 +++++++++++++++++++------------------- src/SharedProperties.props | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b855acd..92e7631 100644 --- a/README.md +++ b/README.md @@ -57,26 +57,26 @@ You can checkout this Github repository or you can use the NuGet package: **Install using the command line from the Package Manager:** ```bash -Install-Package SoloX.CodeQuality.Prod -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Prod -version 3.0.1-preview.2 or -Install-Package SoloX.CodeQuality.Test -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Test -version 3.0.1-preview.2 ``` **Install using the .Net CLI:** ```bash -dotnet add package SoloX.CodeQuality.Prod --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Prod --version 3.0.1-preview.2 or -dotnet add package SoloX.CodeQuality.Test --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Test --version 3.0.1-preview.2 ``` **Install editing your project file (csproj):** ```xml - + all runtime; build; native; contentfiles; analyzers or - + all runtime; build; native; contentfiles; analyzers @@ -223,17 +223,17 @@ You can checkout this Github repository or use the NuGet package: **Install using the command line from the Package Manager:** ```bash -Install-Package SoloX.CodeQuality.Playwright -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Playwright -version 3.0.1-preview.2 ``` **Install using the .Net CLI:** ```bash -dotnet add package SoloX.CodeQuality.Playwright --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Playwright --version 3.0.1-preview.2 ``` **Install editing your project file (csproj):** ```xml - + ``` * * * @@ -429,35 +429,35 @@ You can checkout this Github repository or you can use the NuGet package: **Install using the command line from the Package Manager:** ```bash -Install-Package SoloX.CodeQuality.Test.Helpers -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Test.Helpers -version 3.0.1-preview.2 -Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 3.0.1-preview.2 -Install-Package SoloX.CodeQuality.Test.Helpers.XUnit.V3 -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Test.Helpers.XUnit.V3 -version 3.0.1-preview.2 -Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 3.0.1-preview.1 +Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 3.0.1-preview.2 ``` **Install using the .Net CLI:** ```bash -dotnet add package SoloX.CodeQuality.Test.Helpers --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Test.Helpers --version 3.0.1-preview.2 -dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 3.0.1-preview.2 -dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit.V3 --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit.V3 --version 3.0.1-preview.2 -dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 3.0.1-preview.1 +dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 3.0.1-preview.2 ``` **Install editing your project file (csproj):** ```xml - + - + - + - + ``` * * * diff --git a/src/SharedProperties.props b/src/SharedProperties.props index 51129d9..7564b9b 100644 --- a/src/SharedProperties.props +++ b/src/SharedProperties.props @@ -1,7 +1,7 @@ - 3.0.1-preview.1 + 3.0.1-preview.2 Xavier Solau Copyright © 2021-2026 Xavier Solau MIT