-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathSavePSResourceLocal.Tests.ps1
More file actions
212 lines (179 loc) · 10.2 KB
/
SavePSResourceLocal.Tests.ps1
File metadata and controls
212 lines (179 loc) · 10.2 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$ProgressPreference = "SilentlyContinue"
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
Describe 'Test Save-PSResource for local repositories' -tags 'CI' {
BeforeAll {
$localRepo = "psgettestlocal"
$localUNCRepo = "psgettestlocal3"
$moduleName = "test_local_mod"
$moduleName2 = "test_local_mod2"
$moduleName3 = "testModule99"
Get-NewPSResourceRepositoryFile
Register-LocalRepos
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo "1.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo "3.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName $localRepo "5.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $moduleName2 $localRepo "5.0.0"
$SaveDir = Join-Path $TestDrive 'SavedResources'
$saveItem = New-Item -Item Directory $SaveDir -Force
$SaveDirUNC = $saveItem.FullName -Replace '^(.):', '\\localhost\$1$'
}
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 $moduleName -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
}
It "Save specific module resource by name from UNC repository" {
Save-PSResource -Name $moduleName -Repository $localUNCRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
}
It "Save specific module resource by name to a UNC path" {
Save-PSResource -Name $moduleName -Repository $localRepo -Path $SaveDirUNC -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDirUNC | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
}
It "Save specific module resource by name from UNC repository to a UNC path" {
Save-PSResource -Name $moduleName -Repository $localUNCRepo -Path $SaveDirUNC -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDirUNC | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem $pkgDir.FullName) | Should -HaveCount 1
}
It "Should save resource given no -Path parameter" {
Push-Location $SaveDir
Save-PSResource -Name $moduleName -Repository $localRepo -TrustRepository
$pkgDir = Get-ChildItem $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "5.0.0"
Pop-Location
}
It "Save multiple resources by name" {
$pkgNames = @($moduleName, $moduleName2)
Save-PSResource -Name $pkgNames -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDirs = Get-ChildItem -Path $SaveDir | Where-Object { $_.Name -eq $moduleName -or $_.Name -eq $moduleName2 }
$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 $localRepo -Path $SaveDir -ErrorVariable err -ErrorAction SilentlyContinue -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "NonExistentModule"
$pkgDir.Name | Should -BeNullOrEmpty
}
It "Should save resource given name and exact version" {
Save-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "1.0.0"
}
It "Should save resource given name and version '3.*'" {
Save-PSResource -Name $moduleName -Version "3.*" -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "3.0.0"
}
It "Should save resource given name and exact version with bracket syntax" {
Save-PSResource -Name $moduleName -Version "[1.0.0]" -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "1.0.0"
}
It "Should save resource given name and exact range inclusive [1.0.0, 3.0.0]" {
Save-PSResource -Name $moduleName -Version "[1.0.0, 3.0.0]" -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "3.0.0"
}
It "Should save resource given name and exact range exclusive (1.0.0, 5.0.0)" {
Save-PSResource -Name $moduleName -Version "(1.0.0, 5.0.0)" -Repository $localRepo -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "3.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 $moduleName -Version $Version -Repository $localRepo -Path $SaveDir -ErrorAction SilentlyContinue -TrustRepository
}
catch
{}
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -BeNullOrEmpty
$Error.Count | Should -BeGreaterThan 0
$Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
}
It "Save PSResourceInfo object piped in for prerelease version object" {
Find-PSResource -Name $moduleName -Version "5.0.0" -Repository $localRepo | Save-PSResource -Path $SaveDir -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
(Get-ChildItem -Path $pkgDir.FullName) | Should -HaveCount 1
}
It "Save module as a nupkg" {
Save-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo -Path $SaveDir -AsNupkg -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "$moduleName.1.0.0.nupkg"
$pkgDir | Should -Not -BeNullOrEmpty
}
It "Save module, should search through all repositories and only install from the first repo containing the package" {
Save-PSResource -Name $moduleName3 -Version "0.0.93" -Path $SaveDir -TrustRepository -ErrorVariable ev
$ev | Should -BeNullOrEmpty
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq "$moduleName3"
$pkgDir | Should -Not -BeNullOrEmpty
}
It "Save module and include XML metadata file" {
Save-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo -Path $SaveDir -IncludeXml -TrustRepository
$pkgDir = Get-ChildItem -Path $SaveDir | Where-Object Name -eq $moduleName
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDirVersion = Get-ChildItem -Path $pkgDir.FullName
$pkgDirVersion.Name | Should -Be "1.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 $moduleName -Version "1.0.0" -Repository $localRepo -Path $SaveDir -PassThru -TrustRepository
$res.Name | Should -Be $moduleName
$res.Version | Should -Be "1.0.0"
}
It "Save module via InputObject by piping from Find-PSResource" {
$modules = Find-PSResource -Name "*" -Repository $localRepo
$modules.Count | Should -BeGreaterThan 1
Save-PSResource -Path $SaveDir -TrustRepository -InputObject $modules
$pkgDir = Get-ChildItem -Path $SaveDir
$pkgDir | Should -Not -BeNullOrEmpty
$pkgDir.Count | Should -BeGreaterThan 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)) {
$res = Save-PSResource -Name $moduleName -Version "5.0.0" -AuthenticodeCheck -Repository $localRepo -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue -ErrorVariable err -PassThru
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -BeNullOrEmpty
$err[0].FullyQualifiedErrorId | Should -Contain "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
$err[1].FullyQualifiedErrorId | Should -Contain "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SavePSResource"
}
It "Save module using -Quiet" {
$res = Save-PSResource -Name $moduleName -Version "1.0.0" -Repository $localRepo -Path $SaveDir -PassThru -TrustRepository -Quiet
$res.Name | Should -Be $moduleName
$res.Version | Should -Be "1.0.0"
}
It "Get definition for alias 'svres'" {
(Get-Alias svres).Definition | Should -BeExactly 'Save-PSResource'
}
}