forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateMatrixTestReport.ps1
More file actions
419 lines (335 loc) · 13 KB
/
CreateMatrixTestReport.ps1
File metadata and controls
419 lines (335 loc) · 13 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
410
411
412
413
414
415
416
417
418
419
<#
.Description
MatrixTest - Performs matrix testing of the Coding Standards Suite against
various compilers.
Example Usage:
`MatrixTest.ps1 -Configuration Clang -Suite -SuiteName AUTOSAR `
All Parameters:
-Suite [<SwitchParameter>]
The mode to run the tool in. Valid values are: Suite, Rule, or Package.
Required? true
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-Rule [<SwitchParameter>]
Required? true
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-Package [<SwitchParameter>]
Required? true
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-ReportDir <String>
Required? false
Position? named
Default value (Get-Location)
Accept pipeline input? false
Accept wildcard characters? false
-UseTmpDir [<SwitchParameter>]
Tells the script to use the sytem tmp directory instead of the rule
directory.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-Configuration <String>
The compiler to use. Valid values are 'clang' and 'arm-clang'.
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-SuiteName <String>
For a suite, the suites we support. Valid values are 'CERT-C++' and
'AUTOSAR'.
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-RuleName <String>
The rule to test, e.g.: A0-1-1.
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-PackageName <String>
The package to test. This will test all rules within the specified
package. Valid values are taken from the basename of the `.json` files within the
`rule_packages` directory.
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
#>
param(
# The mode to run the tool in. Valid values are: Suite, Rule, or Package.
[Parameter(Mandatory, ParameterSetName = 'Suite')]
[switch]
$Suite,
[Parameter(Mandatory, ParameterSetName = 'Rule')]
[switch]
$Rule,
[Parameter(Mandatory, ParameterSetName = 'Package')]
[switch]
$Package,
[Parameter(Mandatory, ParameterSetName = 'All')]
[switch]
$AllRules,
[Parameter(Mandatory)]
[ValidateSet('c', 'cpp')]
[string]
$Language,
[Parameter(Mandatory = $false)]
[string]
$ReportDir = (Get-Location),
# Skip summary report -- used for Linux hosts that don't support
# the OLE database stuff.
[Parameter(Mandatory = $false)]
[switch]
$SkipSummaryReport,
# Tells the script to use the sytem tmp directory instead of the rule
# directory.
[Parameter(Mandatory = $false)]
[switch]
$UseTmpDir,
# Number of threads to use
[Parameter(Mandatory = $false)]
[string]
$NumThreads = 10,
# The compiler to use. Valid values are 'clang' and 'arm-clang'.
[Parameter(Mandatory)]
[ValidateSet('clang', 'armclang', 'tiarmclang')]
[string]
$Configuration,
# For a suite, the suites we support. Valid values are 'CERT-C++' and
# 'AUTOSAR' and MISRA-C-2012 and CERT-C
[Parameter(Mandatory, ParameterSetName = 'Suite')]
[ValidateSet("CERT-C++", "AUTOSAR", "MISRA-C-2012", "CERT-C")]
[string]
$SuiteName,
# The rule to test, e.g.: A0-1-1.
[Parameter(Mandatory, ParameterSetName = "Rule")]
[string]
$RuleName,
# The package to test. This will test all rules within the specified
# package. Valid values are taken from the basename of the `.json` files within the
# `rule_packages` directory.
[Parameter(Mandatory, ParameterSetName = "Package")]
[ValidateSet("Allocations",
"BannedSyntax",
"BannedTypes",
"BannedFunctions",
"Classes",
"Concurrency",
"Const",
"Declarations",
"Exceptions1",
"Exceptions2",
"Includes",
"Invariants",
"Iterators",
"Literals",
"Loops",
"Macros",
"Naming",
"Scope",
"Side-effects1",
"Side-effects2",
"Classes",
"SmartPointers1",
"SmartPointers2",
"SideEffects1",
"SideEffects2",
"Strings",
"Templates",
"Classes",
"Freed",
"Initialization",
"Functions",
"Null",
"OperatorInvariants",
"VirtualFunctions",
"Conditionals",
"MoveForward",
"Operators",
"TypeRanges",
"Lambdas",
"Pointers",
"IntegerConversion",
"Expressions")]
[string]
$PackageName
)
Import-Module -Name "$PSScriptRoot\..\PSCodingStandards\CodingStandards"
. "$PSScriptRoot\CreateSummaryReport.ps1"
. "$PSScriptRoot\Get-CompilerExecutable.ps1"
. "$PSScriptRoot\Config.ps1"
$REPORT = @()
$queriesToCheck = @()
#
# Step 1: Load the queries we are going to check
#
$allQueries = @()
$queriesToCheck = @()
# load all the queries
foreach ($s in $AVAILABLE_SUITES) {
$allQueries += Get-RulesInSuite -Suite $s -Language $Language
}
# filter the set down based on the selections
if ($Suite) {
$queriesToCheck = $allQueries | Where-Object { $_.__memberof_suite -eq $SuiteName }
}
elseif ($Package) {
$queriesToCheck = $allQueries | Where-Object { $_.__memberof_package -eq $PackageName }
}
elseif ($Rule) {
$queriesToCheck = $allQueries | Where-Object { $_.__memberof_rule -eq $RuleName }
}
elseif ($AllRules) {
$queriesToCheck = $allQueries
}
if ($queriesToCheck.Count -eq 0) {
throw "No queries loaded."
}
else {
Write-Host "Loaded $($queriesToCheck.Count) Queries."
}
#
# Step 2: Verify All the Required CLI Tools are Installed
#
Write-Host "Checking 'codeql' program...." -NoNewline
Test-ProgramInstalled -Program "codeql"
Write-Host -ForegroundColor ([ConsoleColor]2) "OK"
$CODEQL_VERSION = (codeql version --format json | ConvertFrom-Json).version
Write-Host "Checking 'codeql' version = $REQUIRED_CODEQL_VERSION...." -NoNewline
if (-Not $CODEQL_VERSION -eq $REQUIRED_CODEQL_VERSION) {
throw "Invalid CodeQL version $CODEQL_VERSION. Please install $REQUIRED_CODEQL_VERSION."
}
Write-Host -ForegroundColor ([ConsoleColor]2) "OK"
Write-Host "Checking '$(Get-CompilerExecutable -Configuration $Configuration -Language $Language)' program...." -NoNewline
Test-ProgramInstalled -Program (Get-CompilerExecutable -Configuration $Configuration -Language $Language)
Write-Host -ForegroundColor ([ConsoleColor]2) "OK"
#
# Step 3: For each rule to test, compile a test database and run the query
# against it. Compare the results to the contents of the `.expected` file within
# the directory for the rule's test.
#
$jobRows = $queriesToCheck | ForEach-Object -ThrottleLimit $NumThreads -Parallel {
Import-Module -Name "$using:PSScriptRoot\..\PSCodingStandards\CodingStandards"
#. "$using:PSScriptRoot\GetTestDirectory.ps1"
. "$using:PSScriptRoot\NewDatabaseForRule.ps1"
. "$using:PSScriptRoot\ExecuteQueryAndDecodeAsJson.ps1"
$q = $_
$CurrentSuiteName = $q.__memberof_suite
$CurrentRuleName = $q.__memberof_rule
$CurrentQueryName = $q.short_name
$CurrentPackageName = $q.__memberof_package
# for the report
$row = @{
"SUITE" = $CurrentSuiteName;
"PACKAGE" = $CurrentPackageName;
"RULE" = $CurrentRuleName;
"QUERY" = $CurrentQueryName;
"COMPILE_PASS" = $false;
"COMPILE_ERROR_OUTPUT" = "";
"TEST_PASS" = $false ;
"TEST_DIFFERENCE" = "";
}
Write-Host "Resolving pack 'codeql/cpp-queries'...." -NoNewline
$CODEQL_CPP_QUERIES_PATH = (codeql resolve qlpacks --format json | ConvertFrom-Json)."codeql/cpp-queries"
if ( -Not (Test-Path -Path $CODEQL_CPP_QUERIES_PATH -PathType Container) ) {
Write-Host "Could not resolve pack 'codeql/cpp-queries'. Please install the pack 'codeql/cpp-queries'."
return $row
}
Write-Host -ForegroundColor ([ConsoleColor]2) "OK"
Write-Host "====================[Rule=$CurrentRuleName,Suite=$CurrentSuiteName/Query=$CurrentQueryName]===================="
$testDirectory = (Get-TestDirectory -RuleObject $q -Language $using:Language)
Write-Host "Compiling database in $testDirectory..." -NoNewline
try {
$db = New-Database-For-Rule -RuleName $CurrentRuleName -RuleTestDir $testDirectory -Configuration $using:Configuration -Language $using:Language
Write-Host -ForegroundColor ([ConsoleColor]2) "OK"
}
catch {
Write-Host -ForegroundColor ([ConsoleColor]4) "FAILED"
$row["COMPILE_ERROR_OUTPUT"] = $_
return $row # although it is unlikely to succeed with the next rule skipping to the next rule
# ensures all of the rules will be reported in the
# output.
}
$row["COMPILE_PASS"] = $true
Write-Host "Checking expected output..."
# Dragons below 🐉🐉🐉
#
# Note this technique uses so-called "wizard" settings to make it possible
# to compare hand compiled databases using qltest. The relative paths and
# other options are required to be set as below (especially the detail about
# the relative path of the dataset and the test).
# the "dataset" should be the `db-cpp` directory inside the database
# directory. HOWEVER. It should be the path relative to the test directory.
$rulePath = Resolve-Path $testDirectory
$dbPath = Resolve-Path $db
Write-Host "Resolving database $dbPath relative to test directory $rulePath"
$dataset = Resolve-Path (Join-Path $dbPath "db-cpp")
Push-Location $rulePath
$datasetRelPath = Resolve-Path -Relative $dataset
Pop-Location
Write-Host "Using relative path: $datasetRelPath"
# Actually do the qltest run.
# codeql test run <qltest file> --dataset "relpath"
if ($q.shared_implementation_short_name) {
$qlRefFile = Join-Path $rulePath "$($q.shared_implementation_short_name).ql"
}
else {
$qlRefFile = Join-Path $rulePath "$CurrentQueryName.qlref"
}
Write-Host "codeql test run $qlRefFile --dataset=`"$datasetRelPath`""
$stdOut = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid())
$stdErr = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid())
Write-Host "Standard Out Buffered to: $stdOut"
Write-Host "Standard Error Buffered to: $stdErr"
$procDetails = Start-Process -FilePath "codeql" -PassThru -NoNewWindow -Wait -ArgumentList "test run $qlRefFile --dataset=`"$datasetRelPath`"" -RedirectStandardOutput $stdOut -RedirectStandardError $stdErr
if (-Not $procDetails.ExitCode -eq 0) {
Write-Host -ForegroundColor ([ConsoleColor]4) "FAILED"
Get-Content $stdOut | Out-String | Write-Host
$row["TEST_DIFFERENCE"] = Get-Content $stdOut | Out-String
}
else {
$row["TEST_PASS"] = $true
Write-Host -ForegroundColor ([ConsoleColor]2) "OK"
}
return $row
}
# combine the outputs
$jobRows | ForEach-Object {
$REPORT += $_
}
#
# Step 4: Create reports
#
$fileTag = "$($Configuration.ToLower())-$Language-$(Get-Date -Format "yyyy-MM-dd_HH-mm-ss")"
$reportOutputFile = Join-Path $ReportDir "MatrixTestReport-$fileTag.csv"
$summaryReportOutputFile = Join-Path $ReportDir "MatrixTestReportSummary-$fileTag.csv"
# Write out the detailed report
Write-Host "Writing detailed report to $reportOutputFile"
foreach ($r in $REPORT) {
[PSCustomObject]$r | Export-CSV -Path $reportOutputFile -Append -NoTypeInformation
}
if (-not $SkipSummaryReport){
# write out a summary
Write-Host "Writing summary report to $summaryReportOutputFile"
Create-Summary-Report -DataFile $reportOutputFile -OutputFile $summaryReportOutputFile
}