forked from psget/psget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PsGet.tests.ps1
358 lines (299 loc) · 17.1 KB
/
PsGet.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#Set-StrictMode -Version Latest
$here = (Split-Path -parent $MyInvocation.MyCommand.Definition)
Import-Module ($here + "\PsGet\PsGet.psm1") -Force
$verbose = $false;
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;
}
#Import Custom Pester assertions
. "$here\PsGetPesterAssertionExtensions.ps1"
. "$here\HelperFunctionsForTesting.ps1"
Describe "Install-Module" {
$UserModulePath = Get-UserModulePath
Context "When modules are installed from Web URL Source" {
It "Should support something simple" {
install-module -ModuleUrl https://github.com/psget/psget/raw/master/TestModules/HelloWorld.psm1 -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support urls that command cannot guess module name" {
install-module -ModuleUrl http://bit.ly/ggXoOR -ModuleName "HelloWorld" -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support zipped modules" {
install-module -ModuleUrl https://github.com/psget/psget/raw/master/TestModules/HelloWorld.zip -Verbose:$verbose
"HelloWorldZip" | Should BeInstalled
drop-module "HelloWorldZip"
}
It "Should support zipped in child folder modules" {
install-module -ModuleUrl https://github.com/psget/psget/raw/master/TestModules/HelloWorldInChildFolder.zip -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support alternate install destination" {
install-module -ModuleUrl https://github.com/psget/psget/raw/master/TestModules/HelloWorld.psm1 -Destination $Env:TEMP\Modules -Verbose:$verbose
if (-not (Test-Path -Path $Env:TEMP\Modules\HelloWorld\HelloWorld.psm1)) {
throw "Module was not installed to alternate destination"
}
Remove-Item -Path $Env:TEMP\Modules -Recurse -Force
}
}
Context "When modules installed from local source" {
It "Should support zipped with child modules" {
# The problem was with PSCX, they have many child modules
# And PsGet was loading one of child module instead.
# This test ensues that only main module is loaded
# Related to Issue #12
install-module -ModulePath $here\TestModules\HelloWorldFolderWithChildModules.zip -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support local PSM1 modules" {
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support installing local module directory of loose files" {
install-module -ModulePath $here\TestModules\HelloWorldFolder -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support local zipped modules" {
install-module -ModulePath $here\TestModules\HelloWorld.zip -Verbose:$verbose
"HelloWorldZip" | Should BeInstalled
drop-module "HelloWorldZip"
}
It "Should support zipped modules with a PSD1 manifest" {
install-module -ModulePath $here\TestModules\ManifestTestModule.zip -Verbose:$verbose
if (-not (Get-Command -Name Get-ManifestTestModuleName -Module ManifestTestModule)) {
throw "ManifestTestModule not installed"
}
drop-module ManifestTestModule
}
It "Should support local zipped in child folder modules" {
install-module -ModulePath $here\TestModules\HelloWorldInChildFolder.zip -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should support zipped modules with a PSD1 manifest in a child folder" {
install-module -ModulePath $here\TestModules\ManifestTestModuleInChildFolder.zip -Verbose:$verbose
if (-not (Get-Command -Name Get-ManifestTestModuleName -Module ManifestTestModule)) {
throw "ManifestTestModule not installed"
}
drop-module ManifestTestModule
}
It "Should support modules with install.ps1" {
install-module -ModulePath $here\TestModules\HelloWorldWithInstall.zip -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should not install module twice" {
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -Verbose:$verbose
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should not install module twice When ModuleName specified" {
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -ModuleName HelloWorld -Verbose:$verbose
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -ModuleName HelloWorld -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should install module twice When Update specified" {
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -Verbose:$verbose
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -Update -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
}
Context "When modules from centralized PsGet repository" {
It "Should install module from repo" {
install-module HelloWorld -DirectoryURL "https://github.com/psget/psget/raw/master/TestModules/Directory.xml" -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should update installed module" {
install-module HelloWorld -DirectoryURL "https://github.com/psget/psget/raw/master/TestModules/Directory.xml" -Verbose:$verbose
update-module HelloWorld -DirectoryURL "https://github.com/psget/psget/raw/master/TestModules/Directory.xml" -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
It "Should install zipped module from repo" {
install-module HelloWorldZip -DirectoryURL "https://github.com/psget/psget/raw/master/TestModules/Directory.xml" -Verbose:$verbose
"HelloWorldZip" | Should BeInstalled
drop-module "HelloWorldZip"
}
It "Should install module from directory url specified in global variable" {
$OriginalPsGetDirectoryUrl = $global:PsGetDirectoryUrl
try {
$global:PsGetDirectoryUrl = 'https://github.com/psget/psget/raw/master/TestModules/Directory.xml'
Install-Module -Module HelloWorld -Verbose:$verbose
"HelloWorld" | Should BeInstalled
} finally {
$global:PsGetDirectoryUrl = $OriginalPsGetDirectoryUrl
drop-module "HelloWorld"
}
}
It "Should retrieve information about module by ID" {
$retrieved = Get-PsGetModuleInfo HelloWorld -DirectoryUrl:"file://$here\TestModules\Directory.xml" -Verbose:$verbose
$retrieved | Should Not Be $null
$retrieved.Id | Should Be "HelloWorld"
}
It "Should retrieve information about module and wildcard" {
$retrieved = Get-PsGetModuleInfo Hello* -DirectoryUrl:"file://$here\TestModules\Directory.xml" -Verbose:$verbose
$retrieved | Should HaveCountOf 1
}
It "Should support value pipelining to Get-PsGetModuleInfo" {
$retrieved = 'HelloWorld' | Get-PsGetModuleInfo -DirectoryUrl:"file://$here\TestModules\Directory.xml" -Verbose:$verbose
$retrieved.Id | Should Be "HelloWorld"
}
It "Should support property pipelining to Get-PsGetModuleInfo" {
$retrieved = New-Object -TypeName PSObject -Property @{ ModuleName = 'HelloWorld' } | Get-PsGetModuleInfo -DirectoryUrl:"file://$here\TestModules\Directory.xml" -Verbose:$verbose
$retrieved.Id | Should Be "HelloWorld"
}
It "Should output objects from Get-PsGetModuleInfo that have properties matching parameters of Install-Module" {
$retrieved = Get-PsGetModuleInfo -ModuleName HelloWorld -DirectoryUrl:"file://$here\TestModules\Directory.xml" -Verbose:$verbose
$retrieved.Id | Should Be "HelloWorld"
$retrieved.ModuleUrl | Should Be "https://github.com/psget/psget/raw/master/TestModules/HelloWorld.psm1"
}
It "Should support piping from Get-PsGetModuleInfo to Install-Module" {
Get-PsGetModuleInfo -ModuleName HelloWorld -DirectoryUrl:"file://$here\TestModules\Directory.xml" -Verbose:$verbose |
Install-Module -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module "HelloWorld"
}
}
Context "After installation complete" {
It "Should not modify User or Machine permanent environment variables" {
#Use a unique path so it can be removed from the environment variable
$tempDir = Get-TempDir
$beforeModulePath = $env:PSModulePath
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -ModuleName HelloWorld -Destination $tempDir -Verbose:$verbose
"HelloWorld" | Should BeInstalled $tempDir
"HelloWorld" | Should Not BeInPSModulePath $tempDir
"HelloWorld" | Should Not BeGloballyImportable $tempDir
$env:PSModulePath | Should Be $beforeModulePath
drop-module "HelloWorld"
}
It "Should modify User or Machine permanent environment variable When using -PersistEnvironment switch" {
#Use a unique path so it can be removed from the environment variable
$tempDir = Get-TempDir
$beforeModulePath = $env:PSModulePath
try{
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -ModuleName HelloWorld -Destination $tempDir -PersistEnvironment -Verbose:$verbose
"HelloWorld" | Should BeInPSModulePath $tempDir
"HelloWorld" | Should BeInstalled $tempDir
"HelloWorld" | Should BeGloballyImportable $tempDir
#Because we are persisting the environment variable change, the before and after should be different
$env:PSModulePath | Should Not Be $beforeModulePath
drop-module "HelloWorld"
} finally {
Remove-PathFromPSModulePath -PathToRemove $tempDir -PersistEnvironment -Scope "Machine"
Remove-PathFromPSModulePath -PathToRemove $tempDir -PersistEnvironment -Scope "User"
}
#Just to verify we properly cleaned up
$env:PSModulePath | Should Be $beforeModulePath
}
It "Should install to user modules When PSModulePath has been prefixed" {
$DefaultUserPSModulePath = Get-UserModulePath
$DefaultSystemPSModulePath = $global:CommonGlobalModuleBasePath
$DefaultPSModulePath = $DefaultUserPSModulePath,$DefaultSystemPSModulePath -join ';'
$OriginalPSModulePath = $Env:PSModulePath
$OriginalDestinationModulePath = $PSGetDefaultDestinationModulePath
try {
$Env:PSModulePath = "$Env:ProgramFiles\TestPSModulePath;$DefaultPSModulePath"
install-module -ModulePath $here\TestModules\HelloWorld.psm1 -Verbose:$verbose -Global:$false
if (-not (Test-Path -Path $DefaultUserPSModulePath\HelloWorld\HelloWorld.psm1)) {
throw "Module was not installed to user module path"
}
Remove-Item -Path $DefaultUserPSModulePath\HelloWorld -Recurse -Force
} finally {
# restore paths
$Env:PSModulePath = $OriginalPSModulePath
$PSGetDefaultDestinationModulePath = $OriginalDestinationModulePath
}
}
}
Context "When requiring a specific module hash value" {
It "Should hash module in a folder" {
$Hash = Get-PsGetModuleHash -Path $here\TestModules\HelloWorldFolder
$Hash | Should Be 563E329AFF0785E4A2C3039EF7F60F9E2FA68888CE12EE38C1406BDDC09A87E1
}
It "Should install module matching the expected hash" {
Install-Module -ModulePath $here\TestModules\HelloWorldFolder\HelloWorld.psm1 -ModuleHash 563E329AFF0785E4A2C3039EF7F60F9E2FA68888CE12EE38C1406BDDC09A87E1 -Verbose:$verbose
"HelloWorld" | Should BeInstalled
drop-module HelloWorld
}
It "Should not install a module with a conflicting hash" {
try {
Install-Module -ModulePath $here\TestModules\HelloWorldFolder\HelloWorld.psm1 -ModuleHash AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA -Verbose:$verbose
} catch { $_ }
if (Test-Path $UserModulePath/HelloWorld/HelloWorld.psm1) {
throw "Module HelloWorld was installed but Should not have been installed."
}
drop-module HelloWorld
}
It "Should reinstall a module When the existing installation has a conflicting hash" {
# make sure it is installed but not imported
Install-Module -ModulePath $here\TestModules\HelloWorldFolder\HelloWorld.psm1 -ModuleHash 563E329AFF0785E4A2C3039EF7F60F9E2FA68888CE12EE38C1406BDDC09A87E1 -DoNotImport -Verbose:$verbose
# change the module so the hash is wrong
#Set-Content -Path $UserModulePath\HelloWorld\extrafile.txt -Value ExtraContent
Get-PSGetModuleHash -Path $here\TestModules\HelloWorldFolder
Get-PSGetModuleHash -Path $UserModulePath\HelloWorld
Install-Module -ModulePath $here\TestModules\HelloWorldFolder\HelloWorld.psm1 -ModuleHash 563E329AFF0785E4A2C3039EF7F60F9E2FA68888CE12EE38C1406BDDC09A87E1 -Verbose:$verbose
if ((Get-PSGetModuleHash -Path $UserModulePath\HelloWorld) -ne '563E329AFF0785E4A2C3039EF7F60F9E2FA68888CE12EE38C1406BDDC09A87E1') {
throw "Module HelloWorld was not reinstalled to fix the hash."
}
drop-module HelloWorld
}
}
Context "When installing binary modules" {
It "Should support zipped binary modules" {
# run this test out-of-process so the binary module can be removed without locking issues
& powershell -noprofile -command {
param ($here,$verbose)
Import-Module -Name "$here\PsGet\PsGet.psm1"
install-module -ModulePath $here\TestModules\TestBinaryModule.zip -Verbose:$verbose -Update
Import-Module -Name TestBinaryModule
if (-not (Get-Command -Name Get-Echo -Module TestBinaryModule)) {
throw "TestBinaryModule not installed"
}
} -args $here,$verbose
drop-module TestBinaryModule
}
}
Context "When installing Nuget" {
Context "Script modules"{
It "Should support installing the latest stable version of a custom Nuget package" {
install-module -NugetPackageId PsGetTest -NugetSource http://www.myget.org/F/psgettest -DoNotImport -Verbose:$verbose
"PsGetTest" | Should BeInstalled
drop-module PsGetTest
}
It "Should support installing a latest pre-release version of a custom Nuget package" {
install-module -NugetPackageId PsGetTest -PreRelease -NugetSource http://www.myget.org/F/psgettest -DoNotImport -Verbose:$verbose
"PsGetTest" | Should BeInstalled
drop-module PsGetTest
}
It "Should support installing a specific pre-release version of a custom Nuget package" {
install-module -NugetPackageId PsGetTest -PackageVersion 1.0.0-alpha -NugetSource http://www.myget.org/F/psgettest -DoNotImport -Verbose:$verbose
"PsGetTest" | Should BeInstalled
drop-module PsGetTest
}
}
Context "Binary Modules" {
It "Should support installing the latest version of a public Nuget package" {
Install-ModuleOutOfProcess -module 'mdbc' -FunctionNameToVerify 'Connect-Mdbc'
"mdbc" | Should BeInstalled
drop-module mdbc
}
It "Should support installing a specific version of a public Nuget package" {
# install-module -NugetPackageId mdbc -PackageVersion 1.0.6 -DoNotImport -Verbose:$verbose
Install-ModuleOutOfProcess -module 'mdbc' -FunctionNameToVerify 'Connect-Mdbc' -PackageVersion 1.0.6
"mdbc" | Should BeInstalled
drop-module mdbc
}
}
}
}