Skip to content

Commit

Permalink
Merge pull request chocolatey#2390 from gep13/feature/2377-default-te…
Browse files Browse the repository at this point in the history
…mplate-name

(chocolatey#2377) Add ability to configure default template name
  • Loading branch information
gep13 authored Dec 23, 2021
2 parents 211061f + fd5b8e9 commit c76012d
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2011 - 2017 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.
Expand All @@ -21,11 +21,13 @@ namespace chocolatey.tests.infrastructure.app.services
using System.IO;
using System.Linq;
using System.Text;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.app.templates;
using chocolatey.infrastructure.filesystem;
using Moq;
using NUnit.Framework;
using Should;

public class TemplateServiceSpecs
Expand Down Expand Up @@ -529,5 +531,298 @@ public void should_generate_all_files_and_directories_even_with_outputdirectory(
MockLogger.MessagesFor(LogLevel.Info).Last().ShouldEqual(string.Format(@"Successfully generated Bob package specification files{0} at 'c:\packages\Bob'", Environment.NewLine));
}
}

public class when_generate_is_called_with_defaulttemplatename_in_configuration_but_template_folder_doesnt_exist : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();

public override void Context()
{
base.Context();

fileSystem.Setup(x => x.get_current_directory()).Returns("c:\\chocolatey");
fileSystem.Setup(x => x.combine_paths(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string a, string[] b) => { return a + "\\" + b[0]; });

config.NewCommand.Name = "Bob";
config.DefaultTemplateName = "msi";
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_null_value_for_template()
{
because();

config.NewCommand.TemplateName.ShouldBeNull();
}
}

public class when_generate_is_called_with_defaulttemplatename_in_configuration_and_template_folder_exists : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private string verifiedDirectoryPath;

public override void Context()
{
base.Context();

fileSystem.Setup(x => x.get_current_directory()).Returns("c:\\chocolatey");
fileSystem.Setup(x => x.combine_paths(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string a, string[] b) => { return a + "\\" + b[0]; });
fileSystem.Setup(x => x.directory_exists(Path.Combine(ApplicationParameters.TemplatesLocation, "msi"))).Returns<string>(
x =>
{
verifiedDirectoryPath = x;
return true;
});

config.NewCommand.Name = "Bob";
config.DefaultTemplateName = "msi";
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_template_name_from_configuration()
{
because();

config.NewCommand.TemplateName.ShouldEqual("msi");
}
}

public class when_generate_is_called_with_defaulttemplatename_in_configuration_and_template_name_option_set : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private string verifiedDirectoryPath;

public override void Context()
{
base.Context();

fileSystem.Setup(x => x.get_current_directory()).Returns("c:\\chocolatey");
fileSystem.Setup(x => x.combine_paths(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string a, string[] b) => { return a + "\\" + b[0]; });
fileSystem.Setup(x => x.directory_exists(Path.Combine(ApplicationParameters.TemplatesLocation, "zip"))).Returns<string>(
x =>
{
verifiedDirectoryPath = x;
return true;
});

config.NewCommand.Name = "Bob";
config.NewCommand.TemplateName = "zip";
config.DefaultTemplateName = "msi";
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_template_name_from_command_line_option()
{
because();

config.NewCommand.TemplateName.ShouldEqual("zip");
}
}

public class when_generate_is_called_with_built_in_option_set : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private string verifiedDirectoryPath;

public override void Context()
{
base.Context();

config.NewCommand.Name = "Bob";
config.NewCommand.UseOriginalTemplate = true;
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_null_value_for_template()
{
because();

config.NewCommand.TemplateName.ShouldBeNull();
}
}

public class when_generate_is_called_with_built_in_option_set_and_defaulttemplate_in_configuration : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private string verifiedDirectoryPath;

public override void Context()
{
base.Context();

config.NewCommand.Name = "Bob";
config.NewCommand.UseOriginalTemplate = true;
config.DefaultTemplateName = "msi";
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_null_value_for_template()
{
because();

config.NewCommand.TemplateName.ShouldBeNull();
}
}

