diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index c9a63adfd..3183900fd 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -182,6 +182,8 @@ private bool UninstallPkgHelper(out List errRecords) WriteDebug("In UninstallPSResource::UninstallPkgHelper"); var successfullyUninstalled = false; GetHelper getHelper = new GetHelper(this); + + HashSet requestedPackageNames = new HashSet(Name, StringComparer.InvariantCultureIgnoreCase); List dirsToDelete = getHelper.FilterPkgPathsByName(Name, _pathsToSearch); int totalDirs = dirsToDelete.Count; errRecords = new List(); @@ -257,6 +259,20 @@ private bool UninstallPkgHelper(out List errRecords) return successfullyUninstalled; } + + requestedPackageNames.Remove(pkgName); + } + + // the package requested for uninstallation was found by name, but not satisfied by version criteria (i.e version didn't exist or match prerelease criteria) so write error + if (requestedPackageNames.Count > 0) + { + string[] pkgsFailedToUninstall = requestedPackageNames.ToArray(); + string prereleaseMessage = Prerelease ? "prerelease " : String.Empty; + string versionMessage = Version != null ? $"matching '{Version} '" : String.Empty; + + string warningMessage = $"Cannot uninstall {prereleaseMessage}version(s) {versionMessage}of resource '{String.Join(", ", pkgsFailedToUninstall)}' because it does not exist."; + + WriteWarning(warningMessage); } return successfullyUninstalled; diff --git a/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 b/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 index f4723f612..45b9310a1 100644 --- a/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 @@ -110,6 +110,32 @@ Describe 'Test Uninstall-PSResource for Modules' -tags 'CI' { $pkgs.Version | Should -Not -Contain "1.0.0" } + It "Do not uninstall existing module when requested version does not exist and write warning instead" { + Uninstall-PSResource -Name $testModuleName -Version "9.9.9" -SkipDependencyCheck -WarningVariable warn -WarningAction SilentlyContinue + + # Module should still be present since no prerelease versions were found + $res = Get-InstalledPSResource -Name $testModuleName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $testModuleName + + # Warning should have been written + $warn.Count | Should -Be 1 + $warn[0] | Should -Match "Cannot uninstall version" + } + + It "Do not uninstall existing module when requested version range does not exist and write warning instead" { + Uninstall-PSResource -Name $testModuleName -Version "[9.9.9, 10.0.0]" -SkipDependencyCheck -WarningVariable warn -WarningAction SilentlyContinue + + # Module should still be present since no prerelease versions were found + $res = Get-InstalledPSResource -Name $testModuleName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $testModuleName + + # Warning should have been written + $warn.Count | Should -Be 1 + $warn[0] | Should -Match "Cannot uninstall version" + } + $testCases = @{Version="[1.0.0.0]"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match"}, @{Version="1.0.0.0"; ExpectedVersion="1.0.0.0"; Reason="validate version, exact match without bracket syntax"}, @{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersion="5.0.0.0"; Reason="validate version, exact range inclusive"}, @@ -235,6 +261,35 @@ Describe 'Test Uninstall-PSResource for Modules' -tags 'CI' { $stableVersionPkgs.Count | Should -Be 2 } + It "Write warning when using -Prerelease flag with only stable versions installed" { + # $testModuleName (test_module2) only has stable versions installed + $pkg = Get-InstalledPSResource $testModuleName + $pkg | Should -Not -BeNullOrEmpty + + # Try to uninstall with -Prerelease flag, should show warning + Uninstall-PSResource -Name $testModuleName -Prerelease -SkipDependencyCheck -WarningVariable warn -WarningAction SilentlyContinue + + # Module should still be present since no prerelease versions were found + $res = Get-InstalledPSResource -Name $testModuleName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $testModuleName + $res.Version | Should -Be $pkg.Version + + # Warning should have been written + $warn.Count | Should -Be 1 + $warn[0] | Should -Match "Cannot uninstall prerelease version" + } + + It "Write warning when multiple modules are requested to be uninstalled but one does not exist" { + Uninstall-PSResource $testModuleName, "nonExistantModule" -SkipDependencyCheck -WarningVariable warn -WarningAction SilentlyContinue + $res = Get-InstalledPSResource -Name $testModuleName + $res | Should -BeNullOrEmpty + + # Warning should have been written + $warn.Count | Should -Be 1 + $warn[0] | Should -Match "Cannot uninstall version" + } + It "Uninstall module using -WhatIf, should not uninstall the module" { Start-Transcript .\testUninstallWhatIf.txt Uninstall-PSResource -Name $testModuleName -WhatIf -SkipDependencyCheck