From 99e24f4d2e78b497c1ee14a605afc5ede34f77cd Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Fri, 14 Jan 2022 21:53:28 -0600 Subject: [PATCH] (#2200) upgrade exit 2 if nothing to do 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. --- .../commands/ChocolateyUpgradeCommand.cs | 1 + .../services/ChocolateyPackageService.cs | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs index 06a19b1267..0dd8bb786b 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs @@ -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 diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 2143f89c40..1431ccf7db 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -609,8 +609,8 @@ public virtual ConcurrentDictionary 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; } @@ -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); @@ -804,12 +804,17 @@ public virtual ConcurrentDictionary 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); } @@ -880,13 +885,13 @@ public virtual ConcurrentDictionary 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 @@ -912,7 +917,8 @@ this option only as a last resort. return packageUninstalls; } - private int report_action_summary(ConcurrentDictionary 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 report_action_summary(ConcurrentDictionary 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); @@ -980,7 +986,7 @@ The recent package changes indicate a reboot is necessary. } } - return failures; + return new Tuple(successes.Count(), failures, warnings, rebootPackages); } public virtual void handle_package_uninstall(PackageResult packageResult, ChocolateyConfiguration config)