Skip to content

Commit a2034ef

Browse files
committed
Fixing flakey test and updates
1 parent 53edccc commit a2034ef

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

src/AutoCtor.Tests/AutoCtor.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
4646
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
4747
<PackageReference Include="Microsoft.NET.Test.Sdk" />
48+
<PackageReference Include="System.Linq.Async" />
4849
<PackageReference Include="TUnit" />
4950
<PackageReference Include="Verify.DiffPlex" />
5051
<PackageReference Include="Verify.SourceGenerators" />

src/AutoCtor.Tests/ExampleTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ExampleTests
1212
public async Task ExamplesGeneratedCode(CodeFileTheoryData theoryData)
1313
{
1414
var builder = CreateCompilation(theoryData);
15-
var compilation = await builder.Build(nameof(ExampleTests));
15+
var compilation = await builder.Build(nameof(ExampleTests), TestHelper.CancellationToken);
1616
var driver = new GeneratorDriverBuilder()
1717
.AddGenerator(new AutoConstructSourceGenerator())
1818
.WithAnalyzerOptions(theoryData.Options)
@@ -30,7 +30,7 @@ await Verify(driver)
3030
public async Task CodeCompilesProperly(CodeFileTheoryData theoryData)
3131
{
3232
var builder = CreateCompilation(theoryData);
33-
var compilation = await builder.Build(nameof(ExampleTests));
33+
var compilation = await builder.Build(nameof(ExampleTests), TestHelper.CancellationToken);
3434
new GeneratorDriverBuilder()
3535
.AddGenerator(new AutoConstructSourceGenerator())
3636
.WithAnalyzerOptions(theoryData.Options)
@@ -52,7 +52,7 @@ await Assert.That(outputCompilation.GetDiagnostics(TestHelper.CancellationToken)
5252
public async Task EnsureRunsAreCachedCorrectly(CodeFileTheoryData theoryData)
5353
{
5454
var builder = CreateCompilation(theoryData);
55-
var compilation = await builder.Build(nameof(ExampleTests));
55+
var compilation = await builder.Build(nameof(ExampleTests), TestHelper.CancellationToken);
5656

5757
var driver = new GeneratorDriverBuilder()
5858
.AddGenerator(new AutoConstructSourceGenerator())

src/AutoCtor.Tests/GeneratedAttributeTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public async Task AttributeGeneratedCode()
88
var builder = new CompilationBuilder()
99
.AddNetCoreReference()
1010
.WithPreprocessorSymbols(["AUTOCTOR_EMBED_ATTRIBUTES"]);
11-
var compilation = await builder.Build(nameof(GeneratedAttributeTests));
11+
var compilation = await builder.Build(nameof(GeneratedAttributeTests), TestHelper.CancellationToken);
1212
var driver = new GeneratorDriverBuilder()
1313
.AddGenerator(new AttributeSourceGenerator())
1414
.Build(builder.ParseOptions)
@@ -23,7 +23,7 @@ public async Task AttributeCompilesProperly()
2323
var builder = new CompilationBuilder()
2424
.AddNetCoreReference()
2525
.WithPreprocessorSymbols(["AUTOCTOR_EMBED_ATTRIBUTES"]);
26-
var compilation = await builder.Build(nameof(GeneratedAttributeTests));
26+
var compilation = await builder.Build(nameof(GeneratedAttributeTests), TestHelper.CancellationToken);
2727
var driver = new GeneratorDriverBuilder()
2828
.AddGenerator(new AttributeSourceGenerator())
2929
.Build(builder.ParseOptions)
@@ -47,7 +47,7 @@ public async Task PreserveAttributesTest()
4747
.AddNetCoreReference()
4848
.AddCode("[AutoCtor.AutoConstruct] public partial class Test { }")
4949
.WithPreprocessorSymbols(["AUTOCTOR_EMBED_ATTRIBUTES", "AUTOCTOR_USAGES"]);
50-
var compilation = await builder.Build(nameof(GeneratedAttributeTests));
50+
var compilation = await builder.Build(nameof(GeneratedAttributeTests), TestHelper.CancellationToken);
5151

5252
var driver = new GeneratorDriverBuilder()
5353
.AddGenerator(new AttributeSourceGenerator())
@@ -79,13 +79,13 @@ public async Task EnsureGeneratedAttributesAreNotExternallyVisible()
7979
.Build(compileBuilder.ParseOptions);
8080

8181
var projectA = await compileBuilder
82-
.Build("ProjectA");
82+
.Build("ProjectA", TestHelper.CancellationToken);
8383

8484
genDriver = genDriver.RunGeneratorsAndUpdateCompilation(projectA, out var genProjectA, out _, TestHelper.CancellationToken);
8585

8686
var projectB = await compileBuilder
8787
.AddCompilationReference(genProjectA)
88-
.Build("ProjectB");
88+
.Build("ProjectB", TestHelper.CancellationToken);
8989

9090
genDriver.RunGeneratorsAndUpdateCompilation(
9191
projectB,

src/AutoCtor.Tests/Issue73.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ public sealed partial class TheClass : BaseClass<object, int, string>{}
7373
";
7474
var projectA = await common
7575
.AddCode(projectACode)
76-
.Build("ProjectA");
76+
.Build("ProjectA", TestHelper.CancellationToken);
7777

7878
var projectB = await common
7979
.AddCompilationReference(projectA)
8080
.AddCode(projectBCode)
81-
.Build("ProjectB");
81+
.Build("ProjectB", TestHelper.CancellationToken);
8282

8383
return projectB;
8484
}

src/AutoCtor.Tests/Utilities/CompilationBuilder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal class CompilationBuilder
1414
private CSharpParseOptions _parseOptions;
1515
private CSharpCompilationOptions _compilationOptions;
1616

17-
1817
public CSharpParseOptions ParseOptions => _parseOptions;
1918

2019
public CompilationBuilder()
@@ -37,11 +36,11 @@ private CompilationBuilder(CompilationBuilder other)
3736
_compilationOptions = other._compilationOptions;
3837
}
3938

40-
public async Task<CSharpCompilation> Build(string assemblyName)
39+
public async Task<CSharpCompilation> Build(string assemblyName, CancellationToken cancellationToken = default)
4140
{
42-
var refTasks = _nugetReferences.Select(r => r.ResolveAsync(null, CancellationToken.None));
43-
await Task.WhenAll(refTasks);
44-
var nugetReferences = refTasks.SelectMany(t => t.Result);
41+
var nugetReferences = await _nugetReferences.ToAsyncEnumerable()
42+
.SelectManyAwait(async r => (await r.ResolveAsync(null, cancellationToken)).ToAsyncEnumerable())
43+
.ToListAsync(cancellationToken);
4544

4645
return CSharpCompilation.Create(assemblyName)
4746
.AddReferences(nugetReferences)

src/Directory.Packages.props

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
<PackageVersion Include="Microsoft.NETCore.App.Ref" Version="9.0.10" />
1313
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
1414
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
15-
<PackageVersion Include="TUnit" Version="0.75.30" />
15+
<PackageVersion Include="System.Linq.Async" Version="6.0.3" />
16+
<PackageVersion Include="TUnit" Version="0.77.3" />
1617
<PackageVersion Include="Verify.DiffPlex" Version="3.1.2" />
1718
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
18-
<PackageVersion Include="Verify.TUnit" Version="31.0.4" />
19+
<PackageVersion Include="Verify.TUnit" Version="31.0.5" />
1920
</ItemGroup>
20-
</Project>
21+
</Project>

0 commit comments

Comments
 (0)