Skip to content

Fix incorrect -ReportSummary Pester test grouping #2057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
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
96 changes: 48 additions & 48 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ Describe "Test CustomizedRulePath" {
BeforeAll {
$measureRequired = "CommunityAnalyzerRules\Measure-RequiresModules"
}

Context "When used correctly" {
It "with the module folder path" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules | Where-Object { $_.RuleName -eq $measureRequired }
Expand Down Expand Up @@ -516,7 +517,6 @@ Describe "Test CustomizedRulePath" {
}

Describe "Test -Fix Switch" {

BeforeAll {
$scriptName = "TestScriptWithFixableWarnings.ps1"
$testSource = Join-Path $PSScriptRoot $scriptName
Expand Down Expand Up @@ -585,65 +585,65 @@ Describe "Test -EnableExit Switch" {

$LASTEXITCODE | Should -Be 2
}
}

Describe "-ReportSummary switch" {
BeforeAll {
$pssaPath = (Get-Module PSScriptAnalyzer).Path

if ($IsCoreCLR)
{
$pwshExe = (Get-Process -Id $PID).Path
}
else
{
$pwshExe = 'powershell'
}
Describe "-ReportSummary switch" {
BeforeAll {
$pssaPath = (Get-Module PSScriptAnalyzer).Path

$reportSummaryFor1Warning = '*1 rule violation found. Severity distribution: Error = 0, Warning = 1, Information = 0*'
if ($IsCoreCLR)
{
$pwshExe = (Get-Process -Id $PID).Path
}
else
{
$pwshExe = 'powershell'
}

It "prints the correct report summary using the -NoReportSummary switch" {
$result = & $pwshExe -Command "Import-Module '$pssaPath'; Invoke-ScriptAnalyzer -ScriptDefinition gci -ReportSummary"
$reportSummaryFor1Warning = '*1 rule violation found. Severity distribution: Error = 0, Warning = 1, Information = 0*'
}

"$result" | Should -BeLike $reportSummaryFor1Warning
}
It "does not print the report summary when not using -NoReportSummary switch" {
$result = & $pwshExe -Command "Import-Module '$pssaPath'; Invoke-ScriptAnalyzer -ScriptDefinition gci"
It "prints the correct report summary using the -NoReportSummary switch" {
$result = & $pwshExe -Command "Import-Module '$pssaPath'; Invoke-ScriptAnalyzer -ScriptDefinition gci -ReportSummary"

"$result" | Should -Not -BeLike $reportSummaryFor1Warning
}
"$result" | Should -BeLike $reportSummaryFor1Warning
}
It "does not print the report summary when not using -NoReportSummary switch" {
$result = & $pwshExe -Command "Import-Module '$pssaPath'; Invoke-ScriptAnalyzer -ScriptDefinition gci"

# using statements are only supported in v5+
Describe "Handles parse errors due to unknown types" -Skip:($testingLibraryUsage -or ($PSVersionTable.PSVersion -lt '5.0')) {
BeforeAll {
$script = @'
using namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
Import-Module "AzureRm"
class MyClass { [IStorageContext]$StorageContext } # This will result in a parser error due to [IStorageContext] type that comes from the using statement but is not known at parse time
"$result" | Should -Not -BeLike $reportSummaryFor1Warning
}
}

# using statements are only supported in v5+
Describe "Handles parse errors due to unknown types" -Skip:($testingLibraryUsage -or ($PSVersionTable.PSVersion -lt '5.0')) {
BeforeAll {
$script = @'
using namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
Import-Module "AzureRm"
class MyClass { [IStorageContext]$StorageContext } # This will result in a parser error due to [IStorageContext] type that comes from the using statement but is not known at parse time
'@
}
It "does not throw and detect one expected warning after the parse error has occured when using -ScriptDefintion parameter set" {
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition $script
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'TypeNotFound'
}
}
It "does not throw and detect one expected warning after the parse error has occured when using -ScriptDefintion parameter set" {
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition $script
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'TypeNotFound'
}

It "does not throw and detect one expected warning after the parse error has occured when using -Path parameter set" {
$testFilePath = "TestDrive:\testfile.ps1"
Set-Content $testFilePath -Value $script
$warnings = Invoke-ScriptAnalyzer -Path $testFilePath
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'TypeNotFound'
}
It "does not throw and detect one expected warning after the parse error has occured when using -Path parameter set" {
$testFilePath = "TestDrive:\testfile.ps1"
Set-Content $testFilePath -Value $script
$warnings = Invoke-ScriptAnalyzer -Path $testFilePath
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'TypeNotFound'
}
}

Describe 'Handles static Singleton (issue 1182)' -Skip:($testingLibraryUsage -or ($PSVersionTable.PSVersion -lt '5.0')) {
It 'Does not throw or return diagnostic record' {
$scriptDefinition = 'class T { static [T]$i }; function foo { [CmdletBinding()] param () $script:T.WriteLog() }'
Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -ErrorAction Stop | Should -BeNullOrEmpty
}
Describe 'Handles static Singleton (issue 1182)' -Skip:($testingLibraryUsage -or ($PSVersionTable.PSVersion -lt '5.0')) {
It 'Does not throw or return diagnostic record' {
$scriptDefinition = 'class T { static [T]$i }; function foo { [CmdletBinding()] param () $script:T.WriteLog() }'
Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -ErrorAction Stop | Should -BeNullOrEmpty
}
}

Expand Down
Loading