Skip to content
Open
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
2 changes: 0 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@
<PackageVersion Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="10.0.0" />
<PackageVersion Include="Shouldly" Version="4.3.0"/>
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0"/>
<PackageVersion Include="Verify.XunitV3" Version="32.0.0"/>
<PackageVersion Include="Xunit.Combinatorial" Version="2.0.24"/>
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
<PackageVersion Include="coverlet.msbuild" Version="10.0.1" />
Expand Down
10 changes: 0 additions & 10 deletions tests/bunit.generators.tests/VerifyInitializer.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,90 @@
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();

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);
Comment on lines +25 to +27

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<string> UpdateSnapshots(IEnumerable<GeneratedSourceResult> 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)
{

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (ubuntu-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (ubuntu-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (ubuntu-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (ubuntu-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (windows-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (windows-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (windows-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (windows-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / validate-docs

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (macos-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (macos-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (macos-latest)

Either remove or fill this block of code.

Check failure on line 43 in tests/bunit.generators.tests/Web.AngleSharp/WrapperElementsGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / run-test (macos-latest)

Either remove or fill this block of code.
}

try
{
return CompareAndUpdateSnapshots(sources, Path.GetDirectoryName(testFilePath)!);
}
finally
{
mutex.ReleaseMutex();
}
}

private static List<string> CompareAndUpdateSnapshots(IEnumerable<GeneratedSourceResult> sources, string directory)
{
var changed = new List<string>();
var expectedFiles = new HashSet<string>(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(
Expand Down
8 changes: 6 additions & 2 deletions tests/bunit.generators.tests/bunit.generators.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Verify.SourceGenerators" />
<PackageReference Include="Verify.XunitV3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<Compile Remove="**/*.verified.cs" />
<None Include="**/*.verified.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\bunit.generators.internal\bunit.generators.internal.csproj" />
<ProjectReference Include="..\..\src\bunit.generators\bunit.generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Expand Down
Loading