-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathSavePSResourceV2.Tests.ps1
More file actions
233 lines (199 loc) · 12.5 KB
/
SavePSResourceV2.Tests.ps1
File metadata and controls
233 lines (199 loc) · 12.5 KB
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$ProgressPreference = "SilentlyContinue"
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
Describe 'Test HTTP Save-PSResource for V2 Server Protocol' -tags 'CI' {
BeforeAll {
$PSGalleryName = Get-PSGalleryName
$testModuleName = "test_module"
$testScriptName = "test_script"
$testModuleName2 = "testmodule99"
$PackageManagement = "PackageManagement"
$testModuleNameWithLicense = "ModuleRequireLicenseAcceptance"
Get-NewPSResourceRepositoryFile
$SaveDir = Join-Path $TestDrive 'SavedResources'
New-Item -Item Directory $SaveDir -Force
}
AfterEach {
# Delete contents of save directory
Remove-Item -Path (Join-Path $SaveDir '*') -Recurse -Force -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
It "Save specific module resource by name" {
Save-PSResource -Name $testModuleName -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
}
It "Save specific script resource by name" {
Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_script.ps1"
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
}
It "Save multiple resources by name" {
$pkgNames = @($testModuleName, $testModuleName2)
Save-PSResource -Name $pkgNames -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq $testModuleName -or $_.Name -eq $testModuleName2 }
$pkgDirs | Should -HaveCount 2
(Get-ChildItem $pkgDirs[0].FullName) | Should -HaveCount 1
(Get-ChildItem $pkgDirs[1].FullName) | Should -HaveCount 1
}
It "Should not save resource given nonexistent name" {
Save-PSResource -Name NonExistentModule -Repository $PSGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "NonExistentModule"
$pkgDir.Name | Should -BeNullOrEmpty
}
It "Not Save module with Name containing wildcard" {
Save-PSResource -Name "TestModule*" -Repository $PSGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "NameContainsWildcard,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
}
It "Should save resource given name and exact version" {
Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "1.0.0.0"
}
It "Should save resource given name and version '3.*'" {
Save-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "3.0.0.0"
}
It "Should save resource given name and exact version with bracket syntax" {
Save-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "1.0.0.0"
}
It "Should save resource given name and exact range inclusive [1.0.0, 3.0.0]" {
Save-PSResource -Name $testModuleName -Version "[1.0.0, 3.0.0]" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "3.0.0.0"
}
It "Should save resource given name and exact range exclusive (1.0.0, 5.0.0)" {
Save-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "3.0.0.0"
}
It "Should not save resource with incorrectly formatted version such as exclusive version (1.0.0.0)" {
$Version="(1.0.0.0)"
try {
Save-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -Path $SaveDir -ErrorAction SilentlyContinue -TrustRepository
}
catch
{}
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -BeNullOrEmpty
$Error.Count | Should -BeGreaterThan 0
$Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
}
It "Save resource with latest (including prerelease) version given Prerelease parameter" {
Save-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "5.2.5"
}
It "Save a module with a dependency" {
Save-PSResource -Name "TestModuleWithDependencyE" -Version "1.0.0.0" -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "TestModuleWithDependencyE" -or $_.Name -eq "TestModuleWithDependencyC" -or $_.Name -eq "TestModuleWithDependencyB" -or $_.Name -eq "TestModuleWithDependencyD"}
$pkgDirs.Count | Should -BeGreaterThan 1
(Get-ChildItem $pkgDirs[0].FullName).Count | Should -BeGreaterThan 0
(Get-ChildItem $pkgDirs[1].FullName).Count | Should -BeGreaterThan 0
(Get-ChildItem $pkgDirs[2].FullName).Count | Should -BeGreaterThan 0
(Get-ChildItem $pkgDirs[3].FullName).Count | Should -BeGreaterThan 0
}
It "Save a module with a dependency and skip saving the dependency" {
Save-PSResource -Name "TestModuleWithDependencyE" -Version "1.0.0.0" -SkipDependencyCheck -Repository $PSGalleryName -Path $SaveDir -TrustRepository
$pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq "TestModuleWithDependencyE"}
$pkgDirs | Should -HaveCount 1
(Get-ChildItem $pkgDirs[0].FullName) | Should -HaveCount 1
}
### TODO: this is broken because the "Prerelease" parameter is a boolean, but the type from
### the input object is of type string (ie "true").
It "Save PSResourceInfo object piped in for prerelease version object" -Pending {
Find-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $PSGalleryName | Save-PSResource -Path $SaveDir -TrustRepository -Verbose
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem -Path $pkgDir.FullName) | Should -HaveCount 1
}
It "Save module as a nupkg" {
Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -AsNupkg -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_module.1.0.0.nupkg"
$pkgDir | Should -Not -BeNullOrEmpty
}
It "Save module and include XML metadata file" {
Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -IncludeXml -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $testModuleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "1.0.0.0"
$xmlFile = Get-ChildItem -Path $pkgDirVersion.FullName | Where-Object Name -EQ "PSGetModuleInfo.xml"
$xmlFile | Should -Not -BeNullOrEmpty
}
It "Save module using -PassThru" {
$res = Save-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -Path $SaveDir -PassThru -TrustRepository
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "1.0.0.0"
}
It "Save script without using -IncludeXML" {
{ Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository } | Should -Not -Throw
$SavedScriptFile = Join-Path -Path $SaveDir -ChildPath "$testScriptName.ps1"
Test-Path -Path $SavedScriptFile -PathType 'Leaf' | Should -BeTrue
}
It "Save script using -IncludeXML" {
Save-PSResource -Name $testScriptName -Repository $PSGalleryName -Path $SaveDir -TrustRepository -IncludeXml
$scriptXML = $testScriptName + "_InstalledScriptInfo.xml"
$savedScriptFile = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ "test_script.ps1"
$savedScriptXML = Get-ChildItem -Path $SaveDir | Where-Object Name -EQ $scriptXML
$savedScriptFile | Should -Not -BeNullOrEmpty
(Get-ChildItem $savedScriptFile.FullName) | Should -HaveCount 1
$savedScriptXML | Should -Not -BeNullOrEmpty
(Get-ChildItem $savedScriptXML.FullName) | Should -HaveCount 1
}
# Save module that is not authenticode signed
# Should FAIL to save the module
It "Save module that is not authenticode signed" -Skip:(!(Get-IsWindows)) {
Save-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -Contain "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
$err[1].FullyQualifiedErrorId | Should -Contain "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
}
# Save resource that requires license
It "Save resource that requires accept license with -AcceptLicense flag" {
$pkg = Save-PSResource -Repository $PSGalleryName -TrustRepository -Path $SaveDir -Name $testModuleNameWithLicense -AcceptLicense -PassThru
$pkg.Name | Should -Be $testModuleNameWithLicense
$pkg.Version | Should -Be "2.0"
}
It "Not save module that has path separator in name" {
Save-PSResource -Name "/$testModuleName" -Repository $PSGalleryName -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly 'ErrorFilteringNamesForUnsupportedWildcards,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource'
}
It "Save resource and dependency, while skipping dependency that is listed as external module dependency" {
$testParentModule = "test_module_ext_dep"
$requiredDependency = "test_module10"
$res = Save-PSresource -Name "test_module_ext_dep" -Repository $PSGalleryName -Path $SaveDir -TrustRepository -PassThru
$res.Name | Should -Contain $testParentModule
$res.Name | Should -Contain $requiredDependency
}
It "Save resource and dependency, as .nupkg, while skipping dependency that is listed as external module dependency" {
$testParentModule = "test_module_ext_dep"
$requiredDependency = "test_module10"
$res = Save-PSresource -Name "test_module_ext_dep" -Repository $PSGalleryName -AsNupkg -Path $SaveDir -TrustRepository -PassThru
$res.Name | Should -Contain $testParentModule
$res.Name | Should -Contain $requiredDependency
}
}