Skip to content

Commit

Permalink
fix: pin Pester to 4.10.1 for compatibility with pwsh 6.0.4 (#44)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
lupino3 authored Jul 8, 2022
1 parent 0a5aa0a commit 631b9df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
35 changes: 12 additions & 23 deletions test/FeatureFlags.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
<#
.SYNOPSIS Tests for the FeatureFlags module.
.NOTES Please update to the last version of Pester before running the tests:
https://github.com/pester/Pester/wiki/Installation-and-Update.
After updating, run the Invoke-Pester cmdlet from the project directory.
#>
BeforeAll {
$ModuleName = "FeatureFlags"
Get-Module $ModuleName | Remove-Module -Force
Import-Module $PSScriptRoot\test-functions.psm1

$VerbosePreference = "Continue"
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
if ($null -eq $Module) {
Write-Error "Could not import $ModuleName"
exit 1
}
Write-Host "Done."
Write-Host $Module.ExportedCommands.Values.Name
$ModuleName = "FeatureFlags"
Get-Module $ModuleName | Remove-Module -Force
Import-Module $PSScriptRoot\test-functions.psm1

$VerbosePreference = "Continue"
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
if ($null -eq $Module) {
Write-Error "Could not import $ModuleName"
exit 1
}
Write-Host "Done."

Describe 'Confirm-FeatureFlagConfig' {
Context 'Validation of invalid configuration' {
Expand Down Expand Up @@ -645,7 +636,5 @@ Describe 'Out-EvaluatedFeaturesFiles' -Tag Features {
}
}

AfterAll {
Remove-Module $ModuleName
Remove-Module test-functions
}
Remove-Module $ModuleName
Remove-Module test-functions
28 changes: 22 additions & 6 deletions tools/run-tests.ps1
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
# Fail on the first error.
$ErrorActionPreference = "Stop"

$parentDir = Split-Path -Parent (Split-Path -Parent $PSCommandPath)
$testDir = Join-Path $parentDir -ChildPath "test"

# Debug info.
Write-Host "PowerShell version" $PSVersionTable.PSVersion

$RequiredPesterVersion = "5.3.3"
# As a first step, try to import the FeatureFlags module.
# When loaded, the module will try to read the schema and validate it, so some
# errors can be triggered even before loading Pester.
$ModuleName = "FeatureFlags"
Get-Module $ModuleName | Remove-Module -Force
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
if ($null -eq $Module) {
Write-Error "Could not import $ModuleName"
exit 1
}
Write-Host "FeatureFlags module loads successfully. Removing it."
Get-Module $ModuleName | Remove-Module -Force

# Install the required Pester version.
# We use the latest 4.x because 5.x fails to load under PowerShell 6.0.4,
# which is one of the versions we want to test.
$RequiredPesterVersion = "4.10.1"
$pesterVersions = Get-Module -ListAvailable | Where-Object {$_.Name -eq "Pester" -and $_.Version -eq $RequiredPesterVersion}
$InstallPester = $false
if ($pesterVersions.Count -eq 0) {
Write-Warning "Pester $RequiredPesterVersion not found, installing it."
$InstallPester = $true
}

if ($InstallPester) {
Install-Module Pester -Force -Scope CurrentUser -RequiredVersion $RequiredPesterVersion -SkipPublisherCheck
}

# Load the required Pester module.
Get-Module -Name Pester | Remove-Module
Import-Module Pester -RequiredVersion $RequiredPesterVersion

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

0 comments on commit 631b9df

Please sign in to comment.