Skip to content

Commit c60f314

Browse files
🩹 [Patch]: Refactor PSScriptAnalyzer test setup for improved variable scoping and clarity
1 parent 116252c commit c60f314

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

‎scripts/tests/PSScriptAnalyzer/PSScriptAnalyzer.Tests.ps1‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,41 @@ BeforeDiscovery {
6161
)
6262
}
6363
}
64-
$relativeSettingsFilePath = if ($SettingsFilePath.StartsWith($PSScriptRoot)) {
65-
$SettingsFilePath.Replace($PSScriptRoot, 'Action:').Trim('\').Trim('/')
66-
} elseif ($SettingsFilePath.StartsWith($env:GITHUB_WORKSPACE)) {
67-
$SettingsFilePath.Replace($env:GITHUB_WORKSPACE, 'Workspace:').Trim('\').Trim('/')
68-
} else {
69-
$SettingsFilePath
70-
}
71-
[pscustomobject]@{
72-
relativeSettingsFilePath = $relativeSettingsFilePath
73-
SettingsFilePath = $SettingsFilePath
74-
PSScriptRoot = $PSScriptRoot
75-
GITHUB_WORKSPACE = $env:GITHUB_WORKSPACE
76-
}
77-
LogGroup "Invoke-ScriptAnalyzer -Path [$Path] -Settings [$relativeSettingsFilePath]" {
78-
$testResults = Invoke-ScriptAnalyzer -Path $Path -Settings $SettingsFilePath -Recurse -Verbose
79-
}
80-
LogGroup "TestResults [$($testResults.Count)]" {
81-
$testResults | ForEach-Object {
82-
$_ | Format-List | Out-String -Stream | ForEach-Object {
83-
Write-Verbose $_ -Verbose
64+
}
65+
66+
Describe 'PSScriptAnalyzer' {
67+
BeforeAll {
68+
$relativeSettingsFilePath = if ($SettingsFilePath.StartsWith($PSScriptRoot)) {
69+
$SettingsFilePath.Replace($PSScriptRoot, 'Action:').Trim('\').Trim('/')
70+
} elseif ($SettingsFilePath.StartsWith($env:GITHUB_WORKSPACE)) {
71+
$SettingsFilePath.Replace($env:GITHUB_WORKSPACE, 'Workspace:').Trim('\').Trim('/')
72+
} else {
73+
$SettingsFilePath
74+
}
75+
[pscustomobject]@{
76+
relativeSettingsFilePath = $relativeSettingsFilePath
77+
SettingsFilePath = $SettingsFilePath
78+
PSScriptRoot = $PSScriptRoot
79+
GITHUB_WORKSPACE = $env:GITHUB_WORKSPACE
80+
}
81+
LogGroup "Invoke-ScriptAnalyzer -Path [$Path] -Settings [$relativeSettingsFilePath]" {
82+
$script:testResults = Invoke-ScriptAnalyzer -Path $Path -Settings $SettingsFilePath -Recurse -Verbose
83+
}
84+
LogGroup "TestResults [$($script:testResults.Count)]" {
85+
$script:testResults | ForEach-Object {
86+
$_ | Format-List | Out-String -Stream | ForEach-Object {
87+
Write-Verbose $_ -Verbose
88+
}
8489
}
8590
}
8691
}
87-
}
8892

89-
Describe 'PSScriptAnalyzer' {
9093
foreach ($Severety in $Severeties) {
9194
Context "Severity: $Severety" {
9295
foreach ($rule in $rules | Where-Object -Property Severity -EQ $Severety) {
93-
It "$($rule.CommonName) ($($rule.RuleName))" -Skip:$rule.Skip {
96+
It "$($rule.CommonName) ($($rule.RuleName))" -Skip:$rule.Skip -ForEach @( $rule.RuleName) {
9497
$issues = [Collections.Generic.List[string]]::new()
95-
$testResults | Where-Object -Property RuleName -EQ $rule.RuleName | ForEach-Object {
98+
$script:testResults | Where-Object -Property RuleName -EQ $_ | ForEach-Object {
9699
$relativePath = $_.ScriptPath.Replace($Path, '').Trim('\').Trim('/')
97100
$issues.Add(([Environment]::NewLine + " - $relativePath`:L$($_.Line):C$($_.Column)"))
98101
}

0 commit comments

Comments
 (0)