From 947ee831276d7bf30f7d1f4cfd414889ab47c938 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Mon, 11 Oct 2021 21:14:11 +0100 Subject: [PATCH] (#2377) Add unit tests Unit tests have been added to exercise the generate method of the TemplateService to ensure that the template name that is used matches the configured value. - If option is set at the command line, it should win, even if there is a default value in the chocolatey.config file. - If default value is set in chocolatey.config file but path doesn't exist, should use null value - If default value is set in chocolatey.config file and path exists, should be set to this template value --- .../services/TemplateServiceSpecs.cs | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs b/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs index 400a57af04..5084eb7286 100644 --- a/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs @@ -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 @@ -529,5 +531,134 @@ 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(), It.IsAny())) + .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(), It.IsAny())) + .Returns((string a, string[] b) => { return a + "\\" + b[0]; }); + fileSystem.Setup(x => x.directory_exists(Path.Combine(ApplicationParameters.TemplatesLocation, "msi"))).Returns( + 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(), It.IsAny())) + .Returns((string a, string[] b) => { return a + "\\" + b[0]; }); + fileSystem.Setup(x => x.directory_exists(Path.Combine(ApplicationParameters.TemplatesLocation, "zip"))).Returns( + 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"); + } + } } }