Skip to content

Commit 5dd3aea

Browse files
author
Nathanael BOT
committed
Add Pester coverage for Test-PSBuildScriptAnalysis
1 parent 5e25263 commit 5dd3aea

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

PowerShellBuild/Public/Test-PSBuildScriptAnalysis.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ function Test-PSBuildScriptAnalysis {
2929
Write-Verbose ($LocalizedData.SeverityThresholdSetTo -f $SeverityThreshold)
3030

3131
$analysisResult = Invoke-ScriptAnalyzer -Path $Path -Settings $SettingsPath -Recurse -Verbose:$VerbosePreference
32-
$errors = ($analysisResult.where({ $_Severity -eq 'Error' })).Count
33-
$warnings = ($analysisResult.where({ $_Severity -eq 'Warning' })).Count
34-
$infos = ($analysisResult.where({ $_Severity -eq 'Information' })).Count
32+
$errors = ($analysisResult.where({ $_.Severity -eq 'Error' })).Count
33+
$warnings = ($analysisResult.where({ $_.Severity -eq 'Warning' })).Count
34+
$infos = ($analysisResult.where({ $_.Severity -eq 'Information' })).Count
3535

3636
if ($analysisResult) {
3737
Write-Host $LocalizedData.PSScriptAnalyzerResults -ForegroundColor Yellow
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Describe 'Test-PSBuildScriptAnalysis' {
2+
BeforeAll {
3+
Set-StrictMode -Version Latest
4+
5+
. (Join-Path -Path $PSScriptRoot -ChildPath '../PowerShellBuild/Public/Test-PSBuildScriptAnalysis.ps1')
6+
7+
$script:LocalizedData = @{
8+
SeverityThresholdSetTo = 'Severity threshold set to {0}'
9+
PSScriptAnalyzerResults = 'PSScriptAnalyzer results'
10+
ScriptAnalyzerErrors = 'ScriptAnalyzer errors found'
11+
ScriptAnalyzerWarnings = 'ScriptAnalyzer warnings found'
12+
ScriptAnalyzerIssues = 'ScriptAnalyzer issues found'
13+
}
14+
}
15+
16+
It 'calls Invoke-ScriptAnalyzer with the provided settings path' {
17+
Mock -CommandName Invoke-ScriptAnalyzer -MockWith {
18+
@()
19+
}
20+
21+
Test-PSBuildScriptAnalysis -Path 'function Test-Me { "ok" }' -SeverityThreshold Error -SettingsPath 'tests/ScriptAnalyzerSettings.psd1'
22+
23+
Should -Invoke -CommandName Invoke-ScriptAnalyzer -Times 1 -Exactly -ParameterFilter {
24+
$Path -eq 'function Test-Me { "ok" }' -and
25+
$Settings -eq 'tests/ScriptAnalyzerSettings.psd1' -and
26+
$Recurse
27+
}
28+
}
29+
30+
It 'passes when no results are returned at Error threshold' {
31+
Mock -CommandName Invoke-ScriptAnalyzer -MockWith {
32+
@()
33+
}
34+
35+
{
36+
Test-PSBuildScriptAnalysis -Path 'function Test-Me { "ok" }' -SeverityThreshold Error
37+
} | Should -Not -Throw
38+
}
39+
40+
It 'fails when an error is returned at Error threshold' {
41+
Mock -CommandName Invoke-ScriptAnalyzer -MockWith {
42+
@(
43+
[pscustomobject]@{
44+
Severity = 'Error'
45+
RuleName = 'TestRule'
46+
ScriptName = 'inline.ps1'
47+
Message = 'Boom'
48+
Line = 1
49+
}
50+
)
51+
}
52+
53+
{
54+
Test-PSBuildScriptAnalysis -Path 'function Test-Me { "ok" }' -SeverityThreshold Error
55+
} | Should -Throw
56+
}
57+
}

0 commit comments

Comments
 (0)