diff --git a/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs b/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs index 2f9eef87e5..b97ea81af0 100644 --- a/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs @@ -424,5 +424,117 @@ 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_empty_nested_folders : TemplateServiceSpecsBase + { + private Action because; + private readonly ChocolateyConfiguration config = new ChocolateyConfiguration(); + private readonly List files = new List(); + private readonly HashSet directoryCreated = new HashSet(StringComparer.InvariantCultureIgnoreCase); + private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding(false); + + 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) => + { + if (a.EndsWith("templates") && b[0] == "test") + { + return "templates\\test"; + } + return a + "\\" + b[0]; + }); + fileSystem.Setup(x => x.directory_exists(It.IsAny())).Returns(dirPath => dirPath.EndsWith("templates\\test")); + fileSystem.Setup(x => x.write_file(It.IsAny(), It.IsAny(), Encoding.UTF8)) + .Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath)); + fileSystem.Setup(x => x.write_file(It.IsAny(), It.IsAny(), utf8WithoutBOM)) + .Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath)); + fileSystem.Setup(x => x.delete_directory_if_exists(It.IsAny(), true)); + fileSystem.Setup(x => x.get_files(It.IsAny(), "*.*", SearchOption.AllDirectories)) + .Returns(new[] { "templates\\test\\template.nuspec", "templates\\test\\random.txt", "templates\\test\\tools\\chocolateyInstall.ps1", "templates\\test\\tools\\lower\\another.ps1" }); + fileSystem.Setup(x => x.get_directories(It.IsAny(), "*.*", SearchOption.AllDirectories)) + .Returns(new[] { "templates\\test", "templates\\test\\tools", "templates\\test\\tools\\lower", "templates\\test\\empty", "templates\\test\\empty\\nested" }); + fileSystem.Setup(x => x.create_directory_if_not_exists(It.IsAny())).Callback( + (string directory) => + { + if (!string.IsNullOrWhiteSpace(directory)) + { + directoryCreated.Add(directory); + } + }); + fileSystem.Setup(x => x.get_directory_name(It.IsAny())).Returns(file => Path.GetDirectoryName(file)); + fileSystem.Setup(x => x.get_file_extension(It.IsAny())).Returns(file => Path.GetExtension(file)); + fileSystem.Setup(x => x.read_file(It.IsAny())).Returns(string.Empty); + + config.NewCommand.Name = "Bob"; + config.NewCommand.TemplateName = "test"; + } + + public override void Because() + { + because = () => service.generate(config); + } + + public override void BeforeEachSpec() + { + MockLogger.reset(); + files.Clear(); + directoryCreated.Clear(); + } + + [Fact] + [WindowsOnly] + [Platform(Exclude = "Mono")] + public void should_generate_all_files_and_directories() + { + because(); + + var directories = directoryCreated.ToList(); + directories.Count.ShouldEqual(5, "There should be 5 directories, but there was: " + string.Join(", ", directories)); + directories[0].ShouldEqual("c:\\chocolatey\\Bob"); + directories[1].ShouldEqual("c:\\chocolatey\\Bob\\tools"); + directories[2].ShouldEqual("c:\\chocolatey\\Bob\\tools\\lower"); + directories[3].ShouldEqual("c:\\chocolatey\\Bob\\empty"); + directories[4].ShouldEqual("c:\\chocolatey\\Bob\\empty\\nested"); + + files.Count.ShouldEqual(4, "There should be 4 files, but there was: " + string.Join(", ", files)); + files[0].ShouldEqual("c:\\chocolatey\\Bob\\__name_replace__.nuspec"); + files[1].ShouldEqual("c:\\chocolatey\\Bob\\random.txt"); + files[2].ShouldEqual("c:\\chocolatey\\Bob\\tools\\chocolateyInstall.ps1"); + files[3].ShouldEqual("c:\\chocolatey\\Bob\\tools\\lower\\another.ps1"); + + MockLogger.MessagesFor(LogLevel.Info).Last().ShouldEqual(string.Format(@"Successfully generated Bob package specification files{0} at 'c:\chocolatey\Bob'", Environment.NewLine)); + } + + [Fact] + [WindowsOnly] + [Platform(Exclude = "Mono")] + public void should_generate_all_files_and_directories_even_with_outputdirectory() + { + config.OutputDirectory = "c:\\packages"; + + because(); + + var directories = directoryCreated.ToList(); + directories.Count.ShouldEqual(5, "There should be 5 directories, but there was: " + string.Join(", ", directories)); + directories[0].ShouldEqual("c:\\packages\\Bob"); + directories[1].ShouldEqual("c:\\packages\\Bob\\tools"); + directories[2].ShouldEqual("c:\\packages\\Bob\\tools\\lower"); + directories[3].ShouldEqual("c:\\packages\\Bob\\empty"); + directories[4].ShouldEqual("c:\\packages\\Bob\\empty\\nested"); + + files.Count.ShouldEqual(4, "There should be 4 files, but there was: " + string.Join(", ", files)); + files[0].ShouldEqual("c:\\packages\\Bob\\__name_replace__.nuspec"); + files[1].ShouldEqual("c:\\packages\\Bob\\random.txt"); + files[2].ShouldEqual("c:\\packages\\Bob\\tools\\chocolateyInstall.ps1"); + files[3].ShouldEqual("c:\\packages\\Bob\\tools\\lower\\another.ps1"); + + MockLogger.MessagesFor(LogLevel.Info).Last().ShouldEqual(string.Format(@"Successfully generated Bob package specification files{0} at 'c:\packages\Bob'", Environment.NewLine)); + } + } } }