Skip to content

Commit 03beb17

Browse files
authoredFeb 17, 2025
Fix incorrect -ReportSummary Pester test grouping (#2057)
1 parent 0e46667 commit 03beb17

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed
 

‎Tests/Engine/InvokeScriptAnalyzer.tests.ps1

+48-48
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ Describe "Test CustomizedRulePath" {
372372
BeforeAll {
373373
$measureRequired = "CommunityAnalyzerRules\Measure-RequiresModules"
374374
}
375+
375376
Context "When used correctly" {
376377
It "with the module folder path" {
377378
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules | Where-Object { $_.RuleName -eq $measureRequired }
@@ -516,7 +517,6 @@ Describe "Test CustomizedRulePath" {
516517
}
517518

518519
Describe "Test -Fix Switch" {
519-
520520
BeforeAll {
521521
$scriptName = "TestScriptWithFixableWarnings.ps1"
522522
$testSource = Join-Path $PSScriptRoot $scriptName
@@ -585,65 +585,65 @@ Describe "Test -EnableExit Switch" {
585585

586586
$LASTEXITCODE | Should -Be 2
587587
}
588+
}
588589

589-
Describe "-ReportSummary switch" {
590-
BeforeAll {
591-
$pssaPath = (Get-Module PSScriptAnalyzer).Path
592-
593-
if ($IsCoreCLR)
594-
{
595-
$pwshExe = (Get-Process -Id $PID).Path
596-
}
597-
else
598-
{
599-
$pwshExe = 'powershell'
600-
}
590+
Describe "-ReportSummary switch" {
591+
BeforeAll {
592+
$pssaPath = (Get-Module PSScriptAnalyzer).Path
601593

602-
$reportSummaryFor1Warning = '*1 rule violation found. Severity distribution: Error = 0, Warning = 1, Information = 0*'
594+
if ($IsCoreCLR)
595+
{
596+
$pwshExe = (Get-Process -Id $PID).Path
597+
}
598+
else
599+
{
600+
$pwshExe = 'powershell'
603601
}
604602

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

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

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

617-
# using statements are only supported in v5+
618-
Describe "Handles parse errors due to unknown types" -Skip:($testingLibraryUsage -or ($PSVersionTable.PSVersion -lt '5.0')) {
619-
BeforeAll {
620-
$script = @'
621-
using namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
622-
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
623-
Import-Module "AzureRm"
624-
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
614+
"$result" | Should -Not -BeLike $reportSummaryFor1Warning
615+
}
616+
}
617+
618+
# using statements are only supported in v5+
619+
Describe "Handles parse errors due to unknown types" -Skip:($testingLibraryUsage -or ($PSVersionTable.PSVersion -lt '5.0')) {
620+
BeforeAll {
621+
$script = @'
622+
using namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
623+
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
624+
Import-Module "AzureRm"
625+
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
625626
'@
626-
}
627-
It "does not throw and detect one expected warning after the parse error has occured when using -ScriptDefintion parameter set" {
628-
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition $script
629-
$warnings.Count | Should -Be 1
630-
$warnings.RuleName | Should -Be 'TypeNotFound'
631-
}
627+
}
628+
It "does not throw and detect one expected warning after the parse error has occured when using -ScriptDefintion parameter set" {
629+
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition $script
630+
$warnings.Count | Should -Be 1
631+
$warnings.RuleName | Should -Be 'TypeNotFound'
632+
}
632633

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

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

0 commit comments

Comments
 (0)