Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions PowerShellBuild/Public/Test-PSBuildScriptAnalysis.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function Test-PSBuildScriptAnalysis {
Write-Verbose ($LocalizedData.SeverityThresholdSetTo -f $SeverityThreshold)

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

if ($analysisResult) {
Write-Host $LocalizedData.PSScriptAnalyzerResults -ForegroundColor Yellow
Expand Down
57 changes: 57 additions & 0 deletions tests/Test-PSBuildScriptAnalysis.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Describe 'Test-PSBuildScriptAnalysis' {
BeforeAll {
Set-StrictMode -Version Latest

. (Join-Path -Path $PSScriptRoot -ChildPath '../PowerShellBuild/Public/Test-PSBuildScriptAnalysis.ps1')

$script:LocalizedData = @{
SeverityThresholdSetTo = 'Severity threshold set to {0}'
PSScriptAnalyzerResults = 'PSScriptAnalyzer results'
ScriptAnalyzerErrors = 'ScriptAnalyzer errors found'
ScriptAnalyzerWarnings = 'ScriptAnalyzer warnings found'
ScriptAnalyzerIssues = 'ScriptAnalyzer issues found'
}
}

It 'calls Invoke-ScriptAnalyzer with the provided settings path' {
Mock -CommandName Invoke-ScriptAnalyzer -MockWith {
@()
}

Test-PSBuildScriptAnalysis -Path 'function Test-Me { "ok" }' -SeverityThreshold Error -SettingsPath 'tests/ScriptAnalyzerSettings.psd1'

Should -Invoke -CommandName Invoke-ScriptAnalyzer -Times 1 -Exactly -ParameterFilter {
$Path -eq 'function Test-Me { "ok" }' -and
$Settings -eq 'tests/ScriptAnalyzerSettings.psd1' -and
$Recurse
Comment on lines +21 to +26
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests pass a script source string into -Path, but Test-PSBuildScriptAnalysis (and Invoke-ScriptAnalyzer -Path) are documented/implemented to take a filesystem path (module dir/script file). This makes the tests less representative and could mislead future readers into thinking inline script strings are supported. Consider writing the inline content to a temp file under TestDrive: (or creating a temp dir) and passing that path instead; similarly, construct -SettingsPath via Join-Path $PSScriptRoot ... to avoid depending on the current working directory.

Copilot uses AI. Check for mistakes.
}
}

It 'passes when no results are returned at Error threshold' {
Mock -CommandName Invoke-ScriptAnalyzer -MockWith {
@()
}

{
Test-PSBuildScriptAnalysis -Path 'function Test-Me { "ok" }' -SeverityThreshold Error
} | Should -Not -Throw
}

It 'fails when an error is returned at Error threshold' {
Mock -CommandName Invoke-ScriptAnalyzer -MockWith {
@(
[pscustomobject]@{
Severity = 'Error'
RuleName = 'TestRule'
ScriptName = 'inline.ps1'
Message = 'Boom'
Line = 1
}
)
}

{
Test-PSBuildScriptAnalysis -Path 'function Test-Me { "ok" }' -SeverityThreshold Error
} | Should -Throw
}
}