Skip to content

Commit 2928df4

Browse files
author
anamnavi
committed
add tests
1 parent f2b9921 commit 2928df4

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/code/UninstallPSResource.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ private bool UninstallPkgHelper(out List<ErrorRecord> errRecords)
182182
WriteDebug("In UninstallPSResource::UninstallPkgHelper");
183183
var successfullyUninstalled = false;
184184
GetHelper getHelper = new GetHelper(this);
185+
186+
HashSet<string> requestedPackageNames = new HashSet<string>(Name, StringComparer.InvariantCultureIgnoreCase);
185187
List<string> dirsToDelete = getHelper.FilterPkgPathsByName(Name, _pathsToSearch);
186188
int totalDirs = dirsToDelete.Count;
187189
errRecords = new List<ErrorRecord>();
@@ -257,15 +259,19 @@ private bool UninstallPkgHelper(out List<ErrorRecord> errRecords)
257259

258260
return successfullyUninstalled;
259261
}
262+
263+
requestedPackageNames.Remove(pkgName);
260264
}
261265

262266
// 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
263-
if (currentUninstalledDirCount == 0)
267+
// if (currentUninstalledDirCount == 0)
268+
if (requestedPackageNames.Count > 0)
264269
{
270+
string[] pkgsFailedToUninstall = requestedPackageNames.ToArray();
265271
string prereleaseMessage = Prerelease ? "prerelease " : String.Empty;
266272
string versionMessage = Version != null ? $"matching '{Version} '" : String.Empty;
267273

268-
string warningMessage = $"Cannot uninstall {prereleaseMessage}version(s) {versionMessage}of resource '{String.Join(", ", Name)}' because it does not exist.";
274+
string warningMessage = $"Cannot uninstall {prereleaseMessage}version(s) {versionMessage}of resource '{String.Join(", ", pkgsFailedToUninstall)}' because it does not exist.";
269275

270276
WriteWarning(warningMessage);
271277
}

test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)