Skip to content

Commit 6130fa3

Browse files
SyncFileContentsSyncFileContents
authored andcommitted
Sync scripts\PSBuild.psm1
1 parent c85dbc2 commit 6130fa3

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

scripts/PSBuild.psm1

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,11 +1452,37 @@ function Invoke-DotNetTest {
14521452
Runs dotnet test with code coverage collection.
14531453
.DESCRIPTION
14541454
Runs dotnet test with code coverage collection.
1455+
.PARAMETER Configuration
1456+
The build configuration to use.
1457+
.PARAMETER CoverageOutputPath
1458+
The path to output code coverage results.
14551459
#>
1460+
[CmdletBinding()]
1461+
param (
1462+
[string]$Configuration = "Release",
1463+
[string]$CoverageOutputPath = "coverage"
1464+
)
1465+
14561466
Write-StepHeader "Running Tests with Coverage" -Tags "Invoke-DotNetTest"
1467+
1468+
# Ensure the TestResults directory exists
1469+
$testResultsPath = Join-Path $CoverageOutputPath "TestResults"
1470+
New-Item -Path $testResultsPath -ItemType Directory -Force | Out-Null
1471+
14571472
# Run tests with both coverage collection and TRX logging for SonarQube
1458-
"dotnet-coverage collect `"dotnet test`" -f xml -o `"coverage.xml`"" | Invoke-ExpressionWithLogging | Write-InformationStream -Tags "Invoke-DotNetTest"
1473+
"dotnet test --configuration $Configuration /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=`"coverage.opencover.xml`" --results-directory `"$testResultsPath`" --logger `"trx;LogFileName=TestResults.trx`"" | Invoke-ExpressionWithLogging | Write-InformationStream -Tags "Invoke-DotNetTest"
14591474
Assert-LastExitCode "Tests failed"
1475+
1476+
# Find and copy coverage file to expected location for SonarQube
1477+
$coverageFiles = @(Get-ChildItem -Path . -Recurse -Filter "coverage.opencover.xml" -ErrorAction SilentlyContinue)
1478+
if ($coverageFiles.Count -gt 0) {
1479+
$latestCoverageFile = $coverageFiles | Sort-Object LastWriteTime -Descending | Select-Object -First 1
1480+
$targetCoverageFile = Join-Path $CoverageOutputPath "coverage.opencover.xml"
1481+
Copy-Item -Path $latestCoverageFile.FullName -Destination $targetCoverageFile -Force
1482+
Write-Information "Coverage file copied to: $targetCoverageFile" -Tags "Invoke-DotNetTest"
1483+
} else {
1484+
Write-Information "Warning: No coverage file found" -Tags "Invoke-DotNetTest"
1485+
}
14601486
}
14611487

14621488
function Invoke-DotNetPack {
@@ -2133,7 +2159,7 @@ function Invoke-BuildWorkflow {
21332159
# Build and Test
21342160
Invoke-DotNetRestore | Write-InformationStream -Tags "Invoke-BuildWorkflow"
21352161
Invoke-DotNetBuild -Configuration $Configuration -BuildArgs $BuildArgs | Write-InformationStream -Tags "Invoke-BuildWorkflow"
2136-
Invoke-DotNetTest | Write-InformationStream -Tags "Invoke-BuildWorkflow"
2162+
Invoke-DotNetTest -Configuration $Configuration -CoverageOutputPath "coverage" | Write-InformationStream -Tags "Invoke-BuildWorkflow"
21372163

21382164
return [PSCustomObject]@{
21392165
Success = $true

0 commit comments

Comments
 (0)