forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGenerate-TestWorkflows.ps1
More file actions
409 lines (343 loc) · 19.2 KB
/
Copy pathGenerate-TestWorkflows.ps1
File metadata and controls
409 lines (343 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -----------------------------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the ""License""); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an ""AS IS"" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -----------------------------------------------------------------------------------
<#
.SYNOPSIS
Generates GitHub Actions workflows for running tests upon a pull request action (either a
new pull request or a push to an existing one).
.DESCRIPTION
Generates 1 GitHub Actions workflow file for each project containing the string ".Tests"
in the name. The current project, ProjectReference dependencies, and common files
Directory.Build.*, TestTargetFraemworks.*, TestReferences.Common.* and Dependencies.props
are all used to build filter paths to determine when the workflow will run.
.PARAMETER OutputDirectory
The directory to output the files. This should be in a directory named /.github/workflows
in the root of the repository. The default is the directory of this script file.
.PARAMETER RepoRoot
The directory of the repository root. Defaults to two levels above the directory
of this script file.
.PARAMETER TestFrameworks
A string array of Dotnet target framework monikers to run the tests on. The default is
@('net10.0','net8.0','net472','net48').
.PARAMETER OperatingSystems
A string array of Github Actions operating system monikers to run the tests on.
The default is @('windows-latest', 'ubuntu-latest').
.PARAMETER TestPlatforms
A string array of platforms to run the tests on. Valid values are x64 and x86.
The default is @('x64').
.PARAMETER Configurations
A string array of build configurations to run the tests on. The default is @('Release').
.PARAMETER DotNet10SDKVersion
The SDK version of .NET 10.x to install on the build agent to be used for building and
testing. This SDK is always installed on the build agent. The default is 10.0.x.
.PARAMETER DotNet8SDKVersion
The SDK version of .NET 8.x to install on the build agent to be used for building and
testing. This SDK is always installed on the build agent. The default is 8.0.x.
#>
param(
[string]$OutputDirectory = $PSScriptRoot,
[string]$RepoRoot = (Split-Path (Split-Path $PSScriptRoot)),
[string[]]$TestFrameworks = @('net10.0', 'net8.0', 'net472', 'net48'), # targets under test: net10.0, net8.0, netstandard2.0, net462
[string[]]$OperatingSystems = @('windows-latest', 'ubuntu-latest'),
[string[]]$TestPlatforms = @('x64'),
[string[]]$Configurations = @('Release'),
[string]$DotNet10SDKVersion = '10.0.x',
[string]$DotNet8SDKVersion = '8.0.x'
)
function Resolve-RelativePath([string]$RelativeRoot, [string]$Path) {
Push-Location -Path $RelativeRoot
try {
return Resolve-Path $Path -Relative
} finally {
Pop-Location
}
}
function Get-ProjectDependencies([string]$ProjectPath, [string]$RelativeRoot, [System.Collections.Generic.HashSet[string]]$Result) {
$resolvedProjectPath = $ProjectPath
$rootPath = [System.IO.Path]::GetDirectoryName($resolvedProjectPath)
[xml]$project = Get-Content $resolvedProjectPath
foreach ($name in $project.SelectNodes("//Project/ItemGroup/ProjectReference") | Where-Object { $_.Include -notmatch '^$' } | ForEach-Object { $_.Include -split ';'}) {
$dependencyFullPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($rootPath, $name))
Get-ProjectDependencies $dependencyFullPath $RelativeRoot $Result
$dependency = Resolve-RelativePath $RelativeRoot $dependencyFullPath
$result.Add($dependency) | Out-Null
}
}
function Get-ProjectExternalPaths([string]$ProjectPath, [string]$RelativeRoot, [System.Collections.Generic.HashSet[string]]$Result) {
$resolvedProjectPath = $ProjectPath
$rootPath = [System.IO.Path]::GetDirectoryName($resolvedProjectPath)
[xml]$project = Get-Content $resolvedProjectPath
foreach ($name in $project.SelectNodes("//Project/ItemGroup/Compile") | Where-Object { $_.Include -notmatch '^$' } | ForEach-Object { $_.Include -split ';'}) {
# Temporarily override wildcard patterns so we can resolve the path and then put them back.
$name = $name -replace '\\\*\*\\\*', 'Wildcard1' -replace '\*', 'Wildcard2'
$dependencyFullPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($rootPath, $name)) -replace 'Wildcard1', '\**\*' -replace 'Wildcard2', '*'
# Make the path relative to the repo root.
$dependency = $($($dependencyFullPath.Replace($RelativeRoot, '.')) -replace '\\', '/').TrimStart('./')
$result.Add($dependency) | Out-Null
}
foreach ($name in $project.SelectNodes("//Project/ItemGroup/EmbeddedResource") | Where-Object { $_.Include -notmatch '^$' } | ForEach-Object { $_.Include -split ';'}) {
# Temporarily override wildcard patterns so we can resolve the path and then put them back.
$name = $name -replace '\\\*\*\\\*', 'Wildcard1' -replace '\*', 'Wildcard2'
$dependencyFullPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($rootPath, $name)) -replace 'Wildcard1', '\**\*' -replace 'Wildcard2', '*'
# Make the path relative to the repo root.
$dependency = $($($dependencyFullPath.Replace($RelativeRoot, '.')) -replace '\\', '/').TrimStart('./')
$result.Add($dependency) | Out-Null
}
}
function Get-ProjectPathDirectories([string]$ProjectPath, [string]$RelativeRoot, [System.Collections.Generic.HashSet[string]]$Result) {
$currentPath = New-Object System.IO.DirectoryInfo([System.IO.Path]::GetDirectoryName($ProjectPath))
$currentRelativePath = Resolve-RelativePath $RelativeRoot $currentPath.FullName
$Result.Add($currentRelativePath) | Out-Null
while ($true) {
$prevDirectory = New-Object System.IO.DirectoryInfo($currentPath.FullName)
$currentPath = $prevDirectory.Parent
if ($currentPath -eq $null) {
break
}
if ($currentPath.FullName -eq $RelativeRoot) {
$Result.Add(".") | Out-Null
break
}
$currentRelativePath = Resolve-RelativePath $RelativeRoot $currentPath.FullName
$Result.Add($currentRelativePath) | Out-Null
}
}
function Get-SupportedTargetFrameworksString([Parameter(Mandatory)][string] $ProjectPath) {
# NOTE: This will not appear when run directly in the console with minimal verbosity. MSBuild only produces the output when using a pipe, which is what we are doing here.
$output = dotnet build "$ProjectPath" --verbosity minimal --nologo --no-restore /t:PrintTargetFrameworks /p:TestProjectsOnly=true /p:TestFrameworks=true 2>&1 | Out-String
if ($output -match 'SupportedTargetFrameworks=([^\s]+)') {
return $matches[1]
}
throw "Failed to determine supported target frameworks for project: $ProjectPath"
}
function Ensure-Directory-Exists([string] $path) {
if (!(Test-Path $path)) {
New-Item $path -ItemType Directory
}
}
function Write-TestWorkflow(
[string]$OutputDirectory = $PSScriptRoot, #optional
[string]$RelativeRoot,
[string]$ProjectPath,
[string[]]$Configurations = @('Release'),
[string[]]$TestFrameworks = @('net6.0', 'net48'),
[string[]]$TestPlatforms = @('x64'),
[string[]]$OperatingSystems = @('windows-latest', 'ubuntu-latest', 'macos-latest'),
[string]$DotNet10SDKVersion = $DotNet10SDKVersion,
[string]$DotNet8SDKVersion = $DotNet8SDKVersion) {
$dependencies = New-Object System.Collections.Generic.HashSet[string]
Get-ProjectDependencies $ProjectPath $RelativeRoot $dependencies
$dependencyPaths = [System.Environment]::NewLine
foreach ($dependency in $dependencies) {
$dependencyRelativeDirectory = ([System.IO.Path]::GetDirectoryName($dependency) -replace '\\', '/').TrimStart('./')
$dependencyPaths += " - '$dependencyRelativeDirectory/**/*'" + [System.Environment]::NewLine
}
$projectRelativePath = $(Resolve-RelativePath $RelativeRoot $ProjectPath) -replace '\\', '/'
$projectRelativeDirectory = ([System.IO.Path]::GetDirectoryName($projectRelativePath) -replace '\\', '/').TrimStart('./')
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($ProjectPath)
[bool]$isCLI = if ($projectName -eq "Lucene.Net.Tests.Cli") { $true } else { $false } # Special case
$luceneCliProjectPath = $projectRelativePath -replace "Lucene.Net.Tests.Cli", "lucene-cli" # Special case
[string]$frameworks = '[' + $($TestFrameworks -join ', ') + ']'
[string]$platforms = '[' + $($TestPlatforms -join ', ') + ']'
[string]$oses = '[' + $($OperatingSystems -join ', ') + ']'
[string]$configurations = '[' + $($Configurations -join ', ') + ']'
$directories = New-Object System.Collections.Generic.HashSet[string]
Get-ProjectPathDirectories $projectPath $RepoRoot $directories
$directoryBuildPaths = [System.Environment]::NewLine
foreach ($directory in $directories) {
$relativeDirectory = ([System.IO.Path]::Combine($directory, 'Directory.Build.*') -replace '\\', '/').TrimStart('./')
$directoryBuildPaths += " - '$relativeDirectory'" + [System.Environment]::NewLine
}
$paths = New-Object System.Collections.Generic.HashSet[string]
Get-ProjectExternalPaths $ProjectPath $RelativeRoot $paths
foreach ($path in $paths) {
$directoryBuildPaths += " - '$path'" + [System.Environment]::NewLine
}
$fileText = "####################################################################################
# DO NOT EDIT: This file was automatically generated by Generate-TestWorkflows.ps1
####################################################################################
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# `"License`"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# `"AS IS`" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: '$projectName'
on:
workflow_dispatch:
pull_request:
paths:
- '$projectRelativeDirectory/**/*'
- '.build/dependencies.props'
- '.build/TestReferences.Common.*'
- 'TestTargetFrameworks.*'
- '.github/**/*.yml'
- '*.sln'$directoryBuildPaths
# Dependencies$dependencyPaths
- '!**/*.md'
jobs:
Test:
runs-on: `${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: $oses
framework: $frameworks
platform: $platforms
configuration: $configurations
exclude:
- os: ubuntu-latest
framework: net48
- os: ubuntu-latest
framework: net472
- os: macos-latest
framework: net48
- os: macos-latest
framework: net472
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_NOLOGO: 1
NUGET_PACKAGES: `${{ github.workspace }}/.nuget/packages
BUILD_FOR_ALL_TEST_TARGET_FRAMEWORKS: 'true'
project_path: '$projectRelativePath'"
if ($isCLI) {
$fileText += "
project_under_test_path: '$luceneCliProjectPath'
run_slow_tests: 'true'"
} else {
$fileText += "
run_slow_tests: 'false'"
}
$fileText += "
trx_file_name: 'TestResults.trx'
md_file_name: 'TestResults.md' # Report file name for LiquidTestReports.Markdown
steps:
- name: Checkout Source Code
uses: actions/checkout@v5
- name: Setup .NET 8 SDK
uses: actions/setup-dotnet@v5
with:
dotnet-version: '$DotNet8SDKVersion'
if: `${{ startswith(matrix.framework, 'net8.') }}
- name: Setup .NET 10 SDK
uses: actions/setup-dotnet@v5
with:
dotnet-version: '$DotNet10SDKVersion'
- name: Cache NuGet Packages
uses: actions/cache@v4
with:
# '**/*.*proj' includes .csproj, .vbproj, .fsproj, msbuildproj, etc.
# '**/*.props' includes Directory.Packages.props, Directory.Build.props and Dependencies.props
# '**/*.targets' includes Directory.Build.targets
# '**/*.sln' and '*.sln' ensure root solution files are included (minimatch glitch for file extension .sln)
# 'global.json' included for SDK version changes
key: nuget-`${{ runner.os }}-`${{ env.BUILD_FOR_ALL_TEST_TARGET_FRAMEWORKS }}-`${{ hashFiles('**/*.*proj', '**/*.props', '**/*.targets', '**/*.sln', '*.sln', 'global.json') }}
path: `${{ env.NUGET_PACKAGES }}
- name: Restore
run: dotnet restore /p:TestFrameworks=`${{ env.BUILD_FOR_ALL_TEST_TARGET_FRAMEWORKS }}
- name: Setup Environment Variables
run: |
`$project_name = [System.IO.Path]::GetFileNameWithoutExtension(`$env:project_path)
`$test_results_artifact_name = `"testresults_`${{matrix.os}}_`${{matrix.framework}}_`${{matrix.platform}}_`${{matrix.configuration}}`"
`$working_directory = `"`$env:GITHUB_WORKSPACE`"
Write-Host `"Project Name: `$project_name`"
Write-Host `"Results Artifact Name: `$test_results_artifact_name`"
Write-Host `"Working Directory: `$working_directory`"
echo `"project_name=`$project_name`" | Out-File -FilePath `$env:GITHUB_ENV -Encoding utf8 -Append
echo `"test_results_artifact_name=`$test_results_artifact_name`" | Out-File -FilePath `$env:GITHUB_ENV -Encoding utf8 -Append
# Set the Azure DevOps default working directory env variable, so our tests only need to deal with a single env variable
echo `"SYSTEM_DEFAULTWORKINGDIRECTORY=`$working_directory`" | Out-File -FilePath `$env:GITHUB_ENV -Encoding utf8 -Append
# Title for LiquidTestReports.Markdown
echo `"title=Test Results for `$project_name - `${{matrix.framework}} - `${{matrix.platform}} - `${{matrix.os}}`" | Out-File -FilePath `$env:GITHUB_ENV -Encoding utf8 -Append
shell: pwsh"
if ($isCLI) {
# Special case: Generate lucene-cli.nupkg for installation test so the test runner doesn't have to do it
$fileText += "
- run: dotnet pack `"`${{env.project_under_test_path}}`" --configuration `"`${{matrix.configuration}}`" --no-restore -p:TestFrameworks=`${{ env.BUILD_FOR_ALL_TEST_TARGET_FRAMEWORKS }} -p:PortableDebugTypeOnly=true
shell: bash"
}
$fileText += "
- run: dotnet build `"`${{env.project_path}}`" --configuration `"`${{matrix.configuration}}`" --framework `"`${{matrix.framework}}`" --no-restore -p:TestFrameworks=`${{ env.BUILD_FOR_ALL_TEST_TARGET_FRAMEWORKS }}
shell: bash
- run: dotnet test `"`${{env.project_path}}`" --configuration `"`${{matrix.configuration}}`" --framework `"`${{matrix.framework}}`" --no-build --no-restore --blame-hang --blame-hang-dump-type mini --blame-hang-timeout 20minutes --logger:`"console;verbosity=normal`" --logger:`"trx;LogFileName=`${{env.trx_file_name}}`" --logger:`"liquid.md;LogFileName=`${{env.md_file_name}};Title=`${{env.title}};`" --results-directory:`"`${{github.workspace}}/`${{env.test_results_artifact_name}}/`${{env.project_name}}`" -- RunConfiguration.TargetPlatform=`${{matrix.platform}} NUnit.DisplayName=FullName TestRunParameters.Parameter\(name=\`"tests:slow\`",\ value=\`"\`${{env.run_slow_tests}}\`"\)
shell: bash
# upload reports as build artifacts
- name: Upload a Build Artifact
uses: actions/upload-artifact@v4
if: `${{always()}}
with:
name: '`${{env.test_results_artifact_name}}'
path: '`${{github.workspace}}/`${{env.test_results_artifact_name}}'
- name: Output Test Summary
if: `${{always()}}
shell: pwsh
run: |
`$md_file = Join-Path `${{github.workspace}} `${{env.test_results_artifact_name}} `${{env.project_name}} `${{env.md_file_name}}
if (Test-Path `$md_file) {
Get-Content `$md_file | Add-Content `$env:GITHUB_STEP_SUMMARY
}
"
# GitHub Actions does not support filenames with "." in them, so replace
# with "-"
$projectFileName = $projectName -replace '\.', '-'
$FilePath = "$OutputDirectory/$projectFileName.yml"
#$dir = [System.IO.Path]::GetDirectoryName($File)
Ensure-Directory-Exists $OutputDirectory
Write-Host "Generating workflow file: $FilePath"
# Ensure the file does not get generated with a BOM
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($FilePath, $fileText, $utf8NoBom)
#Write-Host $fileText
}
Push-Location $RelativeRoot
try {
[string[]]$TestProjects = Get-ChildItem -Path "$RepoRoot/**/*.csproj" -Recurse | where { $_.Directory.Name.Contains(".Tests") -and !($_.Directory.FullName.Contains('svn-')) } | Select-Object -ExpandProperty FullName
} finally {
Pop-Location
}
#Write-TestWorkflow -OutputDirectory $OutputDirectory -ProjectPath $projectPath -RelativeRoot $repoRoot -TestFrameworks @('net6.0') -OperatingSystems $OperatingSystems -TestPlatforms $TestPlatforms -Configurations $Configurations -DotNet8SDKVersion $DotNet8SDKVersion
#Write-Host $TestProjects
foreach ($testProject in $TestProjects) {
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($testProject)
# Call the target to get the configured test frameworks for this project.
$frameworksString = Get-SupportedTargetFrameworksString $testProject
if ($frameworksString -eq 'none') {
Write-Host "WARNING: Skipping project '$projectName' because it is not marked with `<IsTestProject`>true`<`/IsTestProject`> and/or it contains no test frameworks for the current environment." -ForegroundColor Yellow
continue
}
[string[]]$frameworks = $frameworksString -split '\s*;\s*'
$frameworks = $frameworks | ? { $TestFrameworks -contains $_ } # IntersectWith
if ($frameworks.Count -eq 0) {
Write-Host "WARNING: ${projectName} contains no matching target frameworks: $frameworksString" -ForegroundColor Yellow
continue
}
Write-Host ""
Write-Host "Frameworks To Test for ${projectName}: $($frameworks -join ';')" -ForegroundColor Cyan
#Write-Host "Project: $projectName"
Write-TestWorkflow -OutputDirectory $OutputDirectory -ProjectPath $testProject -RelativeRoot $RepoRoot -TestFrameworks $frameworks -OperatingSystems $OperatingSystems -TestPlatforms $TestPlatforms -Configurations $Configurations -DotNet8SDKVersion $DotNet8SDKVersion
}