Skip to content

Commit

Permalink
Add pre-publication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannchaudet committed Apr 22, 2021
1 parent 52c6222 commit ade611a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
45 changes: 38 additions & 7 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -36,6 +34,7 @@ switch ($Task) {
$pesterConfiguration = @{
Run = @{
Path = (Join-Path $PSScriptRoot 'src')
Exit = $true
}
CodeCoverage = @{
Enabled = $true
Expand Down Expand Up @@ -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)"
}

Expand All @@ -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
}
}
14 changes: 14 additions & 0 deletions src/TestHeader.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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')
}
5 changes: 2 additions & 3 deletions src/classes/TreeNode.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
5 changes: 2 additions & 3 deletions src/private/ModuleVariables.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
5 changes: 2 additions & 3 deletions src/private/TextFormatting.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
5 changes: 2 additions & 3 deletions src/private/Utilities.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down

0 comments on commit ade611a

Please sign in to comment.