-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPSSAResource.common.v4.Tests.ps1
152 lines (123 loc) · 6.87 KB
/
PSSAResource.common.v4.Tests.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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-ParameterBlockParameterAttribute', '', Scope='Function', Target='*')]
param
(
$ModuleName,
$ModuleBase,
$ModuleManifest,
$ProjectPath,
$SourcePath,
$SourceManifest,
$Tag,
$ExcludeTag,
$ExcludeSourceFile,
[Parameter(ValueFromRemainingArguments = $true)]
$Args
)
$isPester5 = (Get-Module -Name Pester).Version -lt '5.0.0'
# Only run if _not_ Pester 5.
if (-not $isPester5)
{
return
}
Describe 'Common Tests - PS Script Analyzer on Resource Files' -Tag @('DscPSSA','Common Tests - PS Script Analyzer on Resource Files') {
if ($PSVersionTable.PSVersion.Major -lt 5)
{
Write-Warning -Message 'PS Script Analyzer can not run on this platform. Please run tests on a machine with WMF 5.0+.'
return
}
<#
PSSA = PS Script Analyzer
The following PSSA tests will always fail if any violations are found:
- Common Tests - Error-Level Script Analyzer Rules
- Common Tests - Custom Script Analyzer Rules
The following PSSA tests will only fail if a violation is found and
a matching option is found in the opt-in file.
- Common Tests - Required Script Analyzer Rules
- Common Tests - Flagged Script Analyzer Rules
- Common Tests - New Error-Level Script Analyzer Rules
- Common Tests - Custom Script Analyzer Rules
#>
$requiredPSSA = @(
'Common Tests - Required Script Analyzer Rules',
'RequiredPSSA'
)
$flaggedPSSA = @(
'Common Tests - Flagged Script Analyzer Rules',
'FlaggedPSSA'
)
$newErrorPSSA = @(
'Common Tests - New Error-Level Script Analyzer Rules',
'NewErrorPSSA'
)
$customPSSA = @(
'Common Tests - Custom Script Analyzer Rules',
'CustomPSSA',
'DscResource.AnalyzerRules'
)
$testTestShouldBeSkippedParams = @{
Tag = $Tag
ExcludeTag = $ExcludeTag
}
$ShouldSkipRequiredPSSA = Test-TestShouldBeSkipped @testTestShouldBeSkippedParams -TestNames $requiredPSSA
$ShouldSkipFlaggedPSSA = Test-TestShouldBeSkipped @testTestShouldBeSkippedParams -TestNames $flaggedPSSA
$ShouldSkipCustomPSSA = Test-TestShouldBeSkipped @testTestShouldBeSkippedParams -TestNames $customPSSA
$ShouldSkipNewErrorPSSA = Test-TestShouldBeSkipped @testTestShouldBeSkippedParams -TestNames $newErrorPSSA
$PSSA_rule_config = Get-StructuredObjectFromFile -Path (Join-Path -Path (Get-CurrentModuleBase) -ChildPath 'Config/PSSA_rules_config.json')
$dscResourceAnalyzerRulesModule = Import-Module DscResource.AnalyzerRules -PassThru -ErrorAction 'Stop'
$dscResourcesPsm1Files = @(Get-ChildItem -Path $SourcePath -Include *.psm1 -Recurse | WhereSourceFileNotExcluded)
foreach ($dscResourcesPsm1File in $dscResourcesPsm1Files)
{
$invokeScriptAnalyzerParameters = @{
Path = $dscResourcesPsm1File.FullName
CustomRulePath = (Join-Path -Path $dscResourceAnalyzerRulesModule.ModuleBase -ChildPath $dscResourceAnalyzerRulesModule.RootModule)
IncludeRule = @($PSSA_rule_config.required_rules + $PSSA_rule_config.flagged_rules + $PSSA_rule_config.ignore_rules + 'Measure-*')
ErrorVariable = 'MyErrors'
}
$PSSAErrors = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters
$errorPssaRulesOutput = $PSSAErrors.Where{$_.Severity -eq 'Error'}
$requiredPssaRulesOutput = $PSSAErrors.Where{$_.RuleName -in $PSSA_rule_config.required_rules }
$flaggedPssaRulesOutput = $PSSAErrors.Where{$_.RuleName -in $PSSA_rule_config.flagged_rules}
$DSCCustomRulesOutput = $PSSAErrors.Where{$_.RuleName -like "DscResource.AnalyzerRules*"}
$ignoredPssaRulesOutput = $PSSAErrors.Where{$_.RuleName -in $PSSA_rule_config.ignore_rules}
$NewErrorRulesOutput = @($ignoredPssaRulesOutput + $flaggedPssaRulesOutput + $requiredPssaRulesOutput)
Context $dscResourcesPsm1File.Name {
It 'Should not suppress any required PS Script Analyzer rules' {
$requiredRuleIsSuppressed = $false
$suppressedRuleNames = Get-SuppressedPSSARuleNameList -FilePath $dscResourcesPsm1File.FullName
foreach ($suppressedRuleName in $suppressedRuleNames)
{
$suppressedRuleNameNoQuotes = $suppressedRuleName.Replace("'", '')
if ($PSSA_rule_config.required_rules -icontains $suppressedRuleNameNoQuotes)
{
Write-Warning -Message "The file $($dscResourcesPsm1File.Name) contains a suppression of the required PS Script Analyser rule $suppressedRuleNameNoQuotes. Please remove the rule suppression."
$requiredRuleIsSuppressed = $true
}
}
$requiredRuleIsSuppressed | Should -BeFalse
}
It 'Should pass all error-level PS Script Analyzer rules' {
$report = $errorPssaRulesOutput | Format-Table -AutoSize | Out-String -Width 110
$errorPssaRulesOutput | Should -HaveCount 0 -Because "Error-level Rule(s) triggered.`r`n`r`n $report`r`n"
}
It 'Should pass all required PS Script Analyzer rules' -Skip:($requiredPssaRulesOutput.count -gt 0 -and $ShouldSkipRequiredPSSA) {
$report = $requiredPssaRulesOutput | Format-Table -AutoSize | Out-String -Width 110
$requiredPssaRulesOutput | Should -HaveCount 0 -Because "Required Rule(s) triggered.`r`n`r`n $report`r`n"
}
It 'Should pass all flagged PS Script Analyzer rules' -Skip:($flaggedPssaRulesOutput.count -gt 0 -and $ShouldSkipFlaggedPSSA) {
$report = $flaggedPssaRulesOutput | Format-Table -AutoSize | Out-String -Width 110
$flaggedPssaRulesOutput | Should -HaveCount 0 -Because "Flagged Rule(s) triggered.`r`n`r`n $report`r`n"
}
It 'Should pass any recently-added, error-level PS Script Analyzer rules' -skip:($NewErrorRulesOutput.count -gt 0 -and $ShouldSkipNewErrorPSSA) {
$report = $NewErrorRulesOutput | Format-Table -AutoSize | Out-String -Width 110
$NewErrorRulesOutput | Should -HaveCount 0 -Because "New Rules flagged `r`n`r`n $report `r`n"
}
It 'Should pass all custom DSC Resource Kit PSSA rules' -Skip:($DSCCustomRulesOutput.count -gt 0 -and $ShouldSkipCustomPSSA) {
$report = $DSCCustomRulesOutput | Select-Object @{
Name = 'RuleName'
Expression = {$_.RuleName -replace 'DscResource.AnalyzerRules\\'}
},Severity,ScriptName,Line,Message | Format-Table -AutoSize -Wrap | Out-String -Width 110
$DSCCustomRulesOutput | Should -HaveCount 0 -Because "Error-level Rule(s) triggered.`r`n`r`n $report`r`n"
}
}
}
}