Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetPowerShellModule #311

Merged
merged 12 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 136 additions & 25 deletions Test/GetPowerShellModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,40 @@ function Init
$script:context = New-WhiskeyTestContext -ForDeveloper -ForBuildRoot $testRoot
}

function GivenImport
{
$taskParameter['Import'] = 'true'
}

function GivenModule
{
Param(
[String]$Module
)
$script:taskParameter['Name'] = $Module

$script:modulePath = Join-path -Path $context.BuildRoot -ChildPath $PSModulesDirectoryName
$script:modulePath = Join-path -Path $modulePath -ChildPath $taskParameter['Name']
$taskParameter['Name'] = $Module
}

function GivenVersion
{
param(
[String]$Version
)
$script:taskParameter['Version'] = $Version
$taskParameter['Version'] = $Version
}

function GivenPath
{
param(
$Path
)

$taskParameter['Path'] = $Path
}

function GivenPrereleaseAllowed
{
$taskParameter['AllowPrerelease'] = 'true'
}

function GivenNonExistentModule
Expand All @@ -54,11 +71,20 @@ function Reset
Reset-WhiskeyTestPSModule
}

function WhenPowershellModuleIsRan
function WhenTaskRun
{
[CmdletBinding()]
param()


$script:modulePath = Join-Path -Path $context.BuildRoot -ChildPath $PSModulesDirectoryName
if( $taskParameter['Path'] )
{
$script:modulePath = Join-Path $context.BuildRoot -ChildPath $taskParameter['Path']
}
$script:modulePath = Join-Path -Path $modulePath -ChildPath $taskParameter['Name']
$script:modulePath = [IO.Path]::GetFullPath($modulePath)

try
{
Invoke-WhiskeyTask -TaskContext $context -Parameter $taskParameter -Name "GetPowerShellModule"
Expand All @@ -71,22 +97,68 @@ function WhenPowershellModuleIsRan

function ThenModuleInstalled
{
param(
$AtVersion,
$InDirectory = $PSModulesDirectoryName
)

$version = '*.*.*'
$prerelease = ''
if( $AtVersion -match '^(\d+\.\d+\.\d+)(-(.*))?$' )
{
$version = $Matches[1]
$prerelease = $Matches[3]
}

$modulePath = Join-Path -Path $testRoot -ChildPath $InDirectory
$modulePath = Join-Path -Path $modulePath -ChildPath $taskParameter['Name']
$modulePath = Join-Path -Path $modulePath -ChildPath $version
$modulePath = Join-Path -Path $modulePath -ChildPath ('{0}.psd1' -f $taskParameter['Name'])
$modulePath | Should -Exist

$module = Test-ModuleManifest -Path $modulePath
if( $prerelease )
{
$module.PrivateData.PSData.Prerelease | Should -Be $prerelease
}
else
{
if( ($module.PrivateData.PSData | Get-Member 'Prerelease') )
{
$module.PrivateData.PSData.Prerelease | Should -BeNullOrEmpty
}
}
}

function ThenModuleInstalledAtVersion
function ThenModuleImported
{
param(
$Version
[Parameter(Mandatory)]
[String]$Name
)

$moduleVersionPath = Join-Path -Path $modulePath -ChildPath $Version
Get-Module -Name $Name | Should -Not -BeNullOrEmpty
}

$moduleVersionPath | Should -Exist
function ThenModuleNotImported
{
param(
[Parameter(Mandatory)]
[String]$Name
)

Get-Module -Name $Name | Should -BeNullOrEmpty
}

function ThenModuleShouldNotExist
function ThenModuleNotInstalled
{
param(
$InDirectory = $PSModulesDirectoryName
)

$modulePath = Join-Path -Path $testRoot -ChildPath $InDirectory
$modulePath = Join-Path -Path $modulePath -ChildPath $taskParameter['Name']
$modulePath = Join-Path -Path $modulePath -ChildPath ('*.*.*\{0}.psd1' -f $taskParameter['Name'])
$modulePath | Should -Not -Exist
}

Expand All @@ -103,9 +175,11 @@ Describe 'GetPowerShellModule.when given a module Name' {
AfterEach { Reset }
It 'should install the lastest version of that module' {
Init
GivenModule 'Pester'
WhenPowershellModuleIsRan
Get-Module 'Zip' | Remove-Module -Force
GivenModule 'Zip'
WhenTaskRun
ThenModuleInstalled
ThenModuleNotImported 'Zip'
}
}

Expand All @@ -115,9 +189,8 @@ Describe 'GetPowerShellModule.when given a module Name and Version' {
Init
GivenModule 'Pester'
GivenVersion '3.4.0'
WhenPowershellModuleIsRan
ThenModuleInstalled
ThenModuleInstalledAtVersion '3.4.0'
WhenTaskRun
ThenModuleInstalled -AtVersion '3.4.0'
}
}

Expand All @@ -127,9 +200,8 @@ Describe 'GetPowerShellModule.when given a Name and a wildcard Version' {
Init
GivenModule 'Pester'
GivenVersion '3.3.*'
WhenPowershellModuleIsRan
ThenModuleInstalled
ThenModuleInstalledAtVersion '3.3.9'
WhenTaskRun
ThenModuleInstalled -AtVersion '3.3.9'
}
}

