forked from psget/psget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPsGet.tests.ps1
116 lines (94 loc) · 4.82 KB
/
GetPsGet.tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
if(-not $pester) {
Write-Warning 'The tests for GetPsGet should be executed using the Run-Tests.ps1 script or Invoke-AllTests.cmd batch script'
exit -1;
}
$here = (Split-Path -parent $MyInvocation.MyCommand.Definition)
. "$here\HelperFunctionsForTesting.ps1"
function Get-GetPsGet {
Get-Content -Path $here\GetPsGet.ps1 | Out-String
}
# backup current PSModulePath before testing
$OriginalPSModulePath = $env:PSModulePath
# default PSModulePath is '{userpath};{systempath}'
$DefaultUserPSModulePath = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath WindowsPowerShell\Modules
Describe 'GetPsGet.ps1 installs the PsGet module' {
Context 'Installation target can be configured by environment variable ''$PsGetDestinationModulePath''' {
$PsGetDestinationModulePath = (Get-Item -Path 'TestDrive:\').FullName
$expectedPath = ConvertTo-CanonicalPath -Path "$PsGetDestinationModulePath\PsGet"
$env:PSModulePath = $DefaultPSModulePath
Mock Write-Host
Mock Write-Warning
Remove-Module PsGet -ErrorAction SilentlyContinue
Get-GetPsGet | Invoke-Expression
It 'installs PsGet to target path' {
Test-Path -Path $expectedPath\PsGet.psm1 | Should Be $true
}
It 'imports the module from target path' {
(Get-Command Install-Module).Module.ModuleBase | Should Be $expectedPath
}
Remove-Variable -Name PsGetDestinationModulePath
$env:PSModulePath = $OriginalPSModulePath
}
Context 'Installation target will be selected from PSModulePath if ''$PsGetDestinationModulePath'' is not defined' {
Remove-Variable -Name PsGetDestinationModulePath -ErrorAction SilentlyContinue
$testDrive = (Get-Item -Path 'TestDrive:').FullName
$pathA = "$testDrive\A"
$pathB = "$testDrive\B"
$env:PSModulePath = "$pathA;$pathB"
$expectedPath = ConvertTo-CanonicalPath -Path "$pathA\PsGet"
Mock Write-Host
Mock Write-Warning
Remove-Module PsGet -ErrorAction SilentlyContinue
Get-GetPsGet | Invoke-Expression
It 'installs PsGet to first path in PSModulePath' {
Test-Path -Path $expectedPath\PsGet.psm1 | Should Be $true
}
It 'imports the module from that path' {
(Get-Command Install-Module).Module.ModuleBase | Should Be $expectedPath
}
$env:PSModulePath = $OriginalPSModulePath
}
Context 'Installation selects always the default user module path if available in PSModulePath' {
Write-Warning 'This test is not completely isolated and (re-)install PsGet into the users default module path. This potentially new version should be no issue because new versions of PsGet are backward compatible and the executing user develops PsGet changes.'
Remove-Variable -Name PsGetDestinationModulePath -ErrorAction SilentlyContinue
$pathA = (Get-Item -Path 'TestDrive:\').FullName
$env:PSModulePath = "$pathA;$HOME\TestPSModulePath;$env:ProgramFiles\TestPSModulePath;$DefaultUserPSModulePath"
$expectedPath = ConvertTo-CanonicalPath -Path "$DefaultUserPSModulePath\PsGet"
Mock Write-Host
Mock Write-Warning
Remove-Module PsGet -ErrorAction SilentlyContinue
Get-GetPsGet | Invoke-Expression
It 'installs PsGet to first path in PSModulePath' {
Test-Path -Path $expectedPath\PsGet.psm1 | Should Be $true
}
It 'imports the module from that path' {
(Get-Command Install-Module).Module.ModuleBase | Should Be $expectedPath
}
$env:PSModulePath = $OriginalPSModulePath
}
Context 'Installation works with ErrorActionPreference = ''Stop'' and Set-StrictMode Latest' {
$PsGetDestinationModulePath = (Get-Item -Path 'TestDrive:\').FullName
$expectedPath = ConvertTo-CanonicalPath -Path "$PsGetDestinationModulePath\PsGet"
$env:PSModulePath = $DefaultPSModulePath
Mock Write-Host
Mock Write-Warning
Remove-Module PsGet -ErrorAction SilentlyContinue
powershell -noprofile -command {
param ($DownloadedScript, $Destination)
function Write-Host {}
function Write-Warning {}
$PsGetDestinationModulePath = $Destination
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$DownloadedScript | iex
} -args @(Get-GetPsGet; $PsGetDestinationModulePath)
It 'Should support ErrorActionPreference = ''Stop'' and Set-StrictMode Latest' {
-not $? | Should be $false
}
It 'installs PsGet to target path' {
Test-Path -Path $expectedPath\PsGet.psm1 | Should Be $true
}
Remove-Variable -Name PsGetDestinationModulePath
$env:PSModulePath = $OriginalPSModulePath
}
}