diff --git a/test/FeatureFlags.Tests.ps1 b/test/FeatureFlags.Tests.ps1 index 5df21fc..9b04768 100644 --- a/test/FeatureFlags.Tests.ps1 +++ b/test/FeatureFlags.Tests.ps1 @@ -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 +$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 +$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' { @@ -645,7 +636,5 @@ Describe 'Out-EvaluatedFeaturesFiles' -Tag Features { } } -AfterAll { - Remove-Module $ModuleName - Remove-Module test-functions -} \ No newline at end of file +Remove-Module $ModuleName +Remove-Module test-functions diff --git a/tools/run-tests.ps1 b/tools/run-tests.ps1 index 29c5127..1f9dfd3 100644 --- a/tools/run-tests.ps1 +++ b/tools/run-tests.ps1 @@ -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 -} \ No newline at end of file +}