public class when_generate_is_called_with_built_in_option_set_and_template_name_option_set_and_template_folder_exists : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private string verifiedDirectoryPath;

public override void Context()
{
base.Context();

fileSystem.Setup(x => x.get_current_directory()).Returns("c:\\chocolatey");
fileSystem.Setup(x => x.combine_paths(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string a, string[] b) => { return a + "\\" + b[0]; });
fileSystem.Setup(x => x.directory_exists(Path.Combine(ApplicationParameters.TemplatesLocation, "zip"))).Returns<string>(
x =>
{
verifiedDirectoryPath = x;
return true;
});

config.NewCommand.Name = "Bob";
config.NewCommand.TemplateName = "zip";
config.NewCommand.UseOriginalTemplate = true;
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_template_name_from_command_line_option()
{
because();

config.NewCommand.TemplateName.ShouldEqual("zip");
}
}

public class when_generate_is_called_with_built_in_option_set_and_template_name_option_set_and_defaulttemplatename_set_and_template_folder_exists : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private string verifiedDirectoryPath;

public override void Context()
{
base.Context();

fileSystem.Setup(x => x.get_current_directory()).Returns("c:\\chocolatey");
fileSystem.Setup(x => x.combine_paths(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string a, string[] b) => { return a + "\\" + b[0]; });
fileSystem.Setup(x => x.directory_exists(Path.Combine(ApplicationParameters.TemplatesLocation, "zip"))).Returns<string>(
x =>
{
verifiedDirectoryPath = x;
return true;
});

config.NewCommand.Name = "Bob";
config.NewCommand.TemplateName = "zip";
config.DefaultTemplateName = "msi";
config.NewCommand.UseOriginalTemplate = true;
}

public override void Because()
{
because = () => service.generate(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void should_use_template_name_from_command_line_option()
{
because();

config.NewCommand.TemplateName.ShouldEqual("zip");
}
}
}
}
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public static class ConfigSettings
public static readonly string ProxyBypassOnLocal = "proxyBypassOnLocal";
public static readonly string WebRequestTimeoutSeconds = "webRequestTimeoutSeconds";
public static readonly string UpgradeAllExceptions = "upgradeAllExceptions";
public static readonly string DefaultTemplateName = "defaultTemplateName";
}

public static class Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ private static void set_config_items(ChocolateyConfiguration config, ConfigFileS
config.Proxy.BypassList = set_config_item(ApplicationParameters.ConfigSettings.ProxyBypassList, configFileSettings, string.Empty, "Optional proxy bypass list. Comma separated. Available in 0.10.4+.");
config.Proxy.BypassOnLocal = set_config_item(ApplicationParameters.ConfigSettings.ProxyBypassOnLocal, configFileSettings, "true", "Bypass proxy for local connections. Available in 0.10.4+.").is_equal_to(bool.TrueString);
config.UpgradeCommand.PackageNamesToSkip = set_config_item(ApplicationParameters.ConfigSettings.UpgradeAllExceptions, configFileSettings, string.Empty, "A comma-separated list of package names that should not be upgraded when running `choco upgrade all'. Defaults to empty. Available in 0.10.14+.");
config.DefaultTemplateName = set_config_item(ApplicationParameters.ConfigSettings.DefaultTemplateName, configFileSettings, string.Empty, "Default template name used when running 'choco new' command. Available in 0.12.0+.");
}

private static string set_config_item(string configName, ConfigFileSettings configFileSettings, string defaultValue, string description, bool forceSettingValue = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ private void append_output(StringBuilder propertyValues, string append)
public bool ContainsLegacyPackageInstalls { get; set; }
public int CommandExecutionTimeoutSeconds { get; set; }
public int WebRequestTimeoutSeconds { get; set; }
public string DefaultTemplateName { get; set; }

/// <summary>
/// One or more source locations set by configuration or by command line. Separated by semi-colon
Expand Down
Loading

0 comments on commit c76012d

Please sign in to comment.