Skip to content

Commit

Permalink
(GH-365) Exit Non-zero if newer version installed
Browse files Browse the repository at this point in the history
If an older version of a package is requested and allow multiple
versions is not selected, choco will do nothing but exit with zero.
This gives the appearance that choco has installed that version for
automated tools when in fact it has done nothing. Instead it should
exit non-zero with an error.

Allow choco to attempt to install/upgrade to older versions with an
additional `--allow-downgrade` switch.
  • Loading branch information
ferventcoder committed Aug 17, 2015
1 parent 584fc2d commit ad6de62
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati
.Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=",
"PackageParameters - Parameters to pass to the package. Defaults to unspecified.",
option => configuration.PackageParameters = option.remove_surrounding_quotes())
.Add("allowdowngrade|allow-downgrade",
"AllowDowngrade - Should an attempt at downgrading be allowed? Defaults to false.",
option => configuration.AllowDowngrade = option != null)
.Add("m|sxs|sidebyside|side-by-side|allowmultiple|allow-multiple|allowmultipleversions|allow-multiple-versions",
"AllowMultipleVersions - Should multiple versions of a package be installed? Defaults to false.",
option => configuration.AllowMultipleVersions = option != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
.Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=",
"PackageParameters - Parameters to pass to the package. Defaults to unspecified.",
option => configuration.PackageParameters = option.remove_surrounding_quotes())
.Add("allowdowngrade|allow-downgrade",
"AllowDowngrade - Should an attempt at downgrading be allowed? Defaults to false.",
option => configuration.AllowDowngrade = option != null)
.Add("m|sxs|sidebyside|side-by-side|allowmultiple|allow-multiple|allowmultipleversions|allow-multiple-versions",
"AllowMultipleVersions - Should multiple versions of a package be installed? Defaults to false.",
option => configuration.AllowMultipleVersions = option != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ private void append_output(StringBuilder propertyValues, string append)
public string PackageParameters { get; set; }
public bool IgnoreDependencies { get; set; }
public bool AllowMultipleVersions { get; set; }
public bool AllowDowngrade { get; set; }
public bool ForceDependencies { get; set; }

/// <summary>
Expand Down
22 changes: 20 additions & 2 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
version = installedPackage.Version;
}

if (installedPackage != null && version != null && version < installedPackage.Version && !config.AllowMultipleVersions && !config.AllowDowngrade)
{
string logMessage = "A newer version of {0} (v{1}) is already installed.{2} Use --allow-downgrade to attempt to install older versions, or use side by side to allow multiple versions.".format_with(installedPackage.Id, installedPackage.Version, Environment.NewLine);
var nullResult = packageInstalls.GetOrAdd(packageName, new PackageResult(installedPackage, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id)));
nullResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
this.Log().Error(ChocolateyLoggers.Important, logMessage);
continue;
}

IPackage availablePackage = packageManager.SourceRepository.FindPackage(packageName, version, config.Prerelease, allowUnlisted: false);
if (availablePackage == null)
{
Expand Down Expand Up @@ -463,6 +472,15 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
continue;
}

if (version != null && version < installedPackage.Version && !config.AllowMultipleVersions && !config.AllowDowngrade)
{
string logMessage = "A newer version of {0} (v{1}) is already installed.{2} Use --allow-downgrade to attempt to upgrade to older versions, or use side by side to allow multiple versions.".format_with(installedPackage.Id, installedPackage.Version, Environment.NewLine);
var nullResult = packageInstalls.GetOrAdd(packageName, new PackageResult(installedPackage, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id)));
nullResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
this.Log().Error(ChocolateyLoggers.Important, logMessage);
continue;
}

var pkgInfo = _packageInfoService.get_package_information(installedPackage);
bool isPinned = pkgInfo != null && pkgInfo.IsPinned;

Expand Down Expand Up @@ -503,7 +521,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu

var packageResult = packageInstalls.GetOrAdd(packageName, new PackageResult(availablePackage, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, availablePackage.Id)));

if ((installedPackage.Version > availablePackage.Version))
if (installedPackage.Version > availablePackage.Version && !config.AllowDowngrade)
{
string logMessage = "{0} v{1} is newer than the most recent.{2} You must be smarter than the average bear...".format_with(installedPackage.Id, installedPackage.Version, Environment.NewLine);
packageResult.Messages.Add(new ResultMessage(ResultType.Inconclusive, logMessage));
Expand Down Expand Up @@ -553,7 +571,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
if (config.RegularOutput) this.Log().Info(logMessage);
}

if ((availablePackage.Version > installedPackage.Version) || config.Force)
if ((availablePackage.Version > installedPackage.Version) || config.Force || (availablePackage.Version < installedPackage.Version && config.AllowDowngrade))
{
if (availablePackage.Version > installedPackage.Version)
{
Expand Down

0 comments on commit ad6de62

Please sign in to comment.