diff --git a/docs/guids.md b/docs/guids.md index 503f9cde1..55adea984 100644 --- a/docs/guids.md +++ b/docs/guids.md @@ -139,6 +139,33 @@ public static class ModuleInitializer +### Limiting to specific formats + +By default both the `D` and `N` formats are scrubbed. To restrict scrubbing to specific formats, pass a `GuidFormats` value. This is useful for avoiding the scrubbing of 32 character hex content (an MD5 hash for example) that would otherwise match the `N` format: + + + +```cs +// Only the "D" format is scrubbed. The 32 char hex hash is left untouched. +[Fact] +public Task ScrubInlineGuidsDashedOnly() => + Verify("guid: 173535ae-995b-4cc6-a74e-8cd4be57039c hash: 5d41402abc4b2a76b9719d911017c592") + .ScrubInlineGuids(GuidFormats.Dashed); +``` +snippet source | anchor + + +Results in the following, where the `D` format Guid is scrubbed but the hash is left untouched: + + + +```txt +guid: Guid_1 hash: 5d41402abc4b2a76b9719d911017c592 +``` +snippet source | anchor + + + ## Named Guid Specific Guids can be named. When any of those Guids are found, it will be replaced with the supplied name. @@ -184,7 +211,7 @@ public Task NamedGuidFluent() .AddNamedGuid(guid, "instanceNamed"); } ``` -snippet source | anchor +snippet source | anchor @@ -219,7 +246,7 @@ public Task InferredNamedGuidFluent() .AddNamedGuid(namedGuid); } ``` -snippet source | anchor +snippet source | anchor Result: diff --git a/docs/mdsource/guids.source.md b/docs/mdsource/guids.source.md index a8d6127b4..b3a781a27 100644 --- a/docs/mdsource/guids.source.md +++ b/docs/mdsource/guids.source.md @@ -51,6 +51,17 @@ snippet: ScrubInlineGuidsFluent snippet: ScrubInlineGuidsGlobal +### Limiting to specific formats + +By default both the `D` and `N` formats are scrubbed. To restrict scrubbing to specific formats, pass a `GuidFormats` value. This is useful for avoiding the scrubbing of 32 character hex content (an MD5 hash for example) that would otherwise match the `N` format: + +snippet: ScrubInlineGuidsDashedOnly + +Results in the following, where the `D` format Guid is scrubbed but the hash is left untouched: + +snippet: GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt + + ## Named Guid Specific Guids can be named. When any of those Guids are found, it will be replaced with the supplied name. diff --git a/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt b/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt new file mode 100644 index 000000000..9e53577b9 --- /dev/null +++ b/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt @@ -0,0 +1 @@ +guid: Guid_1 hash: 5d41402abc4b2a76b9719d911017c592 \ No newline at end of file diff --git a/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsUndashedOnly.verified.txt b/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsUndashedOnly.verified.txt new file mode 100644 index 000000000..a8e071fcd --- /dev/null +++ b/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsUndashedOnly.verified.txt @@ -0,0 +1 @@ +guid: 173535ae-995b-4cc6-a74e-8cd4be57039c hash: Guid_1 \ No newline at end of file diff --git a/src/Verify.Tests/GuidScrubberTests.cs b/src/Verify.Tests/GuidScrubberTests.cs index ac83d2b95..e18ba9b05 100644 --- a/src/Verify.Tests/GuidScrubberTests.cs +++ b/src/Verify.Tests/GuidScrubberTests.cs @@ -123,6 +123,22 @@ public Task InlineNamedGuidNFormat() => Verify("value: c8eeaf99d5c4434185434597c3fd40c9") .ScrubInlineGuids(); + #region ScrubInlineGuidsDashedOnly + + // Only the "D" format is scrubbed. The 32 char hex hash is left untouched. + [Fact] + public Task ScrubInlineGuidsDashedOnly() => + Verify("guid: 173535ae-995b-4cc6-a74e-8cd4be57039c hash: 5d41402abc4b2a76b9719d911017c592") + .ScrubInlineGuids(GuidFormats.Dashed); + + #endregion + + // Only the "N" format is scrubbed. The "D" format guid is left untouched. + [Fact] + public Task ScrubInlineGuidsUndashedOnly() => + Verify("guid: 173535ae-995b-4cc6-a74e-8cd4be57039c hash: 5d41402abc4b2a76b9719d911017c592") + .ScrubInlineGuids(GuidFormats.Undashed); + #region NamedGuidFluent [Fact] diff --git a/src/Verify/Serialization/Scrubbers/GuidFormats.cs b/src/Verify/Serialization/Scrubbers/GuidFormats.cs new file mode 100644 index 000000000..e497f7fcf --- /dev/null +++ b/src/Verify/Serialization/Scrubbers/GuidFormats.cs @@ -0,0 +1,26 @@ +namespace VerifyTests; + +/// +/// The formats that matches. +/// +[Flags] +public enum GuidFormats +{ + /// + /// The "D" format: 32 hex digits separated by hyphens (e.g. 00000000-0000-0000-0000-000000000000). + /// The "B" and "P" formats are covered by this since they wrap the "D" format in delimiters. + /// + Dashed = 1, + + /// + /// The "N" format: 32 hex digits with no separators (e.g. 00000000000000000000000000000000). + /// Note that any 32 character hex sequence (an MD5 hash for example) is a valid "N" format Guid, so + /// content of that exact length will also be scrubbed. + /// + Undashed = 2, + + /// + /// Both and . + /// + All = Dashed | Undashed +} diff --git a/src/Verify/Serialization/Scrubbers/GuidMatcher.cs b/src/Verify/Serialization/Scrubbers/GuidMatcher.cs index 68853261a..8ec9c7e35 100644 --- a/src/Verify/Serialization/Scrubbers/GuidMatcher.cs +++ b/src/Verify/Serialization/Scrubbers/GuidMatcher.cs @@ -26,6 +26,20 @@ static class GuidMatcher static counter => counter.ScrubGuids, requireWordBoundary: true); + // The scrubbers for the requested formats, in the order they should be applied. + public static IEnumerable ForFormats(GuidFormats formats) + { + if ((formats & GuidFormats.Dashed) != 0) + { + yield return Instance; + } + + if ((formats & GuidFormats.Undashed) != 0) + { + yield return NInstance; + } + } + static string? Match(CharSpan window, Counter counter, IReadOnlyDictionary context) { // Cheap prefilter: the "D" format has dashes at fixed offsets diff --git a/src/Verify/Serialization/Scrubbers/VerifierSettings_ExtensionMappedGlobalScrubbers.cs b/src/Verify/Serialization/Scrubbers/VerifierSettings_ExtensionMappedGlobalScrubbers.cs index 36edbd50b..a3a6a9799 100644 --- a/src/Verify/Serialization/Scrubbers/VerifierSettings_ExtensionMappedGlobalScrubbers.cs +++ b/src/Verify/Serialization/Scrubbers/VerifierSettings_ExtensionMappedGlobalScrubbers.cs @@ -90,10 +90,21 @@ public static void ScrubEmptyLines(string extension) => /// /// Replace inline s with a placeholder. /// - public static void ScrubInlineGuids(string extension) + /// The file extension to apply the scrubber to. + public static void ScrubInlineGuids(string extension) => + ScrubInlineGuids(extension, GuidFormats.All); + + /// + /// Replace inline s with a placeholder. + /// + /// The file extension to apply the scrubber to. + /// The formats to match. + public static void ScrubInlineGuids(string extension, GuidFormats formats) { - AddScrubber(extension, GuidMatcher.Instance); - AddScrubber(extension, GuidMatcher.NInstance); + foreach (var scrubber in GuidMatcher.ForFormats(formats)) + { + AddScrubber(extension, scrubber); + } } /// diff --git a/src/Verify/Serialization/Scrubbers/VerifierSettings_GlobalScrubbers.cs b/src/Verify/Serialization/Scrubbers/VerifierSettings_GlobalScrubbers.cs index c26a5339a..b21b1d910 100644 --- a/src/Verify/Serialization/Scrubbers/VerifierSettings_GlobalScrubbers.cs +++ b/src/Verify/Serialization/Scrubbers/VerifierSettings_GlobalScrubbers.cs @@ -127,10 +127,19 @@ public static void ScrubInlineDates( /// /// Replace inline s with a placeholder. /// - public static void ScrubInlineGuids() + public static void ScrubInlineGuids() => + ScrubInlineGuids(GuidFormats.All); + + /// + /// Replace inline s with a placeholder. + /// + /// The formats to match. + public static void ScrubInlineGuids(GuidFormats formats) { - AddScrubber(GuidMatcher.Instance); - AddScrubber(GuidMatcher.NInstance); + foreach (var scrubber in GuidMatcher.ForFormats(formats)) + { + AddScrubber(scrubber); + } } /// diff --git a/src/Verify/Serialization/Scrubbers/VerifySettings_ExtensionMappedInstanceScrubbers.cs b/src/Verify/Serialization/Scrubbers/VerifySettings_ExtensionMappedInstanceScrubbers.cs index d31d9cb3a..5675b4718 100644 --- a/src/Verify/Serialization/Scrubbers/VerifySettings_ExtensionMappedInstanceScrubbers.cs +++ b/src/Verify/Serialization/Scrubbers/VerifySettings_ExtensionMappedInstanceScrubbers.cs @@ -78,10 +78,21 @@ public void ScrubLinesContaining(string extension, StringComparison comparison, /// /// Replace inline s with a placeholder. /// - public void ScrubInlineGuids(string extension) + /// The file extension to apply the scrubber to. + public void ScrubInlineGuids(string extension) => + ScrubInlineGuids(extension, GuidFormats.All); + + /// + /// Replace inline s with a placeholder. + /// + /// The file extension to apply the scrubber to. + /// The formats to match. + public void ScrubInlineGuids(string extension, GuidFormats formats) { - AddScrubber(extension, GuidMatcher.Instance); - AddScrubber(extension, GuidMatcher.NInstance); + foreach (var scrubber in GuidMatcher.ForFormats(formats)) + { + AddScrubber(extension, scrubber); + } } /// diff --git a/src/Verify/Serialization/Scrubbers/VerifySettings_InstanceScrubbers.cs b/src/Verify/Serialization/Scrubbers/VerifySettings_InstanceScrubbers.cs index 04cebe482..f74b97d7b 100644 --- a/src/Verify/Serialization/Scrubbers/VerifySettings_InstanceScrubbers.cs +++ b/src/Verify/Serialization/Scrubbers/VerifySettings_InstanceScrubbers.cs @@ -85,15 +85,24 @@ public void ScrubLinesContaining(StringComparison comparison, params string[] st /// /// Replace inline s with a placeholder. /// - public void ScrubInlineGuids() + public void ScrubInlineGuids() => + ScrubInlineGuids(GuidFormats.All); + + /// + /// Replace inline s with a placeholder. + /// + /// The formats to match. + public void ScrubInlineGuids(GuidFormats formats) { if (serialization.ScrubGuids == false) { throw new("ScrubGuids is disabled. Call .ScrubGuids() before calling .ScrubInlineGuids()."); } - AddScrubber(GuidMatcher.Instance); - AddScrubber(GuidMatcher.NInstance); + foreach (var scrubber in GuidMatcher.ForFormats(formats)) + { + AddScrubber(scrubber); + } } /// diff --git a/src/Verify/SettingsTask_Scrubbing.cs b/src/Verify/SettingsTask_Scrubbing.cs index 6de2afef6..a69dd1fbb 100644 --- a/src/Verify/SettingsTask_Scrubbing.cs +++ b/src/Verify/SettingsTask_Scrubbing.cs @@ -50,6 +50,14 @@ public SettingsTask ScrubInlineGuids() return this; } + /// + [Pure] + public SettingsTask ScrubInlineGuids(GuidFormats formats) + { + CurrentSettings.ScrubInlineGuids(formats); + return this; + } + /// [Pure] public SettingsTask ScrubGuids() @@ -74,6 +82,14 @@ public SettingsTask ScrubInlineGuids(string extension) return this; } + /// + [Pure] + public SettingsTask ScrubInlineGuids(string extension, GuidFormats formats) + { + CurrentSettings.ScrubInlineGuids(extension, formats); + return this; + } + /// [Pure] public SettingsTask DisableDateCounting()