diff --git a/build.ps1 b/build.ps1 index c2b6f09..5c74364 100755 --- a/build.ps1 +++ b/build.ps1 @@ -16,8 +16,6 @@ The task to run. The name of the module for which the script runs. #> -[CmdletBinding(SupportsShouldProcess=$True)] - param ( [ValidateSet('Test', 'Import', 'Publish')] [Parameter(Position = 0)] @@ -36,6 +34,7 @@ switch ($Task) { $pesterConfiguration = @{ Run = @{ Path = (Join-Path $PSScriptRoot 'src') + Exit = $true } CodeCoverage = @{ Enabled = $true @@ -65,7 +64,7 @@ Invoke-Pester -Configuration `$configuration 'Publish' { # Validate a NUGET_API_KEY environment variable was provided - if (-Not (Test-Path env:NUGET_API_KEY)) { + if (-Not $env:NUGET_API_KEY) { throw "`$env:NUGET_API_KEY is undefined (required to publish the module)" } @@ -75,16 +74,48 @@ Invoke-Pester -Configuration `$configuration Copy-Item ` -Path (Join-Path $PSScriptRoot 'src' '*') ` -Destination $releaseModuleFolder ` - -Exclude '*.Tests.ps1' ` + -Exclude '*.Tests.ps1', 'TestHeader.Tests.ps1' ` -Recurse ` -WhatIf:$false + # + # Test the module in its publish folder + # + + # Save current PSModulePath + $originalModulePath = $env:PSModulePath + try { + # Instruct the test header to import Hospitable from the first installed location + $env:HOSPITABLE_TEST_RELEASE = $true + + # Update PSModulePath so the published folder is the first location to look at + $modulePaths = @() + if ($originalModulePath) { + $modulePaths = $originalModulePath.Split([IO.Path]::PathSeparator) + } + $modulePaths = @(Split-Path $releaseModuleFolder -Parent) + $modulePaths + $env:PSModulePath = $modulePaths -Join [IO.Path]::PathSeparator + + # Test the module + & (Join-Path $PSScriptRoot 'build.ps1') -Task 'Test' + } finally { + # Restore environment variables + $env:HOSPITABLE_TEST_RELEASE = $null + if ($originalModulePath) { + $env:PSModulePath = $originalModulePath + } + } + + # Abort the publication if at least one test failed + if ($LASTEXITCODE -ne 0) { + throw "At least one test failed, aborting publication" + } + # Publish the module Publish-Module ` -Path $releaseModuleFolder ` - -Repository PSGallery ` + -Repository 'PSGallery' ` -NuGetApiKey $env:NUGET_API_KEY ` - -Verbose ` - -WhatIf:$WhatIfPreference + -Verbose } } diff --git a/src/TestHeader.Tests.ps1 b/src/TestHeader.Tests.ps1 new file mode 100644 index 0000000..5242ddd --- /dev/null +++ b/src/TestHeader.Tests.ps1 @@ -0,0 +1,14 @@ +### +### File dot sourced in every tests, responsible for loading the module +### + +# Remove all potentially currently loaded modules +Get-Module -Name 'Hospitable' | Remove-Module -Force + +# Import the module +if ($env:HOSPITABLE_TEST_RELEASE) { + # Import from installed location (used to test the module prior to publishing it) + Import-Module 'Hospitable' +} else { + Import-Module (Join-Path $PSScriptRoot 'Hospitable.psm1') +} \ No newline at end of file diff --git a/src/classes/TreeNode.Tests.ps1 b/src/classes/TreeNode.Tests.ps1 index 2c87706..46c13ae 100644 --- a/src/classes/TreeNode.Tests.ps1 +++ b/src/classes/TreeNode.Tests.ps1 @@ -1,6 +1,5 @@ -# Load (or reload) the module -Remove-Module -Name 'Hospitable' -Force -ErrorAction 'SilentlyContinue' -Import-Module (Join-Path $PSScriptRoot '../Hospitable.psm1') +# Import the module +. (Join-Path $PSScriptRoot '../TestHeader.Tests.ps1') InModuleScope Hospitable { Describe 'TreeNode.ctor' { diff --git a/src/private/ModuleVariables.Tests.ps1 b/src/private/ModuleVariables.Tests.ps1 index c92bcd4..e639888 100644 --- a/src/private/ModuleVariables.Tests.ps1 +++ b/src/private/ModuleVariables.Tests.ps1 @@ -1,6 +1,5 @@ -# Load (or reload) the module -Remove-Module -Name 'Hospitable' -Force -ErrorAction 'SilentlyContinue' -Import-Module (Join-Path $PSScriptRoot '../Hospitable.psm1') +# Import the module +. (Join-Path $PSScriptRoot '../TestHeader.Tests.ps1') InModuleScope Hospitable { Describe 'Get-SettingValue' { diff --git a/src/private/TextFormatting.Tests.ps1 b/src/private/TextFormatting.Tests.ps1 index 5ed26a8..89faf72 100644 --- a/src/private/TextFormatting.Tests.ps1 +++ b/src/private/TextFormatting.Tests.ps1 @@ -1,6 +1,5 @@ -# Load (or reload) the module -Remove-Module -Name 'Hospitable' -Force -ErrorAction 'SilentlyContinue' -Import-Module (Join-Path $PSScriptRoot '../Hospitable.psm1') +# Import the module +. (Join-Path $PSScriptRoot '../TestHeader.Tests.ps1') InModuleScope Hospitable { Describe 'Get-FormattedText' { diff --git a/src/private/Utilities.Tests.ps1 b/src/private/Utilities.Tests.ps1 index 9a1a5f8..00ac8cc 100644 --- a/src/private/Utilities.Tests.ps1 +++ b/src/private/Utilities.Tests.ps1 @@ -1,6 +1,5 @@ -# Load (or reload) the module -Remove-Module -Name 'Hospitable' -Force -ErrorAction 'SilentlyContinue' -Import-Module (Join-Path $PSScriptRoot '../Hospitable.psm1') +# Import the module +. (Join-Path $PSScriptRoot '../TestHeader.Tests.ps1') InModuleScope Hospitable { Describe 'Get-MaxArray' {