Expand All @@ -140,9 +212,9 @@ Describe 'GetPowerShellModule.when an invalid module Name is requested' {
GivenModule 'bad mod'
GivenVersion '3.4.0'
GivenNonExistentModule
WhenPowershellModuleIsRan -ErrorAction SilentlyContinue
WhenTaskRun -ErrorAction SilentlyContinue
ThenErrorShouldBeThrown 'Failed to find PowerShell module bad mod'
ThenModuleShouldNotExist
ThenModuleNotInstalled
}
}

Expand All @@ -153,17 +225,17 @@ Describe 'GetPowerShellModule.when given an invalid Version' {
GivenModule 'Pester'
GivenVersion '0.0.0'
GivenNonExistentModule
WhenPowershellModuleIsRan -ErrorAction SilentlyContinue
WhenTaskRun -ErrorAction SilentlyContinue
ThenErrorShouldBeThrown "Failed to find PowerShell module Pester at version 0.0.0"
ThenModuleShouldNotExist
ThenModuleNotInstalled
}
}

Describe 'GetPowerShellModule.when missing Name property' {
AfterEach { Reset }
It 'should fail' {
Init
WhenPowershellModuleIsRan -ErrorAction SilentlyContinue
WhenTaskRun -ErrorAction SilentlyContinue
ThenErrorShouldBeThrown 'Property\ "Name"\ is mandatory'
}
}
Expand All @@ -174,7 +246,46 @@ Describe 'GetPowerShellModule.when called with clean mode' {
Init
GivenModule 'Rivet'
GivenCleanMode
WhenPowershellModuleIsRan
ThenModuleShouldNotExist
WhenTaskRun
ThenModuleNotInstalled
}
}

Describe 'GetPowerShellModule.when allowing prerelease versions' {
AfterEach { Reset }
It 'should install a prelease version' {
Init
GivenModule 'Whiskey'
GivenVersion '0.43.*-*'
GivenPrereleaseAllowed
WhenTaskRun
ThenModuleInstalled -AtVersion '0.43.0-beta1416'
}
}

Describe 'GetPowerShellModule.when installing to custom directory' {
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
AfterEach { Reset }
It 'should install to a custom directory' {
Init
GivenModule 'Zip'
GivenPath 'FubarSnafu'
WhenTaskRun
ThenModuleInstalled -InDirectory 'FubarSnafu'
GivenCleanMode
WhenTaskRun
ThenModuleNotInstalled -InDirectory 'FubarSnafu'
}
}

Describe 'GetPowerShellModule.when importing module after installation' {
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
AfterEach { Reset }
It 'should import module into global scope' {
Init
Get-Module -Name 'Glob' | Remove-Module -Force
GivenImport
GivenModule 'Glob'
WhenTaskRun
ThenModuleInstalled
ThenModuleImported 'Glob'
}
}
107 changes: 107 additions & 0 deletions Test/Invoke-WhiskeyBuild.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ $publishPipelineName = 'Publish'

