Skip to content

Commit

Permalink
(chocolatey#1003) Add tests to make sure empty folder are created
Browse files Browse the repository at this point in the history
Validates that empty directories from a custom template are recreated
in the generated package source.
  • Loading branch information
TheCakeIsNaOH committed Nov 13, 2021
1 parent 8e80dc4 commit e37fb88
Showing 1 changed file with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> files = new List<string>();
private readonly HashSet<string> directoryCreated = new HashSet<string>(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<string>(), It.IsAny<string>()))
.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<string>())).Returns<string>(dirPath => dirPath.EndsWith("templates\\test"));
fileSystem.Setup(x => x.write_file(It.IsAny<string>(), It.IsAny<string>(), Encoding.UTF8))
.Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath));
fileSystem.Setup(x => x.write_file(It.IsAny<string>(), It.IsAny<string>(), utf8WithoutBOM))
.Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath));
fileSystem.Setup(x => x.delete_directory_if_exists(It.IsAny<string>(), true));
fileSystem.Setup(x => x.get_files(It.IsAny<string>(), "*.*", 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<string>(), "*.*", 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<string>())).Callback(
(string directory) =>
{
if (!string.IsNullOrWhiteSpace(directory))
{
directoryCreated.Add(directory);
}
});
fileSystem.Setup(x => x.get_directory_name(It.IsAny<string>())).Returns<string>(file => Path.GetDirectoryName(file));
fileSystem.Setup(x => x.get_file_extension(It.IsAny<string>())).Returns<string>(file => Path.GetExtension(file));
fileSystem.Setup(x => x.read_file(It.IsAny<string>())).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));
}
}
}
}

0 comments on commit e37fb88

Please sign in to comment.