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 Aug 11, 2022
1 parent 974a3bc commit bf3bdd7
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 @@ -132,6 +132,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 @@ -253,6 +253,12 @@ public void should_add_short_version_of_pin_to_the_option_set()
{
optionSet.Contains("pin").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 @@ -293,6 +293,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 @@ -303,6 +309,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 @@ -113,6 +113,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 @@ -235,6 +235,12 @@ public void should_add_short_version_of_pin_to_the_option_set()
{
optionSet.Contains("pin").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 @@ -183,6 +183,10 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
"Pin Package - Add a pin to the package after install. Available in 1.2.0+",
option => configuration.PinPackage = 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 @@ -222,6 +222,10 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
"Pin Package - Add a pin to the package after upgrade. Available in 1.2.0+",
option => configuration.PinPackage = 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 @@ -155,6 +155,7 @@ private void append_output(StringBuilder propertyValues, string append)
/// </summary>
public string Sources { get; set; }
public SourceType 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 @@ -71,6 +71,7 @@ private ICommand find_command(ChocolateyConfiguration config, Container containe
}

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

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

private void include_machine_sources(ChocolateyConfiguration config)
{
if (config.IncludeMachineSources)
{
if (config.SourceType != SourceType.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 bf3bdd7

Please sign in to comment.