From 34b490bbd4c7e86a4ec12ea4bf4c2abe83e545d3 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 14 Nov 2024 09:15:53 +1100 Subject: [PATCH] remove static uses of PrettyMarkupFormatter PrettyMarkupFormatter is not thread safe https://github.com/VerifyTests/Verify.Bunit/issues/60 --- src/bunit.web/Diffing/DiffMarkupFormatter.cs | 1 + src/bunit.web/Extensions/NodePrintExtensions.cs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bunit.web/Diffing/DiffMarkupFormatter.cs b/src/bunit.web/Diffing/DiffMarkupFormatter.cs index ccb033627..4feffd455 100644 --- a/src/bunit.web/Diffing/DiffMarkupFormatter.cs +++ b/src/bunit.web/Diffing/DiffMarkupFormatter.cs @@ -16,6 +16,7 @@ public class DiffMarkupFormatter : PrettyMarkupFormatter, IMarkupFormatter /// The is not thread safe, so using this singleton /// instance to format elements may not result in the desired effect. /// + [Obsolete("This instance is not thread safe, use a new instance instead.")] public static new readonly DiffMarkupFormatter Instance = new(); /// diff --git a/src/bunit.web/Extensions/NodePrintExtensions.cs b/src/bunit.web/Extensions/NodePrintExtensions.cs index c54924824..d113d2642 100644 --- a/src/bunit.web/Extensions/NodePrintExtensions.cs +++ b/src/bunit.web/Extensions/NodePrintExtensions.cs @@ -109,6 +109,7 @@ public static string ToMarkupElementOnly(this IElement element) if (element is null) throw new ArgumentNullException(nameof(element)); + var diffMarkupFormatter = new DiffMarkupFormatter(); var result = new StringBuilder(); result.Append(Symbols.LessThan); @@ -120,7 +121,7 @@ public static string ToMarkupElementOnly(this IElement element) foreach (var attribute in element.Attributes) { - result.Append(' ').Append(DiffMarkupFormatter.Instance.ConvertToString(attribute)); + result.Append(' ').Append(diffMarkupFormatter.ConvertToString(attribute)); } if (element.HasChildNodes)