Skip to content

Commit

Permalink
(chocolatey#2200) upgrade exit 2 if nothing to do
Browse files Browse the repository at this point in the history
If enhanced exit codes are enabled, this sets the upgrade command to
exit with 2 if there is nothing to do (i.e. if no packages are
upgraded). This entails returning the number of successful package
results from the report_action_summary method.
  • Loading branch information
TheCakeIsNaOH committed Jun 16, 2023
1 parent 91c40c9 commit 636393f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ Exit codes that normally result from running this command.
Normal:
- 0: operation was successful, no issues detected
- -1 or 1: an error has occurred
- 2: nothing to do, no packages outdated
Package Exit Codes:
- 1641: success, reboot initiated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ public virtual ConcurrentDictionary<string, PackageResult> Install(ChocolateyCon
}
finally
{
var installFailures = ReportActionSummary(packageInstalls, "installed");
if (installFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResults = ReportActionSummary(packageInstalls, "installed");
if (actionSummaryResults.failures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
Expand Down Expand Up @@ -931,7 +931,7 @@ public void UpgradeDryRun(ChocolateyConfiguration config)

if (config.RegularOutput)
{
var noopFailures = ReportActionSummary(noopUpgrades, "can upgrade");
var actionSummaryResult = ReportActionSummary(noopUpgrades, "can upgrade");
}

RandomlyNotifyAboutLicensedFeatures(config);
Expand Down Expand Up @@ -991,12 +991,17 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
}
finally
{
var upgradeFailures = ReportActionSummary(packageUpgrades, "upgraded");
if (upgradeFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResult = ReportActionSummary(packageUpgrades, "upgraded");
if (actionSummaryResult.failures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}

if ((actionSummaryResult.successes + actionSummaryResult.failures == 0) && Environment.ExitCode == 0)
{
Environment.ExitCode = 2;
}

RandomlyNotifyAboutLicensedFeatures(config);
}

Expand Down Expand Up @@ -1085,13 +1090,13 @@ public virtual ConcurrentDictionary<string, PackageResult> Uninstall(ChocolateyC
}
finally
{
var uninstallFailures = ReportActionSummary(packageUninstalls, "uninstalled");
if (uninstallFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResults = ReportActionSummary(packageUninstalls, "uninstalled");
if (actionSummaryResults.failures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}

if (uninstallFailures != 0)
if (actionSummaryResults.failures != 0)
{
this.Log().Warn(@"
If a package uninstall is failing and/or you've already uninstalled the
Expand Down Expand Up @@ -1239,7 +1244,7 @@ private void BuildInstallExample(string packageName, StringBuilder sb, string co
}
}

private int ReportActionSummary(ConcurrentDictionary<string, PackageResult> packageResults, string actionName)
private (int successes, int failures, int warnings, int rebootPackages) ReportActionSummary(ConcurrentDictionary<string, PackageResult> packageResults, string actionName)
{
var successes = packageResults.OrEmpty().Where(p => p.Value.Success && !p.Value.Inconclusive).OrderBy(p => p.Value.Name);
var failures = packageResults.Count(p => !p.Value.Success);
Expand Down Expand Up @@ -1307,7 +1312,7 @@ The recent package changes indicate a reboot is necessary.
}
}

return failures;
return (successes.Count(), failures, warnings, rebootPackages);
}

public virtual void HandlePackageUninstall(PackageResult packageResult, ChocolateyConfiguration config)
Expand Down

0 comments on commit 636393f

Please sign in to comment.