Add ForceRun flag to fix source generator tests

This commit is contained in:
Dean Herbert
2023-07-20 16:38:13 +09:00
parent 00c1afdb22
commit 3555aeefdd
4 changed files with 14 additions and 5 deletions

View File

@@ -34,6 +34,9 @@ namespace osu.Framework.SourceGeneration.Tests.Verifiers
{
driver = CSharpGeneratorDriver.Create(generator = new TSourceGenerator());
driver = driver.WithUpdatedParseOptions(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion));
generator.ForceRun = true;
this.commonSources = commonSources;
this.commonGenerated = commonGenerated;
this.multiPhaseSources = multiPhaseSources;

View File

@@ -4,13 +4,13 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using osu.Framework.SourceGeneration.Generators;
namespace osu.Framework.SourceGeneration.Tests.Verifiers
{
public partial class CSharpSourceGeneratorVerifier<TSourceGenerator>
where TSourceGenerator : IIncrementalGenerator, new()
where TSourceGenerator : AbstractIncrementalGenerator, new()
{
public static async Task VerifyAsync(
(string filename, string content)[] commonSources,

View File

@@ -7,11 +7,12 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using osu.Framework.SourceGeneration.Generators;
namespace osu.Framework.SourceGeneration.Tests.Verifiers
{
public partial class CSharpSourceGeneratorVerifier<TSourceGenerator>
where TSourceGenerator : IIncrementalGenerator, new()
where TSourceGenerator : AbstractIncrementalGenerator, new()
{
public class Test : CSharpSourceGeneratorTest<EmptySourceGeneratorProvider, XUnitVerifier>
{
@@ -19,7 +20,7 @@ namespace osu.Framework.SourceGeneration.Tests.Verifiers
protected override IEnumerable<ISourceGenerator> GetSourceGenerators() => new[]
{
new TSourceGenerator().AsSourceGenerator()
new TSourceGenerator { ForceRun = true }.AsSourceGenerator()
};
protected override ParseOptions CreateParseOptions()

View File

@@ -14,6 +14,11 @@ namespace osu.Framework.SourceGeneration.Generators
{
public readonly GeneratorEventDriver EventDriver = new GeneratorEventDriver();
/// <summary>
/// Whether the generator should be forcefully run, even if building as debug.
/// </summary>
public bool ForceRun;
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Stage 1: Create SyntaxTarget objects for all classes.
@@ -23,7 +28,7 @@ namespace osu.Framework.SourceGeneration.Generators
(ctx, _) => returnWithEvent(new IncrementalSyntaxTarget((ClassDeclarationSyntax)ctx.Node, ctx.SemanticModel), EventDriver.OnSyntaxTargetCreated))
.Select((t, _) => t.WithName())
.Combine(context.CompilationProvider)
.Where(c => c.Right.Options.OptimizationLevel == OptimizationLevel.Release)
.Where(c => ForceRun || c.Right.Options.OptimizationLevel == OptimizationLevel.Release)
.Select((t, _) => t.Item1)
.Select((t, _) => returnWithEvent(t.WithSemanticTarget(CreateSemanticTarget), EventDriver.OnSemanticTargetCreated));