From 62cd0e0f5404dc3232845cb676000a62b8280b52 Mon Sep 17 00:00:00 2001 From: Mike Foden Date: Fri, 15 Jan 2016 23:31:31 +1100 Subject: [PATCH] (GH-490) Exception if no source is enabled 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. --- Scenarios.md | 18 +++++++++-- .../scenarios/InstallScenarios.cs | 28 +++++++++++++++++ .../scenarios/ListScenarios.cs | 30 +++++++++++++++++++ .../scenarios/UpgradeScenarios.cs | 27 +++++++++++++++++ .../commands/ChocolateyVersionCommand.cs | 4 ++- .../services/ChocolateyPackageService.cs | 28 ++++++++++++++++- 6 files changed, 130 insertions(+), 5 deletions(-) diff --git a/Scenarios.md b/Scenarios.md index 4817ac4073..d513f29882 100644 --- a/Scenarios.md +++ b/Scenarios.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs b/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs index a414604ab3..297a5dd109 100644 --- a/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs +++ b/src/chocolatey.tests.integration/scenarios/InstallScenarios.cs @@ -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); + } + } } } \ No newline at end of file diff --git a/src/chocolatey.tests.integration/scenarios/ListScenarios.cs b/src/chocolatey.tests.integration/scenarios/ListScenarios.cs index ef2ec14572..f6289eb948 100644 --- a/src/chocolatey.tests.integration/scenarios/ListScenarios.cs +++ b/src/chocolatey.tests.integration/scenarios/ListScenarios.cs @@ -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); + } + + } } } diff --git a/src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs b/src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs index 0c2a778107..8cb45eb71b 100644 --- a/src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs +++ b/src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs @@ -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); + } + } } } \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs index a3fd4de673..1f9e04224a 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyVersionCommand.cs @@ -16,6 +16,7 @@ namespace chocolatey.infrastructure.app.commands { using System.Collections.Generic; + using System.Linq; using attributes; using commandline; using configuration; @@ -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 { diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 356bc57480..f62b4616b4 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -107,6 +107,14 @@ public void list_noop(ChocolateyConfiguration config) public IEnumerable 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(); @@ -304,10 +312,19 @@ public ConcurrentDictionary 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(); + 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 action = null; @@ -497,6 +514,15 @@ public ConcurrentDictionary 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(); + } + 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())