diff --git a/Directory.Packages.props b/Directory.Packages.props index 93b2ce6d4..627a6291f 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -98,8 +98,6 @@ - - diff --git a/tests/bunit.generators.tests/VerifyInitializer.cs b/tests/bunit.generators.tests/VerifyInitializer.cs deleted file mode 100644 index b3c3bf82e..000000000 --- a/tests/bunit.generators.tests/VerifyInitializer.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace Bunit; - -internal static class VerifyInitializer -{ - [ModuleInitializer] - public static void Init() => - VerifySourceGenerators.Initialize(); -} \ No newline at end of file diff --git a/tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs b/tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs index 1cce21a7e..dd8ebe118 100644 --- a/tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs +++ b/tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs @@ -2,13 +2,18 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; namespace Bunit.Web.AngleSharp; public class WrapperElementsGeneratorTest { + private const string SnapshotPrefix = "WrapperElementsGeneratorTest.Generator#"; + private const string SnapshotSuffix = ".verified.cs"; + [Fact] - public Task Generator() + public void Generator() { var inputCompilation = CreateCompilation(); var generator = new WrapperElementsGenerator(); @@ -16,11 +21,71 @@ public Task Generator() GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); driver = driver.RunGenerators(inputCompilation, Xunit.TestContext.Current.CancellationToken); - var settings = new VerifySettings(); - settings.AutoVerify(); - return Verify(driver.GetRunResult(), settings); + var result = driver.GetRunResult(); + Assert.Empty(result.Diagnostics); + var run = Assert.Single(result.Results); + Assert.Null(run.Exception); + + var changed = UpdateSnapshots(run.GeneratedSources); + Assert.True(changed.Count == 0, + $"Snapshots were updated, review and commit the changes:{Environment.NewLine}{string.Join(Environment.NewLine, changed)}"); + } + + private static List UpdateSnapshots(IEnumerable sources, [CallerFilePath] string testFilePath = "") + { + using var mutex = new Mutex(initiallyOwned: false, @"Global\bunit.generators.tests.WrapperElementsSnapshots"); + try + { + if (!mutex.WaitOne(TimeSpan.FromMinutes(1))) + throw new TimeoutException("Timed out waiting for the snapshot lock."); + } + catch (AbandonedMutexException) + { + } + + try + { + return CompareAndUpdateSnapshots(sources, Path.GetDirectoryName(testFilePath)!); + } + finally + { + mutex.ReleaseMutex(); + } } + private static List CompareAndUpdateSnapshots(IEnumerable sources, string directory) + { + var changed = new List(); + var expectedFiles = new HashSet(StringComparer.Ordinal); + + foreach (var source in sources) + { + var fileName = SnapshotPrefix + Path.GetFileNameWithoutExtension(source.HintName) + SnapshotSuffix; + var path = Path.Combine(directory, fileName); + expectedFiles.Add(fileName); + + var actual = Normalize($"//HintName: {source.HintName}\n{source.SourceText}"); + var existing = File.Exists(path) ? Normalize(File.ReadAllText(path)) : null; + if (existing == actual) + continue; + + File.WriteAllText(path, actual, new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)); + changed.Add(fileName); + } + + foreach (var stale in Directory.EnumerateFiles(directory, SnapshotPrefix + "*" + SnapshotSuffix) + .Select(Path.GetFileName) + .Where(fileName => !expectedFiles.Contains(fileName!))) + { + File.Delete(Path.Combine(directory, stale!)); + changed.Add(stale!); + } + + return changed; + } + + private static string Normalize(string text) => text.Replace("\r\n", "\n", StringComparison.Ordinal); + private static Compilation CreateCompilation() { return CSharpCompilation.Create( diff --git a/tests/bunit.generators.tests/bunit.generators.tests.csproj b/tests/bunit.generators.tests/bunit.generators.tests.csproj index 28d889510..ec76aefcb 100644 --- a/tests/bunit.generators.tests/bunit.generators.tests.csproj +++ b/tests/bunit.generators.tests/bunit.generators.tests.csproj @@ -14,8 +14,6 @@ - - @@ -23,6 +21,12 @@ + + + + + +