Skip to content

Commit

Permalink
Merge pull request #2430 from TheCakeIsNaOH/default-push-source
Browse files Browse the repository at this point in the history
(#62) Default push source config
  • Loading branch information
vexx32 authored Mar 17, 2023
2 parents 4b65eca + e3950d0 commit 620d327
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace chocolatey.tests.infrastructure.app.commands
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.commandline;
using Moq;
using NUnit.Framework;
using Should;

public class ChocolateyPushCommandSpecs
Expand Down Expand Up @@ -146,6 +147,7 @@ public void should_set_the_source_to_default_feed_if_not_set_explicitly()
{
reset();
configuration.Sources = "";
configuration.PushCommand.DefaultSource = string.Empty;
because();

configuration.Sources.ShouldEqual(ApplicationParameters.ChocolateyCommunityFeedPushSource);
Expand All @@ -156,6 +158,7 @@ public void should_not_check_for_fallback_community_url()
{
reset();
configuration.Sources = "";
configuration.PushCommand.DefaultSource = "";
configSettingsService.Setup(c => c.get_api_key(
It.Is<ChocolateyConfiguration>(config => config.Sources.is_equal_to(ApplicationParameters.ChocolateyCommunityFeedPushSourceOld)),
null))
Expand All @@ -170,6 +173,64 @@ public void should_not_check_for_fallback_community_url()
configuration.PushCommand.Key.ShouldNotEqual(apiKey);
}

[Fact]
public void should_set_the_source_to_defaultpushsource_if_set_and_no_explicit_source()
{
reset();
configuration.Sources = "";
configuration.PushCommand.DefaultSource = "https://localhost/default/source";
because();

configuration.Sources.ShouldEqual("https://localhost/default/source");
}

[Fact]
public void should_not_override_explicit_source_if_defaultpushsource_is_set()
{
reset();
configuration.Sources = "https://localhost/somewhere/out/there";
configuration.PushCommand.DefaultSource = "https://localhost/default/source";
because();

configuration.Sources.ShouldEqual("https://localhost/somewhere/out/there");
}

[Fact]
public void should_throw_when_defaultpushsource_is_disabled_and_no_explicit_sources()
{
reset();
configuration.PushCommand.DefaultSource = "disabled";
configuration.Sources = "";

var errorred = false;
Exception error = null;

try
{
because();
}
catch (Exception ex)
{
errorred = true;
error = ex;
}

errorred.ShouldBeTrue();
error.ShouldNotBeNull();
error.ShouldBeType<ApplicationException>();
error.Message.ShouldContain("Default push source is disabled.");
}

[Fact]
public void should_continue_when_defaultpushsource_is_disabled_and_explicit_sources_passed()
{
reset();
configuration.Sources = "https://somewhere/out/there";
configuration.PushCommand.Key = "bob";
configuration.PushCommand.DefaultSource = "disabled";
because();
}

[Fact]
public void should_not_set_the_apiKey_if_source_is_not_found()
{
Expand Down Expand Up @@ -216,6 +277,52 @@ public void should_not_try_to_determine_the_key_if_source_is_set_for_an_unc_sour

configSettingsService.Verify(c => c.get_api_key(It.IsAny<ChocolateyConfiguration>(), It.IsAny<Action<ConfigFileApiKeySetting>>()), Times.Never);
}

[Fact]
public void should_throw_if_multiple_sources_are_passed()
{
reset();
configuration.Sources = "https://localhost/somewhere/out/there;https://localhost/somewhere/out/there";

Assert.Throws<ApplicationException>(() => because(), "Multiple sources are not support by push command.");
}

[Fact]
public void should_update_source_if_alias_is_passed()
{
reset();
configuration.Sources = "chocolatey";
configuration.MachineSources = new List<MachineSourceConfiguration>
{
new MachineSourceConfiguration
{
Name = "chocolatey",
Key = "https://localhost/somewhere/out/there"
}
};
because();

configuration.Sources.ShouldEqual("https://localhost/somewhere/out/there");
}

[Fact]
public void should_update_source_if_alias_is_passed_via_defaultpushsource()
{
reset();
configuration.Sources = "";
configuration.PushCommand.DefaultSource = "myrepo";
configuration.MachineSources = new List<MachineSourceConfiguration>
{
new MachineSourceConfiguration
{
Name = "myrepo",
Key = "https://localhost/somewhere/out/there"
}
};
because();

configuration.Sources.ShouldEqual("https://localhost/somewhere/out/there");
}
}

public class when_handling_validation : ChocolateyPushCommandSpecsBase
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public static class ConfigSettings
public static readonly string WebRequestTimeoutSeconds = "webRequestTimeoutSeconds";
public static readonly string UpgradeAllExceptions = "upgradeAllExceptions";
public static readonly string DefaultTemplateName = "defaultTemplateName";
public static readonly string DefaultPushSource = "defaultPushSource";
}

