Skip to content

Commit 19a5114

Browse files
Invoke-ManualPester - Fix some issues (#9739)
1 parent 3a53e3a commit 19a5114

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

private/testing/Invoke-ManualPester.ps1

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function Invoke-ManualPester {
9898
[switch]$ScriptAnalyzer
9999
)
100100
begin {
101-
Remove-Module -Name Pester
101+
Remove-Module -Name Pester -ErrorAction SilentlyContinue
102102
$stopProcess = $false
103103
function Get-CoverageIndications($Path, $ModuleBase) {
104104
# takes a test file path and figures out what to analyze for coverage (i.e. dependencies)
@@ -140,8 +140,7 @@ function Invoke-ManualPester {
140140

141141
function Get-PesterTestVersion($testFilePath) {
142142
$testFileContent = Get-Content -Path $testFilePath -Raw
143-
if ($testFileContent -match '#Requires\s+-Module\s+@\{\s+ModuleName="Pester";\s+ModuleVersion="5\.')
144-
{
143+
if ($testFileContent -match '#Requires\s+-Module\s+@\{\s+ModuleName="Pester";\s+ModuleVersion="5\.') {
145144
return '5'
146145
}
147146
return '4'
@@ -180,7 +179,7 @@ function Invoke-ManualPester {
180179
Write-Warning " or go to https://github.com/PowerShell/PSScriptAnalyzer"
181180
} else {
182181
if ($invokeFormatterVersion -ne $ScriptAnalyzerCorrectVersion) {
183-
Remove-Module PSScriptAnalyzer
182+
Remove-Module PSScriptAnalyzer -ErrorAction SilentlyContinue
184183
try {
185184
Import-Module PSScriptAnalyzer -RequiredVersion $ScriptAnalyzerCorrectVersion -ErrorAction Stop
186185
} catch {
@@ -214,7 +213,7 @@ function Invoke-ManualPester {
214213

215214

216215
}
217-
process {
216+
process {
218217
if ($stopProcess) {
219218
return
220219
}
@@ -237,6 +236,9 @@ function Invoke-ManualPester {
237236
Write-DetailedMessage "Importing dbatools psm1"
238237
Import-Module "$ModuleBase\dbatools.psm1" -DisableNameChecking -Force -NoClobber
239238

239+
Write-DetailedMessage "Reading test configuration"
240+
$TestConfig = Get-TestConfig
241+
240242
$ScriptAnalyzerRulesExclude = @('PSUseOutputTypeCorrectly', 'PSAvoidUsingPlainTextForPassword', 'PSUseBOMForUnicodeEncodedFile')
241243

242244
$testInt = $false
@@ -293,11 +295,11 @@ function Invoke-ManualPester {
293295

294296
if ($pesterVersionToUse -eq '5') {
295297
Write-DetailedMessage "Running Pester 5 tests $($f.Name)"
296-
Remove-Module -Name pester -ErrorAction SilentlyContinue
297-
Import-Module pester -MinimumVersion 5.6.1 -ErrorAction Stop
298+
Remove-Module -Name Pester -ErrorAction SilentlyContinue
299+
Import-Module Pester -MinimumVersion 5.6.1 -ErrorAction Stop
298300
$pester5Config = New-PesterConfiguration
299301
$pester5Config.Run.Path = $f.FullName
300-
if ($PassThru){
302+
if ($PassThru) {
301303
$pester5config.Run.PassThru = $passThru
302304
}
303305
$pester5config.Output.Verbosity = $show
@@ -309,9 +311,10 @@ function Invoke-ManualPester {
309311
$pester5Config.Filter.ExcludeTag = "IntegrationTests"
310312
}
311313
Invoke-Pester -Configuration $pester5config
312-
}
313-
else {
314+
} else {
314315
Write-DetailedMessage "Running Pester 4 tests $($f.FullName)"
316+
Remove-Module -Name Pester -ErrorAction SilentlyContinue
317+
Import-Module Pester -MaximumVersion 4.99 -ErrorAction Stop
315318
$pester4Show = 'Default'
316319
switch ($Show) {
317320
'None' { $pester4Show = 'None' }
@@ -324,8 +327,7 @@ function Invoke-ManualPester {
324327
'Show' = $pester4Show
325328
'PassThru' = $passThru
326329
}
327-
if ($Coverage)
328-
{
330+
if ($Coverage) {
329331
$PesterSplat['CodeCoverage'] = $CoverFilesPester
330332
}
331333
if (!($testInt)) {

0 commit comments

Comments
 (0)