Skip to content

Commit

Permalink
Remove parameter hashing from unsupported tfm (#920)
Browse files Browse the repository at this point in the history
* remove parameter hashing from unsupported TFM

* .
  • Loading branch information
SimonCropp authored Jun 20, 2023
1 parent 4cde1d9 commit 226e6a0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Verify.Tests/Naming/NamerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ public Task FrameworkName()
})
.UniqueForRuntimeAndVersion();
}
#if NET6_0_OR_GREATER || NETFRAMEWORK

[Theory]
[InlineData(true, false)]
Expand Down Expand Up @@ -622,4 +623,5 @@ public Task HashParametersFluent(bool a, bool b) =>
Verify("ContentHashParametersFluent")
.UseHashedParameters(a, b)
.HashParameters();
#endif
}
8 changes: 7 additions & 1 deletion src/Verify/Naming/FileNameBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO.Hashing;
#if NET6_0_OR_GREATER || NETFRAMEWORK
using System.IO.Hashing;
#endif

static class FileNameBuilder
{
Expand Down Expand Up @@ -38,14 +40,17 @@ public static string GetParameterText(IReadOnlyList<string>? methodParameters, V
builder.Length -= 1;
var parameterText = builder.ToString();

#if NET6_0_OR_GREATER || NETFRAMEWORK
if (settings.hashParameters)
{
var hashed = HashString(parameterText);
return $"_{hashed}";
}
#endif
return parameterText;
}

#if NET6_0_OR_GREATER || NETFRAMEWORK
static string HashString(string value)
{
var data = XxHash64.Hash(Encoding.UTF8.GetBytes(value));
Expand All @@ -59,4 +64,5 @@ static string HashString(string value)

return builder.ToString();
}
#endif
}
2 changes: 2 additions & 0 deletions src/Verify/Naming/ParameterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void IgnoreParametersForVerified(params object?[] parameters)
ignoreParametersForVerified = true;
}

#if NET6_0_OR_GREATER || NETFRAMEWORK
internal bool hashParameters;

/// <summary>
Expand All @@ -75,4 +76,5 @@ public void UseHashedParameters(params object?[] parameters)
UseParameters(parameters);
HashParameters();
}
#endif
}
3 changes: 3 additions & 0 deletions src/Verify/SettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public SettingsTask UniqueForRuntimeAndVersion()
return this;
}

#if NET6_0_OR_GREATER || NETFRAMEWORK
/// <summary>
/// Provide parameters to hash together and pass to <see cref="UseTextForParameters"/>.
/// Used to get a deterministic file name while avoiding long paths.
Expand All @@ -267,6 +268,8 @@ public SettingsTask HashParameters()
return this;
}

#endif

/// <summary>
/// Use the current processor architecture (x86/x64/arm/arm64) to make the test results unique.
/// Used when a test produces different results based on processor architecture.
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Verify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Polyfill" Version="1.23.0" PrivateAssets="all" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" Condition="$(TargetFrameworkIdentifier) == '.NETFramework'" />
<PackageReference Include="System.IO.Hashing" Version="7.0.0" />
<PackageReference Include="System.IO.Hashing" Version="7.0.0" Condition="$(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework) == 'net6.0' or $(TargetFramework) == 'net7.0' or $(TargetFramework) == 'net8.0'"/>
<PackageReference Include="System.Memory" Version="4.5.5" Condition="$(TargetFrameworkIdentifier) == '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp2'))" />
<Using Include="System.ReadOnlySpan&lt;System.Char&gt;" Alias="CharSpan" />
<Using Include="Argon" />
Expand Down
4 changes: 4 additions & 0 deletions src/Verify/VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public VerifySettings(VerifySettings? settings)
streamComparer = settings.streamComparer;
parameters = settings.parameters;
ignoreParametersForVerified = settings.ignoreParametersForVerified;

#if NET6_0_OR_GREATER || NETFRAMEWORK
hashParameters = settings.hashParameters;
#endif

parametersText = settings.parametersText;
fileName = settings.fileName;
UniquePrefixDisabled = settings.UniquePrefixDisabled;
Expand Down

0 comments on commit 226e6a0

Please sign in to comment.