diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml index cc44006f1..6f0e7f6ff 100644 --- a/.github/workflows/code-analysis.yml +++ b/.github/workflows/code-analysis.yml @@ -22,7 +22,7 @@ jobs: - name: Build Module shell: powershell run: | - # Module is built so that examples can be scanned. + Write-Information -MessageData 'Module is being built so that examples can be scanned.' -InformationAction 'Continue' .\build.ps1 -ResolveDependency -Tasks 'build' - name: Run PSScriptAnalyzer shell: powershell @@ -46,19 +46,19 @@ jobs: $pssaError = $filesToScan | Invoke-ScriptAnalyzer -Settings './.vscode/analyzersettings.psd1' - Write-Information -MessageData 'Converting PSScriptAnalyzer result to SARIF.' -InformationAction 'Continue' - - <# - Filter out rules. + $parseErrorTypes = @( + 'TypeNotFound' + 'RequiresModuleInvalid' + ) + Write-Information -MessageData ('Filter out reported parse errors that is unable to be resolved in source files: {0}' -f ($parseErrorTypes -join ', ')) -InformationAction 'Continue' + $pssaError = $pssaError | + Where-Object -FilterScript { + $_.RuleName -notin $parseErrorTypes + } - TODO: The rules (e.g. "TypeNotFound") are not excluded correctly even if it - is excluded in the file 'analyzersettings.psd1'. This is a workaround - until it is properly excluded for source files, and instead only is - run for the built module script module file (SqlServerDsc.psm1). - #> + Write-Information -MessageData 'Converting PSScriptAnalyzer result to SARIF.' -InformationAction 'Continue' $pssaError | - Where-Object -FilterScript { $_.RuleName -notin @('TypeNotFound', 'RequiresModuleInvalid') } | - ConvertTo-SARIF -FilePath 'results.sarif' + ConvertTo-SARIF -FilePath 'results.sarif' Write-Information -MessageData 'Analyzing done.' -InformationAction 'Continue' - name: Upload SARIF results diff --git a/.vscode/analyzersettings.psd1 b/.vscode/analyzersettings.psd1 index 30932a84d..192f3b679 100644 --- a/.vscode/analyzersettings.psd1 +++ b/.vscode/analyzersettings.psd1 @@ -72,13 +72,14 @@ ) <# - TODO: This is not excluded correctly, see test QA/ScriptAnalyzer.Tests.ps1 - or the GitHub Action workflow Code Analysis for more information. + The following types are not rules but parse errors reported by PSScriptAnalyzer + so they cannot be ecluded. They need to be filtered out from the result of + Invoke-ScriptAnalyzer. + + TypeNotFound - Because classes in the project cannot be found unless built. + RequiresModuleInvalid - Because 'using module' in prefix.ps1 cannot be resolved as source file. #> - ExcludeRules = @( - 'TypeNotFound' # Because classes in the project cannot be found unless built. - 'RequiresModuleInvalid' # Because 'using module' in prefix.ps1 cannot be resolved as source file. - ) + ExcludeRules = @() Rules = @{ PSUseConsistentWhitespace = @{ diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 index a982ee251..0590e6eb4 100644 --- a/tests/QA/ScriptAnalyzer.Tests.ps1 +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -72,17 +72,20 @@ Describe 'Script Analyzer Rules' { It 'Should pass all PS Script Analyzer rules for file ''''' -ForEach $testCases { $pssaError = Invoke-ScriptAnalyzer -Path $ScriptPath -Settings $scriptAnalyzerSettingsPath - <# - Filter out rules. + $parseErrorTypes = @( + 'TypeNotFound' + 'RequiresModuleInvalid' + ) - TODO: The rules (e.g. "TypeNotFound") are not excluded correctly even if it - is excluded in the file 'analyzersettings.psd1'. This is a workaround - until it is properly excluded for source files, and instead only is - run for the built module script module file (SqlServerDsc.psm1). - #> - $pssaError = $pssaError | Where-Object -FilterScript { $_.RuleName -notin @('TypeNotFound', 'RequiresModuleInvalid') } + # Filter out reported parse errors that is unable to be resolved in source files + $pssaError = $pssaError | + Where-Object -FilterScript { + $_.RuleName -notin $parseErrorTypes + } + + $report = $pssaError | + Format-Table -AutoSize | Out-String -Width 200 - $report = $pssaError | Format-Table -AutoSize | Out-String -Width 200 $pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n $report`r`n" } }