Skip to content

Commit

Permalink
(chocolateyGH-63) Support passing sources by name to push command.
Browse files Browse the repository at this point in the history
If multiple sources are passed throw exception.
  • Loading branch information
DamianMaslanka5 authored and TheCakeIsNaOH committed Jan 17, 2022
1 parent 7bdb81b commit 91bb317
Show file tree
Hide file tree
Showing 2 changed files with 44 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 @@ -196,6 +197,33 @@ 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");
}
}

public class when_handling_validation : ChocolateyPushCommandSpecsBase
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 @@ -100,6 +101,21 @@ The latter source url is now considered deprecated and will not be

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

0 comments on commit 91bb317

Please sign in to comment.