Skip to content

Commit

Permalink
(GH-417) Choco config command
Browse files Browse the repository at this point in the history
Add a config section to the configuration file. Move the existing items
into that section that are not apikeys, sources, or features. Search
for existing settings and port the value to the new location.

Allow querying and setting these configuration values.
  • Loading branch information
ferventcoder committed Sep 21, 2015
1 parent 5f77d6c commit 362507c
Show file tree
Hide file tree
Showing 15 changed files with 524 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="GetChocolateySpecs.cs" />
<Compile Include="infrastructure.app\attributes\CommandForAttributeSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyApiKeyCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyFeatureCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyInstallCommandSpecs.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyListCommandSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.tests.infrastructure.app.commands
{
using System;
using System.Collections.Generic;
using System.Linq;
using Moq;
using Should;
using chocolatey.infrastructure.app.attributes;
using chocolatey.infrastructure.app.commands;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.domain;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.commandline;

public class ChocolateyConfigCommandSpecs
{
public abstract class ChocolateyConfigCommandSpecsBase : TinySpec
{
protected ChocolateyConfigCommand command;
protected Mock<IChocolateyConfigSettingsService> configSettingsService = new Mock<IChocolateyConfigSettingsService>();
protected ChocolateyConfiguration configuration = new ChocolateyConfiguration();

public override void Context()
{
command = new ChocolateyConfigCommand(configSettingsService.Object);
}
}

public class when_implementing_command_for : ChocolateyConfigCommandSpecsBase
{
private List<string> results;

public override void Because()
{
results = command.GetType().GetCustomAttributes(typeof(CommandForAttribute), false).Cast<CommandForAttribute>().Select(a => a.CommandName).ToList();
}

[Fact]
public void should_implement_config()
{
results.ShouldContain(CommandNameType.config.to_string());
}
}

public class when_configurating_the_argument_parser : ChocolateyConfigCommandSpecsBase
{
private OptionSet optionSet;

public override void Context()
{
base.Context();
optionSet = new OptionSet();
}

public override void Because()
{
command.configure_argument_parser(optionSet, configuration);
}

[Fact]
public void should_add_name_to_the_option_set()
{
optionSet.Contains("name").ShouldBeTrue();
}

[Fact]
public void should_add_value_to_the_option_set()
{
optionSet.Contains("value").ShouldBeTrue();
}
}

public class when_noop_is_called : ChocolateyConfigCommandSpecsBase
{
public override void Because()
{
command.noop(configuration);
}

[Fact]
public void should_call_service_noop()
{
configSettingsService.Verify(c => c.noop(configuration), Times.Once);
}
}

public class when_run_is_called : ChocolateyConfigCommandSpecsBase
{
private Action because;

public override void Because()
{
because = () => command.run(configuration);
}

[Fact]
public void should_call_service_source_list_when_command_is_list()
{
configuration.ConfigCommand.Command = ConfigCommandType.list;
because();
configSettingsService.Verify(c => c.config_list(configuration), Times.Once);
}

[Fact]
public void should_call_service_source_disable_when_command_is_disable()
{
configuration.ConfigCommand.Command = ConfigCommandType.get;
because();
configSettingsService.Verify(c => c.config_get(configuration), Times.Once);
}

[Fact]
public void should_call_service_source_enable_when_command_is_enable()
{
configuration.ConfigCommand.Command = ConfigCommandType.set;
because();
configSettingsService.Verify(c => c.config_set(configuration), Times.Once);
}
}
}
}
3 changes: 3 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
</Compile>
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="GetChocolatey.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyConfigCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyFeatureCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyNewCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyOutdatedCommand.cs" />
Expand All @@ -91,9 +92,11 @@
<Compile Include="infrastructure.app\commands\ChocolateyUpdateCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyUpgradeCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyVersionCommand.cs" />
<Compile Include="infrastructure.app\configuration\ConfigFileConfigSetting.cs" />
<Compile Include="infrastructure.app\configuration\ConfigFileFeatureSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFilePackageSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFileSettings.cs" />
<Compile Include="infrastructure.app\domain\ConfigCommandType.cs" />
<Compile Include="infrastructure.app\domain\FeatureCommandType.cs" />
<Compile Include="infrastructure.app\domain\InstallerBase.cs" />
<Compile Include="infrastructure.app\domain\PackageFile.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public static class Tools
public static readonly string ShimGenExe = _fileSystem.combine_paths(InstallLocation, "tools", "shimgen.exe");
}

public static class ConfigSettings
{
public static readonly string CacheLocation = "cacheLocation";
public static readonly string ContainsLegacyPackageInstalls = "containsLegacyPackageInstalls";
public static readonly string CommandExecutionTimeoutSeconds = "commandExecutionTimeoutSeconds";
}

