diff --git a/src/code/GetPSResource.cs b/src/code/GetPSResource.cs index 703500d0c..367262f7a 100644 --- a/src/code/GetPSResource.cs +++ b/src/code/GetPSResource.cs @@ -44,6 +44,12 @@ public sealed class GetPSResource : PSCmdlet [Parameter] [ValidateNotNullOrEmpty()] public string Path { get; set; } + + /// + /// Specifies the scope of installation. + /// + [Parameter] + public ScopeType Scope { get; set; } #endregion @@ -102,7 +108,7 @@ protected override void BeginProcessing() else { // retrieve all possible paths - _pathsToSearch = Utils.GetAllResourcePaths(this); + _pathsToSearch = Utils.GetAllResourcePaths(this, Scope); } } diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 5dfe13c00..019484191 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -54,6 +54,12 @@ public sealed class UninstallPSResource : PSCmdlet [Parameter] public SwitchParameter SkipDependencyCheck { get; set; } + /// + /// Specifies the scope of installation. + /// + [Parameter] + public ScopeType Scope { get; set; } + #endregion #region Members @@ -71,7 +77,7 @@ public sealed class UninstallPSResource : PSCmdlet protected override void BeginProcessing() { - _pathsToSearch = Utils.GetAllResourcePaths(this); + _pathsToSearch = Utils.GetAllResourcePaths(this, Scope); } protected override void ProcessRecord() diff --git a/test/GetInstalledPSResource.Tests.ps1 b/test/GetPSResource.Tests.ps1 similarity index 76% rename from test/GetInstalledPSResource.Tests.ps1 rename to test/GetPSResource.Tests.ps1 index 1f3492921..f0f4b0c25 100644 --- a/test/GetInstalledPSResource.Tests.ps1 +++ b/test/GetPSResource.Tests.ps1 @@ -131,4 +131,42 @@ $testCases = $res.Version | Should -Be "3.0.0" $res.Prerelease | Should -Be "alpha" } + + # Windows only + It "Get resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) { + $pkg = Get-PSResource -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-PSResource -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-PSResource -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-PSResource -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-PSResource "testmodule99" -Scope CurrentUser + $pkg.Name | Should -contain "testmodule99" + $pkg.InstalledLocation.ToString().Contains("/.local") | Should -Be $true + } } diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index c6f173483..e887265ed 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -168,10 +168,10 @@ Describe 'Test Install-PSResource for Module' { # Windows only It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) { - Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope AllUsers - $pkg = Get-PSResource $testModuleName - $pkg.Name | Should -Be $testModuleName - $pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true + Install-PSResource -Name "testmodule99" -Repository $PSGalleryName -TrustRepository -Scope AllUsers -Verbose + $pkg = Get-Module "testmodule99" -ListAvailable + $pkg.Name | Should -Be "testmodule99" + $pkg.Path.ToString().Contains("Program Files") } # Windows only @@ -222,8 +222,8 @@ Describe 'Test Install-PSResource for Module' { It "Restore resource after reinstall fails" { Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository $pkg = Get-Module $testModuleName -ListAvailable - $pkg.Name | Should -Be $testModuleName - $pkg.Version | Should -Be "5.0.0.0" + $pkg.Name | Should -Contain $testModuleName + $pkg.Version | Should -Contain "5.0.0.0" $resourcePath = Split-Path -Path $pkg.Path -Parent $resourceFiles = Get-ChildItem -Path $resourcePath -Recurse diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index 54cfeb062..0c0850e60 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -43,10 +43,10 @@ Describe 'Test Uninstall-PSResource for Modules' { } It "Uninstall a list of modules by name" { - $null = Install-PSResource "RequiredModule1" -Repository $PSGalleryName -TrustRepository -WarningAction SilentlyContinue -SkipDependencyCheck + $null = Install-PSResource "testmodule99" -Repository $PSGalleryName -TrustRepository -WarningAction SilentlyContinue -SkipDependencyCheck - Uninstall-PSResource -Name $testModuleName, "RequiredModule1" - Get-PSResource $testModuleName, "RequiredModule1" | Should -BeNullOrEmpty + Uninstall-PSResource -Name $testModuleName, "testmodule99" + Get-PSResource $testModuleName, "testmodule99" | Should -BeNullOrEmpty } It "Uninstall a specific script by name" { @@ -267,4 +267,24 @@ Describe 'Test Uninstall-PSResource for Modules' { $res = Get-PSResource -Name $testModuleName -Version "2.5.0-beta" $res | Should -BeNullOrEmpty } + + # Windows only + It "Uninstall resource under CurrentUser scope only- Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) { + Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope AllUsers -Reinstall + Uninstall-PSResource -Name $testModuleName -Scope CurrentUser + + $pkg = Get-Module $testModuleName -ListAvailable + $pkg.Name | Should -Be $testModuleName + $pkg.Path.ToString().Contains("Program Files") | Should -Be $true + } + + # Windows only + It "Uninstall resource under AllUsers scope only- Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) { + Install-PSResource $testModuleName -Repository $PSGalleryName -TrustRepository -Scope AllUsers -Reinstall + Uninstall-PSResource -Name $testModuleName -Scope AllUsers + + $pkg = Get-Module $testModuleName -ListAvailable + $pkg.Name | Should -Be $testModuleName + $pkg.Path.ToString().Contains("Documents") | Should -Be $true + } } diff --git a/test/UpdatePSResource.Tests.ps1 b/test/UpdatePSResource.Tests.ps1 index 05b2bbf7b..4c3bf082b 100644 --- a/test/UpdatePSResource.Tests.ps1 +++ b/test/UpdatePSResource.Tests.ps1 @@ -180,23 +180,14 @@ Describe 'Test Update-PSResource' { # Windows only It "update resource under AllUsers scope" -skip:(!($IsWindows -and (Test-IsAdmin))) { - Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope AllUsers - Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser + Install-PSResource -Name "testmodule99" -Version "0.0.91" -Repository $PSGalleryName -TrustRepository -Scope AllUsers -Verbose + Install-PSResource -Name "testmodule99" -Version "0.0.91" -Repository $PSGalleryName -TrustRepository -Scope CurrentUser -Verbose - Update-PSResource -Name $testModuleName -Version "3.0.0.0" -Repository $PSGalleryName -TrustRepository -Scope AllUsers + Update-PSResource -Name "testmodule99" -Version "0.0.93" -Repository $PSGalleryName -TrustRepository -Scope AllUsers -Verbose - $res = Get-PSResource -Name $testModuleName - $isPkgUpdated = $false - foreach ($pkg in $res) - { - if ([System.Version]$pkg.Version -gt [System.Version]"1.0.0.0") - { - $pkg.InstalledLocation.Contains("Program Files") | Should -Be $true - $isPkgUpdated = $true - } - } - - $isPkgUpdated | Should -Be $true + $res = Get-Module -Name "testmodule99" -ListAvailable + $res | Should -Not -BeNullOrEmpty + $res.Version | Should -Contain "0.0.93" } # Windows only