Skip to content

Commit

Permalink
(GH-490) Exception if no source is enabled
Browse files Browse the repository at this point in the history
Previously if no sources were available during installation of a
package, installation would fail with an exception. This fix will make
the choco client check the source list to make sure it is not null
before attempting to install, update, or list packages.
  • Loading branch information
MikeFoden authored and ferventcoder committed Jan 18, 2016
1 parent d9d59ff commit 62cd0e0
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 5 deletions.
18 changes: 15 additions & 3 deletions Scenarios.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Chocolatey Usage Scenarios

### ChocolateyInstallCommand [ 34 Scenario(s), 287 Observation(s) ]
### ChocolateyInstallCommand [ 35 Scenario(s), 288 Observation(s) ]

#### when force installing a package that depends on an unavailable newer version of an installed dependency forcing dependencies

Expand Down Expand Up @@ -325,6 +325,10 @@
* [PENDING] should not install the conflicting package in the lib directory
* [PENDING] should not upgrade the exact version dependency

#### when installing a package with no sources enabled

* should have no sources enabled result

#### when installing a side by side package

* config should match package result name
Expand Down Expand Up @@ -391,7 +395,7 @@
* should not have inconclusive package result
* should not have warning package result

### ChocolateyListCommand [ 6 Scenario(s), 30 Observation(s) ]
### ChocolateyListCommand [ 7 Scenario(s), 31 Observation(s) ]

#### when listing local packages

Expand All @@ -408,6 +412,10 @@
* should not contain packages and versions with a space between them
* should only have messages related to package information

#### when listing packages with no sources enabled

* should have no sources enabled result

#### when searching all available packages

* should contain a summary
Expand Down Expand Up @@ -613,7 +621,7 @@

* should throw an error that it is not allowed

### ChocolateyUpgradeCommand [ 26 Scenario(s), 214 Observation(s) ]
### ChocolateyUpgradeCommand [ 27 Scenario(s), 215 Observation(s) ]

#### when force upgrading a package

Expand Down Expand Up @@ -853,6 +861,10 @@
* should upgrade the minimum version dependency
* should upgrade the package

#### when upgrading a package with no sources enabled

* should have no sources enabled result

#### when upgrading a package with readonly files

* should contain a warning message that it upgraded successfully
Expand Down
28 changes: 28 additions & 0 deletions src/chocolatey.tests.integration/scenarios/InstallScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3333,5 +3333,33 @@ public void should_add_the_insert_value_in_the_config_due_to_XDT_InsertIfMissing
_xPathNavigator.SelectSingleNode("//configuration/appSettings/add[@key='insert']/@value").TypedValue.to_string().ShouldEqual("1.0.0");
}
}

[Concern(typeof(ChocolateyInstallCommand))]
public class when_installing_a_package_with_no_sources_enabled : ScenariosBase
{

public override void Context()
{
base.Context();
Configuration.Sources = null;
}

public override void Because()
{
Results = Service.install_run(Configuration);
}

[Fact]
public void should_have_no_sources_enabled_result()
{
MockLogger.contains_message("Installation was NOT successful. There are no sources enabled for", LogLevel.Error).ShouldBeTrue();
}

[Fact]
public void should_not_install_any_packages()
{
Results.Count().ShouldEqual(0);
}
}
}
}
30 changes: 30 additions & 0 deletions src/chocolatey.tests.integration/scenarios/ListScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,35 @@ public void should_not_contain_debugging_messages()
MockLogger.contains_message("End of List", LogLevel.Debug).ShouldBeFalse();
}
}

[Concern(typeof(ChocolateyListCommand))]
public class when_listing_packages_with_no_sources_enabled : ScenariosBase
{

public override void Context()
{
base.Context();
Configuration.Sources = null;
}

public override void Because()
{
MockLogger.reset();
Results = Service.list_run(Configuration).ToList();
}

[Fact]
public void should_have_no_sources_enabled_result()
{
MockLogger.contains_message("Unable to search for packages when there are no sources enabled for", LogLevel.Error).ShouldBeTrue();
}

[Fact]
public void should_not_list_any_packages()
{
Results.Count().ShouldEqual(0);
}

}
}
}
27 changes: 27 additions & 0 deletions src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2382,5 +2382,32 @@ public void should_have_a_config_with_the_comment_from_the_original()
}
}

[Concern(typeof(ChocolateyUpgradeCommand))]
public class when_upgrading_a_package_with_no_sources_enabled : ScenariosBase
{

public override void Context()
{
base.Context();
Configuration.Sources = null;
}

public override void Because()
{
Results = Service.upgrade_run(Configuration);
}

[Fact]
public void should_have_no_sources_enabled_result()
{
MockLogger.contains_message("Upgrading was NOT successful. There are no sources enabled for", LogLevel.Error).ShouldBeTrue();
}

[Fact]
public void should_not_have_any_packages_upgraded()
{
Results.Count().ShouldEqual(0);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.app.commands
{
using System.Collections.Generic;
using System.Linq;
using attributes;
using commandline;
using configuration;
Expand Down Expand Up @@ -115,7 +116,8 @@ public override void run(ChocolateyConfiguration configuration)
{
if (configuration.ListCommand.LocalOnly)
{
_packageService.list_run(configuration);
// note: you must leave the .ToList() here or else the method may not be evaluated!
_packageService.list_run(configuration).ToList();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public void list_noop(ChocolateyConfiguration config)

public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config)
{
if (string.IsNullOrWhiteSpace(config.Sources))
{
this.Log().Error(ChocolateyLoggers.Important, @"Unable to search for packages when there are no sources enabled for
packages and none were passed as arguments.");
Environment.ExitCode = 1;
yield break;
}

if (config.RegularOutput) this.Log().Debug(() => "Searching for package information");

var packages = new List<IPackage>();
Expand Down Expand Up @@ -304,10 +312,19 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
{
this.Log().Info(@"Installing the following packages:");
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));
this.Log().Info(@"By installing you accept licenses for the packages.");

var packageInstalls = new ConcurrentDictionary<string, PackageResult>();

if (string.IsNullOrWhiteSpace(config.Sources))
{
this.Log().Error(ChocolateyLoggers.Important, @"Installation was NOT successful. There are no sources enabled for
packages and none were passed as arguments.");
Environment.ExitCode = 1;
return packageInstalls;
}

this.Log().Info(@"By installing you accept licenses for the packages.");

foreach (var packageConfig in set_config_from_package_names_and_packages_config(config, packageInstalls).or_empty_list_if_null())
{
Action<PackageResult> action = null;
Expand Down Expand Up @@ -497,6 +514,15 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
{
this.Log().Info(@"Upgrading the following packages:");
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));

if (string.IsNullOrWhiteSpace(config.Sources))
{
this.Log().Error(ChocolateyLoggers.Important, @"Upgrading was NOT successful. There are no sources enabled for
packages and none were passed as arguments.");
Environment.ExitCode = 1;
return new ConcurrentDictionary<string, PackageResult>();
}

this.Log().Info(@"By upgrading you accept licenses for the packages.");

foreach (var packageConfigFile in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Where(p => p.EndsWith(".config")).ToList())
Expand Down

0 comments on commit 62cd0e0

Please sign in to comment.