Skip to content

Commit

Permalink
Add static auto verify (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 3, 2022
1 parent f9b48ce commit 502105a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/mdsource/verify-options.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ This can be done using `AutoVerify()`:

snippet: AutoVerify

Or globally

snippet: StaticAutoVerify

Note that auto accepted changes in `.verified.` files remain visible in source control tooling.


Expand Down
10 changes: 10 additions & 0 deletions docs/verify-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ settings.AutoVerify();
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L97-L102' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally

<!-- snippet: StaticAutoVerify -->
<a id='snippet-staticautoverify'></a>
```cs
VerifierSettings.AutoVerify();
```
<sup><a href='/src/AutoVerifyTests/Tests.cs#L6-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-staticautoverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Note that auto accepted changes in `.verified.` files remain visible in source control tooling.


Expand Down
27 changes: 27 additions & 0 deletions src/AutoVerifyTests/AutoVerifyTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<IsPackageProject>false</IsPackageProject>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
<PackageReference Include="ProjectDefaults" Version="1.0.78" PrivateAssets="all" />
<ProjectReference Include="..\Verify.Xunit\Verify.Xunit.csproj" />
<ProjectReference Include="..\Verify\Verify.csproj" />
</ItemGroup>
<Import Project="$(ProjectDir)..\Verify\buildTransitive\Verify.props" />
<Import Project="$(ProjectDir)..\Verify\buildTransitive\Verify.targets" />
<Import Project="$(ProjectDir)..\Verify.Xunit\buildTransitive\Verify.Xunit.props" />
<!--Work around: https://github.com/dotnet/sdk/issues/21006-->
<Target Name="DisableAnalyzers" BeforeTargets="CoreCompile">
<ItemGroup>
<Analyzer Remove="@(Analyzer)" />
</ItemGroup>
</Target>
</Project>
23 changes: 23 additions & 0 deletions src/AutoVerifyTests/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[UsesVerify]
public class Tests
{
static Tests()
{
#region StaticAutoVerify

VerifierSettings.AutoVerify();

#endregion
}


[Fact]
public async Task Simple()
{
var path = Path.Combine(AttributeReader.GetProjectDirectory(), "Tests.Simple.verified.txt");
var fullPath = Path.GetFullPath(path);
File.Delete(fullPath);
await Verify("Foo");
File.Delete(fullPath);
}
}
8 changes: 8 additions & 0 deletions src/Verify.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplyScrubbersTests", "Appl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullComparer", "NullComparer\NullComparer.csproj", "{3C759E19-3482-48A7-B2E2-95DA5E1EF159}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoVerifyTests", "AutoVerifyTests\AutoVerifyTests.csproj", "{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -239,6 +241,12 @@ Global
{3C759E19-3482-48A7-B2E2-95DA5E1EF159}.DebugNet6|Any CPU.Build.0 = DebugNet6|Any CPU
{3C759E19-3482-48A7-B2E2-95DA5E1EF159}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C759E19-3482-48A7-B2E2-95DA5E1EF159}.Release|Any CPU.Build.0 = Release|Any CPU
{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}.DebugNet6|Any CPU.ActiveCfg = DebugNet6|Any CPU
{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}.DebugNet6|Any CPU.Build.0 = DebugNet6|Any CPU
{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E3F9E6E-72D9-4D40-90EE-933B4B1C26DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions src/Verify/Verifier/VerifyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public async Task ThrowIfRequired()
await ProcessNew();

await ProcessNotEquals();
if (!settings.autoVerify)
if (!settings.IsAutoVerify)
{
var message = VerifyExceptionMessageBuilder.Build(directory, @new, notEquals, delete, equal);
throw new VerifyException(message);
Expand All @@ -141,7 +141,7 @@ async Task ProcessDeletes(string file)
{
await VerifierSettings.RunOnVerifyDelete(file);

if (settings.autoVerify)
if (settings.IsAutoVerify)
{
File.Delete(file);
return;
Expand Down Expand Up @@ -187,7 +187,7 @@ Task RunDiffAutoCheck(FilePair file)
return Task.CompletedTask;
}

if (settings.autoVerify)
if (settings.IsAutoVerify)
{
AcceptChanges(file);
return Task.CompletedTask;
Expand Down
24 changes: 23 additions & 1 deletion src/Verify/VerifierSettings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
namespace VerifyTests;
using DiffEngine;

namespace VerifyTests;

public static partial class VerifierSettings
{
internal static bool omitContentFromException;

public static void OmitContentFromException() =>
omitContentFromException = true;

/// <summary>
/// Automatically accept the results of all tests.
/// </summary>
public static void AutoVerify(bool includeBuildServer = true)
{
if (includeBuildServer)
{
autoVerify = true;
}
else
{
if (!BuildServerDetector.Detected)
{
autoVerify = true;
}
}
}

internal static bool autoVerify;
}
6 changes: 5 additions & 1 deletion src/Verify/VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ internal string ExtensionOrTxt(string defaultValue = "txt") =>
internal string ExtensionOrBin() =>
extension ?? "bin";

internal bool autoVerify;
internal bool IsAutoVerify =>
VerifierSettings.autoVerify ||
autoVerify;

bool autoVerify;

/// <summary>
/// Automatically accept the results of the current test.
Expand Down

0 comments on commit 502105a

Please sign in to comment.