Skip to content
Merged
8 changes: 7 additions & 1 deletion src/code/GetPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public sealed class GetPSResource : PSCmdlet
[Parameter]
[ValidateNotNullOrEmpty()]
public string Path { get; set; }

/// <summary>
/// Specifies the scope of installation.
/// </summary>
[Parameter]
public ScopeType Scope { get; set; }

#endregion

Expand Down Expand Up @@ -102,7 +108,7 @@ protected override void BeginProcessing()
else
{
// retrieve all possible paths
_pathsToSearch = Utils.GetAllResourcePaths(this);
_pathsToSearch = Utils.GetAllResourcePaths(this, Scope);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/code/UninstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public sealed class UninstallPSResource : PSCmdlet
[Parameter]
public SwitchParameter SkipDependencyCheck { get; set; }

/// <summary>
/// Specifies the scope of installation.
/// </summary>
[Parameter]
public ScopeType Scope { get; set; }

#endregion

#region Members
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
12 changes: 6 additions & 6 deletions test/InstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions test/UninstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}
}
21 changes: 6 additions & 15 deletions test/UpdatePSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down