forked from SonarSource/sonar-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression-test.ps1
291 lines (240 loc) · 11.2 KB
/
regression-test.ps1
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
[CmdletBinding(PositionalBinding = $false)]
param
(
[Parameter(HelpMessage = "The key of the rule to test, e.g. S1234. If omitted, all rules will be tested.")]
[ValidatePattern("^S[0-9]+")]
[string]
$ruleId,
[Parameter(HelpMessage = "The name of single project to build. If ommited, all projects will be build.")]
[ValidateSet("AnalyzeGenerated.CS", "AnalyzeGenerated.VB", "akka.net", "AutoMapper", "BlazorSample", "CSharpLatest", "Ember-MM", "Nancy", "ManuallyAddedNoncompliantIssues.CS", "ManuallyAddedNoncompliantIssues.VB", "RazorSample", "Roslyn.1.3.1", "SkipGenerated.CS", "SkipGenerated.VB", "SonarLintExclusions", "WebConfig")]
[string]
$project
)
Set-StrictMode -version 2.0
$ErrorActionPreference = "Stop"
$DifferencesMsg = "ERROR: There are differences between the actual and the expected issues."
if ($PSBoundParameters['Verbose'] -Or $PSBoundParameters['Debug']) {
$global:DebugPreference = "Continue"
}
function Prepare-Project([string]$ProjectName){
$Output = ".\output\$ProjectName"
New-Item -ItemType directory -Path $Output | out-null
$SourcePath = ".\config\$ProjectName\SonarLint.xml"
if(-Not (Test-Path $SourcePath)){
$SourcePath = ".\config\SonarLint.xml"
}
$Content = Get-Content -Path $SourcePath -Raw
if($ruleId){
$RuleFragment = " <Rule><Key>$ruleId</Key></Rule>"
} else {
$HotspotFiles = Get-ChildItem ..\rspec -Filter *.json -Recurse | Select-String "SECURITY_HOTSPOT" | Select-Object -ExpandProperty FileName
$HotspotIDs = $HotspotFiles -Replace ".json", "" | Select-Object -Unique
$RuleFragment = ""
foreach($HotspotID in $HotspotIDs){
$RuleFragment = $RuleFragment + " <Rule><Key>$HotspotID</Key></Rule>`n"
}
}
$Content = $Content -Replace "<Rules>", "<Rules>`n$RuleFragment"
Set-Content -Path "$Output\SonarLint.xml" -Value $Content
Write-Host "Using $Output\SonarLint.xml"
}
function Build-Project-MSBuild([string]$ProjectName, [string]$SolutionRelativePath, [int]$CpuCount = 4) {
if ($project -And -Not ($ProjectName -eq $project)) {
Write-Host "Build skipped: $ProjectName"
return
}
Prepare-Project($ProjectName)
$solutionPath = Resolve-Path ".\Projects\${ProjectName}\${SolutionRelativePath}"
# The PROJECT env variable is used by 'Directory.Build.targets'
Write-Debug "Setting PROJECT environment variable to '${ProjectName}'"
$Env:PROJECT = $ProjectName
Restore-Packages $solutionPath
Invoke-MSBuild $solutionPath `
/m:$CpuCount `
/t:rebuild `
/p:Configuration=Debug `
/clp:"Summary;ErrorsOnly" `
/fl `
/flp:"logFile=output\${ProjectName}\Build.log;verbosity=d"
}
function Build-Project-DotnetTool([string]$ProjectName, [string]$SolutionRelativePath) {
if ($project -And -Not ($ProjectName -eq $project)) {
Write-Host "Build skipped: $ProjectName"
return
}
$projectGlobalJsonPath = ".\Projects\${ProjectName}\global.json"
$globalJsonContent = $(Get-Content $projectGlobalJsonPath)
Write-Host "Will build dotnet project: '${ProjectName}' (with ${projectGlobalJsonPath}) with dotnet version '${globalJsonContent}'."
Prepare-Project($ProjectName)
$solutionPath = Resolve-Path ".\Projects\${ProjectName}\${SolutionRelativePath}"
# The PROJECT env variable is used by 'Directory.Build.targets'
Write-Debug "Setting PROJECT environment variable to '${ProjectName}'"
$Env:PROJECT = $ProjectName
# Copy the global.json in the folder where we do analysis.
$tempGlobalJsonPath = ".\global.json"
Copy-Item $projectGlobalJsonPath $tempGlobalJsonPath
dotnet --version
dotnet restore --locked-mode $solutionPath
# To change the verbosity, comment out the '-clp' parameter and add the '-v' parameter.
Exec { & dotnet build $solutionPath `
--no-restore `
-t:rebuild `
-p:Configuration=Debug `
-clp:"Summary;ErrorsOnly" `
-fl `
-flp:"logFile=output\${ProjectName}\Build.log;verbosity=d" `
} -errorMessage "ERROR: Build FAILED."
Remove-Item $tempGlobalJsonPath
}
function Initialize-ActualFolder() {
Write-Host "Initializing the actual issues folder with the expected result"
if (Test-Path .\actual) {
Write-Host "Removing existing folder 'actual'"
Remove-Item -Recurse -Force actual
}
# this copies no files if ruleId is not set, and all but ending with ruleId if set
Copy-FolderRecursively -From .\expected -To .\actual -Exclude "${ruleId}*.json"
}
function Initialize-OutputFolder() {
Write-Host "Initializing the output folder"
if (Test-Path .\output) {
Write-Host "Removing existing folder 'output'"
Remove-Item -Recurse -Force output
}
Write-Debug "Creating folder 'output'"
New-Item -ItemType directory -Path .\output | out-null
if ($ruleId) {
Write-Host "Running ITs with only rule ${ruleId} turned on."
$template = Get-Content -Path ".\SingleRule.ruleset.template" -Raw
$rulesetWithOneRule = $template.Replace("<Rule Id=`"$ruleId`" Action=`"None`" />", `
"<Rule Id=`"$ruleId`" Action=`"Warning`" />")
Set-Content -Path ".\output\AllSonarAnalyzerRules.ruleset" -Value $rulesetWithOneRule
}
else {
Write-Host "Running ITs with all rules turned on."
Copy-Item ".\AllSonarAnalyzerRules.ruleset" -Destination ".\output"
}
Write-Host "The rule set we use is .\output\AllSonarAnalyzerRules.ruleset."
}
function Get-FullPath($Folder) {
return [System.IO.Path]::GetFullPath((Join-Path (Get-Location).Path $Folder))
}
function Copy-FolderRecursively($From, $To, $Include, $Exclude) {
$fromPath = Get-FullPath -Folder $From
$toPath = Get-FullPath -Folder $To
$files = if ($Include) {
Get-ChildItem -Path $fromPath -Recurse -Include $Include
} else {
Get-ChildItem -Path $fromPath -Recurse -Exclude $Exclude
}
foreach ($file in $files) {
$path = Join-Path $toPath $file.FullName.Substring($fromPath.length)
$parent = split-path $path -parent
if (-Not (Test-Path $parent)) {
New-Item $parent -Type Directory -Force | out-null
}
Copy-Item $file.FullName -Destination $path
}
}
function Show-DiffResults() {
if (Test-Path .\diff) {
Write-Host "Removing existing folder 'diff'"
Remove-Item -Recurse -Force .\diff
}
if (!$ruleId -And !$project)
{
Write-Host "Will find differences for all projects, all rules."
Exec { & git diff --no-index --exit-code ./expected ./actual } -errorMessage $DifferencesMsg
}
else
{
# do a partial diff
New-Item ".\diff" -Type Directory | out-null
New-Item ".\diff\actual" -Type Directory | out-null
New-Item ".\diff\expected" -Type Directory | out-null
if (!$ruleId -And $project) {
Write-Host "Will find differences for '${project}', all rules."
Copy-FolderRecursively -From ".\expected\${project}" -To .\diff\expected
Copy-FolderRecursively -From ".\actual\${project}" -To .\diff\actual
} elseif ($ruleId -And !$project) {
Write-Host "Will find differences for all projects, rule ${ruleId}."
Copy-FolderRecursively -From .\expected -To .\diff\expected -Include "${ruleId}-*.json"
Copy-FolderRecursively -From .\actual -To .\diff\actual -Include "${ruleId}-*.json"
} else {
Write-Host "Will find differences for '${project}', rule ${ruleId}."
Copy-FolderRecursively -From ".\expected\${project}" -To .\diff\expected -Include "${ruleId}-*.json"
Copy-FolderRecursively -From ".\actual\${project}" -To .\diff\actual -Include "${ruleId}-*.json"
}
Exec { & git diff --no-index --exit-code .\diff\expected .\diff\actual } -errorMessage $DifferencesMsg
}
}
function Invoke-JsonParser()
{
$JsonParser = Join-Path $PSScriptRoot "..\packaging\binaries\ITs.JsonParser\ITs.JsonParser.exe"
$Arguments = @(" ")
if ($ruleId){
$Arguments += "--rule"
$Arguments += $ruleId
}
if ($project){
$Arguments += "--project"
$Arguments += $project
}
Start-Process $JsonParser -ArgumentList $Arguments -NoNewWindow -Wait
}
try {
. (Join-Path $PSScriptRoot "..\..\scripts\build\build-utils.ps1")
Push-Location $PSScriptRoot
Test-FileExists "..\packaging\binaries\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.dll"
Test-FileExists "..\packaging\binaries\SonarAnalyzer.VisualBasic\SonarAnalyzer.VisualBasic.dll"
Test-FileExists "..\packaging\binaries\ITs.JsonParser\ITs.JsonParser.exe"
Write-Header "Initializing the environment"
Initialize-ActualFolder
Initialize-OutputFolder
# Note: Automapper has multiple configurations that are built simultaneously and sometimes
# it happens that a the same project is built in parallel in different configurations. The
# protobuf-generating rules try to write their output in the same folder and fail, even
# though there is a basic lock, because it is process-wide and not machine-wide.
# Parallel builds are not a problem when run through the SonarScanner for .NET because it
# redirects the outputs of the different configurations in separate folders.
# Do not forget to update ValidateSet of -project parameter when new project is added.
Build-Project-MSBuild "ManuallyAddedNoncompliantIssues.CS" "ManuallyAddedNoncompliantIssues.CS.sln"
Build-Project-MSBuild "ManuallyAddedNoncompliantIssues.VB" "ManuallyAddedNoncompliantIssues.VB.sln"
Build-Project-MSBuild "AnalyzeGenerated.CS" "AnalyzeGenerated.CS.sln"
Build-Project-MSBuild "AnalyzeGenerated.VB" "AnalyzeGenerated.VB.sln"
Build-Project-MSBuild "Ember-MM" "Ember Media Manager.sln"
Build-Project-MSBuild "Nancy" "Nancy.sln"
Build-Project-MSBuild "Roslyn.1.3.1" "Roslyn.1.3.1.sln"
Build-Project-MSBuild "SkipGenerated.CS" "SkipGenerated.CS.sln"
Build-Project-MSBuild "SkipGenerated.VB" "SkipGenerated.VB.sln"
Build-Project-MSBuild "WebConfig" "WebConfig.sln"
Build-Project-DotnetTool "akka.net" "src\Akka.sln"
Build-Project-DotnetTool "AutoMapper" "AutoMapper.sln"
Build-Project-DotnetTool "SonarLintExclusions" "SonarLintExclusions.sln"
Build-Project-DotnetTool "RazorSample" "RazorSample.sln"
Build-Project-DotnetTool "BlazorSample" "BlazorSample.sln"
Build-Project-DotnetTool "CSharpLatest" "CSharpLatest.sln"
Invoke-JsonParser
# ToDo: Migrate all of the remaining logic to JsonParser
Write-Host "Checking for differences..."
Show-DiffResults
Write-Host -ForegroundColor Green "SUCCESS: ITs were successful! No differences were found!"
exit 0
}
catch {
Write-Host -ForegroundColor Red $_
# ToDo: Migrate this to JsonParser, remove update-expected.ps1 file
if($_.FullyQualifiedErrorId -eq $DifferencesMsg) {
./update-expected.ps1 -Project $Project
} else {
Write-Host "----"
Write-Host $_.Exception
Write-Host "----"
Write-Host $_.ScriptStackTrace
}
exit 1
}
finally {
Pop-Location
Remove-Item -ErrorAction Ignore -Force global.json
}