Skip to content

Commit 49830a1

Browse files
Merge pull request #1719 from RocketSurgeonsGuild/feature/cancellation
Add cancelation support
2 parents a8083f0 + 2fa21a7 commit 49830a1

3 files changed

Lines changed: 83 additions & 57 deletions

File tree

src/Testing.SourceGenerators/GeneratorTestContext.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.CodeAnalysis.Diagnostics;
1010
using Serilog;
1111
using Serilog.Events;
12+
// ReSharper disable UseCollectionExpression
1213

1314
namespace Rocket.Surgery.Extensions.Testing.SourceGenerators;
1415

@@ -159,7 +160,7 @@ ImmutableArray<GeneratorTestResultsCustomizer> customizers
159160
/// </summary>
160161
/// <returns></returns>
161162
/// <exception cref="InvalidOperationException"></exception>
162-
public async Task<GeneratorTestResults> GenerateAsync()
163+
public async Task<GeneratorTestResults> GenerateAsync(CancellationToken cancellationToken = default)
163164
{
164165
_logger.Information("Starting Generation for {SourceCount}", _sources.Length);
165166
if (_logger.IsEnabled(LogEventLevel.Verbose))
@@ -173,7 +174,7 @@ public async Task<GeneratorTestResults> GenerateAsync()
173174

174175
var project = GenerationHelpers.CreateProject(_projectName, _metadataReferences, _parseOptions, _sources, _additionalTexts);
175176

176-
var compilation = (CSharpCompilation?)await project.GetCompilationAsync().ConfigureAwait(false);
177+
var compilation = (CSharpCompilation?)await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
177178
if (compilation is null) throw new InvalidOperationException("Could not compile the sources");
178179

179180
var diagnostics = compilation.GetDiagnostics();
@@ -244,7 +245,7 @@ public async Task<GeneratorTestResults> GenerateAsync()
244245
foreach (var t in trees)
245246
{
246247
_logger.Verbose(" FilePath: {Name}", t.FilePath);
247-
_logger.Verbose(" Source:\n{Name}", ( await t.GetTextAsync().ConfigureAwait(false) ).ToString());
248+
_logger.Verbose(" Source:\n{Name}", ( await t.GetTextAsync(cancellationToken).ConfigureAwait(false) ).ToString());
248249
}
249250
}
250251

@@ -278,7 +279,7 @@ public async Task<GeneratorTestResults> GenerateAsync()
278279
new AnalyzerOptions(_additionalTexts, new OptionsProvider(_fileOptions, _globalOptions))
279280
);
280281

281-
analysisResult = await compilationWithAnalyzers.GetAnalysisResultAsync(CancellationToken.None);
282+
analysisResult = await compilationWithAnalyzers.GetAnalysisResultAsync(cancellationToken);
282283
foreach (var analyzer in analyzers)
283284
{
284285
var analyzerResults = analysisResult.GetAllDiagnostics(analyzer.Value);
@@ -316,7 +317,7 @@ public async Task<GeneratorTestResults> GenerateAsync()
316317
_logger.Information("--- Code Fix Providers ---");
317318
foreach (var provider in projectInformation.CodeFixProviders.Values)
318319
{
319-
results = await results.AddCodeFix(provider);
320+
results = await results.AddCodeFix(provider, cancellationToken);
320321
}
321322
}
322323

@@ -325,7 +326,7 @@ public async Task<GeneratorTestResults> GenerateAsync()
325326
_logger.Information("--- Code Refactoring Providers ---");
326327
foreach (var provider in projectInformation.CodeRefactoringProviders.Values)
327328
{
328-
results = await results.AddCodeRefactoring(provider);
329+
results = await results.AddCodeRefactoring(provider, cancellationToken);
329330
}
330331
}
331332

src/Testing.SourceGenerators/GeneratorTestContextBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,10 @@ public GeneratorTestContext Build()
577577
/// <summary>
578578
/// Generate and return the results of the generators
579579
/// </summary>
580+
/// <param name="cancellationToken"></param>
580581
/// <returns></returns>
581-
public Task<GeneratorTestResults> GenerateAsync()
582+
public Task<GeneratorTestResults> GenerateAsync(CancellationToken cancellationToken = default)
582583
{
583-
return Build().GenerateAsync();
584+
return Build().GenerateAsync(cancellationToken);
584585
}
585586
}

0 commit comments

Comments
 (0)