From 25f0c1b06e56b5d1592dfcf91d953964f61b442d Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Thu, 22 Jul 2021 00:30:58 -0700 Subject: [PATCH 01/13] Change WriteDebug to WriteError when throwing an exception in InstallHelper --- src/code/InstallHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 1679c06fa..f74204be5 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -347,7 +347,7 @@ private List InstallPackage(IEnumerable pkgsToInstall, s } catch (Exception e) { - _cmdletPassedIn.WriteDebug(string.Format("Error attempting download: '{0}'", e.Message)); + _cmdletPassedIn.WriteError(string.Format("Error attempting download: '{0}'", e.Message)); } finally { From c1da6df8aef32d7724c56f4cf230784b52bd10b3 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Thu, 22 Jul 2021 23:45:10 -0700 Subject: [PATCH 02/13] Update install tests for linux --- src/code/InstallHelper.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index f74204be5..8ebc73f30 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -21,6 +21,15 @@ using NuGet.Versioning; using Microsoft.PowerShell.PowerShellGet.UtilClasses; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Management.Automation; +using System.Threading; +using Microsoft.PowerShell.PowerShellGet.UtilClasses; +using NuGet.Versioning; + namespace Microsoft.PowerShell.PowerShellGet.Cmdlets { /// @@ -347,7 +356,7 @@ private List InstallPackage(IEnumerable pkgsToInstall, s } catch (Exception e) { - _cmdletPassedIn.WriteError(string.Format("Error attempting download: '{0}'", e.Message)); + _cmdletPassedIn.WriteDebug(string.Format("Error attempting download: '{0}'", e.Message)); } finally { From 70a6e5d0dcc691fa083532ec307503469852e7de Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Fri, 23 Jul 2021 14:25:11 -0700 Subject: [PATCH 03/13] Add linux tests to install tests --- test/InstallPSResource.Tests.ps1 | 33 +++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 56238bfef..f90d9ce46 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -125,7 +125,7 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under CurrentUser scope" -Skip:(!$IsWindows) { + It "Install resource under CurrentUser scope - Windows only" -Skip:(!$IsWindows) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope CurrentUser $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -133,7 +133,7 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under AllUsers scope" -Skip:(!$IsWindows) { + It "Install resource under AllUsers scope - Windows only" -Skip:(!$IsWindows) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -141,13 +141,40 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under no specified scope" -Skip:(!$IsWindows) { + It "Install resource under no specified scope - Windows only" -Skip:(!$IsWindows) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" $pkg.Path.Contains("Documents") | Should -Be $true } + # Unix only + # Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules' + It "Install resource under CurrentUser scope - Unix only" -Skip:($IsWindows) { + Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope CurrentUser + $pkg = Get-Module "TestModule" -ListAvailable + $pkg.Name | Should -Be "TestModule" + $pkg.Path.Contains("/home/") | Should -Be $true + } + + # Unix only + # Expected path should be similar to: '/usr/local/share/powershell/Modules' + It "Install resource under AllUsers scope - Unix only" -Skip:($IsWindows) { + Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers + $pkg = Get-Module "TestModule" -ListAvailable + $pkg.Name | Should -Be "TestModule" + $pkg.Path.Contains("/usr/") | Should -Be $true + } + + # Unix only + # Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules' + It "Install resource under no specified scope - Unix only" -Skip:($IsWindows) { + Install-PSResource -Name "TestModule" -Repository $TestGalleryName + $pkg = Get-Module "TestModule" -ListAvailable + $pkg.Name | Should -Be "TestModule" + $pkg.Path.Contains("/home/") | Should -Be $true + } + It "Should not install resource that is already installed" { Install-PSResource -Name "TestModule" -Repository $TestGalleryName $pkg = Get-Module "TestModule" -ListAvailable From 5b6486f9b21287ded626d8656ea43130e654d17b Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Fri, 23 Jul 2021 15:40:27 -0700 Subject: [PATCH 04/13] Uncomment and update Get-InstalledPSResource tests --- test/GetInstalledPSResource.Tests.ps1 | 52 ++++++++++++++------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/test/GetInstalledPSResource.Tests.ps1 b/test/GetInstalledPSResource.Tests.ps1 index 3b9665a46..937ab4e72 100644 --- a/test/GetInstalledPSResource.Tests.ps1 +++ b/test/GetInstalledPSResource.Tests.ps1 @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -<# // Temporarily commenting out until Install-PSResource is complete Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe 'Test Get-InstalledPSResource for Module' { @@ -10,8 +9,11 @@ Describe 'Test Get-InstalledPSResource for Module' { $TestGalleryName = Get-PoshTestGalleryName Get-NewPSResourceRepositoryFile - Register-PSRepository $TestGalleryName -SourceLocation "https://www.poshtestgallery.com/api/v2" - Install-Module ContosoServer -Repository $TestGalleryName + Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository + Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "2.0" + Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "1.5" + Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -Version "1.0" + Install-PSResource TestTestScript -Repository $TestGalleryName -TrustRepository } AfterAll { @@ -25,39 +27,40 @@ Describe 'Test Get-InstalledPSResource for Module' { It "Get specific module resource by name" { $pkg = Get-InstalledPSResource -Name ContosoServer - $pkg.Name | Should -Be "ContosoServer" + $pkg.Name | Should -Contain "ContosoServer" } It "Get specific script resource by name" { - $pkg = Get-InstalledPSResource -Name adsql - $pkg.Name | Should -Be "adsql" + $pkg = Get-InstalledPSResource -Name TestTestScript + $pkg.Name | Should -Be "TestTestScript" } It "Get resource when given Name to " -TestCases @( - @{Name="*ShellG*"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard at beginning and end of name: *ShellG*"}, - @{Name="PowerShell*"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard at end of name: PowerShellG*"}, - @{Name="*ShellGet"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard at beginning of name: *ShellGet"}, - @{Name="Power*Get"; ExpectedName="PowerShellGet"; Reason="validate name, with wildcard in middle of name: Power*Get"} + @{Name="*tosoSer*"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at beginning and end of name: *tosoSer*"}, + @{Name="ContosoSer*"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at end of name: ContosoSer*"}, + @{Name="*tosoServer"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard at beginning of name: *tosoServer"}, + @{Name="Cont*erver"; ExpectedName="ContosoServer"; Reason="validate name, with wildcard in middle of name: Cont*erver"} ) { param($Version, $ExpectedVersion) $pkgs = Get-InstalledPSResource -Name $Name - $pkgs.Name | Should -Be "PowerShellGet" + $pkgs.Name | Should -Contain "ContosoServer" } - It "Get resource when given Name to " -TestCases @( - @{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"}, - @{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"}, - @{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion="2.5.0.0"; Reason="validate version, exact range inclusive"}, - @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"}, - @{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"}, - @{Version="[1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version inclusive"}, - @{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"}, - @{Version="(,1.5.0.0]"; ExpectedVersion="1.5.0.0"; Reason="validate version, maximum version inclusive"}, - @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"} - ) { +$testCases = + @{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"}, + @{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"}, + @{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, exact range inclusive"}, + @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion=@("2.0.0.0", "1.5.0.0"); Reason="validate version, exact range exclusive"}, + @{Version="(1.0.0.0,)"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0"); Reason="validate version, minimum version exclusive"}, + @{Version="[1.0.0.0,)"; ExpectedVersion=@("2.5.0.0", "2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, minimum version inclusive"}, + @{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"}, + @{Version="(,1.5.0.0]"; ExpectedVersion=@("1.5.0.0", "1.0.0.0"); Reason="validate version, maximum version inclusive"}, + @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion=@("2.0.0.0", "1.5.0.0", "1.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"} + + It "Get resource when given Name to " -TestCases $testCases { param($Version, $ExpectedVersion) $pkgs = Get-InstalledPSResource -Name "ContosoServer" -Version $Version - $pkgs.Name | Should -Be "ContosoServer" + $pkgs.Name | Should -Contain "ContosoServer" $pkgs.Version | Should -Be $ExpectedVersion } @@ -100,8 +103,7 @@ Describe 'Test Get-InstalledPSResource for Module' { } It "Get resources when given Name, and Version is '*'" { - $pkgs = Get-InstalledPSResource -Name Carbon -Version "*" + $pkgs = Get-InstalledPSResource -Name ContosoServer -Version "*" $pkgs.Count | Should -BeGreaterOrEqual 2 } } -#> \ No newline at end of file From 963a43613cfb194a0da61117670fecd3e9830dad Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 01:27:12 -0700 Subject: [PATCH 05/13] Uncomment and update uninstall tests --- test/UninstallPSResource.Tests.ps1 | 234 ++++++++++++----------------- 1 file changed, 97 insertions(+), 137 deletions(-) diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index a1258d3ef..e7e8e3126 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -<#// Temporarily comment out tests until Install-PSResource is complete Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe 'Test Uninstall-PSResource for Modules' { @@ -10,180 +9,141 @@ Describe 'Test Uninstall-PSResource for Modules' { $TestGalleryName = Get-PoshTestGalleryName $PSGalleryName = Get-PSGalleryName Get-NewPSResourceRepositoryFile + $res = Uninstall-PSResource -name ContosoServer -Version "*" + } + BeforeEach{ + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource BaseTestPackage -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue } - AfterAll { Get-RevertPSResourceRepositoryFile } It "Uninstall a specific module by name" { - $pkg = Uninstall-PSResource -name Bicep + $res = Uninstall-PSResource -name ContosoServer + Get-Module ContosoServer -ListAvailable | Should -Be $null } It "Uninstall a list of modules by name" { - $pkg = Uninstall-PSResource -Name BaseTestPackage, bicep + $res = Uninstall-PSResource -Name BaseTestPackage, ContosoServer + Get-Module ContosoServer, BaseTestPackage -ListAvailable | Should -be $null } - It "Uninstall a module when given name and specifying all versions" { - $res = Uninstall-PSResource -Name "Carbon" -version "*" - $res | Should -BeNullOrEmpty + It "Uninstall a specific script by name" { + $null = Install-PSResource Test-RPC -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + + $pkg = Uninstall-PSResource -name Test-RPC } - It "Uninstall module when given Name and specifying exact version" { - $res = Uninstall-PSResource -Name "ContosoServer" -Version "1.0.0" - $res | Should -BeNullOrEmpty - } - - It "Uninstall module when given Name to " -TestCases @( - @{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"}, - @{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"}, - @{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion="2.5.0.0"; Reason="validate version, exact range inclusive"}, - @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"}, - @{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"}, - @{Version="[1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version inclusive"}, - @{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"}, - @{Version="(,1.5.0.0]"; ExpectedVersion="1.5.0.0"; Reason="validate version, maximum version inclusive"}, - @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"} - ) { - param($Version, $ExpectedVersion) - $res = Uninstall-PSResource -Name "Pester" -Version $Version -Repository $TestGalleryName - $res.Name | Should -Be "Pester" - $res.Version | Should -Be $ExpectedVersion - } - - It "Do not uninstall module with incorrectly formatted version such as " -TestCases @( - @{Version='(1.5.0.0)'; Description="exlcusive version (8.1.0.0)"}, - @{Version='[1-5-0-0]'; Description="version formatted with invalid delimiter"}, - @{Version='[1.*.0]'; Description="version with wilcard in middle"}, - @{Version='[*.5.0.0]'; Description="version with wilcard at start"}, - @{Version='[1.*.0.0]'; Description="version with wildcard at second digit"}, - @{Version='[1.5.*.0]'; Description="version with wildcard at third digit"} - @{Version='[1.5.0.*]'; Description="version with wildcard at end"}, - @{Version='[1..0.0]'; Description="version with missing digit in middle"}, - @{Version='[1.5.0.]'; Description="version with missing digit at end"}, - @{Version='[1.5.0.0.0]'; Description="version with more than 4 digits"} - ) { - param($Version, $Description) + It "Uninstall a list of scripts by name" { + $null = Install-PSResource adsql, airoute -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - $res = $null - try { - $res = Uninstall-PSResource -Name "ContosoServer" -Version $Version -Repository $TestGalleryName -ErrorAction Ignore - } - catch {} - - $res | Should -BeNullOrEmpty + $pkg = Uninstall-PSResource -Name adsql, airoute } - It "Does not uninstall when given Name and an invalid version" { - $res = Uninstall-PSResource -Name "ContosoServer" -Version "(0.0.0.1)" - $res | Should -BeNullOrEmpty + It "Uninstall a module when given name and specifying all versions" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue + + $res = Uninstall-PSResource -Name "Carbon" -version "*" + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain "1.0.0" + $pkgs.Version | Should -Not -Contain "1.5.0" + $pkgs.Version | Should -Not -Contain "2.0.0" + $pkgs.Version | Should -Not -Contain "2.5.0" } - It "Uninstall lastest version of a particular module " { - # test_module resource's latest version is a prerelease version, before that it has a non-prerelease version - $res = Uninstall-PSResource -Name "test_module" - $res.Version | Should -Be "5.0.0.0" + It "Uninstall all versions of a module when given name and not specifying any version param" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - $resPrerelease = Uninstall-PSResource -Name "test_module" -Prerelease - $resPrerelease.Version | Should -Be "5.2.5.0" + $res = Uninstall-PSResource -Name "Carbon" + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs | Should -Be $null } - It "Uninstall module using -WhatIf, should not uninstall the module" { - $res = Uninstall-PSResource -Name "ActiveDirectoryTools" -WhatIf - } + It "Uninstall module when given Name and specifying exact version" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - It "Do not Uninstall module that is a dependency for another module" { - $res = Uninstall-PSResource -Name "PackageManagement" - } + $res = Uninstall-PSResource -Name "ContosoServer" -Version "1.0.0" + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain "1.0.0" + } + + $testCases = @{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"}, + @{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"}, + @{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion="2.5.0.0"; Reason="validate version, exact range inclusive"}, + @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"}, + @{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"}, + @{Version="[1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version inclusive"}, + @{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"}, + @{Version="(,1.5.0.0]"; ExpectedVersion="1.5.0.0"; Reason="validate version, maximum version inclusive"}, + @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"} + + It "Uninstall module when given Name to " -TestCases $testCases { + param($Version, $ExpectedVersion) + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - It "Uninstall module that is a dependency for another module using -Force" { - $res = Uninstall-PSResource -Name "PackageManagement" -Force + $res = Uninstall-PSResource -Name ContosoServer -Version $Version + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain $Version } -} + $testCases2 = @{Version='[1.*.0]'; Description="version with wilcard in middle"}, + @{Version='[*.5.0.0]'; Description="version with wilcard at start"}, + @{Version='[1.*.0.0]'; Description="version with wildcard at second digit"}, + @{Version='[1.5.*.0]'; Description="version with wildcard at third digit"} + @{Version='[1.5.0.*]'; Description="version with wildcard at end"}, + @{Version='[1..0.0]'; Description="version with missing digit in middle"}, + @{Version='[1.5.0.]'; Description="version with missing digit at end"}, + @{Version='[1.5.0.0.0]'; Description="version with more than 4 digits"} -Describe 'Test Uninstall-PSResource for Scripts' { + It "Do not uninstall module with incorrectly formatted version such as " -TestCases $testCases2 { + param($Version, $Description) - BeforeAll{ - $TestGalleryName = Get-PoshTestGalleryName - $PSGalleryName = Get-PSGalleryName - Get-NewPSResourceRepositoryFile + {Uninstall-PSResource -Name "ContosoServer" -Version $Version} | Should -Throw "Argument for -Version parameter is not in the proper format." } - AfterAll { - Get-RevertPSResourceRepositoryFile - } + $testCases3 = @{Version='(2.5.0.0)'; Description="exclusive version (8.1.0.0)"}, + @{Version='[2-5-0-0]'; Description="version formatted with invalid delimiter"} - It "Uninstall a specific script by name" { - $pkg = Uninstall-PSResource -name Test-RPC - } + It "Do not uninstall module with incorrectly formatted version such as " -TestCases $testCases3 { + param($Version, $Description) - It "Uninstall a list of scripts by name" { - $pkg = Uninstall-PSResource -Name adsql, airoute - } + Uninstall-PSResource -Name "ContosoServer" -Version $Version - It "Uninstall a script when given name and specifying all versions" { - $res = Uninstall-PSResource -Name "NetworkingDSC" -version "*" - $res | Should -BeNullOrEmpty + $pkg = Get-Module ContosoServer -ListAvailable + $pkg.Version | Should -Be "2.5" } - It "Uninstall script when given Name and specifying exact version" { - $res = Uninstall-PSResource -Name "ContosoServer" -Version "1.0.0" - $res | Should -BeNullOrEmpty - } + It "Uninstall module using -WhatIf, should not uninstall the module" { + $res = Uninstall-PSResource -Name "ContosoServer" -WhatIf - It "Does not uninstall a script when given Name and a version that does not exist" { - $res = Uninstall-PSResource -Name "ContosoServer" -Version "3.0.0" - $res | Should -BeNullOrEmpty + $pkg = Get-Module ContosoServer -ListAvailable + $pkg.Version | Should -Be "2.5" } +#> + It "Do not Uninstall module that is a dependency for another module" { + $null = Install-PSResource "test_module" -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + + Uninstall-PSResource -Name "RequiredModule1" -ErrorVariable ev -ErrorAction SilentlyContinue - It "Uninstall script when given Name to " -TestCases @( - @{Version="[2.0.0.0]"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match"}, - @{Version="2.0.0.0"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact match without bracket syntax"}, - @{Version="[1.0.0.0, 2.5.0.0]"; ExpectedVersion="2.5.0.0"; Reason="validate version, exact range inclusive"}, - @{Version="(1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, exact range exclusive"}, - @{Version="(1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version exclusive"}, - @{Version="[1.0.0.0,)"; ExpectedVersion="2.5.0.0"; Reason="validate version, minimum version inclusive"}, - @{Version="(,1.5.0.0)"; ExpectedVersion="1.0.0.0"; Reason="validate version, maximum version exclusive"}, - @{Version="(,1.5.0.0]"; ExpectedVersion="1.5.0.0"; Reason="validate version, maximum version inclusive"}, - @{Version="[1.0.0.0, 2.5.0.0)"; ExpectedVersion="2.0.0.0"; Reason="validate version, mixed inclusive minimum and exclusive maximum version"} - ) { - param($Version, $ExpectedVersion) - $res = Uninstall-PSResource -Name "Pester" -Version $Version -Repository $TestGalleryName - $res.Name | Should -Be "Pester" - $res.Version | Should -Be $ExpectedVersion - } - - It "Do not uninstall module with incorrectly formatted version such as " -TestCases @( - @{Version='(1.5.0.0)'; Description="exlcusive version (8.1.0.0)"}, - @{Version='[1-5-0-0]'; Description="version formatted with invalid delimiter"}, - @{Version='[1.*.0]'; Description="version with wilcard in middle"}, - @{Version='[*.5.0.0]'; Description="version with wilcard at start"}, - @{Version='[1.*.0.0]'; Description="version with wildcard at second digit"}, - @{Version='[1.5.*.0]'; Description="version with wildcard at third digit"} - @{Version='[1.5.0.*]'; Description="version with wildcard at end"}, - @{Version='[1..0.0]'; Description="version with missing digit in middle"}, - @{Version='[1.5.0.]'; Description="version with missing digit at end"}, - @{Version='[1.5.0.0.0]'; Description="version with more than 4 digits"} - ) { - param($Version, $Description) - - $res = $null - try { - $res = Uninstall-PSResource -Name "ContosoServer" -Version $Version -Repository $TestGalleryName -ErrorAction Ignore - } - catch {} - - $res | Should -BeNullOrEmpty + $pkg = Get-Module "RequiredModule1" -ListAvailable + $pkg | Should -Not -Be $null } - It "Does not uninstall when given Name and an invalid version" { - $res = Uninstall-PSResource -Name "ContosoServer" -Version "(0.0.0.1)" - $res | Should -BeNullOrEmpty - } + It "Uninstall module that is a dependency for another module using -Force" { + $null = Install-PSResource "test_module" -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - It "Uninstall script using -WhatIf" { - $res = Uninstall-PSResource -Name "ActiveDirectoryTools" -WhatIf + $res = Uninstall-PSResource -Name "RequiredModule1" -Force + + $pkg = Get-Module "RequiredModule1" -ListAvailable + $pkg | Should -Be $null } -} -#> \ No newline at end of file +} \ No newline at end of file From 366baeceee9f47aba094e8348638a743b9134897 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 01:53:33 -0700 Subject: [PATCH 06/13] Add error check for dependency test --- test/UninstallPSResource.Tests.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index e7e8e3126..f256ce953 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -13,7 +13,6 @@ Describe 'Test Uninstall-PSResource for Modules' { } BeforeEach{ $null = Install-PSResource ContosoServer -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource BaseTestPackage -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue } AfterAll { Get-RevertPSResourceRepositoryFile @@ -25,6 +24,8 @@ Describe 'Test Uninstall-PSResource for Modules' { } It "Uninstall a list of modules by name" { + $null = Install-PSResource BaseTestPackage -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + $res = Uninstall-PSResource -Name BaseTestPackage, ContosoServer Get-Module ContosoServer, BaseTestPackage -ListAvailable | Should -be $null } @@ -128,7 +129,7 @@ Describe 'Test Uninstall-PSResource for Modules' { $pkg = Get-Module ContosoServer -ListAvailable $pkg.Version | Should -Be "2.5" } -#> + It "Do not Uninstall module that is a dependency for another module" { $null = Install-PSResource "test_module" -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue @@ -136,6 +137,8 @@ Describe 'Test Uninstall-PSResource for Modules' { $pkg = Get-Module "RequiredModule1" -ListAvailable $pkg | Should -Not -Be $null + + $ev | Should -Be "Cannot uninstall 'RequiredModule1', the following package(s) take a dependency on this package: test_module. If you would still like to uninstall, rerun the command with -Force" } It "Uninstall module that is a dependency for another module using -Force" { From 301e4b1d99e1014496f3a6751e93c34b4682c932 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 01:54:07 -0700 Subject: [PATCH 07/13] Have error record populate to calling method before it gets written out --- src/code/UninstallPSResource.cs | 62 +++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 782fe3db7..9cf7bcd7e 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -154,27 +154,33 @@ private bool UninstallPkgHelper() continue; } + ErrorRecord errRecord = null; if (pkgPath.EndsWith(".ps1")) { - successfullyUninstalled = UninstallScriptHelper(pkgPath, pkgName); + successfullyUninstalled = UninstallScriptHelper(pkgPath, pkgName, out errRecord); } else { - successfullyUninstalled = UninstallModuleHelper(pkgPath, pkgName); + successfullyUninstalled = UninstallModuleHelper(pkgPath, pkgName, out errRecord); } // if we can't find the resource, write non-terminating error and return if (!successfullyUninstalled) { - string message = Version == null || Version.Trim().Equals("*") ? - string.Format("Could not find any version of the resource '{0}' in any path", pkgName) : - string.Format("Could not find verison '{0}' of the resource '{1}' in any path", Version, pkgName); - - WriteError(new ErrorRecord( - new PSInvalidOperationException(message), - "ErrorRetrievingSpecifiedResource", - ErrorCategory.ObjectNotFound, - this)); + if (errRecord == null) + { + string message = Version == null || Version.Trim().Equals("*") ? + string.Format("Could not find any version of the resource '{0}' in any path", pkgName) : + string.Format("Could not find verison '{0}' of the resource '{1}' in any path", Version, pkgName); + + errRecord = new ErrorRecord( + new PSInvalidOperationException(message), + "ErrorRetrievingSpecifiedResource", + ErrorCategory.ObjectNotFound, + this); + } + + WriteError(errRecord); } } @@ -182,13 +188,14 @@ private bool UninstallPkgHelper() } /* uninstalls a module */ - private bool UninstallModuleHelper(string pkgPath, string pkgName) + private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorRecord errRecord) { + errRecord = null; var successfullyUninstalledPkg = false; - + // if -Force is not specified and the pkg is a dependency for another package, - // an error will be written and we return false - if (!Force && CheckIfDependency(pkgName)) + // an error will be written and we return fa lse + if (!Force && CheckIfDependency(pkgName, out errRecord)) { return false; } @@ -216,7 +223,8 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName) var exMessage = String.Format("Parent directory '{0}' could not be deleted: {1}", dir.Parent.FullName, e.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingParentDirectory = new ErrorRecord(ex, "ErrorDeletingParentDirectory", ErrorCategory.InvalidArgument, null); - WriteError(ErrorDeletingParentDirectory); + //WriteError(ErrorDeletingParentDirectory); + errRecord = ErrorDeletingParentDirectory; } } catch (Exception err) { @@ -224,15 +232,17 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName) var exMessage = String.Format("Directory '{0}' could not be deleted: {1}", dir.FullName, err.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingDirectory = new ErrorRecord(ex, "ErrorDeletingDirectory", ErrorCategory.PermissionDenied, null); - WriteError(ErrorDeletingDirectory); + //WriteError(ErrorDeletingDirectory); + errRecord = ErrorDeletingDirectory; } return successfullyUninstalledPkg; } /* uninstalls a script */ - private bool UninstallScriptHelper(string pkgPath, string pkgName) + private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorRecord errRecord) { + errRecord = null; var successfullyUninstalledPkg = false; // delete the appropriate file @@ -258,21 +268,24 @@ private bool UninstallScriptHelper(string pkgPath, string pkgName) var exMessage = String.Format("Script metadata file '{0}' could not be deleted: {1}", scriptXML, e.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingScriptMetadataFile = new ErrorRecord(ex, "ErrorDeletingScriptMetadataFile", ErrorCategory.PermissionDenied, null); - WriteError(ErrorDeletingScriptMetadataFile); + //WriteError(ErrorDeletingScriptMetadataFile); + errRecord = ErrorDeletingScriptMetadataFile; } } catch (Exception err){ var exMessage = String.Format("Script '{0}' could not be deleted: {1}", pkgPath, err.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingScript = new ErrorRecord(ex, "ErrorDeletingScript", ErrorCategory.PermissionDenied, null); - WriteError(ErrorDeletingScript); + //WriteError(ErrorDeletingScript); + errRecord = ErrorDeletingScript; } return successfullyUninstalledPkg; } - private bool CheckIfDependency(string pkgName) + private bool CheckIfDependency(string pkgName, out ErrorRecord errRecord) { + errRecord = null; // this is a primitive implementation // TODO: implement a dependencies database for querying dependency info // cannot uninstall a module if another module is dependent on it @@ -296,7 +309,8 @@ private bool CheckIfDependency(string pkgName) var exMessage = String.Format("Error checking if resource is a dependency: {0}. If you would still like to uninstall, rerun the command with -Force", e.Message); var ex = new ArgumentException(exMessage); var DependencyCheckError = new ErrorRecord(ex, "DependencyCheckError", ErrorCategory.OperationStopped, null); - WriteError(DependencyCheckError); + // WriteError(DependencyCheckError); + errRecord = DependencyCheckError; } if (pkgsWithRequiredModules.Any()) @@ -307,8 +321,10 @@ private bool CheckIfDependency(string pkgName) var exMessage = String.Format("Cannot uninstall '{0}', the following package(s) take a dependency on this package: {1}. If you would still like to uninstall, rerun the command with -Force", pkgName, strUniquePkgNames); var ex = new ArgumentException(exMessage); var PackageIsaDependency = new ErrorRecord(ex, "PackageIsaDependency", ErrorCategory.OperationStopped, null); - WriteError(PackageIsaDependency); + //WriteError(PackageIsaDependency); + errRecord = PackageIsaDependency; return true; + } } return false; From 200c1ea1b5b09d31b9391879e19de62caa7bbcf2 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 09:09:05 -0700 Subject: [PATCH 08/13] Fix some syntax issues --- src/code/InstallHelper.cs | 9 --------- src/code/UninstallPSResource.cs | 14 ++++---------- test/UninstallPSResource.Tests.ps1 | 2 +- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 8ebc73f30..1679c06fa 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -21,15 +21,6 @@ using NuGet.Versioning; using Microsoft.PowerShell.PowerShellGet.UtilClasses; - -using System; -using System.Collections.Generic; -using System.IO; -using System.Management.Automation; -using System.Threading; -using Microsoft.PowerShell.PowerShellGet.UtilClasses; -using NuGet.Versioning; - namespace Microsoft.PowerShell.PowerShellGet.Cmdlets { /// diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 9cf7bcd7e..0a69e4617 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -165,7 +165,7 @@ private bool UninstallPkgHelper() } // if we can't find the resource, write non-terminating error and return - if (!successfullyUninstalled) + if (!successfullyUninstalled || errRecord != null) { if (errRecord == null) { @@ -192,9 +192,9 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorReco { errRecord = null; var successfullyUninstalledPkg = false; - + // if -Force is not specified and the pkg is a dependency for another package, - // an error will be written and we return fa lse + // an error will be written and we return false if (!Force && CheckIfDependency(pkgName, out errRecord)) { return false; @@ -223,7 +223,6 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorReco var exMessage = String.Format("Parent directory '{0}' could not be deleted: {1}", dir.Parent.FullName, e.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingParentDirectory = new ErrorRecord(ex, "ErrorDeletingParentDirectory", ErrorCategory.InvalidArgument, null); - //WriteError(ErrorDeletingParentDirectory); errRecord = ErrorDeletingParentDirectory; } } @@ -232,7 +231,6 @@ private bool UninstallModuleHelper(string pkgPath, string pkgName, out ErrorReco var exMessage = String.Format("Directory '{0}' could not be deleted: {1}", dir.FullName, err.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingDirectory = new ErrorRecord(ex, "ErrorDeletingDirectory", ErrorCategory.PermissionDenied, null); - //WriteError(ErrorDeletingDirectory); errRecord = ErrorDeletingDirectory; } @@ -268,7 +266,6 @@ private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorReco var exMessage = String.Format("Script metadata file '{0}' could not be deleted: {1}", scriptXML, e.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingScriptMetadataFile = new ErrorRecord(ex, "ErrorDeletingScriptMetadataFile", ErrorCategory.PermissionDenied, null); - //WriteError(ErrorDeletingScriptMetadataFile); errRecord = ErrorDeletingScriptMetadataFile; } } @@ -276,7 +273,6 @@ private bool UninstallScriptHelper(string pkgPath, string pkgName, out ErrorReco var exMessage = String.Format("Script '{0}' could not be deleted: {1}", pkgPath, err.Message); var ex = new ArgumentException(exMessage); var ErrorDeletingScript = new ErrorRecord(ex, "ErrorDeletingScript", ErrorCategory.PermissionDenied, null); - //WriteError(ErrorDeletingScript); errRecord = ErrorDeletingScript; } @@ -309,7 +305,6 @@ private bool CheckIfDependency(string pkgName, out ErrorRecord errRecord) var exMessage = String.Format("Error checking if resource is a dependency: {0}. If you would still like to uninstall, rerun the command with -Force", e.Message); var ex = new ArgumentException(exMessage); var DependencyCheckError = new ErrorRecord(ex, "DependencyCheckError", ErrorCategory.OperationStopped, null); - // WriteError(DependencyCheckError); errRecord = DependencyCheckError; } @@ -321,10 +316,9 @@ private bool CheckIfDependency(string pkgName, out ErrorRecord errRecord) var exMessage = String.Format("Cannot uninstall '{0}', the following package(s) take a dependency on this package: {1}. If you would still like to uninstall, rerun the command with -Force", pkgName, strUniquePkgNames); var ex = new ArgumentException(exMessage); var PackageIsaDependency = new ErrorRecord(ex, "PackageIsaDependency", ErrorCategory.OperationStopped, null); - //WriteError(PackageIsaDependency); errRecord = PackageIsaDependency; - return true; + return true; } } return false; diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index f256ce953..de6e56d5a 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -149,4 +149,4 @@ Describe 'Test Uninstall-PSResource for Modules' { $pkg = Get-Module "RequiredModule1" -ListAvailable $pkg | Should -Be $null } -} \ No newline at end of file +} From ec9cb14c789b851d0b1251e571b90db239c35c8e Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 12:13:05 -0700 Subject: [PATCH 09/13] Remove uninstall test checking default -version param (will be added in another PR) --- test/UninstallPSResource.Tests.ps1 | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index de6e56d5a..9296303ca 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -55,16 +55,6 @@ Describe 'Test Uninstall-PSResource for Modules' { $pkgs.Version | Should -Not -Contain "2.5.0" } - It "Uninstall all versions of a module when given name and not specifying any version param" { - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue - $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - - $res = Uninstall-PSResource -Name "Carbon" - $pkgs = Get-Module ContosoServer -ListAvailable - $pkgs | Should -Be $null - } - It "Uninstall module when given Name and specifying exact version" { $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue From 30b31bf0739fb5cf2160cb36bf3f1c2ac41cb96b Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 14:09:02 -0700 Subject: [PATCH 10/13] Add Get-IsWindows utils function for tests --- test/InstallPSResource.Tests.ps1 | 12 ++++++------ test/PSGetTestUtils.psm1 | 10 +++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index f90d9ce46..0a3a245c8 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -125,7 +125,7 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under CurrentUser scope - Windows only" -Skip:(!$IsWindows) { + It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope CurrentUser $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -133,7 +133,7 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under AllUsers scope - Windows only" -Skip:(!$IsWindows) { + It "Install resource under AllUsers scope - Windows only" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -141,7 +141,7 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under no specified scope - Windows only" -Skip:(!$IsWindows) { + It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -150,7 +150,7 @@ Describe 'Test Install-PSResource for Module' { # Unix only # Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules' - It "Install resource under CurrentUser scope - Unix only" -Skip:($IsWindows) { + It "Install resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope CurrentUser $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -159,7 +159,7 @@ Describe 'Test Install-PSResource for Module' { # Unix only # Expected path should be similar to: '/usr/local/share/powershell/Modules' - It "Install resource under AllUsers scope - Unix only" -Skip:($IsWindows) { + It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" @@ -168,7 +168,7 @@ Describe 'Test Install-PSResource for Module' { # Unix only # Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules' - It "Install resource under no specified scope - Unix only" -Skip:($IsWindows) { + It "Install resource under no specified scope - Unix only" -Skip:(Get-IsWindows) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" diff --git a/test/PSGetTestUtils.psm1 b/test/PSGetTestUtils.psm1 index f3410ffaf..4cae5c5ec 100644 --- a/test/PSGetTestUtils.psm1 +++ b/test/PSGetTestUtils.psm1 @@ -7,7 +7,11 @@ $script:EnvPATHValueBackup = $null $script:PowerShellGet = 'PowerShellGet' $script:IsInbox = $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase) -$script:IsWindows = (-not (Get-Variable -Name IsWindows -ErrorAction Ignore)) -or $IsWindows +$script:IsWindows = $IsWindows +if ($IsWindows -eq $null) { + $script:IsWindows = ($PSVersionTable.PSVersion.Major -eq 5) +} + $script:IsLinux = (Get-Variable -Name IsLinux -ErrorAction Ignore) -and $IsLinux $script:IsMacOS = (Get-Variable -Name IsMacOS -ErrorAction Ignore) -and $IsMacOS $script:IsCoreCLR = $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core' @@ -96,6 +100,10 @@ $script:moduleSourcesFilePath = Microsoft.PowerShell.Management\Join-Path -Path $script:CurrentPSGetFormatVersion = "1.0" $script:PSGetFormatVersionPrefix = "PowerShellGetFormatVersion_" +function Get-IsWindows { + return $script:IsWindows +} + function Get-AllUsersModulesPath { return $script:ProgramFilesModulesPath } From c3084765667a4f9736258781c082447a9b7ba43d Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 27 Jul 2021 14:40:42 -0700 Subject: [PATCH 11/13] Update uninstall tests for all versions (explict and default) --- test/UninstallPSResource.Tests.ps1 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index 9296303ca..511f43016 100644 --- a/test/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResource.Tests.ps1 @@ -47,7 +47,20 @@ Describe 'Test Uninstall-PSResource for Modules' { $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue - $res = Uninstall-PSResource -Name "Carbon" -version "*" + $res = Uninstall-PSResource -Name ContosoServer -version "*" + $pkgs = Get-Module ContosoServer -ListAvailable + $pkgs.Version | Should -Not -Contain "1.0.0" + $pkgs.Version | Should -Not -Contain "1.5.0" + $pkgs.Version | Should -Not -Contain "2.0.0" + $pkgs.Version | Should -Not -Contain "2.5.0" + } + + It "Uninstall a module when given name and using the default version (ie all versions, not explicitly specified)" { + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.0.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "1.5.0" -TrustRepository -WarningAction SilentlyContinue + $null = Install-PSResource ContosoServer -Repository $TestGalleryName -Version "2.0.0" -TrustRepository -WarningAction SilentlyContinue + + $res = Uninstall-PSResource -Name ContosoServer $pkgs = Get-Module ContosoServer -ListAvailable $pkgs.Version | Should -Not -Contain "1.0.0" $pkgs.Version | Should -Not -Contain "1.5.0" From c18494b39f691ac1a5defec21983b74ac277e08e Mon Sep 17 00:00:00 2001 From: alerickson <25858831+alerickson@users.noreply.github.com> Date: Tue, 27 Jul 2021 15:36:29 -0700 Subject: [PATCH 12/13] Add new line to test/GetInstalledPSResource.Tests.ps1 Co-authored-by: Anam Navied From a336c42bb900e6cf63d4a52c108635f6f08b4c72 Mon Sep 17 00:00:00 2001 From: alerickson <25858831+alerickson@users.noreply.github.com> Date: Tue, 27 Jul 2021 15:36:48 -0700 Subject: [PATCH 13/13] Add new line to test/UninstallPSResource.Tests.ps1 Co-authored-by: Anam Navied