public static class Features
{
public static readonly string CheckSumFiles = "checksumFiles";
Expand Down
69 changes: 56 additions & 13 deletions src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,14 @@ private static void set_file_configuration(ChocolateyConfiguration config, IFile
}

set_machine_sources(config, configFileSettings);

config.CacheLocation = !string.IsNullOrWhiteSpace(configFileSettings.CacheLocation) ? configFileSettings.CacheLocation : System.Environment.GetEnvironmentVariable("TEMP");
if (string.IsNullOrWhiteSpace(config.CacheLocation))
{
config.CacheLocation = fileSystem.combine_paths(ApplicationParameters.InstallLocation, "temp");
}

set_config_items(config, configFileSettings, fileSystem);

FaultTolerance.try_catch_with_logging_exception(
() => fileSystem.create_directory_if_not_exists(config.CacheLocation),
"Could not create temp directory at '{0}'".format_with(config.CacheLocation),
logWarningInsteadOfError: true);

config.ContainsLegacyPackageInstalls = configFileSettings.ContainsLegacyPackageInstalls;
if (configFileSettings.CommandExecutionTimeoutSeconds <= 0)
{
configFileSettings.CommandExecutionTimeoutSeconds = ApplicationParameters.DefaultWaitForExitInSeconds;
}
config.CommandExecutionTimeoutSeconds = configFileSettings.CommandExecutionTimeoutSeconds;

set_feature_flags(config, configFileSettings);

// save so all updated configuration items get set to existing config
Expand All @@ -134,6 +123,60 @@ private static void set_machine_sources(ChocolateyConfiguration config, ConfigFi
}
}

private static void set_config_items(ChocolateyConfiguration config, ConfigFileSettings configFileSettings, IFileSystem fileSystem)
{
var cacheLocation = set_config_item(ApplicationParameters.ConfigSettings.CacheLocation, configFileSettings, string.IsNullOrWhiteSpace(configFileSettings.CacheLocation) ? string.Empty : configFileSettings.CacheLocation, "Cache location if not TEMP folder.");
config.CacheLocation = !string.IsNullOrWhiteSpace(cacheLocation) ? cacheLocation : System.Environment.GetEnvironmentVariable("TEMP");
if (string.IsNullOrWhiteSpace(config.CacheLocation))
{
config.CacheLocation = fileSystem.combine_paths(ApplicationParameters.InstallLocation, "temp");
}

var originalCommandTimeout = configFileSettings.CommandExecutionTimeoutSeconds;
var commandExecutionTimeoutSeconds = -1;
int.TryParse(
set_config_item(
ApplicationParameters.ConfigSettings.CommandExecutionTimeoutSeconds,
configFileSettings,
originalCommandTimeout == 0 ?
ApplicationParameters.DefaultWaitForExitInSeconds.to_string()
: originalCommandTimeout.to_string(),
"Default timeout for command execution."),
out commandExecutionTimeoutSeconds);
config.CommandExecutionTimeoutSeconds = commandExecutionTimeoutSeconds;
if (configFileSettings.CommandExecutionTimeoutSeconds <= 0)
{
set_config_item(ApplicationParameters.ConfigSettings.CommandExecutionTimeoutSeconds, configFileSettings, ApplicationParameters.DefaultWaitForExitInSeconds.to_string(), "Default timeout for command execution.", forceSettingValue: true);
config.CommandExecutionTimeoutSeconds = ApplicationParameters.DefaultWaitForExitInSeconds;
}

config.ContainsLegacyPackageInstalls = set_config_item(ApplicationParameters.ConfigSettings.ContainsLegacyPackageInstalls, configFileSettings, "true", "Install has packages installed prior to 0.9.9 series.").is_equal_to(bool.TrueString);
}

private static string set_config_item(string configName, ConfigFileSettings configFileSettings, string defaultValue, string description, bool forceSettingValue = false)
{
var config = configFileSettings.ConfigSettings.FirstOrDefault(f => f.Key.is_equal_to(configName));
if (config == null)
{
config = new ConfigFileConfigSetting
{
Key = configName,
Value = defaultValue,
Description = description
};

configFileSettings.ConfigSettings.Add(config);
}
if (forceSettingValue)
{
config.Value = defaultValue;
}

config.Description = description;

return config.Value;
}

private static void set_feature_flags(ChocolateyConfiguration config, ConfigFileSettings configFileSettings)
{
config.Features.CheckSumFiles = set_feature_flag(ApplicationParameters.Features.CheckSumFiles, configFileSettings, "Checksum files when pulled in from internet (based on package).");
Expand Down
Loading

0 comments on commit 362507c

Please sign in to comment.