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
31 changes: 29 additions & 2 deletions docs/guids.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ public static class ModuleInitializer
<!-- endSnippet -->


### 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 -->
<a id='snippet-ScrubInlineGuidsDashedOnly'></a>
```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);
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L126-L134' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScrubInlineGuidsDashedOnly' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in the following, where the `D` format Guid is scrubbed but the hash is left untouched:

<!-- snippet: GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt -->
<a id='snippet-GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt'></a>
```txt
guid: Guid_1 hash: 5d41402abc4b2a76b9719d911017c592
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt#L1-L1' title='Snippet source file'>snippet source</a> | <a href='#snippet-GuidScrubberTests.ScrubInlineGuidsDashedOnly.verified.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## Named Guid

Specific Guids can be named. When any of those Guids are found, it will be replaced with the supplied name.
Expand Down Expand Up @@ -184,7 +211,7 @@ public Task NamedGuidFluent()
.AddNamedGuid(guid, "instanceNamed");
}
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L126-L140' title='Snippet source file'>snippet source</a> | <a href='#snippet-NamedGuidFluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L142-L156' title='Snippet source file'>snippet source</a> | <a href='#snippet-NamedGuidFluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -219,7 +246,7 @@ public Task InferredNamedGuidFluent()
.AddNamedGuid(namedGuid);
}
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L142-L156' title='Snippet source file'>snippet source</a> | <a href='#snippet-InferredNamedGuidFluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L158-L172' title='Snippet source file'>snippet source</a> | <a href='#snippet-InferredNamedGuidFluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down
11 changes: 11 additions & 0 deletions docs/mdsource/guids.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
guid: Guid_1 hash: 5d41402abc4b2a76b9719d911017c592
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
guid: 173535ae-995b-4cc6-a74e-8cd4be57039c hash: Guid_1
16 changes: 16 additions & 0 deletions src/Verify.Tests/GuidScrubberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions src/Verify/Serialization/Scrubbers/GuidFormats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace VerifyTests;

/// <summary>
/// The <see cref="Guid" /> formats that <see cref="VerifySettings.ScrubInlineGuids(GuidFormats)" /> matches.
/// </summary>
[Flags]
public enum GuidFormats
{
/// <summary>
/// The "D" format: 32 hex digits separated by hyphens (e.g. <c>00000000-0000-0000-0000-000000000000</c>).
/// The "B" and "P" formats are covered by this since they wrap the "D" format in delimiters.
/// </summary>
Dashed = 1,

/// <summary>
/// The "N" format: 32 hex digits with no separators (e.g. <c>00000000000000000000000000000000</c>).
/// 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.
/// </summary>
Undashed = 2,

/// <summary>
/// Both <see cref="Dashed" /> and <see cref="Undashed" />.
/// </summary>
All = Dashed | Undashed
}
14 changes: 14 additions & 0 deletions src/Verify/Serialization/Scrubbers/GuidMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scrubber> 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<string, object> context)
{
// Cheap prefilter: the "D" format has dashes at fixed offsets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,21 @@ public static void ScrubEmptyLines(string extension) =>
/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
public static void ScrubInlineGuids(string extension)
/// <param name="extension">The file extension to apply the scrubber to.</param>
public static void ScrubInlineGuids(string extension) =>
ScrubInlineGuids(extension, GuidFormats.All);

/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
/// <param name="extension">The file extension to apply the scrubber to.</param>
/// <param name="formats">The <see cref="Guid" /> formats to match.</param>
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);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,19 @@ public static void ScrubInlineDates(
/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
public static void ScrubInlineGuids()
public static void ScrubInlineGuids() =>
ScrubInlineGuids(GuidFormats.All);

/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
/// <param name="formats">The <see cref="Guid" /> formats to match.</param>
public static void ScrubInlineGuids(GuidFormats formats)
{
AddScrubber(GuidMatcher.Instance);
AddScrubber(GuidMatcher.NInstance);
foreach (var scrubber in GuidMatcher.ForFormats(formats))
{
AddScrubber(scrubber);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,21 @@ public void ScrubLinesContaining(string extension, StringComparison comparison,
/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
public void ScrubInlineGuids(string extension)
/// <param name="extension">The file extension to apply the scrubber to.</param>
public void ScrubInlineGuids(string extension) =>
ScrubInlineGuids(extension, GuidFormats.All);

/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
/// <param name="extension">The file extension to apply the scrubber to.</param>
/// <param name="formats">The <see cref="Guid" /> formats to match.</param>
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);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,24 @@ public void ScrubLinesContaining(StringComparison comparison, params string[] st
/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
public void ScrubInlineGuids()
public void ScrubInlineGuids() =>
ScrubInlineGuids(GuidFormats.All);

/// <summary>
/// Replace inline <see cref="Guid" />s with a placeholder.
/// </summary>
/// <param name="formats">The <see cref="Guid" /> formats to match.</param>
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);
}
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Verify/SettingsTask_Scrubbing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public SettingsTask ScrubInlineGuids()
return this;
}

/// <inheritdoc cref="VerifySettings.ScrubInlineGuids(GuidFormats)"/>
[Pure]
public SettingsTask ScrubInlineGuids(GuidFormats formats)
{
CurrentSettings.ScrubInlineGuids(formats);
return this;
}

/// <inheritdoc cref="VerifySettings.ScrubGuids()"/>
[Pure]
public SettingsTask ScrubGuids()
Expand All @@ -74,6 +82,14 @@ public SettingsTask ScrubInlineGuids(string extension)
return this;
}

/// <inheritdoc cref="VerifySettings.ScrubInlineGuids(string,GuidFormats)"/>
[Pure]
public SettingsTask ScrubInlineGuids(string extension, GuidFormats formats)
{
CurrentSettings.ScrubInlineGuids(extension, formats);
return this;
}

/// <inheritdoc cref="VerifySettings.DisableDateCounting()"/>
[Pure]
public SettingsTask DisableDateCounting()
Expand Down
Loading