-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-PSScriptAnalyzerViaPester.ps1
More file actions
48 lines (43 loc) · 2.13 KB
/
Invoke-PSScriptAnalyzerViaPester.ps1
File metadata and controls
48 lines (43 loc) · 2.13 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
<#
.SYNOPSIS
Use Pester to run PSScriptAnalyzer
.DESCRIPTION
Use Pester to run and publish PSScriptAnalyzer output
.EXAMPLE
PS C:\> Invoke-Invoke-PSScriptAnalyzerViaPester -OutputFile <desired NUnitXML file path> -PesterTestsPath <Path to Pester test file(s)>
Pester executes on file(s) in path and produced an NUnitXML file report in desired location
.OUTPUTS
NUnit XML file in location specified by `-OutputFile`
.NOTES
This is a hack to get an NUnitXML report for PSScriptAnalyzer output, as PSScriptAnalyzer does not produce NUnit XML output
~Script like a champion today~
.LINK
https://github.com/DevOpsDrum/PSScriptAnalyzer
#>
[OutputType([System.String])]
[CmdletBinding()]
param(
[string] $OutputFile = './output/psscriptanalyzer-nunit.xml',
[string] $PesterTestsPath = './PSScriptAnalyzer.tests.ps1',
[ValidateSet('NUnitXML', 'JUnitXMl')] # only 2 formats supported by Pester
[string] $OutputFormat = 'NUnitXML',
[ValidateSet('Diagnostic', 'Detailed', 'Normal', 'Minimal', 'None')]
[string] $OutputVerbosity = 'Detailed'
)
begin {
# install/import Pester module
if ($null -eq (Get-Module -ListAvailable -Name Pester)) { Write-Output "Installing Pester module..."; Set-PSRepository PSGallery -InstallationPolicy Trusted; Install-Module -Name Pester -Scope CurrentUser -SkipPublisherCheck } else { Write-Verbose "Pester module already installed." -Verbose; Import-Module -Name Pester }
# create pester configuration object using doc here: https://pester-docs.netlify.app/docs/commands/New-PesterConfiguration
$config = New-PesterConfiguration
$config.Run.Path = $PesterTestsPath
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = $OutputFormat # default is NUnitXML
$config.TestResult.OutputPath = $OutputFile
$config.Output.Verbosity = $OutputVerbosity
}
process {
# call Pester `Invoke-Pester` function, which will run PSScriptAnalyzer tests in script set in var: $PesterTestsPath
Invoke-Pester -Configuration $config
Write-Output "`n📊 Pester $OutputFormat report: $OutputFile`n"
}
end {}