From 502105aae6d52a288ada6c569b0351ca3801a52d Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 3 Jun 2022 21:06:10 +1000 Subject: [PATCH] Add static auto verify (#537) --- docs/mdsource/verify-options.source.md | 4 ++++ docs/verify-options.md | 10 ++++++++ src/AutoVerifyTests/AutoVerifyTests.csproj | 27 ++++++++++++++++++++++ src/AutoVerifyTests/Tests.cs | 23 ++++++++++++++++++ src/Verify.sln | 8 +++++++ src/Verify/Verifier/VerifyEngine.cs | 6 ++--- src/Verify/VerifierSettings.cs | 24 ++++++++++++++++++- src/Verify/VerifySettings.cs | 6 ++++- 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 src/AutoVerifyTests/AutoVerifyTests.csproj create mode 100644 src/AutoVerifyTests/Tests.cs diff --git a/docs/mdsource/verify-options.source.md b/docs/mdsource/verify-options.source.md index 21e4256c91..ca1b39c65f 100644 --- a/docs/mdsource/verify-options.source.md +++ b/docs/mdsource/verify-options.source.md @@ -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. diff --git a/docs/verify-options.md b/docs/verify-options.md index cd04531175..1a8f7c1b70 100644 --- a/docs/verify-options.md +++ b/docs/verify-options.md @@ -25,6 +25,16 @@ settings.AutoVerify(); snippet source | anchor +Or globally + + + +```cs +VerifierSettings.AutoVerify(); +``` +snippet source | anchor + + Note that auto accepted changes in `.verified.` files remain visible in source control tooling. diff --git a/src/AutoVerifyTests/AutoVerifyTests.csproj b/src/AutoVerifyTests/AutoVerifyTests.csproj new file mode 100644 index 0000000000..aa328601d9 --- /dev/null +++ b/src/AutoVerifyTests/AutoVerifyTests.csproj @@ -0,0 +1,27 @@ + + + + net6 + true + false + true + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AutoVerifyTests/Tests.cs b/src/AutoVerifyTests/Tests.cs new file mode 100644 index 0000000000..6e2022eecf --- /dev/null +++ b/src/AutoVerifyTests/Tests.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/Verify.sln b/src/Verify.sln index a5be88090d..fa2caeeca6 100644 --- a/src/Verify.sln +++ b/src/Verify.sln @@ -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 @@ -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 diff --git a/src/Verify/Verifier/VerifyEngine.cs b/src/Verify/Verifier/VerifyEngine.cs index 84b8290179..dc6792b1b4 100644 --- a/src/Verify/Verifier/VerifyEngine.cs +++ b/src/Verify/Verifier/VerifyEngine.cs @@ -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); @@ -141,7 +141,7 @@ async Task ProcessDeletes(string file) { await VerifierSettings.RunOnVerifyDelete(file); - if (settings.autoVerify) + if (settings.IsAutoVerify) { File.Delete(file); return; @@ -187,7 +187,7 @@ Task RunDiffAutoCheck(FilePair file) return Task.CompletedTask; } - if (settings.autoVerify) + if (settings.IsAutoVerify) { AcceptChanges(file); return Task.CompletedTask; diff --git a/src/Verify/VerifierSettings.cs b/src/Verify/VerifierSettings.cs index cda4d7ffbb..28c1da777a 100644 --- a/src/Verify/VerifierSettings.cs +++ b/src/Verify/VerifierSettings.cs @@ -1,4 +1,6 @@ -namespace VerifyTests; +using DiffEngine; + +namespace VerifyTests; public static partial class VerifierSettings { @@ -6,4 +8,24 @@ public static partial class VerifierSettings public static void OmitContentFromException() => omitContentFromException = true; + + /// + /// Automatically accept the results of all tests. + /// + public static void AutoVerify(bool includeBuildServer = true) + { + if (includeBuildServer) + { + autoVerify = true; + } + else + { + if (!BuildServerDetector.Detected) + { + autoVerify = true; + } + } + } + + internal static bool autoVerify; } \ No newline at end of file diff --git a/src/Verify/VerifySettings.cs b/src/Verify/VerifySettings.cs index bd2dae4101..607948b227 100644 --- a/src/Verify/VerifySettings.cs +++ b/src/Verify/VerifySettings.cs @@ -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; /// /// Automatically accept the results of the current test.