Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test
  • Loading branch information
MariusStorhaug committed Mar 28, 2024
commit 78d6c371e84f87ac704c68e568775fa8b90e595a
71 changes: 25 additions & 46 deletions scripts/tests/PSScriptAnalyzer/PSScriptAnalyzer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,37 @@ Param(
)

BeforeDiscovery {
$ruleCollection = @{
Error = [Collections.Generic.List[hashtable]]::new()
Warning = [Collections.Generic.List[hashtable]]::new()
Information = [Collections.Generic.List[hashtable]]::new()
$rules = [Collections.Generic.List[hashtable]]::new()
$ruleObjects = Get-ScriptAnalyzerRule | Sort-Object -Property Severity
foreach ($ruleObject in $ruleObjects) {
$rules.Add(
@{
RuleName = $ruleObject.RuleName
CommonName = $ruleObject.CommonName
Severity = $ruleObject.Severity
Description = $ruleObject.Description
}
)
}
'Error', 'Warning', 'Information' | ForEach-Object {
$Severity = $_
Get-ScriptAnalyzerRule -Severity $Severity | Sort-Object -Property Name | ForEach-Object {
$rule = $_
$ruleCollection[$Severity].Add(
[ordered]@{
RuleName = $rule.RuleName
CommonName = $rule.CommonName
Description = $rule.Description
}
)
}
}

Write-Warning "Discovered [$($rules.Count)] rules"
$relativeSettingsFilePath = $SettingsFilePath.Replace($PSScriptRoot, '').Trim('\').Trim('/')
}

Describe "PSScriptAnalyzer tests using settings file [$relativeSettingsFilePath]" {
Context '<_.key>' -ForEach $ruleCollection {
It '<CommonName> (<RuleName>)' -ForEach $_.Value {
BeforeAll {
$testResults = Invoke-ScriptAnalyzer -Path $Path -Settings $SettingsFilePath -Recurse
Write-Warning "Found [$($testResults.Count)] issues"
}

It '<CommonName> (<RuleName>)' -ForEach $rules {
$issues = @('')
$issues += $testResults | Where-Object -Property RuleName -EQ $ruleName | ForEach-Object {
$relativePath = $_.ScriptPath.Replace($Path, '').Trim('\').Trim('/')
" - $relativePath`:L$($_.Line):C$($_.Column): $($_.Message)"
}
if ($issues.Count -gt 1) {
$issues[0] = "[$($issues.Count - 1)] issues found:"
}
$issues -join [Environment]::NewLine | Should -BeNullOrEmpty
}
}


# $severity = $_
# $rules = $ruleCollection[$severity]
# It "<Severity> rules" -ForEach $rules {
# $ruleName = $_.RuleName
# $commonName = $_.CommonName
# $description = $_.Description
# It "<CommonName> (<RuleName>)" {
# $testResults = Invoke-ScriptAnalyzer -Path $Path -Settings $SettingsFilePath -IncludeRule $ruleName
# $testResults | Should -BeNullOrEmpty
# }
# }
# }
# It '<CommonName> (<RuleName>)' -ForEach $rules {
# $issues = @('')
# $issues += $testResults | Where-Object -Property RuleName -EQ $ruleName | ForEach-Object {
# $relativePath = $_.ScriptPath.Replace($Path, '').Trim('\').Trim('/')
# " - $relativePath`:L$($_.Line):C$($_.Column): $($_.Message)"
# }
# if ($issues.Count -gt 1) {
# $issues[0] = "[$($issues.Count - 1)] issues found:"
# }
# $issues -join [Environment]::NewLine | Should -BeNullOrEmpty
# }
# }