-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathGetInstalledPSResource.Tests.ps1
More file actions
201 lines (168 loc) · 10.3 KB
/
GetInstalledPSResource.Tests.ps1
File metadata and controls
201 lines (168 loc) · 10.3 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$ProgressPreference = "SilentlyContinue"
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
Describe 'Test Get-InstalledPSResource for Module' -tags 'CI' {
BeforeAll{
$PSGalleryName = Get-PSGalleryName
$testModuleName = "test_module"
$testScriptName = "test_script"
$TestEmptyDirectoryPath = [System.IO.Path]::Combine($env:TEMP,'EmptyDir')
Get-NewPSResourceRepositoryFile
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Version "1.0"
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Version "3.0"
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Version "5.0"
Install-PSResource -Name $testScriptName -Repository $PSGalleryName -TrustRepository
$null = New-Item -Path $TestEmptyDirectoryPath -ItemType 'Directory'
}
AfterAll {
Uninstall-PSResource -Name $testModuleName -Version "*" -ErrorAction SilentlyContinue
Uninstall-PSResource -Name $testScriptName -Version "*" -ErrorAction SilentlyContinue
Get-RevertPSResourceRepositoryFile
if (Test-Path -Path $TestEmptyDirectoryPath -PathType 'Container') {
Remove-Item -Path $TestEmptyDirectoryPath -Recurse -Force
}
}
It "Get resources without any parameter values" {
$pkgs = Get-InstalledPSResource
$pkgs.Count | Should -BeGreaterThan 1
}
It "Get specific module resource by name" {
$pkg = Get-InstalledPSResource -Name $testModuleName
$pkg.Name | Should -Contain $testModuleName
$pkg.Type | Should -Contain "Module"
}
It "Get specific script resource by name" {
$pkg = Get-InstalledPSResource -Name $testScriptName
$pkg.Name | Should -Be $testScriptName
$pkg.Type | Should -Contain "Script"
}
It "Get resource when given Name to <Reason> <Version>" -TestCases @(
@{Name="*est_modul*"; ExpectedName=$testModuleName; Reason="validate name, with wildcard at beginning and end of name: *est_modul*"},
@{Name="test_mod*"; ExpectedName=$testModuleName; Reason="validate name, with wildcard at end of name: test_mod*"},
@{Name="*est_module"; ExpectedName=$testModuleName; Reason="validate name, with wildcard at beginning of name: *est_module"},
@{Name="tes*ule"; ExpectedName=$testModuleName; Reason="validate name, with wildcard in middle of name: tes*ule"}
) {
param($Version, $ExpectedVersion)
$pkgs = Get-InstalledPSResource -Name $Name
$pkgs.Name | Should -Contain $testModuleName
}
$testCases = [array](
@{Version="[1.0.0.0]"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match"},
@{Version="1.0.0.0"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersion=@("3.0.0.0"); Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersion=@("5.0.0.0", "3.0.0.0"); Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, minimum version inclusive"},
@{Version="(,5.0.0.0)"; ExpectedVersion=@("3.0.0.0", "1.0.0.0"); Reason="validate version, maximum version exclusive"},
@{Version="(,5.0.0.0]"; ExpectedVersion=@("5.0.0.0", "3.0.0.0", "1.0.0.0"); Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 5.0.0.0)"; ExpectedVersion=@("3.0.0.0", "1.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
)
It "Get resource when given Name to <Reason> <Version>" -TestCases $testCases {
param($Version, $ExpectedVersion)
$pkgs = Get-InstalledPSResource -Name $testModuleName -Version $Version
$pkgs.Name | Should -Contain $testModuleName
$pkgs.Version | Should -Be $ExpectedVersion
}
It "Throw invalid version error when passing incorrectly formatted version such as <Description>" -TestCases @(
@{Version='[1.*.0]'; Description="version with wildcard in middle"},
@{Version='[*.0.0.0]'; Description="version with wildcard at start"},
@{Version='[1.*.0.0]'; Description="version with wildcard at second digit"},
@{Version='[1.0.*.0]'; Description="version with wildcard at third digit"},
@{Version='[1.0.0.*'; Description="version with wildcard at end"},
@{Version='[1..0.0]'; Description="version with missing digit in middle"},
@{Version='[1.0.0.]'; Description="version with missing digit at end"},
@{Version='[1.0.0.0.0]'; Description="version with more than 4 digits"}
) {
param($Version, $Description)
$res = $null
try {
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -ErrorAction Ignore
}
catch {}
$res | Should -BeNullOrEmpty
}
# These versions technically parse into proper NuGet versions, but will not return the version expected
It "Does not return resource when passing incorrectly formatted version such as <Description>, does not throw error" -TestCases @(
@{Version='(1.0.0.0)'; Description="exclusive version (8.1.0.0)"},
@{Version='[1-0-0-0]'; Description="version formatted with invalid delimiter"}
) {
param($Version, $Description)
$res = $null
try {
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -ErrorAction Ignore
}
catch {}
$res | Should -BeNullOrEmpty
}
It "Get resources when given Name, and Version is '*'" {
$pkgs = Get-InstalledPSResource -Name $testModuleName -Version "*"
$pkgs.Count | Should -BeGreaterOrEqual 2
}
It "Get prerelease version module when version with correct prerelease label is specified" {
Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName -Version "5.2.5" -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$res = Get-InstalledPSResource -Name $testModuleName -Version "5.2.5-alpha001"
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.2.5"
$res.Prerelease | Should -Be "alpha001"
}
It "Get prerelease version script when version with correct prerelease label is specified" {
Install-PSResource -Name $testScriptName -Version "3.0.0-alpha" -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource -Name $testScriptName -Version "3.0.0" -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$res = Get-InstalledPSResource -Name $testScriptName -Version "3.0.0-alpha"
$res.Name | Should -Be $testScriptName
$res.Version | Should -Be "3.0.0"
$res.Prerelease | Should -Be "alpha"
}
It "Throw package not found error when searching for module that does not exist" {
Get-InstalledPSResource -Name "DoesNotExist" -ErrorVariable err -ErrorAction SilentlyContinue
$err[0].FullyQualifiedErrorId | Should -BeExactly "InstalledPackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.GetInstalledPSResourceCommand"
}
It "Get definition for alias 'Get-PSResource'" {
(Get-Alias Get-PSResource).Definition | Should -BeExactly 'Get-InstalledPSResource'
}
It "Get definition for alias 'gres'" {
(Get-Alias gres).Definition | Should -BeExactly 'Get-InstalledPSResource'
}
It "Should not throw on ErrorAction ignore when no subdirectories are found" {
{ Get-InstalledPSResource -Path $TestEmptyDirectoryPath -ErrorAction 'Ignore' } | Should -Not -Throw
}
# Windows only
It "Get resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
$pkg = Get-InstalledPSResource -Name $testModuleName -Scope CurrentUser
$pkg[0].Name | Should -Be $testModuleName
$pkg[0].InstalledLocation.ToString().Contains("Documents") | Should -Be $true
}
# Windows only
It "Get resource under AllUsers scope when module is installed under CurrentUser - Windows only" -Skip:(!(Get-IsWindows)) {
$pkg = Get-InstalledPSResource -Name $testModuleName -Scope AllUsers
$pkg | Should -BeNullOrEmpty
}
# Windows only
It "Get resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope AllUsers
$pkg = Get-InstalledPSResource -Name $testModuleName -Scope AllUsers
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true
}
# Windows only
It "Get resource under CurrentUser scope when module is installed under AllUsers - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
Uninstall-PSResource -Name $testModuleName -Version "*"
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope AllUsers
$pkg = Get-InstalledPSResource -Name $testModuleName -Scope CurrentUser
$pkg | Should -BeNullOrEmpty
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Get resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name "testmodule99" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
$pkg = Get-InstalledPSResource "testmodule99" -Scope CurrentUser
$pkg.Name | Should -contain "testmodule99"
$pkg.InstalledLocation.ToString().Contains("/.local") | Should -Be $true
}
}