|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +param( |
| 3 | + [ValidateSet('c', 'cpp')] |
| 4 | + [string] |
| 5 | + $Language = 'c', |
| 6 | + [Parameter(Mandatory = $false)] |
| 7 | + [string] |
| 8 | + $ReportDir = (Get-Location), |
| 9 | + [switch] |
| 10 | + $CIMode |
| 11 | +) |
| 12 | + |
| 13 | +Import-Module -Force -Name "$PSScriptRoot/../PSCodingStandards/CodingStandards" |
| 14 | + |
| 15 | +$allQueries = @() |
| 16 | +$queriesToCheck = @() |
| 17 | + |
| 18 | +# load all the queries |
| 19 | +foreach ($s in $AVAILABLE_SUITES) { |
| 20 | + $allQueries += Get-RulesInSuite -Suite $s -Language $Language |
| 21 | +} |
| 22 | + |
| 23 | +foreach ($q in $allQueries){ |
| 24 | + if($q | Get-Member "shared_implementation_short_name"){ |
| 25 | + $queriesToCheck += $q |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +if ($queriesToCheck.Count -eq 0) { |
| 30 | + throw "No queries loaded." |
| 31 | +} |
| 32 | +else { |
| 33 | + Write-Host "Loaded $($queriesToCheck.Count) queries with shared implementations." |
| 34 | +} |
| 35 | + |
| 36 | +# What we want to verify is that IF a shared implementation is used, then we |
| 37 | +# have a valid test case WITHIN the language that is using it. |
| 38 | + |
| 39 | +$REPORT = @() |
| 40 | + |
| 41 | +foreach($q in $queriesToCheck){ |
| 42 | + # Get test directory |
| 43 | + $testDirectory = Get-TestDirectory -RuleObject $q -Language $Language |
| 44 | + Write-Host "Verifying $Language language tests in $testDirectory..." |
| 45 | + |
| 46 | + |
| 47 | + $row = @{ |
| 48 | + "SUITE" = $q.__memberof_suite; |
| 49 | + "PACKAGE" = $q.__memberof_package; |
| 50 | + "RULE" = $q.__memberof_rule; |
| 51 | + "QUERY" = $q.short_name; |
| 52 | + "SHARED_NAME" = $q.shared_implementation_short_name; |
| 53 | + "TEST_DIR_EXISTS" = $false; |
| 54 | + "SOURCE_CODE_EXISTS" = $false; |
| 55 | + "EXPECTED_EXISTS" = $false; |
| 56 | + "REFERENCE_EXISTS" = $false; |
| 57 | + } |
| 58 | + |
| 59 | + # require a .c for language cpp |
| 60 | + # require a .expected |
| 61 | + # require a .ql |
| 62 | + |
| 63 | + if(-not (Test-Path $testDirectory)){ |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + $dirName = (Get-Item $testDirectory).Basename |
| 68 | + $dirNameLower = $dirName.ToLower() |
| 69 | + |
| 70 | + $row["TEST_DIR_EXISTS"] = $true |
| 71 | + |
| 72 | + if((Test-Path (Join-Path $testDirectory "test.$Language"))){ |
| 73 | + $row["SOURCE_CODE_EXISTS"] = $true |
| 74 | + } |
| 75 | + |
| 76 | + if((Test-Path (Join-Path $testDirectory "$dirNameLower.expected"))){ |
| 77 | + $row["EXPECTED_EXISTS"] = $true |
| 78 | + } |
| 79 | + |
| 80 | + if((Test-Path (Join-Path $testDirectory "$dirNameLower.ql"))){ |
| 81 | + $row["REFERENCE_EXISTS"] = $true |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + $REPORT += $row |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +# output a CSV containing the elements that do not contain |
| 91 | +$fileTag = "$Language-$(Get-Date -Format "yyyy-MM-dd_HH-mm-ss")" |
| 92 | +$reportOutputFile = Join-Path $ReportDir "TestReport-$fileTag.csv" |
| 93 | +$missingReportOutputFile = Join-Path $ReportDir "MissingTestReport-$fileTag.csv" |
| 94 | + |
| 95 | +$failCount = 0 |
| 96 | +foreach ($r in $REPORT) { |
| 97 | + if(($r["TEST_DIR_EXISTS"] -eq $false) -or ($r["SOURCE_CODE_EXISTS"] -eq $false) -or ($r["EXPECTED_EXISTS"] -eq $false) -or ($r["REFERENCE_EXISTS"] -eq $false)){ |
| 98 | + $failCount += 1 |
| 99 | + [PSCustomObject]$r | Export-CSV -Path $missingReportOutputFile -Append -NoTypeInformation |
| 100 | + |
| 101 | + } |
| 102 | + [PSCustomObject]$r | Export-CSV -Path $reportOutputFile -Append -NoTypeInformation |
| 103 | +} |
| 104 | + |
| 105 | +Write-Host "Write report to $reportOutputFile" |
| 106 | + |
| 107 | +if(($CIMode) -and ($failCount -gt 0)){ |
| 108 | + throw "Found $failCount/$($queriesToCheck.Count) invalid shared test uses" |
| 109 | +}else{ |
| 110 | + Write-Host "Found $failCount/$($queriesToCheck.Count) invalid shared test uses" |
| 111 | +} |
0 commit comments