Skip to content

Commit 9a47c08

Browse files
build - generate test reports in a separate step
1 parent 7818534 commit 9a47c08

12 files changed

+173
-58
lines changed

.github/workflow-gen/Program.cs

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
GenerateReleaseWorkflow(component);
3434
}
3535

36+
GenerateUploadTestResultsWorkflow();
37+
38+
3639
void GenerateCiWorkflow(Component component)
3740
{
38-
var workflow = new Workflow($"{component.Name}/ci");
41+
var workflow = new Workflow(component.CiWorkflowName);
3942
var paths = new[]
4043
{
4144
$".github/workflows/{component.Name}-**",
@@ -76,9 +79,11 @@ void GenerateCiWorkflow(Component component)
7679

7780
foreach (var testProject in component.Tests)
7881
{
79-
job.StepTestAndReport(component.Name, testProject);
82+
job.StepTest(component.Name, testProject);
8083
}
8184

85+
job.StepUploadTestResultsAsArtifact(component);
86+
8287
job.StepToolRestore();
8388

8489
foreach (var project in component.Projects)
@@ -100,7 +105,7 @@ void GenerateCiWorkflow(Component component)
100105

101106
void GenerateReleaseWorkflow(Component component)
102107
{
103-
var workflow = new Workflow($"{component.Name}/release");
108+
var workflow = new Workflow(component.ReleaseWorkflowName);
104109

105110
workflow.On
106111
.WorkflowDispatch()
@@ -166,14 +171,49 @@ git tag -a {component.TagPrefix}-{contexts.Event.Input.Version} -m ""Release v{c
166171
WriteWorkflow(workflow, fileName);
167172
}
168173

174+
void GenerateUploadTestResultsWorkflow()
175+
{
176+
var workflow = new Workflow("generate-test-reports");
177+
workflow.On
178+
.WorkflowRun()
179+
.Workflows(components.Select(x => x.CiWorkflowName).ToArray())
180+
.Types("completed");
181+
182+
var job = workflow
183+
.Job("report")
184+
.Name("report")
185+
.RunsOn(GitHubHostedRunners.UbuntuLatest);
186+
187+
job.Permissions(
188+
actions: Permission.Read,
189+
contents: Permission.Read,
190+
checks: Permission.Write,
191+
packages: Permission.Write);
192+
193+
foreach (var component in components)
194+
{
195+
foreach (var testProject in component.Tests)
196+
{
197+
job.StepGenerateReportFromTestArtifact(component, testProject);
198+
}
199+
}
200+
201+
var fileName = $"generate-test-reports";
202+
WriteWorkflow(workflow, fileName);
203+
}
204+
169205
void WriteWorkflow(Workflow workflow, string fileName)
170206
{
171207
var filePath = $"../workflows/{fileName}.yml";
172208
workflow.WriteYaml(filePath);
173209
Console.WriteLine($"Wrote workflow to {filePath}");
174210
}
175211

176-
record Component(string Name, string[] Projects, string[] Tests, string TagPrefix);
212+
record Component(string Name, string[] Projects, string[] Tests, string TagPrefix)
213+
{
214+
public string CiWorkflowName => $"{Name}/ci";
215+
public string ReleaseWorkflowName => $"{Name}/release";
216+
}
177217

178218
public static class StepExtensions
179219
{
@@ -190,28 +230,43 @@ public static void StepSetupDotNet(this Job job)
190230
public static Step IfRefMain(this Step step)
191231
=> step.If("github.ref == 'refs/heads/main'");
192232

193-
public static Job RunEitherOnBranchOrAsPR(this Job job)
194-
=> job.If(
195-
"(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')");
196-
197-
public static void StepTestAndReport(this Job job, string componentName, string testProject)
233+
public static void StepTest(this Job job, string componentName, string testProject)
198234
{
199235
var path = $"test/{testProject}";
200-
var logFileName = "Tests.trx";
236+
var logFileName = $"{testProject}.trx";
201237
var flags = $"--logger \"console;verbosity=normal\" " +
202238
$"--logger \"trx;LogFileName={logFileName}\" " +
203239
$"--collect:\"XPlat Code Coverage\"";
204240
job.Step()
205241
.Name($"Test - {testProject}")
206242
.Run($"dotnet test -c Release {path} {flags}");
207243

244+
}
245+
246+
internal static void StepUploadTestResultsAsArtifact(this Job job, Component component)
247+
{
248+
job.Step()
249+
.Name($"Test report")
250+
.If("success() || failure()")
251+
.Uses("actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882") // 4.4.3
252+
.With(
253+
("name", "test-results"),
254+
("path", string.Join(Environment.NewLine, component.Tests
255+
.Select(testProject => $"{component.Name}/test/{testProject}/TestResults/{testProject}.trx"))),
256+
("retention-days", "5"));
257+
}
258+
259+
internal static void StepGenerateReportFromTestArtifact(this Job job, Component component, string testProject)
260+
{
261+
var path = $"test/{testProject}";
208262
job.Step()
209-
.Name($"Test report - {testProject}")
263+
.Name($"Test report - {component.Name} - {testProject}")
210264
.Uses("dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5") // v1.9.1
211-
.If("github.event_name == 'push' && (success() || failure())")
265+
.If($"event.workflow.name == '{component.CiWorkflowName}'")
212266
.With(
267+
("artifact", "test-results"),
213268
("name", $"Test Report - {testProject}"),
214-
("path", $"{componentName}/{path}/TestResults/{logFileName}"),
269+
("path", $"{testProject}.trx"),
215270
("reporter", "dotnet-trx"),
216271
("fail-on-error", "true"),
217272
("fail-on-empty", "true"));
@@ -272,7 +327,7 @@ public static void StepUploadArtifacts(this Job job, string componentName)
272327
var path = $"{componentName}/artifacts/*.nupkg";
273328
job.Step()
274329
.Name("Upload Artifacts")
275-
.IfRefMain()
330+
.IfGithubEventIsPush()
276331
.Uses("actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882") // 4.4.3
277332
.With(
278333
("name", "artifacts"),

.github/workflows/access-token-management-ci.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ jobs:
4343
8.0.x
4444
9.0.x
4545
- name: Test - AccessTokenManagement.Tests
46-
run: dotnet test -c Release test/AccessTokenManagement.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - AccessTokenManagement.Tests
48-
if: github.event_name == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
46+
run: dotnet test -c Release test/AccessTokenManagement.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=AccessTokenManagement.Tests.trx" --collect:"XPlat Code Coverage"
47+
- name: Test report
48+
if: success() || failure()
49+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5050
with:
51-
name: Test Report - AccessTokenManagement.Tests
52-
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
51+
name: test-results
52+
path: access-token-management/test/AccessTokenManagement.Tests/TestResults/AccessTokenManagement.Tests.trx
53+
retention-days: 5
5654
- name: Tool restore
5755
run: dotnet tool restore
5856
- name: Pack AccessTokenManagement
@@ -72,7 +70,7 @@ jobs:
7270
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7371
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7472
- name: Upload Artifacts
75-
if: github.ref == 'refs/heads/main'
73+
if: github.event_name == 'push'
7674
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
7775
with:
7876
name: artifacts

.github/workflows/access-token-management-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6363
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6464
- name: Upload Artifacts
65-
if: github.ref == 'refs/heads/main'
65+
if: github.event_name == 'push'
6666
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
6767
with:
6868
name: artifacts
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This was generated by tool. Edits will be overwritten.
2+
3+
name: generate-test-reports
4+
on:
5+
workflow_run:
6+
workflows:
7+
- 'ignore-this/ci'
8+
- 'access-token-management/ci'
9+
- 'identity-model/ci'
10+
- 'identity-model-oidc-client/ci'
11+
types:
12+
- completed
13+
jobs:
14+
report:
15+
name: report
16+
runs-on: ubuntu-latest
17+
permissions:
18+
actions: read
19+
checks: write
20+
contents: read
21+
packages: write
22+
steps:
23+
- name: Test report - ignore-this - IgnoreThis.Tests
24+
if: event.workflow.name == 'ignore-this/ci'
25+
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
26+
with:
27+
artifact: test-results
28+
name: Test Report - IgnoreThis.Tests
29+
path: IgnoreThis.Tests.trx
30+
reporter: dotnet-trx
31+
fail-on-error: true
32+
fail-on-empty: true
33+
- name: Test report - access-token-management - AccessTokenManagement.Tests
34+
if: event.workflow.name == 'access-token-management/ci'
35+
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
36+
with:
37+
artifact: test-results
38+
name: Test Report - AccessTokenManagement.Tests
39+
path: AccessTokenManagement.Tests.trx
40+
reporter: dotnet-trx
41+
fail-on-error: true
42+
fail-on-empty: true
43+
- name: Test report - identity-model - IdentityModel.Tests
44+
if: event.workflow.name == 'identity-model/ci'
45+
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
46+
with:
47+
artifact: test-results
48+
name: Test Report - IdentityModel.Tests
49+
path: IdentityModel.Tests.trx
50+
reporter: dotnet-trx
51+
fail-on-error: true
52+
fail-on-empty: true
53+
- name: Test report - identity-model-oidc-client - IdentityModel.OidcClient.Tests
54+
if: event.workflow.name == 'identity-model-oidc-client/ci'
55+
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
56+
with:
57+
artifact: test-results
58+
name: Test Report - IdentityModel.OidcClient.Tests
59+
path: IdentityModel.OidcClient.Tests.trx
60+
reporter: dotnet-trx
61+
fail-on-error: true
62+
fail-on-empty: true

.github/workflows/identity-model-ci.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ jobs:
4343
8.0.x
4444
9.0.x
4545
- name: Test - IdentityModel.Tests
46-
run: dotnet test -c Release test/IdentityModel.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - IdentityModel.Tests
48-
if: github.event_name == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
46+
run: dotnet test -c Release test/IdentityModel.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=IdentityModel.Tests.trx" --collect:"XPlat Code Coverage"
47+
- name: Test report
48+
if: success() || failure()
49+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5050
with:
51-
name: Test Report - IdentityModel.Tests
52-
path: identity-model/test/IdentityModel.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
51+
name: test-results
52+
path: identity-model/test/IdentityModel.Tests/TestResults/IdentityModel.Tests.trx
53+
retention-days: 5
5654
- name: Tool restore
5755
run: dotnet tool restore
5856
- name: Pack IdentityModel
@@ -70,7 +68,7 @@ jobs:
7068
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7169
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7270
- name: Upload Artifacts
73-
if: github.ref == 'refs/heads/main'
71+
if: github.event_name == 'push'
7472
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
7573
with:
7674
name: artifacts

.github/workflows/identity-model-oidc-client-ci.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ jobs:
4343
8.0.x
4444
9.0.x
4545
- name: Test - IdentityModel.OidcClient.Tests
46-
run: dotnet test -c Release test/IdentityModel.OidcClient.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - IdentityModel.OidcClient.Tests
48-
if: github.event_name == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
46+
run: dotnet test -c Release test/IdentityModel.OidcClient.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=IdentityModel.OidcClient.Tests.trx" --collect:"XPlat Code Coverage"
47+
- name: Test report
48+
if: success() || failure()
49+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5050
with:
51-
name: Test Report - IdentityModel.OidcClient.Tests
52-
path: identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
51+
name: test-results
52+
path: identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/TestResults/IdentityModel.OidcClient.Tests.trx
53+
retention-days: 5
5654
- name: Tool restore
5755
run: dotnet tool restore
5856
- name: Pack IdentityModel.OidcClient
@@ -72,7 +70,7 @@ jobs:
7270
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7371
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7472
- name: Upload Artifacts
75-
if: github.ref == 'refs/heads/main'
73+
if: github.event_name == 'push'
7674
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
7775
with:
7876
name: artifacts

.github/workflows/identity-model-oidc-client-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6363
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6464
- name: Upload Artifacts
65-
if: github.ref == 'refs/heads/main'
65+
if: github.event_name == 'push'
6666
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
6767
with:
6868
name: artifacts

.github/workflows/identity-model-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6161
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6262
- name: Upload Artifacts
63-
if: github.ref == 'refs/heads/main'
63+
if: github.event_name == 'push'
6464
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
6565
with:
6666
name: artifacts

.github/workflows/ignore-this-ci.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ jobs:
4343
8.0.x
4444
9.0.x
4545
- name: Test - IgnoreThis.Tests
46-
run: dotnet test -c Release test/IgnoreThis.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=Tests.trx" --collect:"XPlat Code Coverage"
47-
- name: Test report - IgnoreThis.Tests
48-
if: github.event_name == 'push' && (success() || failure())
49-
uses: dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5
46+
run: dotnet test -c Release test/IgnoreThis.Tests --logger "console;verbosity=normal" --logger "trx;LogFileName=IgnoreThis.Tests.trx" --collect:"XPlat Code Coverage"
47+
- name: Test report
48+
if: success() || failure()
49+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
5050
with:
51-
name: Test Report - IgnoreThis.Tests
52-
path: ignore-this/test/IgnoreThis.Tests/TestResults/Tests.trx
53-
reporter: dotnet-trx
54-
fail-on-error: true
55-
fail-on-empty: true
51+
name: test-results
52+
path: ignore-this/test/IgnoreThis.Tests/TestResults/IgnoreThis.Tests.trx
53+
retention-days: 5
5654
- name: Tool restore
5755
run: dotnet tool restore
5856
- name: Pack IgnoreThis
@@ -70,7 +68,7 @@ jobs:
7068
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7169
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7270
- name: Upload Artifacts
73-
if: github.ref == 'refs/heads/main'
71+
if: github.event_name == 'push'
7472
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
7573
with:
7674
name: artifacts

.github/workflows/ignore-this-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6161
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6262
- name: Upload Artifacts
63-
if: github.ref == 'refs/heads/main'
63+
if: github.event_name == 'push'
6464
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882
6565
with:
6666
name: artifacts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ProjectConfiguration>
2+
<Settings />
3+
</ProjectConfiguration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ProjectConfiguration>
2+
<Settings />
3+
</ProjectConfiguration>

0 commit comments

Comments
 (0)