Skip to content

Commit 2015aa4

Browse files
committed
Enable running example tests massively in parallel via test runner UI and dotnet test
1 parent 1731e15 commit 2015aa4

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

tools/ExampleExtractor/ExampleMetadata.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ public class ExampleMetadata
8484

8585
[JsonIgnore]
8686
public string Source => $"{MarkdownFile}:{StartLine}-{EndLine}";
87+
88+
public override string ToString() => Name;
8789
}

tools/ExampleTester/ExampleTester.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -11,6 +11,9 @@
1111
<PackageReference Include="Microsoft.Build.Locator" Version="1.9.1" />
1212
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
1313
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.14.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
15+
<PackageReference Include="NUnit" Version="4.3.2" />
16+
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
1417
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
1518
</ItemGroup>
1619

tools/ExampleTester/ExampleTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using NUnit.Framework;
2+
using Utilities;
3+
4+
[assembly: Parallelizable(ParallelScope.Children)]
5+
6+
namespace ExampleTester;
7+
8+
public static class ExampleTests
9+
{
10+
private static TesterConfiguration TesterConfiguration { get; } = new(FindTmpDirectory());
11+
private static StatusCheckLogger StatusCheckLogger { get; } = new("..", "Example tester");
12+
13+
public static IEnumerable<object[]> LoadExamples() =>
14+
from example in GeneratedExample.LoadAllExamples(TesterConfiguration.ExtractedOutputDirectory)
15+
select new object[] { example };
16+
17+
[TestCaseSource(nameof(LoadExamples))]
18+
public static async Task ExamplePasses(GeneratedExample example)
19+
{
20+
if (!await example.Test(TesterConfiguration, StatusCheckLogger))
21+
Assert.Fail("There were one or more failures. See the logged output for details.");
22+
}
23+
24+
private static string FindTmpDirectory()
25+
{
26+
for (string? current = AppDomain.CurrentDomain.BaseDirectory; current != null; current = Path.GetDirectoryName(current))
27+
{
28+
string testPath = Path.Join(current, "tmp");
29+
if (Directory.Exists(testPath))
30+
return testPath;
31+
}
32+
33+
throw new InvalidOperationException($"Can't find 'tmp' directory in {AppDomain.CurrentDomain.BaseDirectory} or any parent directories.");
34+
}
35+
}

tools/ExampleTester/GeneratedExample.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ private GeneratedExample(string directory)
2626
Metadata = JsonConvert.DeserializeObject<ExampleMetadata>(metadataJson) ?? throw new ArgumentException($"Invalid (null) metadata in {directory}");
2727
}
2828

29+
public override string? ToString() => Metadata.ToString();
30+
2931
internal static List<GeneratedExample> LoadAllExamples(string parentDirectory) =>
3032
Directory.GetDirectories(parentDirectory).Select(Load).ToList();
3133

tools/ExampleTester/TesterConfiguration.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ namespace ExampleTester;
55

66
public record TesterConfiguration(
77
string ExtractedOutputDirectory,
8-
bool Quiet,
9-
string? SourceFile,
10-
string? ExampleName)
11-
{
12-
13-
}
8+
bool Quiet = false,
9+
string? SourceFile = null,
10+
string? ExampleName = null);
1411

1512
public class TesterConfigurationBinder : BinderBase<TesterConfiguration>
1613
{

0 commit comments

Comments
 (0)