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. This is returned as a Tuple, which is not
ideal as it's members are not named, but this should be able to be
switched to named members after this is uplifted to .Net 4.7 or higher.
  • Loading branch information
TheCakeIsNaOH committed Mar 21, 2022
1 parent 3a9779f commit 99e24f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,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 @@ -609,8 +609,8 @@ public virtual ConcurrentDictionary<string, PackageResult> install_run(Chocolate
}
finally
{
var installFailures = report_action_summary(packageInstalls, "installed");
if (installFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResults = report_action_summary(packageInstalls, "installed");
if (actionSummaryResults.Item2 != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ public void upgrade_noop(ChocolateyConfiguration config)
var noopUpgrades = perform_source_runner_function(config, r => r.upgrade_noop(config, action));
if (config.RegularOutput)
{
var noopFailures = report_action_summary(noopUpgrades, "can upgrade");
var actionSummaryResult = report_action_summary(noopUpgrades, "can upgrade");
}

randomly_notify_about_pro_business(config);
Expand Down Expand Up @@ -804,12 +804,17 @@ public virtual ConcurrentDictionary<string, PackageResult> upgrade_run(Chocolate
}
finally
{
var upgradeFailures = report_action_summary(packageUpgrades, "upgraded");
if (upgradeFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResult = report_action_summary(packageUpgrades, "upgraded");
if (actionSummaryResult.Item2 != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}

if ((actionSummaryResult.Item1 + actionSummaryResult.Item2 == 0) && Environment.ExitCode == 0)
{
Environment.ExitCode = 2;
}

randomly_notify_about_pro_business(config);
}

Expand Down Expand Up @@ -880,13 +885,13 @@ public virtual ConcurrentDictionary<string, PackageResult> uninstall_run(Chocola
}
finally
{
var uninstallFailures = report_action_summary(packageUninstalls, "uninstalled");
if (uninstallFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResult = report_action_summary(packageUninstalls, "uninstalled");
if (actionSummaryResult.Item2 != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}

if (uninstallFailures != 0)
if (actionSummaryResult.Item2 != 0)
{
this.Log().Warn(@"
If a package uninstall is failing and/or you've already uninstalled the
Expand All @@ -912,7 +917,8 @@ this option only as a last resort.
return packageUninstalls;
}

private int report_action_summary(ConcurrentDictionary<string, PackageResult> packageResults, string actionName)
//TODO once project upgraded to .Net 4.8, switch return type to (int successes, int failures, int warnings, int rebootPackages)
private Tuple<int,int,int,int> report_action_summary(ConcurrentDictionary<string, PackageResult> packageResults, string actionName)
{
var successes = packageResults.or_empty_list_if_null().Where(p => p.Value.Success && !p.Value.Inconclusive);
var failures = packageResults.Count(p => !p.Value.Success);
Expand Down Expand Up @@ -980,7 +986,7 @@ The recent package changes indicate a reboot is necessary.
}
}

return failures;
return new Tuple<int, int, int, int>(successes.Count(), failures, warnings, rebootPackages);
}

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

0 comments on commit 99e24f4

Please sign in to comment.