Skip to content

Commit 9fda4a5

Browse files
committed
test: move project path calculation from runner to source gen
1 parent a054e35 commit 9fda4a5

File tree

2 files changed

+99
-46
lines changed

2 files changed

+99
-46
lines changed
Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Collections.Generic;
12
using System.Collections.Immutable;
23
using System.Linq;
34
using System.Text;
45
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
57

68
namespace Allure.Build.SourceGenerators;
79

@@ -16,6 +18,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1618
pair.Right
1719
.GetOptions(pair.Left)
1820
.TryGetValue("build_metadata.AdditionalFiles.Allure_ProjectSuffix", out var value)
21+
&& SyntaxFacts.IsValidIdentifier(value)
1922
? value
2023
: null)
2124
.Where(static (suffix) => !string.IsNullOrEmpty(suffix))
@@ -24,42 +27,98 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2427

2528
context.RegisterSourceOutput(sampleProjectSuffixes, static (spc, suffixes) =>
2629
{
27-
var sb = new StringBuilder();
28-
sb.AppendLine("namespace Allure.Testing;");
29-
sb.AppendLine();
30-
sb.AppendLine("/// <summary>");
31-
sb.AppendLine("/// Use the static properties of this class to pass selected samples to");
32-
sb.AppendLine("/// <see cref=\"global::Allure.Testing.AllureSampleRunner\"/> methods.");
33-
sb.AppendLine("/// </summary>");
34-
sb.AppendLine("/// <remarks>");
35-
sb.AppendLine("/// This class was generated by Allure.Build.SourceGenerator based on the");
36-
sb.AppendLine("/// content of this project's <c>AllureSample</c> items");
37-
sb.AppendLine("/// (by default, these are the files from the <c>./Samples/</c> directory).");
38-
sb.AppendLine("/// Please, make sure the top-level elements in this directory have names");
39-
sb.AppendLine("/// that are valid C# identifiers (a .cs extension is allowed at the end of C#");
40-
sb.AppendLine("/// source files).");
41-
sb.AppendLine("/// </remarks>");
42-
sb.AppendLine("internal record class AllureSampleRegistry");
43-
sb.AppendLine("{");
44-
sb.AppendLine(" public string Name { get; init; }");
45-
sb.AppendLine();
46-
sb.AppendLine(" AllureSampleRegistry(string name) => this.Name = name;");
47-
sb.AppendLine();
48-
bool extraNewLine = false;
49-
foreach (var suffix in suffixes)
50-
{
51-
if (extraNewLine)
52-
{
53-
sb.AppendLine();
54-
}
30+
var code = GenerateSampleRegistryCode(suffixes);
5531

56-
sb.AppendFormat(" public static AllureSampleRegistry {0} {{ get; set; }} = new(\"{0}\");", suffix);
57-
sb.AppendLine();
58-
extraNewLine = true;
59-
}
60-
sb.AppendLine("}");
61-
62-
spc.AddSource("AllureSampleRegistry.g.cs", sb.ToString());
32+
spc.AddSource("AllureSampleRegistry.g.cs", code);
6333
});
6434
}
35+
36+
static string GenerateSampleRegistryCode(IEnumerable<string> sampleSuffixes)
37+
{
38+
var sb = new StringBuilder();
39+
sb.AppendLine("namespace Allure.Testing;");
40+
sb.AppendLine();
41+
AppendRegistryEntryClass(sb);
42+
sb.AppendLine();
43+
AppendRegistryClass(sb, sampleSuffixes);
44+
return sb.ToString();
45+
}
46+
47+
static void AppendRegistryEntryClass(StringBuilder sb)
48+
{
49+
sb.AppendLine("/// <summary>");
50+
sb.AppendLine("/// Contains the data required to run a specific sample project and");
51+
sb.AppendLine("/// access its result files.");
52+
sb.AppendLine("/// </summary>");
53+
sb.AppendLine("/// <remarks>");
54+
sb.AppendLine("/// Use the instances exposed by");
55+
sb.AppendLine("/// <see cref=\"global::Allure.Testing.AllureSampleRegistry\"/>");
56+
sb.AppendLine("/// instead of creating your own.");
57+
sb.AppendLine("/// </remarks>");
58+
sb.AppendLine("internal record class AllureSampleRegistryEntry(");
59+
sb.AppendLine(" string Name,");
60+
sb.AppendLine(" string ProjectPath,");
61+
sb.AppendLine(" string DefaultResultsPath");
62+
sb.AppendLine(");");
63+
}
64+
65+
static void AppendRegistryClass(StringBuilder sb, IEnumerable<string> suffixes)
66+
{
67+
sb.AppendLine("/// <summary>");
68+
sb.AppendLine("/// Exposes a set of testing samples available to this project");
69+
sb.AppendLine("/// via a set of static properties.");
70+
sb.AppendLine("/// </summary>");
71+
sb.AppendLine("/// <remarks>");
72+
sb.AppendLine("/// Pass a selected sample to");
73+
sb.AppendLine("/// <see cref=\"global::Allure.Testing.AllureSampleRunner\"/> methods");
74+
sb.AppendLine("/// to run it and access the test results.");
75+
sb.AppendLine("/// </remarks>");
76+
sb.AppendLine("internal static class AllureSampleRegistry");
77+
sb.AppendLine("{");
78+
AppendRegistryEntryProperties(sb, suffixes);
79+
sb.AppendLine("}");
80+
}
81+
82+
static void AppendRegistryEntryProperties(StringBuilder sb, IEnumerable<string> suffixes)
83+
{
84+
var first = suffixes.FirstOrDefault();
85+
if (first is not null)
86+
{
87+
AppendRegistryEntryProperty(sb, first);
88+
}
89+
90+
foreach (var rest in suffixes.Skip(1))
91+
{
92+
sb.AppendLine();
93+
AppendRegistryEntryProperty(sb, rest);
94+
}
95+
}
96+
97+
static void AppendRegistryEntryProperty(StringBuilder sb, string suffix)
98+
{
99+
sb.AppendFormat(" public static global::Allure.Testing.AllureSampleRegistryEntry {0} {{ get; }}", suffix);
100+
sb.AppendLine();
101+
sb.AppendLine(" = new(");
102+
sb.AppendFormat(" \"{0}\",", suffix);
103+
sb.AppendLine();
104+
sb.AppendLine(" global::System.IO.Path.Combine(");
105+
sb.AppendLine(" global::Allure.Testing.AllureBuildProperties.Allure_SampleSolutionDir,");
106+
sb.AppendLine(" string.Format(");
107+
sb.AppendFormat(" \"{{0}}.{0}\",", suffix);
108+
sb.AppendLine();
109+
sb.AppendLine(" global::Allure.Testing.AllureBuildProperties.Allure_SampleSolutionName");
110+
sb.AppendLine(" ),");
111+
sb.AppendLine(" string.Format(");
112+
sb.AppendFormat(" \"{{0}}.{0}.csproj\",", suffix);
113+
sb.AppendLine();
114+
sb.AppendLine(" global::Allure.Testing.AllureBuildProperties.Allure_SampleSolutionName");
115+
sb.AppendLine(" )");
116+
sb.AppendLine(" ),");
117+
sb.AppendLine(" string.Format(");
118+
sb.AppendLine(" global::Allure.Testing.AllureBuildProperties.Allure_SampleResultsDirectoryFormat,");
119+
sb.AppendFormat(" \"{0}\"", suffix);
120+
sb.AppendLine();
121+
sb.AppendLine(" )");
122+
sb.AppendLine(" );");
123+
}
65124
}

