Skip to content

Commit

Permalink
(chocolatey#72) Add ability to include sources from config
Browse files Browse the repository at this point in the history
This adds the --include-sources-from-config switch which will append
any sources in the config to manually specified sources (via --source).
It checks if the source already exists before appending, which prevents
duplication when the manually specified sources and the sources from
config are combined.
  • Loading branch information
TheCakeIsNaOH committed Dec 22, 2022
1 parent 0e6bca6 commit 661fe5e
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public void should_add_short_version_of_password_to_the_option_set()
{
optionSet.Contains("p").ShouldBeTrue();
}

[Fact]
public void should_add_include_sources_from_config_to_the_option_set()
{
optionSet.Contains("include-sources-from-config").ShouldBeTrue();
}
}

public class when_handling_additional_argument_parsing : ChocolateyInfoCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ public void should_add_short_version_of_skip_hooks_to_the_option_set()
{
optionSet.Contains("skiphooks").ShouldBeTrue();
}

[Fact]
public void should_add_include_sources_from_config_to_the_option_set()
{
optionSet.Contains("include-sources-from-config").ShouldBeTrue();
}
}

public class when_handling_additional_argument_parsing : ChocolateyInstallCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ public void should_add_short_version_of_password_to_the_option_set()
optionSet.Contains("p").ShouldBeTrue();
}

[Fact]
public void should_add_include_sources_from_config_to_the_option_set()
{
optionSet.Contains("include-sources-from-config").ShouldBeTrue();
}

[NUnit.Framework.Theory]
[NUnit.Framework.TestCase("localonly")]
[NUnit.Framework.TestCase("source")]
Expand All @@ -304,6 +310,7 @@ public void should_add_short_version_of_password_to_the_option_set()
[NUnit.Framework.TestCase("approved-only")]
[NUnit.Framework.TestCase("download-cache-only")]
[NUnit.Framework.TestCase("disable-package-repository-optimizations")]
[NUnit.Framework.TestCase("include-sources-from-config")]
public void should_add_deprecation_notice_to_option(string argument)
{
optionSet[argument].Description.ShouldNotContain("DEPRECATION NOTICE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public void should_add_ignore_pinned_to_the_option_set()
{
optionSet.Contains("ignore-pinned").ShouldBeTrue();
}

[Fact]
public void should_add_include_sources_from_config_to_the_option_set()
{
optionSet.Contains("include-sources-from-config").ShouldBeTrue();
}
}

public class when_noop_is_called : ChocolateyOutdatedCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ public void should_add_short_version_of_skip_hooks_to_the_option_set()
{
optionSet.Contains("skiphooks").ShouldBeTrue();
}

[Fact]
public void should_add_include_sources_from_config_to_the_option_set()
{
optionSet.Contains("include-sources-from-config").ShouldBeTrue();
}
}

public class when_handling_additional_argument_parsing : ChocolateyUpgradeCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public override void configure_argument_parser(OptionSet optionSet, ChocolateyCo
configuration.Features.UsePackageRepositoryOptimizations = false;
}
})
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 1.2.0+",
option => configuration.IncludeMachineSources = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
"Skip hooks - Do not run hook scripts. Available in 1.2.0+",
option => configuration.SkipHookScripts = option != null
)
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 1.2.0+",
option => configuration.IncludeMachineSources = option != null
)
;

//todo: #770 package name can be a url / installertype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
configuration.Features.UsePackageRepositoryOptimizations = false;
}
})
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 1.2.0+",
option => configuration.IncludeMachineSources = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
configuration.Features.UsePackageRepositoryOptimizations = false;
}
})
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 1.2.0+",
option => configuration.IncludeMachineSources = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
"Skip hooks - Do not run hook scripts. Available in 1.2.0+",
option => configuration.SkipHookScripts = option != null
)
.Add("include-sources-from-config",
"Include Sources From Configuration - Include the sources used when --source is not specified, which are configured by the source command. Available in 1.2.0+",
option => configuration.IncludeMachineSources = option != null
)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ private void append_output(StringBuilder propertyValues, string append)
public string Sources { get; set; }

public string SourceType { get; set; }
public bool IncludeMachineSources { get; set; }

// top level commands

Expand Down
24 changes: 24 additions & 0 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private ICommand find_command(ChocolateyConfiguration config, Container containe
}

set_source_type(config, container);
include_machine_sources(config);
// guaranteed that all settings are set.
EnvironmentSettings.set_environment_variables(config);

Expand Down Expand Up @@ -127,6 +128,29 @@ private void set_source_type(ChocolateyConfiguration config, Container container
this.Log().Debug(() => "The source '{0}' evaluated to a '{1}' source type".format_with(config.Sources, sourceType));
}

private void include_machine_sources(ChocolateyConfiguration config)
{
if (config.IncludeMachineSources)
{
if (config.SourceType != SourceTypes.NORMAL)
{
this.Log().Warn("Not including sources from config because {0} is an alternate source".format_with(config.Sources));
}
else
{
foreach (var machineSource in config.MachineSources.or_empty_list_if_null())
{
if (!config.Sources.contains(machineSource.Key))
{
config.Sources += ";" + machineSource.Key;
}
}

this.Log().Debug("Including sources from config");
}
}
}

public void fail_when_license_is_missing_or_invalid_if_requested(ChocolateyConfiguration config)
{
if (!config.Features.FailOnInvalidOrMissingLicense ||
Expand Down

0 comments on commit 661fe5e

Please sign in to comment.