Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/code/UninstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ private bool UninstallPkgHelper(out List<ErrorRecord> errRecords)
WriteDebug("In UninstallPSResource::UninstallPkgHelper");
var successfullyUninstalled = false;
GetHelper getHelper = new GetHelper(this);

HashSet<string> requestedPackageNames = new HashSet<string>(Name, StringComparer.InvariantCultureIgnoreCase);
List<string> dirsToDelete = getHelper.FilterPkgPathsByName(Name, _pathsToSearch);
int totalDirs = dirsToDelete.Count;
errRecords = new List<ErrorRecord>();
Expand Down Expand Up @@ -257,6 +259,20 @@ private bool UninstallPkgHelper(out List<ErrorRecord> 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;
Expand Down
55 changes: 55 additions & 0 deletions test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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
Expand Down
Loading