public static class Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ private static void set_config_items(ChocolateyConfiguration config, ConfigFileS
config.Proxy.BypassOnLocal = set_config_item(ApplicationParameters.ConfigSettings.ProxyBypassOnLocal, configFileSettings, "true", "Bypass proxy for local connections. Available in 0.10.4+.").is_equal_to(bool.TrueString);
config.UpgradeCommand.PackageNamesToSkip = set_config_item(ApplicationParameters.ConfigSettings.UpgradeAllExceptions, configFileSettings, string.Empty, "A comma-separated list of package names that should not be upgraded when running `choco upgrade all'. Defaults to empty. Available in 0.10.14+.");
config.DefaultTemplateName = set_config_item(ApplicationParameters.ConfigSettings.DefaultTemplateName, configFileSettings, string.Empty, "Default template name used when running 'choco new' command. Available in 0.12.0+.");
config.PushCommand.DefaultSource = set_config_item(ApplicationParameters.ConfigSettings.DefaultPushSource, configFileSettings, string.Empty, "Default source to push packages to when running 'choco push' command. Available in 0.12.0 +.");
}

private static string set_config_item(string configName, ConfigFileSettings configFileSettings, string defaultValue, string description, bool forceSettingValue = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.app.commands
{
using System;
using System.Collections.Generic;
using System.Linq;
using attributes;
using commandline;
using configuration;
Expand Down Expand Up @@ -59,13 +60,39 @@ public virtual void handle_additional_argument_parsing(IList<string> unparsedArg
{
configuration.Input = string.Join(" ", unparsedArguments); // path to .nupkg - assume relative

if (string.IsNullOrWhiteSpace(configuration.Sources) && !string.IsNullOrWhiteSpace(configuration.PushCommand.DefaultSource))
{
configuration.Sources = configuration.PushCommand.DefaultSource;

// Can't put this in handle_validation, since "disabled" is not a URI, it would fail earlier
if (configuration.Sources.is_equal_to("disabled"))
{
throw new ApplicationException("Default push source is disabled. Please pass a source to push to, such as --source={0}".format_with(ApplicationParameters.ChocolateyCommunityFeedPushSource));
}
}

if (string.IsNullOrWhiteSpace(configuration.Sources))
{
configuration.Sources = ApplicationParameters.ChocolateyCommunityFeedPushSource;
}

if (!string.IsNullOrWhiteSpace(configuration.Sources))
{
IEnumerable<string> sources = configuration.Sources.Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);

if (sources.Count() > 1)
{
throw new ApplicationException("Multiple sources are not support by push command.");
}

var machineSource = configuration.MachineSources.FirstOrDefault(m => m.Name.is_equal_to(configuration.Sources));
if (machineSource != null)
{
"chocolatey".Log().Debug("Switching source name {0} to actual source value '{1}'.".format_with(configuration.Sources, machineSource.Key.to_string()));

configuration.Sources = machineSource.Key;
}

var remoteSource = new Uri(configuration.Sources);
if (string.IsNullOrWhiteSpace(configuration.PushCommand.Key) && !remoteSource.IsUnc && !remoteSource.IsFile)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ public PackCommandConfiguration()
public sealed class PushCommandConfiguration
{
public string Key { get; set; }
public string DefaultSource { get; set; }
//DisableBuffering?
}

Expand Down

0 comments on commit 620d327

Please sign in to comment.