Skip to content
Draft
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
17 changes: 17 additions & 0 deletions src/code/UninstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ private bool UninstallPkgHelper(out List<ErrorRecord> errRecords)
}
}

// If we had packages but didn't uninstall any (e.g., due to version/prerelease filters), write a warning
if (currentUninstalledDirCount == 0 && totalDirs > 0)
{
string warningMessage = Prerelease
? $"No prerelease versions of '{String.Join(", ", Name)}' were found to uninstall."
: $"No packages matching the specified criteria for '{String.Join(", ", Name)}' were found to uninstall.";

if (Version != null && !Version.Trim().Equals("*"))
{
warningMessage = Prerelease
? $"No prerelease versions of '{String.Join(", ", Name)}' matching version '{Version}' were found to uninstall."
: $"No packages of '{String.Join(", ", Name)}' matching version '{Version}' were found to uninstall.";
}

WriteWarning(warningMessage);
}

return successfullyUninstalled;
}

Expand Down
19 changes: 19 additions & 0 deletions test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ 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" {
# BeforeEach already installs a stable version of $testModuleName (5.0.0.0)
# Verify it's 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

# Warning should have been written
$warn.Count | Should -Be 1
$warn[0] | Should -Match "No prerelease versions"
}

It "Uninstall module using -WhatIf, should not uninstall the module" {
Start-Transcript .\testUninstallWhatIf.txt
Uninstall-PSResource -Name $testModuleName -WhatIf -SkipDependencyCheck
Expand Down
Loading