@@ -110,6 +110,32 @@ Describe 'Test Uninstall-PSResource for Modules' -tags 'CI' {
110110 $pkgs.Version | Should -Not - Contain " 1.0.0"
111111 }
112112
113+ It " Do not uninstall existing module when requested version does not exist and write warning instead" {
114+ Uninstall-PSResource - Name $testModuleName - Version " 9.9.9" - SkipDependencyCheck - WarningVariable warn - WarningAction SilentlyContinue
115+
116+ # Module should still be present since no prerelease versions were found
117+ $res = Get-InstalledPSResource - Name $testModuleName
118+ $res | Should -Not - BeNullOrEmpty
119+ $res.Name | Should - Be $testModuleName
120+
121+ # Warning should have been written
122+ $warn.Count | Should - Be 1
123+ $warn [0 ] | Should -Match " Cannot uninstall version"
124+ }
125+
126+ It " Do not uninstall existing module when requested version range does not exist and write warning instead" {
127+ Uninstall-PSResource - Name $testModuleName - Version " [9.9.9, 10.0.0]" - SkipDependencyCheck - WarningVariable warn - WarningAction SilentlyContinue
128+
129+ # Module should still be present since no prerelease versions were found
130+ $res = Get-InstalledPSResource - Name $testModuleName
131+ $res | Should -Not - BeNullOrEmpty
132+ $res.Name | Should - Be $testModuleName
133+
134+ # Warning should have been written
135+ $warn.Count | Should - Be 1
136+ $warn [0 ] | Should -Match " Cannot uninstall version"
137+ }
138+
113139 $testCases = @ {Version = " [1.0.0.0]" ; ExpectedVersion = " 1.0.0.0" ; Reason = " validate version, exact match" },
114140 @ {Version = " 1.0.0.0" ; ExpectedVersion = " 1.0.0.0" ; Reason = " validate version, exact match without bracket syntax" },
115141 @ {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' {
235261 $stableVersionPkgs.Count | Should - Be 2
236262 }
237263
264+ It " Write warning when using -Prerelease flag with only stable versions installed" {
265+ # $testModuleName (test_module2) only has stable versions installed
266+ $pkg = Get-InstalledPSResource $testModuleName
267+ $pkg | Should -Not - BeNullOrEmpty
268+
269+ # Try to uninstall with -Prerelease flag, should show warning
270+ Uninstall-PSResource - Name $testModuleName - Prerelease - SkipDependencyCheck - WarningVariable warn - WarningAction SilentlyContinue
271+
272+ # Module should still be present since no prerelease versions were found
273+ $res = Get-InstalledPSResource - Name $testModuleName
274+ $res | Should -Not - BeNullOrEmpty
275+ $res.Name | Should - Be $testModuleName
276+ $res.Version | Should - Be $pkg.Version
277+
278+ # Warning should have been written
279+ $warn.Count | Should - Be 1
280+ $warn [0 ] | Should -Match " Cannot uninstall prerelease version"
281+ }
282+
283+ It " Write warning when multiple modules are requested to be uninstalled but one does not exist" {
284+ Uninstall-PSResource $testModuleName , " nonExistantModule" - SkipDependencyCheck - WarningVariable warn - WarningAction SilentlyContinue
285+ $res = Get-InstalledPSResource - Name $testModuleName
286+ $res | Should - BeNullOrEmpty
287+
288+ # Warning should have been written
289+ $warn.Count | Should - Be 1
290+ $warn [0 ] | Should -Match " Cannot uninstall version"
291+ }
292+
238293 It " Uninstall module using -WhatIf, should not uninstall the module" {
239294 Start-Transcript .\testUninstallWhatIf.txt
240295 Uninstall-PSResource - Name $testModuleName - WhatIf - SkipDependencyCheck
0 commit comments