|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +$ProgressPreference = "SilentlyContinue" |
| 5 | +$modPath = "$psscriptroot/../PSGetTestUtils.psm1" |
| 6 | +Import-Module $modPath -Force -Verbose |
| 7 | + |
| 8 | +Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' { |
| 9 | + |
| 10 | + BeforeAll { |
| 11 | + $testModuleName = "test_local_mod" |
| 12 | + $testModuleName2 = "test_local_mod2" |
| 13 | + $testScriptName = "test_ado_script" |
| 14 | + $ADORepoName = "PSGetTestingPublicFeed" |
| 15 | + $ADORepoUri = "https://pkgs.dev.azure.com/powershell/PowerShell/_packaging/psresourceget-public-test-ci/nuget/v2" |
| 16 | + Get-NewPSResourceRepositoryFile |
| 17 | + Register-PSResourceRepository -Name $ADORepoName -Uri $ADORepoUri |
| 18 | + } |
| 19 | + |
| 20 | + AfterEach { |
| 21 | + Uninstall-PSResource $testModuleName, $testModuleName2, $testScriptName -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue |
| 22 | + } |
| 23 | + |
| 24 | + AfterAll { |
| 25 | + Get-RevertPSResourceRepositoryFile |
| 26 | + } |
| 27 | + |
| 28 | + $testCases = @{Name="*"; ErrorId="NameContainsWildcard"}, |
| 29 | + @{Name="Test_local_m*"; ErrorId="NameContainsWildcard"}, |
| 30 | + @{Name="Test?local","Test[local"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"} |
| 31 | + |
| 32 | + It "Should not install resource with wildcard in name" -TestCases $testCases { |
| 33 | + param($Name, $ErrorId) |
| 34 | + Install-PSResource -Name $Name -Repository $ADORepoName -ErrorVariable err -ErrorAction SilentlyContinue |
| 35 | + $err.Count | Should -BeGreaterThan 0 |
| 36 | + $err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" |
| 37 | + $res = Get-InstalledPSResource $testModuleName |
| 38 | + $res | Should -BeNullOrEmpty |
| 39 | + } |
| 40 | + |
| 41 | + It "Install specific module resource by name" { |
| 42 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository |
| 43 | + $pkg = Get-InstalledPSResource $testModuleName |
| 44 | + $pkg.Name | Should -Be $testModuleName |
| 45 | + $pkg.Version | Should -Be "5.0.0" |
| 46 | + } |
| 47 | + |
| 48 | + It "Install specific script resource by name" { |
| 49 | + Install-PSResource -Name $testScriptName -Repository $ADORepoName -TrustRepository |
| 50 | + $pkg = Get-InstalledPSResource $testScriptName |
| 51 | + $pkg.Name | Should -Be $testScriptName |
| 52 | + $pkg.Version | Should -Be "1.0.0" |
| 53 | + } |
| 54 | + |
| 55 | + It "Install multiple resources by name" { |
| 56 | + $pkgNames = @($testModuleName, $testModuleName2) |
| 57 | + Install-PSResource -Name $pkgNames -Repository $ADORepoName -TrustRepository |
| 58 | + $pkg = Get-InstalledPSResource $pkgNames |
| 59 | + $pkg.Name | Should -Be $pkgNames |
| 60 | + } |
| 61 | + |
| 62 | + It "Should not install resource given nonexistant name" { |
| 63 | + Install-PSResource -Name "NonExistantModule" -Repository $ADORepoName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue |
| 64 | + $pkg = Get-InstalledPSResource "NonExistantModule" |
| 65 | + $pkg | Should -BeNullOrEmpty |
| 66 | + $err.Count | Should -BeGreaterThan 0 |
| 67 | + $err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" |
| 68 | + } |
| 69 | + |
| 70 | + # Do some version testing, but Find-PSResource should be doing thorough testing |
| 71 | + It "Should install resource given name and exact version" { |
| 72 | + Install-PSResource -Name $testModuleName -Version "1.0.0" -Repository $ADORepoName -TrustRepository |
| 73 | + $pkg = Get-InstalledPSResource $testModuleName |
| 74 | + $pkg.Name | Should -Be $testModuleName |
| 75 | + $pkg.Version | Should -Be "1.0.0" |
| 76 | + } |
| 77 | + |
| 78 | + It "Should install resource given name and exact version with bracket syntax" { |
| 79 | + Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $ADORepoName -TrustRepository |
| 80 | + $pkg = Get-InstalledPSResource $testModuleName |
| 81 | + $pkg.Name | Should -Be $testModuleName |
| 82 | + $pkg.Version | Should -Be "1.0.0" |
| 83 | + } |
| 84 | + |
| 85 | + It "Should install resource given name and exact range inclusive [1.0.0, 5.0.0]" { |
| 86 | + Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $ADORepoName -TrustRepository |
| 87 | + $pkg = Get-InstalledPSResource $testModuleName |
| 88 | + $pkg.Name | Should -Be $testModuleName |
| 89 | + $pkg.Version | Should -Be "5.0.0" |
| 90 | + } |
| 91 | + |
| 92 | + It "Should install resource given name and exact range exclusive (1.0.0, 5.0.0)" { |
| 93 | + Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $ADORepoName -TrustRepository |
| 94 | + $pkg = Get-InstalledPSResource $testModuleName |
| 95 | + $pkg.Name | Should -Be $testModuleName |
| 96 | + $pkg.Version | Should -Be "3.0.0" |
| 97 | + } |
| 98 | + |
| 99 | + # TODO: Update this test and others like it that use try/catch blocks instead of Should -Throw |
| 100 | + It "Should not install resource with incorrectly formatted version such as exclusive version (1.0.0.0)" { |
| 101 | + $Version = "(1.0.0.0)" |
| 102 | + try { |
| 103 | + Install-PSResource -Name $testModuleName -Version $Version -Repository $ADORepoName -TrustRepository -ErrorAction SilentlyContinue |
| 104 | + } |
| 105 | + catch |
| 106 | + {} |
| 107 | + $Error[0].FullyQualifiedErrorId | Should -be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource" |
| 108 | + |
| 109 | + $res = Get-InstalledPSResource $testModuleName |
| 110 | + $res | Should -BeNullOrEmpty |
| 111 | + } |
| 112 | + |
| 113 | + It "Install resource when given Name, Version '*', should install the latest version" { |
| 114 | + Install-PSResource -Name $testModuleName -Version "*" -Repository $ADORepoName -TrustRepository |
| 115 | + $pkg = Get-InstalledPSResource $testModuleName |
| 116 | + $pkg.Name | Should -Be $testModuleName |
| 117 | + $pkg.Version | Should -Be "5.0.0" |
| 118 | + } |
| 119 | + |
| 120 | + It "Install resource with latest (including prerelease) version given Prerelease parameter" { |
| 121 | + Install-PSResource -Name $testModuleName -Prerelease -Repository $ADORepoName -TrustRepository |
| 122 | + $pkg = Get-InstalledPSResource $testModuleName |
| 123 | + $pkg.Name | Should -Be $testModuleName |
| 124 | + $pkg.Version | Should -Be "5.2.5" |
| 125 | + $pkg.Prerelease | Should -Be "alpha001" |
| 126 | + } |
| 127 | + |
| 128 | + It "Install resource via InputObject by piping from Find-PSresource" { |
| 129 | + Find-PSResource -Name $testModuleName -Repository $ADORepoName | Install-PSResource -TrustRepository |
| 130 | + $pkg = Get-InstalledPSResource $testModuleName |
| 131 | + $pkg.Name | Should -Be $testModuleName |
| 132 | + $pkg.Version | Should -Be "5.0.0" |
| 133 | + } |
| 134 | + |
| 135 | + It "Install resource with companyname and repository source location and validate properties" { |
| 136 | + Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $ADORepoName -TrustRepository |
| 137 | + $pkg = Get-InstalledPSResource $testModuleName |
| 138 | + $pkg.Version | Should -Be "5.2.5" |
| 139 | + $pkg.Prerelease | Should -Be "alpha001" |
| 140 | + |
| 141 | + $pkg.CompanyName | Should -Be "None" |
| 142 | + $pkg.RepositorySourceLocation | Should -Be $ADORepoUri |
| 143 | + } |
| 144 | + |
| 145 | + # Windows only |
| 146 | + It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) { |
| 147 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -Scope CurrentUser |
| 148 | + $pkg = Get-InstalledPSResource $testModuleName |
| 149 | + $pkg.Name | Should -Be $testModuleName |
| 150 | + $pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true |
| 151 | + } |
| 152 | + |
| 153 | + # Windows only |
| 154 | + It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) { |
| 155 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -Scope AllUsers -Verbose |
| 156 | + $pkg = Get-InstalledPSResource $testModuleName -Scope AllUsers |
| 157 | + $pkg.Name | Should -Be $testModuleName |
| 158 | + $pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true |
| 159 | + } |
| 160 | + |
| 161 | + # Windows only |
| 162 | + It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) { |
| 163 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository |
| 164 | + $pkg = Get-InstalledPSResource $testModuleName |
| 165 | + $pkg.Name | Should -Be $testModuleName |
| 166 | + $pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true |
| 167 | + } |
| 168 | + |
| 169 | + # Unix only |
| 170 | + # Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules' |
| 171 | + It "Install resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) { |
| 172 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -Scope CurrentUser |
| 173 | + $pkg = Get-InstalledPSResource $testModuleName |
| 174 | + $pkg.Name | Should -Be $testModuleName |
| 175 | + $pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true |
| 176 | + } |
| 177 | + |
| 178 | + # Unix only |
| 179 | + # Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules' |
| 180 | + It "Install resource under no specified scope - Unix only" -Skip:(Get-IsWindows) { |
| 181 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository |
| 182 | + $pkg = Get-InstalledPSResource $testModuleName |
| 183 | + $pkg.Name | Should -Be $testModuleName |
| 184 | + $pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true |
| 185 | + } |
| 186 | + |
| 187 | + It "Should not install resource that is already installed" { |
| 188 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository |
| 189 | + $pkg = Get-InstalledPSResource $testModuleName |
| 190 | + $pkg.Name | Should -Be $testModuleName |
| 191 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -WarningVariable WarningVar -warningaction SilentlyContinue |
| 192 | + $WarningVar | Should -Not -BeNullOrEmpty |
| 193 | + } |
| 194 | + |
| 195 | + It "Reinstall resource that is already installed with -Reinstall parameter" { |
| 196 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository |
| 197 | + $pkg = Get-InstalledPSResource $testModuleName |
| 198 | + $pkg.Name | Should -Be $testModuleName |
| 199 | + $pkg.Version | Should -Be "5.0.0" |
| 200 | + Install-PSResource -Name $testModuleName -Repository $ADORepoName -Reinstall -TrustRepository |
| 201 | + $pkg = Get-InstalledPSResource $testModuleName |
| 202 | + $pkg.Name | Should -Be $testModuleName |
| 203 | + $pkg.Version | Should -Be "5.0.0" |
| 204 | + } |
| 205 | + |
| 206 | + It "Install PSResourceInfo object piped in" { |
| 207 | + Find-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $ADORepoName | Install-PSResource -TrustRepository |
| 208 | + $res = Get-InstalledPSResource -Name $testModuleName |
| 209 | + $res.Name | Should -Be $testModuleName |
| 210 | + $res.Version | Should -Be "1.0.0" |
| 211 | + } |
| 212 | + |
| 213 | + It "Install module using -PassThru" { |
| 214 | + $res = Install-PSResource -Name $testModuleName -Repository $ADORepoName -PassThru -TrustRepository |
| 215 | + $res.Name | Should -Contain $testModuleName |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +Describe 'Test Install-PSResource for V3Server scenarios' -tags 'ManualValidationOnly' { |
| 220 | + |
| 221 | + BeforeAll { |
| 222 | + $testModuleName = "TestModule" |
| 223 | + $testModuleName2 = "testModuleWithlicense" |
| 224 | + Get-NewPSResourceRepositoryFile |
| 225 | + Register-LocalRepos |
| 226 | + } |
| 227 | + |
| 228 | + AfterEach { |
| 229 | + Uninstall-PSResource $testModuleName, $testModuleName2 -SkipDependencyCheck -ErrorAction SilentlyContinue |
| 230 | + } |
| 231 | + |
| 232 | + AfterAll { |
| 233 | + Get-RevertPSResourceRepositoryFile |
| 234 | + } |
| 235 | + |
| 236 | + # Unix only manual test |
| 237 | + # Expected path should be similar to: '/usr/local/share/powershell/Modules' |
| 238 | + It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) { |
| 239 | + Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Scope AllUsers |
| 240 | + $pkg = Get-Module $testModuleName -ListAvailable |
| 241 | + $pkg.Name | Should -Be $testModuleName |
| 242 | + $pkg.Path.Contains("/usr/") | Should -Be $true |
| 243 | + } |
| 244 | + |
| 245 | + # This needs to be manually tested due to prompt |
| 246 | + It "Install resource that requires accept license without -AcceptLicense flag" { |
| 247 | + Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName |
| 248 | + $pkg = Get-InstalledPSResource $testModuleName2 |
| 249 | + $pkg.Name | Should -Be $testModuleName2 |
| 250 | + $pkg.Version | Should -Be "0.0.1.0" |
| 251 | + } |
| 252 | + |
| 253 | + # This needs to be manually tested due to prompt |
| 254 | + It "Install resource should prompt 'trust repository' if repository is not trusted" { |
| 255 | + Set-PSResourceRepository PoshTestGallery -Trusted:$false |
| 256 | + |
| 257 | + Install-PSResource -Name $testModuleName -Repository $TestGalleryName -confirm:$false |
| 258 | + |
| 259 | + $pkg = Get-Module $testModuleName -ListAvailable |
| 260 | + $pkg.Name | Should -Be $testModuleName |
| 261 | + |
| 262 | + Set-PSResourceRepository PoshTestGallery -Trusted |
| 263 | + } |
| 264 | +} |
0 commit comments