Repository navigation
145 lines (142 loc) · 7.49 KB
/
Copy pathsonarcloud.yml
File metadata and controls
145 lines (142 loc) · 7.49 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
name: Sonarcloud
on:
workflow_dispatch:
push:
branches:
- main
- support/v1
- support/v2
paths-ignore: ['.vscode/**']
pull_request:
types: [opened, synchronize, reopened]
paths-ignore: ['.vscode/**']
permissions:
contents: read
pull-requests: read
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
jobs:
checksecret:
name: check if SONAR_TOKEN is set in github secrets
runs-on: ubuntu-latest
outputs:
is_SONAR_TOKEN_set: ${{ steps.checksecret_job.outputs.is_SONAR_TOKEN_set }}
steps:
- name: Check whether unity activation requests should be done
id: checksecret_job
run: |
echo "is_SONAR_TOKEN_set=${{ env.SONAR_TOKEN != '' }}" >> $GITHUB_OUTPUT
build:
needs: [checksecret]
if: needs.checksecret.outputs.is_SONAR_TOKEN_set == 'true'
name: Build
runs-on: windows-latest
steps:
- name: Set up JDK 17
uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1
with:
distribution: 'temurin'
java-version: 17
- name: Setup .NET
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: |
8.0.x
10.0.x
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Cache SonarCloud packages
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Install SonarCloud scanner
run: dotnet tool install dotnet-sonarscanner --create-manifest-if-needed
- name: Install PowerShell test dependency
shell: pwsh
run: Install-Module Pester -RequiredVersion 5.7.1 -Scope CurrentUser -Force
- name: Test Hidi NuGet helper with measured coverage
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
Import-Module Pester -RequiredVersion 5.7.1 -ErrorAction Stop
$helperPath = 'scripts/check-nuget-package-published.ps1'
$helper = (Resolve-Path $helperPath).Path
$outputRoot = Join-Path $PWD 'artifacts\hidi\powershell-coverage'
New-Item -ItemType Directory -Force -Path $outputRoot | Out-Null
$configuration = New-PesterConfiguration
$configuration.Run.Path = 'test\Microsoft.OpenApi.Hidi.Tests\check-nuget-package-published.Tests.ps1'
$configuration.Run.PassThru = $true
$configuration.CodeCoverage.Enabled = $true
$configuration.CodeCoverage.Path = $helper
$configuration.CodeCoverage.OutputFormat = 'JaCoCo'
$configuration.CodeCoverage.OutputPath = Join-Path $outputRoot 'pester-coverage.xml'
$configuration.CodeCoverage.CoveragePercentTarget = 80
$result = Invoke-Pester -Configuration $configuration
if ($result.FailedCount -gt 0 -or $result.PassedCount -lt 24) {
throw "Hidi NuGet helper tests failed or did not execute all 24 cases."
}
$report = [xml](Get-Content $configuration.CodeCoverage.OutputPath.Value -Raw)
$sourceFiles = @($report.SelectNodes('//sourcefile'))
if ($sourceFiles.Count -ne 1 -or $sourceFiles[0].name -ne [IO.Path]::GetFileName($helper)) {
throw 'Expected measured coverage of only the Hidi NuGet helper.'
}
$lines = @($sourceFiles[0].SelectNodes('line'))
$sourceLineCount = @(Get-Content $helper).Count
$lineNumbers = @($lines | ForEach-Object { [int]$_.nr })
if ($lines.Count -eq 0 -or @($lineNumbers | Select-Object -Unique).Count -ne $lines.Count -or
@($lines | Where-Object {
[int]$_.nr -le 0 -or [int]$_.nr -gt $sourceLineCount -or
-not $_.HasAttribute('ci') -or -not $_.HasAttribute('mi') -or
[int]$_.ci -lt 0 -or [int]$_.mi -lt 0 -or ([int]$_.ci + [int]$_.mi) -le 0
}).Count -gt 0) {
throw 'Missing or invalid measured helper coverage lines.'
}
$covered = @($lines | Where-Object { [int]$_.ci -gt 0 }).Count
if ($covered / $lines.Count -lt 0.8) {
throw "Insufficient measured Hidi helper coverage: $covered/$($lines.Count) lines."
}
$coverage = [xml]'<coverage version="1"/>'
$file = $coverage.CreateElement('file')
$file.SetAttribute('path', $helperPath)
[void]$coverage.DocumentElement.AppendChild($file)
foreach ($line in $lines) {
$entry = $coverage.CreateElement('lineToCover')
$entry.SetAttribute('lineNumber', $line.nr)
$entry.SetAttribute('covered', ([int]$line.ci -gt 0).ToString().ToLowerInvariant())
[void]$file.AppendChild($entry)
}
$coverage.Save((Join-Path $outputRoot 'sonar-coverage.xml'))
Write-Host "Measured Hidi NuGet helper coverage: $covered/$($lines.Count) lines; Sonar generic report generated."
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
CollectCoverage: true
CoverletOutputFormat: 'opencover' # https://github.com/microsoft/vstest/issues/4014#issuecomment-1307913682
shell: pwsh
run: |
dotnet tool run dotnet-sonarscanner begin /k:"microsoft_OpenAPI.NET.OData" /o:"microsoft" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="test/**/coverage.net8.0.opencover.xml,artifacts/hidi/coverage/*opencover*.xml" /d:sonar.coverageReportPaths="artifacts/hidi/powershell-coverage/sonar-coverage.xml"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
dotnet workload restore
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
dotnet build
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
dotnet test test\Microsoft.OpenAPI.OData.Reader.Tests\Microsoft.OpenAPI.OData.Reader.Tests.csproj --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
dotnet run --project test\Microsoft.OpenApi.Hidi.Tests\Microsoft.OpenApi.Hidi.Tests.csproj --no-build -- --minimum-expected-tests 1 --coverlet --coverlet-output-format opencover --coverlet-include "[Microsoft.OpenApi.Hidi]*" --results-directory artifacts\hidi\coverage
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$reports = @(Get-ChildItem artifacts\hidi\coverage -Filter '*opencover*.xml')
if ($reports.Count -eq 0) { throw "Hidi OpenCover report was not generated." }
$hidiModules = @($reports | ForEach-Object {
([xml](Get-Content $_.FullName -Raw)).CoverageSession.Modules.Module
} | Where-Object { $_.ModuleName -eq 'Microsoft.OpenApi.Hidi' })
if (-not ($hidiModules | Where-Object {
$_.SelectNodes('./Classes/Class/Methods/Method/SequencePoints/SequencePoint').Count -gt 0 -and
$_.SelectNodes('./Classes/Class/Methods/Method/SequencePoints/SequencePoint[@vc > 0]').Count -gt 0
})) {
throw "Hidi OpenCover report contains no covered Hidi sequence points."
}
dotnet tool run dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }