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 all 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
167 changes: 142 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 $TestPSModulesDirectoryName
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,74 @@ function WhenPowershellModuleIsRan

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

$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
$module = Get-Module -Name $Name
$module | Should -Not -BeNullOrEmpty
# Cross-platform path generation
$expectedPath = Join-Path -Path $testRoot -ChildPath $TestPSModulesDirectoryName
$expectedPath = Join-Path -Path $expectedPath -ChildPath $Name
$expectedPath = Join-Path -Path $expectedPath -ChildPath '*'
$module.Path | Should -BeLike $expectedPath
}

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

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

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

$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 +181,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 +195,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 +206,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 +218,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 +231,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 +252,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
Import-Module -Name (Join-Path $PSScriptRoot -ChildPath ('..\{0}\Glob' -f $TestPSModulesDirectoryName) -Resolve) -Force
GivenImport
GivenModule 'Glob'
WhenTaskRun
ThenModuleInstalled
ThenModuleImported 'Glob'
}
}
6 changes: 3 additions & 3 deletions Test/Install-WhiskeyPowerShellModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function ThenModuleInstalled
$AtVersion
)

Join-Path -Path $testRoot -ChildPath ('{0}\{1}\{2}' -f $PSModulesDirectoryName,$Name,$AtVersion) | Should -Exist
Join-Path -Path $testRoot -ChildPath ('{0}\{1}\{2}' -f $TestPSModulesDirectoryName,$Name,$AtVersion) | Should -Exist
}

Describe 'Install-WhiskeyPowerShellModule.when installing and re-installing a PowerShell module' {
Expand Down Expand Up @@ -204,7 +204,7 @@ Describe 'Install-WhiskeyPowerShellModule.when PowerShell module directory exist
AfterEach { Reset }
It 'should still install the module' {
Init
$moduleRootDir = Join-Path -Path $testRoot -ChildPath ('{0}\Zip' -f $PSModulesDirectoryName)
$moduleRootDir = Join-Path -Path $testRoot -ChildPath ('{0}\Zip' -f $TestPSModulesDirectoryName)
New-Item -Path $moduleRootDir -ItemType Directory | Write-WhiskeyDebug
Invoke-PowershellInstall -ForModule 'Zip' -Version $latestZip.Version
}
Expand All @@ -215,7 +215,7 @@ Describe 'Install-WhiskeyPowerShellModule.when PowerShell module can''t be impor
It 'should re-download the module' {
Init
Install-WhiskeyPowerShellModule -Name 'Zip' -Version $latestZip.Version
$moduleManifest = Join-Path -Path $testRoot -ChildPath ('{0}\Zip\{1}\Zip.psd1' -f $PSModulesDirectoryName,$latestZip.Version) -Resolve
$moduleManifest = Join-Path -Path $testRoot -ChildPath ('{0}\Zip\{1}\Zip.psd1' -f $TestPSModulesDirectoryName,$latestZip.Version) -Resolve
'@{ }' | Set-Content -Path $moduleManifest
{ Test-ModuleManifest -Path $moduleManifest -ErrorAction Ignore } | Should -Throw
$Global:Error.Clear()
Expand Down
Loading