Skip to content

Commit f2deff2

Browse files
committed
Add invokeDotnetTest.ps1
1 parent 4a277fb commit f2deff2

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

CI/invokeDotnetTest.ps1

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Copyright (c) 2022-2024 Matthias Wolf, Mawosoft.
2+
3+
<#
4+
.SYNOPSIS
5+
Run dotnet test for projects * configs * frameworks.
6+
#>
7+
8+
#Requires -Version 7.4
9+
10+
Param (
11+
[Parameter(Mandatory)]
12+
[Alias('p')]
13+
[string[]]$Projects,
14+
15+
[ValidateSet('Debug', 'Release')]
16+
[Alias('c')]
17+
[string[]]$Configs = @('Debug', 'Release'),
18+
19+
[Parameter(Mandatory)]
20+
# Subset of Microsoft.NET.SupportedTargetFrameworks.props
21+
[ValidateSet('net461', 'net462', 'net472', 'net48', 'netstandard2.0', 'netcoreapp3.1', 'net5.0', 'net6.0', 'net7.0', 'net8.0', 'net9.0')]
22+
[Alias('f')]
23+
[string[]]$Frameworks,
24+
25+
[ValidateSet('quiet', 'minimal', 'normal', 'detailed')]
26+
[Alias('v')]
27+
[string]$Verbosity = 'minimal',
28+
29+
[Alias('b')]
30+
# Doesn't add --no-build to dotnet test args
31+
[switch]$Build,
32+
33+
[Alias('ff')]
34+
# Stop after first failing test
35+
[switch]$FailFast,
36+
37+
[Alias('r')]
38+
# Defaults to ./TestResults
39+
[string]$ResultsDirectory,
40+
41+
[Alias('cc')]
42+
[switch]$CodeCoverage,
43+
44+
[Alias('s')]
45+
# If provided, turns on -cc automatically
46+
[string]$Settings,
47+
48+
[Alias('a')]
49+
# Any additional dotnet test args.
50+
[string[]]$AdditionalArguments
51+
)
52+
53+
Set-StrictMode -Version 3.0
54+
$ErrorActionPreference = 'Stop'
55+
$PSNativeCommandUseErrorActionPreference = $true
56+
57+
[int]$matrixCount = $Projects.Count * $Configs.Count * $Frameworks.Count
58+
[int]$failedTestCount = 0
59+
60+
# Init CI log grouping if available
61+
[string]$groupPrefix = [System.Environment]::NewLine
62+
[string]$groupSuffix = ''
63+
64+
if ($env:GITHUB_ACTIONS -and $matrixCount -gt 1) {
65+
$groupPrefix = '::group::'
66+
$groupSuffix = '::endgroup::'
67+
}
68+
elseif ($env:TF_BUILD -and $matrixCount -gt 1) {
69+
$groupPrefix = '##[group]'
70+
$groupSuffix = '##[endgroup]'
71+
}
72+
73+
if ($ResultsDirectory -eq '') {
74+
$ResultsDirectory = Join-Path '.' 'TestResults'
75+
}
76+
77+
# Run test matrix
78+
foreach ($project in $Projects) {
79+
if ($Projects.Count -eq 1) {
80+
[string]$projectResultsDirectory = $ResultsDirectory
81+
}
82+
else {
83+
[string]$projectResultsDirectory =
84+
Join-Path $ResultsDirectory ([System.IO.Path]::GetFileNameWithoutExtension($project))
85+
}
86+
foreach ($config in $Configs) {
87+
foreach ($framework in $Frameworks) {
88+
89+
[string]$currentResultsDirectory = Join-Path $projectResultsDirectory $config $framework
90+
91+
# Clear result directory
92+
Remove-Item (Join-Path $currentResultsDirectory '*') -Recurse -ErrorAction Ignore
93+
94+
# Create dotnet command line
95+
[string[]] $params = @('test', "$project", '--nologo')
96+
if (-not $Build.IsPresent) {
97+
$params += '--no-build'
98+
}
99+
# Dont't use aliases like -r, they could change (-r is now --runtime in SDK >= 7.0)
100+
# See https://github.com/dotnet/sdk/issues/21952
101+
$params += '--configuration', $config
102+
$params += '--framework', $framework
103+
$params += '--logger', "console;verbosity=$Verbosity"
104+
$params += '--results-directory', $currentResultsDirectory
105+
$params += '--logger', 'trx'
106+
if ($CodeCoverage.IsPresent -or $Settings -ne '') {
107+
$params += '--collect', 'XPlat Code Coverage'
108+
if ($Settings -ne '') {
109+
$params += '--settings', $Settings
110+
}
111+
}
112+
$params += '--diag', $(Join-Path $currentResultsDirectory 'diag.log')
113+
if ($AdditionalArguments) {
114+
$params += $AdditionalArguments
115+
}
116+
117+
# Execute and log dotnet test
118+
Write-Host ($groupPrefix + 'dotnet') $params -Separator ' '
119+
[bool]$hasTestErrors = $false
120+
try {
121+
dotnet $params
122+
if ($LASTEXITCODE -ne 0) { $hasTestErrors = $true }
123+
}
124+
catch {
125+
$hasTestErrors = $true
126+
}
127+
Write-Host $groupSuffix
128+
if ($hasTestErrors) {
129+
$failedTestCount++
130+
if ($FailFast.IsPresent) {
131+
throw 'Test completed with failures and -FailFast was specified.'
132+
}
133+
if ($matrixCount -gt 1) {
134+
Write-Warning 'Test completed with failures.'
135+
}
136+
}
137+
138+
# Copy coverage results (if any) from random subdir into results dir, then remove subdirs
139+
# Directory may not exist if the test didn't run
140+
if ([System.IO.Directory]::Exists($currentResultsDirectory)) {
141+
Get-ChildItem (Join-Path $currentResultsDirectory '*' '*') -File |
142+
Copy-Item -Destination $currentResultsDirectory
143+
Get-ChildItem $currentResultsDirectory -Directory | Remove-Item -Recurse
144+
}
145+
}
146+
}
147+
}
148+
149+
if ($failedTestCount -gt 0) {
150+
throw 'Some tests were not successful.'
151+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Miscellaneous scripts.
44

5+
- **CI**\
6+
CI scripts.
7+
58
- **PowerShell**
69

710
- **Profile**\

0 commit comments

Comments
 (0)