From 2df70385c694c4d02a1c77bfba256fa3304c7aed Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Wed, 1 Feb 2012 23:49:24 +1100 Subject: [PATCH 1/3] another couple of edge cases --- .../Templating/Jekyll/JekyllEngine.cs | 7 +++-- .../Templating/Jekyll/LiquidEngineTests.cs | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs b/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs index 95389c829..a42e46e86 100644 --- a/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs +++ b/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs @@ -26,6 +26,10 @@ public void Process() var outputFile = Path.Combine(outputDirectory, relativePath); + var directory = Path.GetDirectoryName(outputFile); + if (!fileSystem.Directory.Exists(directory)) + fileSystem.Directory.CreateDirectory(directory); + var extension = Path.GetExtension(file); if (extension.IsImageFormat()) { @@ -44,7 +48,6 @@ public void Process() // markdown file should not be treated differently // output from markdown file should be sent to template pipeline - if (extension.IsMarkdownFile()) { outputFile = outputFile.Replace(extension, ".html"); @@ -55,7 +58,7 @@ public void Process() } else { - RenderTemplate(inputFile, outputFile); + RenderTemplate(inputFile.ExcludeHeader(), outputFile); } } } diff --git a/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs b/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs index 6d5df50a5..65fd9bdcd 100644 --- a/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs +++ b/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs @@ -279,5 +279,31 @@ public void The_Folder_Should_Be_Ignored() Assert.False(FileSystem.File.Exists(@"C:\website\_site\.gems\file.txt")); } } + + + public class When_Aeoth_Tests_The_Edge_Cases_Of_Handling_YAML_Front_Matter : BakingEnvironment + { + const string PageContents = "---\n---"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\file.txt", new MockFileData(PageContents)); + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_Yaml_Matter_Should_Be_Cleared() + { + var text = FileSystem.File.ReadAllText(@"C:\website\_site\file.txt"); + Assert.False(text.StartsWith("---")); + } + } } } \ No newline at end of file From 78b53b24f2841a0871a790789f5c409f2075f27e Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Thu, 2 Feb 2012 00:31:13 +1100 Subject: [PATCH 2/3] added MEF support for composing site engines, added conventions for jekyll and discovery --- src/Pretzel.Logic/Pretzel.Logic.csproj | 3 + src/Pretzel.Logic/Templating/ISiteEngine.cs | 3 + .../Templating/ISiteEngineInfo.cs | 7 + .../Templating/Jekyll/JekyllEngine.cs | 11 +- .../Templating/SiteEngineInfoAttribute.cs | 16 + src/Pretzel.Tests/Pretzel.Tests.csproj | 3 +- .../Templating/Jekyll/JekyllEngineTests.cs | 298 ++++++++++++++++++ .../Templating/Jekyll/StringTestExtensions.cs | 10 + src/Pretzel/Commands/BakeCommand.cs | 52 ++- src/Pretzel/Commands/CommandCollection.cs | 1 + src/Pretzel/Program.cs | 17 +- 11 files changed, 403 insertions(+), 18 deletions(-) create mode 100644 src/Pretzel.Logic/Templating/ISiteEngineInfo.cs create mode 100644 src/Pretzel.Logic/Templating/SiteEngineInfoAttribute.cs create mode 100644 src/Pretzel.Tests/Templating/Jekyll/JekyllEngineTests.cs create mode 100644 src/Pretzel.Tests/Templating/Jekyll/StringTestExtensions.cs diff --git a/src/Pretzel.Logic/Pretzel.Logic.csproj b/src/Pretzel.Logic/Pretzel.Logic.csproj index 40aa83aa3..60b9cf2de 100644 --- a/src/Pretzel.Logic/Pretzel.Logic.csproj +++ b/src/Pretzel.Logic/Pretzel.Logic.csproj @@ -46,6 +46,7 @@ ..\packages\MarkdownDeep.NET.1.4\lib\.NetFramework 3.5\MarkdownDeep.dll + @@ -70,6 +71,8 @@ + + diff --git a/src/Pretzel.Logic/Templating/ISiteEngine.cs b/src/Pretzel.Logic/Templating/ISiteEngine.cs index 8bd473086..3ed7a259a 100644 --- a/src/Pretzel.Logic/Templating/ISiteEngine.cs +++ b/src/Pretzel.Logic/Templating/ISiteEngine.cs @@ -1,10 +1,13 @@ +using System.ComponentModel.Composition; using System.IO.Abstractions; using Pretzel.Logic.Templating.Jekyll; namespace Pretzel.Logic.Templating { + [InheritedExport] public interface ISiteEngine { + bool CanProcess(IFileSystem fileSystem, string directory); void Initialize(IFileSystem fileSystem, SiteContext context); void Process(); } diff --git a/src/Pretzel.Logic/Templating/ISiteEngineInfo.cs b/src/Pretzel.Logic/Templating/ISiteEngineInfo.cs new file mode 100644 index 000000000..09d26f0f1 --- /dev/null +++ b/src/Pretzel.Logic/Templating/ISiteEngineInfo.cs @@ -0,0 +1,7 @@ +namespace Pretzel.Logic.Templating +{ + public interface ISiteEngineInfo + { + string Engine { get; } + } +} diff --git a/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs b/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs index a42e46e86..325508a76 100644 --- a/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs +++ b/src/Pretzel.Logic/Templating/Jekyll/JekyllEngine.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel.Composition; using System.IO; using System.IO.Abstractions; using DotLiquid; @@ -7,6 +8,8 @@ namespace Pretzel.Logic.Templating.Jekyll { + [PartCreationPolicy(CreationPolicy.Shared)] + [SiteEngineInfo(Engine = "jekyll")] public class JekyllEngine : ISiteEngine { private static readonly Markdown Markdown = new Markdown(); @@ -33,7 +36,7 @@ public void Process() var extension = Path.GetExtension(file); if (extension.IsImageFormat()) { - fileSystem.File.Copy(file, outputFile); + fileSystem.File.Copy(file, outputFile, true); continue; } @@ -118,6 +121,12 @@ private static string RenderTemplate(string templateContents, Hash data) return template.Render(data); } + public bool CanProcess(IFileSystem fileSystem, string directory) + { + var configPath = Path.Combine(directory, "_config.yml"); + return fileSystem.File.Exists(configPath); + } + public void Initialize(IFileSystem fileSystem, SiteContext context) { this.fileSystem = fileSystem; diff --git a/src/Pretzel.Logic/Templating/SiteEngineInfoAttribute.cs b/src/Pretzel.Logic/Templating/SiteEngineInfoAttribute.cs new file mode 100644 index 000000000..f8f021a96 --- /dev/null +++ b/src/Pretzel.Logic/Templating/SiteEngineInfoAttribute.cs @@ -0,0 +1,16 @@ +using System; +using System.ComponentModel.Composition; + +namespace Pretzel.Logic.Templating +{ + [MetadataAttribute] + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public sealed class SiteEngineInfoAttribute : ExportAttribute + { + public string Engine { get; set; } + + public SiteEngineInfoAttribute() : base(typeof(ISiteEngine)) + { + } + } +} diff --git a/src/Pretzel.Tests/Pretzel.Tests.csproj b/src/Pretzel.Tests/Pretzel.Tests.csproj index a7341d92d..9d422485c 100644 --- a/src/Pretzel.Tests/Pretzel.Tests.csproj +++ b/src/Pretzel.Tests/Pretzel.Tests.csproj @@ -58,8 +58,9 @@ - + + diff --git a/src/Pretzel.Tests/Templating/Jekyll/JekyllEngineTests.cs b/src/Pretzel.Tests/Templating/Jekyll/JekyllEngineTests.cs new file mode 100644 index 000000000..c3045c88a --- /dev/null +++ b/src/Pretzel.Tests/Templating/Jekyll/JekyllEngineTests.cs @@ -0,0 +1,298 @@ +using System.IO.Abstractions.TestingHelpers; +using Pretzel.Logic.Templating.Jekyll; +using Xunit; + +namespace Pretzel.Tests.Templating.Jekyll +{ + public class JekyllEngineTests + { + public class When_Recieving_A_Folder_Containing_One_File : BakingEnvironment + { + const string FileContents = ""; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + var context = new SiteContext {Folder = @"C:\website\"}; + FileSystem.AddFile(@"C:\website\index.html", new MockFileData(FileContents)); + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void That_Site_Folder_Is_Created() + { + Assert.True(FileSystem.Directory.Exists(@"C:\website\_site")); + } + + [Fact] + public void The_File_Is_Added_At_The_Root() + { + Assert.True(FileSystem.File.Exists(@"C:\website\_site\index.html")); + } + + [Fact] + public void The_File_Is_Identical() + { + Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html")); + } + } + + public class When_Recieving_A_Folder_Without_A_Trailing_Slash : BakingEnvironment + { + const string FileContents = ""; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + var context = new SiteContext { Folder = @"C:\website" }; + FileSystem.AddFile(@"C:\website\index.html", new MockFileData(FileContents)); + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_File_Is_Added_At_The_Root() + { + Assert.True(FileSystem.File.Exists(@"C:\website\_site\index.html")); + } + } + + public class When_Recieving_A_Folder_Containing_One_File_In_A_Subfolder : BakingEnvironment + { + const string FileContents = ""; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + var context = new SiteContext { Folder = @"C:\website\" }; + FileSystem.AddFile(@"C:\website\content\index.html", new MockFileData(FileContents)); + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void That_Child_Folder_Is_Created() + { + Assert.True(FileSystem.Directory.Exists(@"C:\website\_site\content")); + } + + [Fact] + public void The_File_Is_Added_At_The_Root() + { + Assert.True(FileSystem.File.Exists(@"C:\website\_site\content\index.html")); + } + + [Fact] + public void The_File_Is_Identical() + { + Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\content\index.html")); + } + } + + public class When_Recieving_A_File_Without_Metadata : BakingEnvironment + { + const string FileContents = "{{ page.title }}"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + FileSystem.AddFile(@"C:\website\index.html", new MockFileData(FileContents)); + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_File_Is_Unaltered() + { + Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html")); + } + } + + public class When_Recieving_A_Markdown_File : BakingEnvironment + { + const string TemplateContents = "{{ page.title }}{{ content }}"; + const string PageContents = "---\r\n layout: default\r\n---\r\n\r\n# Hello World!"; + const string ExpectedfileContents = "My Web Site

Hello World!

"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents)); + FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_File_Is_Applies_Data_To_The_Template() + { + Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace()); + } + + [Fact] + public void Does_Not_Copy_Template_To_Output() + { + Assert.False(FileSystem.File.Exists(@"C:\website\_site\_layouts\default.html")); + } + } + + public class When_A_Template_Also_Has_A_Layout_Value : BakingEnvironment + { + const string ParentTemplateContents = "{{ page.title }}{{ content }}"; + const string InnerTemplateContents = "---\r\n layout: parent\r\n---\r\n\r\n

Title

{{content}}"; + const string PageContents = "---\r\n layout: inner\r\n---\r\n\r\n## Hello World!"; + const string ExpectedfileContents = "My Web Site

Title

Hello World!

"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\_layouts\parent.html", new MockFileData(ParentTemplateContents)); + FileSystem.AddFile(@"C:\website\_layouts\inner.html", new MockFileData(InnerTemplateContents)); + FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); + + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_File_Is_Applies_Data_To_The_Template() + { + Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace()); + } + + [Fact] + public void Does_Not_Copy_Template_To_Output() + { + Assert.False(FileSystem.File.Exists(@"C:\website\_site\_layouts\default.html")); + } + } + + public class Given_Markdown_Page_Has_A_Permalink : BakingEnvironment + { + const string PageContents = "---\r\npermalink: /somepage.html\r\n---\r\n\r\n# Hello World!"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_File_Should_Be_At_A_Different_Path() + { + Assert.True(FileSystem.File.Exists(@"C:\website\_site\somepage.html")); + } + } + + public class Given_Markdown_Page_Has_A_Title : BakingEnvironment + { + const string TemplateContents = "{{ page.title }}{{ content }}"; + const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!"; + const string ExpectedfileContents = "A different title

Hello World!

"; + + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents)); + FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_Output_Should_Override_The_Site_Title() + { + Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace()); + } + } + + public class Given_A_Folder_Starting_With_Dot : BakingEnvironment + { + const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\.gems\file.txt", new MockFileData(PageContents)); + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_Folder_Should_Be_Ignored() + { + Assert.False(FileSystem.File.Exists(@"C:\website\_site\.gems\file.txt")); + } + } + + public class When_Aeoth_Tests_The_Edge_Cases_Of_Handling_YAML_Front_Matter : BakingEnvironment + { + const string PageContents = "---\n---"; + + public override JekyllEngine Given() + { + return new JekyllEngine(); + } + + public override void When() + { + FileSystem.AddFile(@"C:\website\file.txt", new MockFileData(PageContents)); + var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; + Subject.Initialize(FileSystem, context); + Subject.Process(); + } + + [Fact] + public void The_Yaml_Matter_Should_Be_Cleared() + { + Assert.Equal("", FileSystem.File.ReadAllText(@"C:\website\_site\file.txt")); + } + } + } +} \ No newline at end of file diff --git a/src/Pretzel.Tests/Templating/Jekyll/StringTestExtensions.cs b/src/Pretzel.Tests/Templating/Jekyll/StringTestExtensions.cs new file mode 100644 index 000000000..dcc91d7ce --- /dev/null +++ b/src/Pretzel.Tests/Templating/Jekyll/StringTestExtensions.cs @@ -0,0 +1,10 @@ +namespace Pretzel.Tests.Templating.Jekyll +{ + public static class StringTestExtensions + { + public static string RemoveWhiteSpace(this string s) + { + return s.Replace("\r\n", "").Replace("\n", ""); + } + } +} \ No newline at end of file diff --git a/src/Pretzel/Commands/BakeCommand.cs b/src/Pretzel/Commands/BakeCommand.cs index 11c02b6ef..9a6936cc5 100644 --- a/src/Pretzel/Commands/BakeCommand.cs +++ b/src/Pretzel/Commands/BakeCommand.cs @@ -1,16 +1,23 @@ using System; +using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.IO.Abstractions; using NDesk.Options; +using Pretzel.Logic.Templating; using Pretzel.Logic.Templating.Jekyll; namespace Pretzel.Commands { [PartCreationPolicy(CreationPolicy.Shared)] [CommandInfo(CommandName = "bake")] - public sealed class BakeCommand : ICommand + public sealed class BakeCommand : ICommand, IPartImportsSatisfiedNotification { + private Dictionary engineMap; + + [ImportMany] + private Lazy[] Engines { get; set; } + public string Path { get; private set; } public string Engine { get; private set; } public bool Debug { get; private set; } @@ -31,38 +38,59 @@ private OptionSet Settings public void Execute(string[] arguments) { Settings.Parse(arguments); - Console.WriteLine("Path: " + Path); - Console.WriteLine("Engine: " + Engine); - Console.WriteLine("Debug: " + Debug); + + var fileSystem = new FileSystem(); if (string.IsNullOrWhiteSpace(Path)) { Path = Directory.GetCurrentDirectory(); } + if (string.IsNullOrWhiteSpace(Engine)) { - Engine = InferEngineFromDirectory(Path); + Engine = InferEngineFromDirectory(Path, fileSystem); } - if (Engine.ToLower() == "jekyll") + ISiteEngine engine; + + if (engineMap.TryGetValue(Engine, out engine)) { - var engine = new JekyllEngine(); - var fileSystem = new FileSystem(); - var context = new SiteContext { Folder = Path }; // TODO: process _config.yml file + var context = new SiteContext { Folder = Path }; engine.Initialize(fileSystem, context); engine.Process(); } - + else + { + System.Diagnostics.Debug.WriteLine("Cannot find engine for input: '{0}'", Engine); + } } - private string InferEngineFromDirectory(string path) + private string InferEngineFromDirectory(string path, IFileSystem fileSystem) { - return "jekyll"; // TODO: logic + foreach(var engine in engineMap) + { + if (!engine.Value.CanProcess(fileSystem, path)) continue; + System.Diagnostics.Debug.WriteLine("Recommended engine for directory: '{0}'", engine.Key); + return engine.Key; + } + + return string.Empty; } public void WriteHelp(TextWriter writer) { Settings.WriteOptionDescriptions(writer); } + + public void OnImportsSatisfied() + { + engineMap = new Dictionary(Engines.Length); + + foreach (var command in Engines) + { + if (!engineMap.ContainsKey(command.Metadata.Engine)) + engineMap.Add(command.Metadata.Engine, command.Value); + } + } } } diff --git a/src/Pretzel/Commands/CommandCollection.cs b/src/Pretzel/Commands/CommandCollection.cs index 4d43275f1..a85f8ae80 100644 --- a/src/Pretzel/Commands/CommandCollection.cs +++ b/src/Pretzel/Commands/CommandCollection.cs @@ -5,6 +5,7 @@ namespace Pretzel.Commands { [Export] + [PartCreationPolicy(CreationPolicy.Shared)] public sealed class CommandCollection : IPartImportsSatisfiedNotification { [ImportMany] diff --git a/src/Pretzel/Program.cs b/src/Pretzel/Program.cs index 609df7dfe..772d23f97 100644 --- a/src/Pretzel/Program.cs +++ b/src/Pretzel/Program.cs @@ -1,9 +1,11 @@ -using System.ComponentModel.Composition; +using System; +using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; +using System.Diagnostics; using System.Linq; using System.Reflection; using Pretzel.Commands; - +using Pretzel.Logic.Templating; namespace Pretzel { @@ -30,12 +32,19 @@ public void Run(string[] args) var commandName = args[0]; var commandArgs = args.Skip(1).ToArray(); Commands[commandName].Execute(commandArgs); + WaitForClose(); + } + + [Conditional("DEBUG")] + public void WaitForClose() + { + Console.ReadLine(); } public void Compose() { - var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); - var container = new CompositionContainer(catalog); + var first = new AssemblyCatalog(Assembly.GetExecutingAssembly()); + var container = new CompositionContainer(first); container.ComposeParts(this); } } From 867e0a62b422b53e0793f162a8a48746849bd30e Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Thu, 2 Feb 2012 00:31:33 +1100 Subject: [PATCH 3/3] removed old test file --- .../Templating/Jekyll/LiquidEngineTests.cs | 309 ------------------ 1 file changed, 309 deletions(-) delete mode 100644 src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs diff --git a/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs b/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs deleted file mode 100644 index 65fd9bdcd..000000000 --- a/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs +++ /dev/null @@ -1,309 +0,0 @@ -using System.IO.Abstractions.TestingHelpers; -using Pretzel.Logic.Templating.Jekyll; -using Xunit; - -namespace Pretzel.Tests.Templating.Jekyll -{ - public static class TestExtensions - { - public static string RemoveWhiteSpace(this string s) - { - return s.Replace("\r\n", "").Replace("\n", ""); - } - } - - public class LiquidEngineTests - { - public class When_Recieving_A_Folder_Containing_One_File : BakingEnvironment - { - const string FileContents = ""; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - var context = new SiteContext {Folder = @"C:\website\"}; - FileSystem.AddFile(@"C:\website\index.html", new MockFileData(FileContents)); - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void That_Site_Folder_Is_Created() - { - Assert.True(FileSystem.Directory.Exists(@"C:\website\_site")); - } - - [Fact] - public void The_File_Is_Added_At_The_Root() - { - Assert.True(FileSystem.File.Exists(@"C:\website\_site\index.html")); - } - - [Fact] - public void The_File_Is_Identical() - { - Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html")); - } - } - - - public class When_Recieving_A_Folder_Without_A_Trailing_Slash : BakingEnvironment - { - const string FileContents = ""; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - var context = new SiteContext { Folder = @"C:\website" }; - FileSystem.AddFile(@"C:\website\index.html", new MockFileData(FileContents)); - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_File_Is_Added_At_The_Root() - { - Assert.True(FileSystem.File.Exists(@"C:\website\_site\index.html")); - } - } - - public class When_Recieving_A_Folder_Containing_One_File_In_A_Subfolder : BakingEnvironment - { - const string FileContents = ""; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - var context = new SiteContext { Folder = @"C:\website\" }; - FileSystem.AddFile(@"C:\website\content\index.html", new MockFileData(FileContents)); - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void That_Child_Folder_Is_Created() - { - Assert.True(FileSystem.Directory.Exists(@"C:\website\_site\content")); - } - - [Fact] - public void The_File_Is_Added_At_The_Root() - { - Assert.True(FileSystem.File.Exists(@"C:\website\_site\content\index.html")); - } - - [Fact] - public void The_File_Is_Identical() - { - Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\content\index.html")); - } - } - - public class When_Recieving_A_File_Without_Metadata : BakingEnvironment - { - const string FileContents = "{{ page.title }}"; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - FileSystem.AddFile(@"C:\website\index.html", new MockFileData(FileContents)); - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_File_Is_Unaltered() - { - Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html")); - } - } - - public class When_Recieving_A_Markdown_File : BakingEnvironment - { - const string TemplateContents = "{{ page.title }}{{ content }}"; - const string PageContents = "---\r\n layout: default\r\n---\r\n\r\n# Hello World!"; - const string ExpectedfileContents = "My Web Site

Hello World!

"; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents)); - FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_File_Is_Applies_Data_To_The_Template() - { - Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace()); - } - - [Fact] - public void Does_Not_Copy_Template_To_Output() - { - Assert.False(FileSystem.File.Exists(@"C:\website\_site\_layouts\default.html")); - } - } - - public class When_A_Template_Also_Has_A_Layout_Value : BakingEnvironment - { - const string ParentTemplateContents = "{{ page.title }}{{ content }}"; - const string InnerTemplateContents = "---\r\n layout: parent\r\n---\r\n\r\n

Title

{{content}}"; - const string PageContents = "---\r\n layout: inner\r\n---\r\n\r\n## Hello World!"; - const string ExpectedfileContents = "My Web Site

Title

Hello World!

"; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - FileSystem.AddFile(@"C:\website\_layouts\parent.html", new MockFileData(ParentTemplateContents)); - FileSystem.AddFile(@"C:\website\_layouts\inner.html", new MockFileData(InnerTemplateContents)); - FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); - - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_File_Is_Applies_Data_To_The_Template() - { - Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace()); - } - - [Fact] - public void Does_Not_Copy_Template_To_Output() - { - Assert.False(FileSystem.File.Exists(@"C:\website\_site\_layouts\default.html")); - } - } - - public class Given_Markdown_Page_Has_A_Permalink : BakingEnvironment - { - const string PageContents = "---\r\npermalink: /somepage.html\r\n---\r\n\r\n# Hello World!"; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_File_Should_Be_At_A_Different_Path() - { - Assert.True(FileSystem.File.Exists(@"C:\website\_site\somepage.html")); - } - } - - public class Given_Markdown_Page_Has_A_Title : BakingEnvironment - { - const string TemplateContents = "{{ page.title }}{{ content }}"; - const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!"; - const string ExpectedfileContents = "A different title

Hello World!

"; - - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents)); - FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_Output_Should_Override_The_Site_Title() - { - Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace()); - } - } - - public class Given_A_Folder_Starting_With_Dot : BakingEnvironment - { - const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!"; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - FileSystem.AddFile(@"C:\website\.gems\file.txt", new MockFileData(PageContents)); - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_Folder_Should_Be_Ignored() - { - Assert.False(FileSystem.File.Exists(@"C:\website\_site\.gems\file.txt")); - } - } - - - public class When_Aeoth_Tests_The_Edge_Cases_Of_Handling_YAML_Front_Matter : BakingEnvironment - { - const string PageContents = "---\n---"; - - public override JekyllEngine Given() - { - return new JekyllEngine(); - } - - public override void When() - { - FileSystem.AddFile(@"C:\website\file.txt", new MockFileData(PageContents)); - var context = new SiteContext { Folder = @"C:\website\", Title = "My Web Site" }; - Subject.Initialize(FileSystem, context); - Subject.Process(); - } - - [Fact] - public void The_Yaml_Matter_Should_Be_Cleared() - { - var text = FileSystem.File.ReadAllText(@"C:\website\_site\file.txt"); - Assert.False(text.StartsWith("---")); - } - } - } -} \ No newline at end of file