Skip to content

Commit 631b9df

Browse files
authored
fix: pin Pester to 4.10.1 for compatibility with pwsh 6.0.4 (#44)
* fix: pin Pester to 4.10.1 This makes tests work under PowerShell 6.0.4. Also, import the FeatureFlags module first in run-tests.ps1, so that any failures in importing the module are detected early. * fix: remove BeforeAll/AfterAll They don't seem to work well with Pester 4.10.1.
1 parent 0a5aa0a commit 631b9df

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

test/FeatureFlags.Tests.ps1

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
<#
2-
.SYNOPSIS Tests for the FeatureFlags module.
3-
.NOTES Please update to the last version of Pester before running the tests:
4-
https://github.com/pester/Pester/wiki/Installation-and-Update.
5-
After updating, run the Invoke-Pester cmdlet from the project directory.
6-
#>
7-
BeforeAll {
8-
$ModuleName = "FeatureFlags"
9-
Get-Module $ModuleName | Remove-Module -Force
10-
Import-Module $PSScriptRoot\test-functions.psm1
11-
12-
$VerbosePreference = "Continue"
13-
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
14-
if ($null -eq $Module) {
15-
Write-Error "Could not import $ModuleName"
16-
exit 1
17-
}
18-
Write-Host "Done."
19-
Write-Host $Module.ExportedCommands.Values.Name
1+
$ModuleName = "FeatureFlags"
2+
Get-Module $ModuleName | Remove-Module -Force
3+
Import-Module $PSScriptRoot\test-functions.psm1
4+
5+
$VerbosePreference = "Continue"
6+
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
7+
if ($null -eq $Module) {
8+
Write-Error "Could not import $ModuleName"
9+
exit 1
2010
}
11+
Write-Host "Done."
2112

2213
Describe 'Confirm-FeatureFlagConfig' {
2314
Context 'Validation of invalid configuration' {
@@ -645,7 +636,5 @@ Describe 'Out-EvaluatedFeaturesFiles' -Tag Features {
645636
}
646637
}
647638

648-
AfterAll {
649-
Remove-Module $ModuleName
650-
Remove-Module test-functions
651-
}
639+
Remove-Module $ModuleName
640+
Remove-Module test-functions

tools/run-tests.ps1

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
# Fail on the first error.
22
$ErrorActionPreference = "Stop"
33

4-
$parentDir = Split-Path -Parent (Split-Path -Parent $PSCommandPath)
5-
$testDir = Join-Path $parentDir -ChildPath "test"
6-
74
# Debug info.
85
Write-Host "PowerShell version" $PSVersionTable.PSVersion
96

10-
$RequiredPesterVersion = "5.3.3"
7+
# As a first step, try to import the FeatureFlags module.
8+
# When loaded, the module will try to read the schema and validate it, so some
9+
# errors can be triggered even before loading Pester.
10+
$ModuleName = "FeatureFlags"
11+
Get-Module $ModuleName | Remove-Module -Force
12+
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
13+
if ($null -eq $Module) {
14+
Write-Error "Could not import $ModuleName"
15+
exit 1
16+
}
17+
Write-Host "FeatureFlags module loads successfully. Removing it."
18+
Get-Module $ModuleName | Remove-Module -Force
19+
20+
# Install the required Pester version.
21+
# We use the latest 4.x because 5.x fails to load under PowerShell 6.0.4,
22+
# which is one of the versions we want to test.
23+
$RequiredPesterVersion = "4.10.1"
1124
$pesterVersions = Get-Module -ListAvailable | Where-Object {$_.Name -eq "Pester" -and $_.Version -eq $RequiredPesterVersion}
1225
$InstallPester = $false
1326
if ($pesterVersions.Count -eq 0) {
1427
Write-Warning "Pester $RequiredPesterVersion not found, installing it."
1528
$InstallPester = $true
1629
}
17-
1830
if ($InstallPester) {
1931
Install-Module Pester -Force -Scope CurrentUser -RequiredVersion $RequiredPesterVersion -SkipPublisherCheck
2032
}
2133

34+
# Load the required Pester module.
2235
Get-Module -Name Pester | Remove-Module
2336
Import-Module Pester -RequiredVersion $RequiredPesterVersion
2437

38+
# Invoke Pester.
39+
$parentDir = Split-Path -Parent (Split-Path -Parent $PSCommandPath)
40+
$testDir = Join-Path $parentDir -ChildPath "test"
2541
$FailedTests = Invoke-Pester $testDir -EnableExit -OutputFile "test/results.xml" -OutputFormat "NUnitXML" -CodeCoverage "$parentDir/FeatureFlags.psm1"
2642
if ($FailedTests -gt 0) {
2743
Write-Error "Error: $FailedTests Pester tests failed."
2844
exit $FailedTests
29-
}
45+
}

0 commit comments

Comments
 (0)