diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 782fe3db7..0a69e4617 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) + if (!successfullyUninstalled || 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); - - 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)) + if (!Force && CheckIfDependency(pkgName, out errRecord)) { return false; } @@ -216,7 +223,7 @@ 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); + errRecord = ErrorDeletingParentDirectory; } } catch (Exception err) { @@ -224,15 +231,16 @@ 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); + 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 +266,22 @@ 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); + 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); + 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 +305,7 @@ 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); + errRecord = DependencyCheckError; } if (pkgsWithRequiredModules.Any()) @@ -307,7 +316,8 @@ 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); + errRecord = PackageIsaDependency; + return true; } } 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 diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 56238bfef..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" -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" -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,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:(!(Get-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:(Get-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:(Get-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:(Get-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 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 } diff --git a/test/UninstallPSResource.Tests.ps1 b/test/UninstallPSResource.Tests.ps1 index a1258d3ef..511f43016 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,147 @@ 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 } - 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 - } + $null = Install-PSResource BaseTestPackage -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue - It "Uninstall a module when given name and specifying all versions" { - $res = Uninstall-PSResource -Name "Carbon" -version "*" - $res | Should -BeNullOrEmpty + $res = Uninstall-PSResource -Name BaseTestPackage, ContosoServer + Get-Module ContosoServer, BaseTestPackage -ListAvailable | Should -be $null } - 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 specific script by name" { + $null = Install-PSResource Test-RPC -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 Test-RPC } - 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 list of scripts by name" { + $null = Install-PSResource adsql, airoute -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + + $pkg = Uninstall-PSResource -Name adsql, airoute } - 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 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 - $resPrerelease = Uninstall-PSResource -Name "test_module" -Prerelease - $resPrerelease.Version | Should -Be "5.2.5.0" + $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 module using -WhatIf, should not uninstall the module" { - $res = Uninstall-PSResource -Name "ActiveDirectoryTools" -WhatIf - } + 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 - It "Do not Uninstall module that is a dependency for another module" { - $res = Uninstall-PSResource -Name "PackageManagement" + $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" + $pkgs.Version | Should -Not -Contain "2.0.0" + $pkgs.Version | Should -Not -Contain "2.5.0" } - It "Uninstall module that is a dependency for another module using -Force" { - $res = Uninstall-PSResource -Name "PackageManagement" -Force + 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 + + $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 + + $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 "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) + 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 - $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 + $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 script using -WhatIf" { - $res = Uninstall-PSResource -Name "ActiveDirectoryTools" -WhatIf + It "Uninstall module that is a dependency for another module using -Force" { + $null = Install-PSResource "test_module" -Repository $TestGalleryName -TrustRepository -WarningAction SilentlyContinue + + $res = Uninstall-PSResource -Name "RequiredModule1" -Force + + $pkg = Get-Module "RequiredModule1" -ListAvailable + $pkg | Should -Be $null } } -#> \ No newline at end of file