tests/Allure.NUnit.Tests/SampleRunner.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,22 @@ internal class SampleRunner
2323
static readonly Encoding encoding = new UTF8Encoding(false, false);
2424
static readonly JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true };
2525

26-
public static async Task<RunResult> RunAsync(AllureSampleRegistry sample) =>
26+
public static async Task<RunResult> RunAsync(AllureSampleRegistryEntry sample) =>
2727
await RunAsync(sample, SampleRunnerInput.Default, CancellationToken.None);
2828

29-
public static async Task<RunResult> RunAsync(AllureSampleRegistry sample, CancellationToken token) =>
29+
public static async Task<RunResult> RunAsync(AllureSampleRegistryEntry sample, CancellationToken token) =>
3030
await RunAsync(sample, SampleRunnerInput.Default, token);
3131

32-
public static async Task<RunResult> RunAsync(AllureSampleRegistry sample, SampleRunnerInput input) =>
32+
public static async Task<RunResult> RunAsync(AllureSampleRegistryEntry sample, SampleRunnerInput input) =>
3333
await RunAsync(sample, input, CancellationToken.None);
3434

35-
public static async Task<RunResult> RunAsync(AllureSampleRegistry sample, SampleRunnerInput input, CancellationToken token)
35+
public static async Task<RunResult> RunAsync(AllureSampleRegistryEntry sample, SampleRunnerInput input, CancellationToken token)
3636
{
37-
38-
var projectDir = Path.Combine(
39-
AllureBuildProperties.Allure_SampleSolutionDir,
40-
$"{AllureBuildProperties.Allure_SampleSolutionName}.{sample.Name}"
41-
);
42-
4337
var psi = new ProcessStartInfo(
4438
"dotnet",
4539
[
4640
"test",
47-
projectDir,
41+
sample.ProjectPath,
4842
"--framework",
4943
AllureBuildProperties.Allure_SampleSelectedTargetFramework,
5044
"--configuration",

0 commit comments

Comments
 (0)