From ed572edb5f759c884052daab4273ee74381ecc95 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 23 Jul 2026 03:59:02 +0200 Subject: [PATCH] Discover test projects in build pipelines Replace duplicated hardcoded test project lists with framework-aware discovery shared by the primary and net48 coverage workflows. --- .github/workflows/net48-compatibility.yml | 17 +++++----- Build/build-functions.psm1 | 38 ++++++++++++++++------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/.github/workflows/net48-compatibility.yml b/.github/workflows/net48-compatibility.yml index 959e57ddc2..0f6e070f14 100644 --- a/.github/workflows/net48-compatibility.yml +++ b/.github/workflows/net48-compatibility.yml @@ -50,20 +50,17 @@ jobs: - name: Run the complete net48 test suite with coverage shell: pwsh run: | - $testProjects = @( - 'UnitsNet.Tests/UnitsNet.Tests.csproj', - 'UnitsNet.NumberExtensions.Tests/UnitsNet.NumberExtensions.Tests.csproj', - 'UnitsNet.NumberExtensions.CS14.Tests/UnitsNet.NumberExtensions.CS14.Tests.csproj', - 'UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj' - ) + Import-Module ./Build/build-functions.psm1 + $testProjects = @(Get-TestProjects -Framework 'net48') + if ($testProjects.Count -eq 0) { + throw 'No test projects targeting net48 were found.' + } New-Item -ItemType Directory -Force -Path Artifacts/Coverage | Out-Null foreach ($testProject in $testProjects) { - $projectName = [IO.Path]::GetFileNameWithoutExtension($testProject) - $testArguments = @( - $testProject, + $testProject.FullName, '--configuration', 'Release', '--framework', 'net48', '--logger', 'trx', @@ -73,7 +70,7 @@ jobs: dotnet tool run dotCover -- cover-dotnet ` --TargetWorkingDir $PWD.Path ` - --Output "Artifacts/Coverage/$projectName.coverage.xml" ` + --Output "Artifacts/Coverage/$($testProject.BaseName).coverage.xml" ` --ReportType DetailedXML ` --Filters '+:module=UnitsNet*;-:module=*Tests' ` --ReturnTargetExitCode ` diff --git a/Build/build-functions.psm1 b/Build/build-functions.psm1 index 88cd3b3e7f..5b3367425f 100644 --- a/Build/build-functions.psm1 +++ b/Build/build-functions.psm1 @@ -33,17 +33,34 @@ function Start-Build { write-host -foreground blue "Start-Build...END`n" } +function Get-TestProjects { + Param( + [Parameter(Mandatory)] + [string] $Framework + ) + + $testProjects = Get-ChildItem -Path $root -Recurse -Filter "*.Tests.csproj" -File | Sort-Object FullName + foreach ($testProject in $testProjects) { + $propertiesJson = & dotnet msbuild $testProject.FullName ` + -getProperty:TargetFrameworks,TargetFramework ` + -nologo + if ($lastexitcode -ne 0) { throw "Failed to read target frameworks from $($testProject.FullName)." } + + $properties = $propertiesJson | ConvertFrom-Json + $targetFrameworks = @($properties.Properties.TargetFrameworks -split ';') + $properties.Properties.TargetFramework + if ($targetFrameworks -contains $Framework) { + $testProject + } + } +} + function Start-Tests { Param( [switch] $SkipCoverage ) - $projectPaths = @( - "UnitsNet.Tests/UnitsNet.Tests.csproj", - "UnitsNet.NumberExtensions.Tests/UnitsNet.NumberExtensions.Tests.csproj", - "UnitsNet.NumberExtensions.CS14.Tests/UnitsNet.NumberExtensions.CS14.Tests.csproj", - "UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj" - ) + $testProjects = @(Get-TestProjects -Framework "net10.0") + if ($testProjects.Count -eq 0) { throw "No test projects targeting net10.0 were found." } # Parent dir must exist before xunit tries to write files to it new-item -type directory -force $testReportDir 1> $null @@ -52,10 +69,9 @@ function Start-Tests { } write-host -foreground blue "Run tests...`n---" - foreach ($projectPath in $projectPaths) { - $projectFileNameNoEx = [System.IO.Path]::GetFileNameWithoutExtension($projectPath) - $coverageReportFile = Join-Path $testCoverageDir "${projectFileNameNoEx}.coverage.xml" - $projectDir = Join-Path $root ([System.IO.Path]::GetDirectoryName($projectPath)) + foreach ($testProject in $testProjects) { + $coverageReportFile = Join-Path $testCoverageDir "$($testProject.BaseName).coverage.xml" + $projectDir = $testProject.DirectoryName # dotnet commands (xunit, dotcover) must run in same dir as project push-location $projectDir @@ -138,4 +154,4 @@ function Compress-ArtifactsAsZip { write-host -foreground blue "Zip artifacts...END`n" } -export-modulemember -function Remove-ArtifactsDir, Update-GeneratedCode, Start-Build, Start-Tests, Start-PackNugets, Compress-ArtifactsAsZip +export-modulemember -function Remove-ArtifactsDir, Update-GeneratedCode, Start-Build, Get-TestProjects, Start-Tests, Start-PackNugets, Compress-ArtifactsAsZip