-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSScriptAnalyzer.tests.ps1
More file actions
64 lines (54 loc) · 3.19 KB
/
PSScriptAnalyzer.tests.ps1
File metadata and controls
64 lines (54 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<#
.SYNOPSIS
Pester test to publish PSScriptAnalyzer results
.DESCRIPTION
Run PSScriptAnalyzer on all PS files in working folder, then use Pester to generate proper test output
.NOTES
PSScriptAnalyzer does not output NUnit XML, so Pester is used to generate that report
~Script like a champion today~
.LINK
https://github.com/DevOpsDrum/PSScriptAnalyzer
#>
# PSScriptAnalyzer suppression items:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Suppressing because Invoke-Expression is needed to read in PSScriptAnalyzer settings file properly.')]
[CmdletBinding()]
param() # needed to prevent suppression message attribute from causing error
begin {
# install/import PSScriptAnalyzer module
if ($null -eq (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { Write-Output "Installing PSScriptAnalyzer module..."; Set-PSRepository PSGallery -InstallationPolicy Trusted; Install-Module -Name PSScriptAnalyzer -Scope CurrentUser } else { Write-Verbose "PSScriptAnalyzer module already installed." -Verbose; Import-Module -Name PSScriptAnalyzer }
$pssaSettingsFile = './PSScriptAnalyzerSettings.psd1'
if ((Test-Path $pssaSettingsFile) -eq $false) { throw "PS Script Analyzer settings not found at $pssaSettingsFile" }
# get rules from settings file instead of `Get-ScriptAnalyzerRule`, which gets all rules, whether they're used or not.
$pssaSettingsData = Get-Content -Path $pssaSettingsFile -Raw # get rules file content as string, which is a hash table
$pssaSettingsHashtable = Invoke-Expression -Command $pssaSettingsData # convert string into hashtable object
$pssaSettingsRules = $pssaSettingsHashtable.IncludeRules # use list of included rules when looping through rules below
# run PSScriptAnalyzer with settings file, recursively in repo files, and store results in $analysis var
$analysis = Invoke-ScriptAnalyzer -Path . -Recurse -Settings $pssaSettingsFile # -IncludeSuppressed
# build TestCases array to pass into `It` block
$testCases = @() # instantiate array to store test cases
foreach ($settingsRule in $pssaSettingsRules) {
if (($analysis.RuleName) -contains $settingsRule) {
# one or more issues were found for this rule, so save the items that match the rule name to $record
$record = $analysis | Where-Object { $_.RuleName -like $settingsRule }
}
# add rule name and record object (as JSON) to $testCases array
$testCases += @{
rule = $settingsRule # rule name
record = if ($null -ne $record) { $record | ConvertTo-Json -Depth 4 } # diagnostic record stored as JSON if not null, otherwise null/empty
}
# reset to null after each loop
$record = $null
}
}
process {
# pester blocks
Describe 'PSScriptAnalyzer' {
Context 'OTBS (K&R) and PSGallery Formatting' {
It "Should not get results for rule: <_.rule>" -TestCases $testCases {
# if record is null, the test passed
$_.record | Should -BeNullOrEmpty
}
}
}
}
end {}