From 736854d4471e197d67d9c8dbf9ffe73b21725cf6 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 16 Oct 2025 12:39:53 -0400 Subject: [PATCH 1/4] initial error message --- src/code/UninstallPSResource.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index c9a63adfd..6584ab982 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -259,6 +259,17 @@ private bool UninstallPkgHelper(out List errRecords) } } + // If we found package to uninstall by name, but not satisfied by version criteria (i.e version didn't exist or match prerelease criteria) write error + if (currentUninstalledDirCount == 0) + { + 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(", ", Name)}' because it does not exist."; + + WriteWarning(warningMessage); + } + return successfullyUninstalled; } From f2b9921db09e509a2d105b7e76c2570aac97f78c Mon Sep 17 00:00:00 2001 From: anamnavi Date: Fri, 17 Oct 2025 12:42:47 -0400 Subject: [PATCH 2/4] add warnings --- src/code/UninstallPSResource.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 6584ab982..de9297afb 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -259,13 +259,13 @@ private bool UninstallPkgHelper(out List errRecords) } } - // If we found package to uninstall by name, but not satisfied by version criteria (i.e version didn't exist or match prerelease criteria) write error + // 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 (currentUninstalledDirCount == 0) { string prereleaseMessage = Prerelease ? "prerelease " : String.Empty; - string versionMessage = Version != null ? "matching '{Version} '" : String.Empty; + string versionMessage = Version != null ? $"matching '{Version} '" : String.Empty; - string warningMessage = $"Cannot uninstall {prereleaseMessage}version(s) '{versionMessage}'of resource '{String.Join(", ", Name)}' because it does not exist."; + string warningMessage = $"Cannot uninstall {prereleaseMessage}version(s) {versionMessage}of resource '{String.Join(", ", Name)}' because it does not exist."; WriteWarning(warningMessage); } From 2928df4489459d1d8c4a93c905978680763382c8 Mon Sep 17 00:00:00 2001 From: anamnavi Date: Mon, 20 Oct 2025 00:15:54 -0400 Subject: [PATCH 3/4] add tests --- src/code/UninstallPSResource.cs | 10 +++- .../UninstallPSResource.Tests.ps1 | 55 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index de9297afb..31558f038 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,15 +259,19 @@ 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 (currentUninstalledDirCount == 0) + // if (currentUninstalledDirCount == 0) + 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(", ", Name)}' because it does not exist."; + string warningMessage = $"Cannot uninstall {prereleaseMessage}version(s) {versionMessage}of resource '{String.Join(", ", pkgsFailedToUninstall)}' because it does not exist."; WriteWarning(warningMessage); } 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 From f7f5e490078e901e3c50b34c878a368b6c060e1a Mon Sep 17 00:00:00 2001 From: anamnavi Date: Mon, 20 Oct 2025 00:17:55 -0400 Subject: [PATCH 4/4] clean up comment --- src/code/UninstallPSResource.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 31558f038..3183900fd 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -264,7 +264,6 @@ private bool UninstallPkgHelper(out List errRecords) } // 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 (currentUninstalledDirCount == 0) if (requestedPackageNames.Count > 0) { string[] pkgsFailedToUninstall = requestedPackageNames.ToArray();