Skip to content

Commit

Permalink
(chocolatey#62) Add unit tests for DefaultPushSource
Browse files Browse the repository at this point in the history
Adds tests for DefaultPushSource to check it works correctly.

If DefaultPushSource is set, it should set the source assuming that
a source is not passed in explicitly with --source. If a source is
passed in explicitly, then DefaultPushSource should not override it.

If DefaultPushSource is set to "disabled", it should throw an error if
there is not an explicit source passed in. But if an explicit source is
passed in, it should not error.

If the DefaultPushSource is set to the name/alias of a machine source,
it should resolve to that machine source.
  • Loading branch information
TheCakeIsNaOH authored and vexx32 committed Mar 17, 2023
1 parent 32b8af4 commit e3950d0
Showing 1 changed file with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,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 @@ -157,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 @@ -171,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 @@ -244,6 +304,25 @@ public void should_update_source_if_alias_is_passed()

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

0 comments on commit e3950d0

Please sign in to comment.