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 2 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'
}
}
39 changes: 17 additions & 22 deletions Whiskey/Functions/Import-WhiskeyPowerShellModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Import-WhiskeyPowerShellModule

[Parameter(Mandatory)]
# The path to the build root, where the PSModules directory can be found.
[String]$BuildRoot
[String]$PSModulesRoot
)

Set-StrictMode -Version 'Latest'
Expand All @@ -41,33 +41,28 @@ function Import-WhiskeyPowerShellModule
Get-Module -Name $Name | Remove-Module -Force -WhatIf:$false
}

$searchPaths = & {
Join-Path -Path $BuildRoot -ChildPath $powerShellModulesDirectoryName
}
$relativePSModulesRoot = Resolve-Path -Path $PSModulesRoot -Relative -ErrorAction Ignore

foreach( $moduleName in $Name )
{
foreach( $searchDir in $searchPaths )
$moduleDir = Join-Path -Path $PSModulesRoot -ChildPath $moduleName
if( (Test-Path -Path $moduleDir -PathType Container) )
{
$moduleDir = Join-Path -Path $searchDir -ChildPath $moduleName
if( (Test-Path -Path $moduleDir -PathType Container) )
Write-WhiskeyDebug -Message ('PSModuleAutoLoadingPreference = "{0}"' -f $PSModuleAutoLoadingPreference)
Write-WhiskeyVerbose -Message ('Importing PowerShell module "{0}" from "{1}".' -f $moduleName,$relativePSModulesRoot)
$numErrorsBefore = $Global:Error.Count
& {
$VerbosePreference = 'SilentlyContinue'
Import-Module -Name $moduleDir -Global -Force -ErrorAction Stop -Verbose:$false
} 4> $null
# Some modules (...cough...PowerShellGet...cough...) write silent errors during import. This causes our tests
# to fail. I know this is a little extreme.
$numErrorsAfter = $Global:Error.Count - $numErrorsBefore
splatteredbits marked this conversation as resolved.
Show resolved Hide resolved
for( $idx = 0; $idx -lt $numErrorsAfter; ++$idx )
{
Write-WhiskeyDebug -Message ('PSModuleAutoLoadingPreference = "{0}"' -f $PSModuleAutoLoadingPreference)
Write-WhiskeyVerbose -Message ('Import PowerShell module "{0}" from "{1}".' -f $moduleName,$searchDir)
$numErrorsBefore = $Global:Error.Count
& {
$VerbosePreference = 'SilentlyContinue'
Import-Module -Name $moduleDir -Global -Force -ErrorAction Stop
} 4> $null
# Some modules (...cough...PowerShellGet...cough...) write silent errors during import. This causes our tests
# to fail. I know this is a little extreme.
$numErrorsAfter = $Global:Error.Count - $numErrorsBefore
for( $idx = 0; $idx -lt $numErrorsAfter; ++$idx )
{
$Global:Error.RemoveAt(0)
}
break
$Global:Error.RemoveAt(0)
}
break
}

if( -not (Get-Module -Name $moduleName) )
Expand Down
Loading