function Init
{
$Global:Error.Clear()

[Whiskey.Context]$script:context = $null
$script:runByDeveloper = $false
$script:runByBuildServer = $false
Expand Down Expand Up @@ -140,6 +142,20 @@ function GivenWhiskeyYml

$Content | Set-Content -Path (Join-Path $testRoot -ChildPath 'whiskey.yml')
}

function ThenBuildFailed
{
param(
$WithErrorMessage
)

$threwException | Should -BeTrue
if( $WithErrorMessage )
{
$Global:Error | Select-Object -First 1 | Should -Match $WithErrorMessage
}
}

function ThenBuildOutputRemoved
{
It ('should remove .output directory') {
Expand Down Expand Up @@ -359,6 +375,35 @@ function WhenRunningBuild
}
}

function WhenRunningBuildFromBuildPs1
{
[CmdletBinding()]
param(
)

$script:threwException = $false
$buildPs1Path = Join-Path $testRoot -ChildPath 'build.ps1'
@"
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
Set-Location -Path "$($testRoot)"
Import-Module -Name "$(Join-Path -Path $PSScriptRoot -ChildPath '..\Whiskey' -Resolve)"
Import-Module -Name "$(Join-Path -Path $PSScriptRoot -ChildPath 'WhiskeyTestTasks.psm1' -Resolve)"
`$context = New-WhiskeyContext -Environment Verification -ConfigurationPath '.\whiskey.yml'
Invoke-WhiskeyBuild -Context `$context
New-Item -Path 'passed'
"@ | Set-Content -Path $buildPs1Path

# PowerShell's error handling is very different between starting a build from a build.ps1 script vs. a Pester test
# calling Invoke-WhiskeyBuild.
Start-Job -ScriptBlock {
& $using:buildPs1Path
} | Receive-Job -Wait -AutoRemoveJob

if( -not (Test-Path -Path (Join-Path -Path $testRoot -ChildPath 'passed') ) )
{
$script:threwException = $true
}
}

Describe 'Invoke-WhiskeyBuild.when build passes' {
Context 'By Developer' {
Init
Expand Down Expand Up @@ -543,3 +588,65 @@ Build:
$infos | Where-Object { $_ -match 'InformationPreference\ enabled!' } | Should -BeNullOrEmpty
}
}

Describe 'Invoke-WhiskeyBuild.when task violates a strict mode rule' {
It 'should fail the build' {
Init
GivenWhiskeyYml @'
Build:
- Version:
Version: 0.0.0
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
- SetStrictModeViolationTask
'@
WhenRunningBuildFromBuildPs1 -ErrorAction SilentlyContinue
ThenBuildFailed -WithErrorMessage 'Build\ failed\.'
$Global:Error[1] | Should -Match 'has\ not\ been\ set'
}
}

Describe 'Invoke-WhiskeyBuild.when task invokes a command that doesn''t exist' {
It 'should fail the build' {
Init
GivenWhiskeyYml @'
Build:
- Version:
Version: 0.0.0
- CommandNotFoundTask
'@
WhenRunningBuildFromBuildPs1 -ErrorAction SilentlyContinue
ThenBuildFailed -WithErrorMessage 'Build\ failed.'
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
$Global:Error[1] | Should -Match 'is\ not\ recognized'
}
}

Describe 'Invoke-WhiskeyBuild.when task fails' {
It 'should fail the build' {
Init
GivenWhiskeyYml @'
Build:
- Version:
Version: 0.0.0
- FailingTask:
Message: fdsafjkfsdafjdsf
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
'@
WhenRunningBuildFromBuildPs1 -ErrorAction SilentlyContinue
ThenBuildFailed -WithErrorMessage '\bfdsafjkfsdafjdsf\b'
$Global:Error | Should -Not -Match 'Build\ failed\.'
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
}
}

Describe 'Invoke-WhiskeyBuild.when cmdlet fails because ErrorAction is Stop' {
It 'should fail the build' {
Init
GivenWhiskeyYml @'
Build:
- Version:
Version: 0.0.0
- CmdletErrorActionStopTask:
Path: ruirwemdsfirewmk
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
'@
WhenRunningBuildFromBuildPs1 -ErrorAction SilentlyContinue
ThenBuildFailed -WithErrorMessage '\bruirwemdsfirewmk\b'
$Global:Error | Should -Not -Match 'Build\ failed\.'
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading