public void page_frontmatter_should_have_priority_over_defaults_in_config() { // Arrange fileSystem.AddFile(@"C:\TestSite\_config.yml", new MockFileData(@" defaults: - scope: path: '' values: author: 'default-author' ")); fileSystem.AddFile(@"C:\TestSite\about.md", new MockFileData(@"--- author: 'page-specific-author' --- # About page ")); var config = new Configuration(fileSystem); config.ReadFromFile(@"C:\TestSite"); var sut = new SiteContextGenerator(fileSystem, new LinkHelper(), config); // Act var siteContext = sut.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); // Assert Assert.Equal("page-specific-author", siteContext.Pages[0].Bag["author"]); }
public void render_with_ContentTransformer_exception_should_trace_the_error() { fileSystem.AddFile(@"C:\TestSite\SomeFile.md", new MockFileData("---\r\n---# Title\r\n[foo]")); StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); Tracing.Logger.AddCategory(Tracing.Category.Info); Tracing.Logger.AddCategory(Tracing.Category.Error); Tracing.Logger.AddCategory(Tracing.Category.Debug); var contentTransformer = Substitute.For <IContentTransform>(); contentTransformer.Transform(Arg.Any <string>()).Returns(s => { throw new Exception("foo bar"); }); var generator = new SiteContextGenerator(fileSystem, new[] { contentTransformer }, new LinkHelper()); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); Assert.Equal(1, siteContext.Pages.Count); Assert.Equal("<p><b>Error converting markdown</b></p><pre>---\r\n---# Title\r\n[foo]</pre>", siteContext.Pages[0].Content); Assert.Contains(@"Error (foo bar) converting C:\TestSite\SomeFile.md", sb.ToString()); Assert.Contains(@"System.Exception: foo bar", sb.ToString()); }
public void CanBeIncluded_Scenarios_Exclude() { // arrange var generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock(@"--- exclude: [folder, .htaccess, some-file.tmp, test\somefile.txt, subfolder\childfolder, anotherfolder\tempfile.tmp] ---")); Func <string, bool> function = generator.CanBeIncluded; // assert Assert.False(function("folder")); Assert.False(function("folder\file.txt")); Assert.False(function("_folder")); // .htaccess is excluded Assert.False(function(".htaccess")); Assert.False(function(".something-else")); // temp files are ignored Assert.False(function("some-file.tmp")); Assert.False(function("some-file.TMP")); Assert.True(function("another-file.bar")); Assert.False(function(@"test\somefile.txt")); Assert.False(function(@"subfolder\childfolder")); Assert.False(function(@"anotherfolder\tempfile.tmp")); }
public void CanBeIncluded_Scenarios_IncludeExclude() { // arrange var generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock(@"include: [_folder, .something-else] exclude: [folder, test\somefile.txt]")); Func <string, bool> function = generator.CanBeIncluded; // underscores are ignored Assert.False(function("folder")); Assert.True(function("_folder")); Assert.True(function("somefolder")); Assert.False(function("_somefolder")); // .htaccess is included Assert.True(function(".htaccess")); Assert.True(function(".something-else")); Assert.False(function(".pointedfile")); // temp files are ignored Assert.False(function("some-file.tmp")); Assert.False(function("some-file.TMP")); Assert.True(function("another-file.bar")); // and these ones are causing me headaches in Sublime Assert.False(function(@"docs\pages\features\.a4agat3qqt3.tmp")); Assert.False(function(@"test\somefile.txt")); Assert.True(function(@"subfolder\childfolder")); Assert.False(function(@"anotherfolder\tempfile.tmp")); Assert.True(function(@"anotherfolder\textfile.txt")); }
public void defaults_in_config_should_be_combined_with_page_frontmatter_in_page_bag() { // Arrange fileSystem.AddFile(@"C:\TestSite\_config.yml", new MockFileData(@" defaults: - scope: path: '' values: author: 'default-author' ")); fileSystem.AddFile(@"C:\TestSite\about.md", new MockFileData(@"--- title: 'about' --- # About page ")); var config = new Configuration(fileSystem); config.ReadFromFile(@"C:\TestSite"); var sut = new SiteContextGenerator(fileSystem, new LinkHelper(), config); // Act var siteContext = sut.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); // Assert Assert.Equal("about", siteContext.Pages[0].Bag["title"]); // from page frontmatter Assert.Equal("default-author", siteContext.Pages[0].Bag["author"]); // from config defaults }
public override void When() { FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents)); FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents)); var generator = new SiteContextGenerator(FileSystem, Enumerable.Empty <IContentTransform>()); var context = generator.BuildContext(@"C:\website\", false); Subject.FileSystem = FileSystem; Subject.Process(context); }
public void file_with_2_ioexception_on_ReadAllText_is_not_processed_and_exception_traced() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; // arrange string filePath = Path.Combine(Path.GetTempPath(), "SomeFile.md"); var fileSubstitute = Substitute.For <FileBase>(); fileSubstitute.OpenText(Arg.Any <string>()).Returns(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes("---")))); fileSubstitute.ReadAllText(Arg.Any <string>()).Returns(x => { throw new IOException(); }); fileSubstitute.Exists(filePath).Returns(true); var directorySubstitute = Substitute.For <DirectoryBase>(); directorySubstitute.GetFiles(Arg.Any <string>(), "*.*", SearchOption.AllDirectories).Returns(new[] { @"C:\TestSite\SomeFile.md" }); var fileInfoSubstitute = Substitute.For <FileInfoBase>(); fileInfoSubstitute.Name.Returns("SomeFile.md"); var fileInfoFactorySubstitute = Substitute.For <IFileInfoFactory>(); fileInfoFactorySubstitute.FromFileName(Arg.Any <string>()).Returns(fileInfoSubstitute); var fileSystemSubstitute = Substitute.For <System.IO.Abstractions.IFileSystem>(); fileSystemSubstitute.File.Returns(fileSubstitute); fileSystemSubstitute.Directory.Returns(directorySubstitute); fileSystemSubstitute.FileInfo.Returns(fileInfoFactorySubstitute); StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); Tracing.Logger.AddCategory(Tracing.Category.Info); Tracing.Logger.AddCategory(Tracing.Category.Error); Tracing.Logger.AddCategory(Tracing.Category.Debug); var generator = new SiteContextGenerator(fileSystemSubstitute, new LinkHelper(), new Configuration()); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); // assert Assert.Equal(0, siteContext.Pages.Count); Assert.Contains(@"Failed to build post from File: C:\TestSite\SomeFile.md", sb.ToString()); Assert.Contains(@"I/O error occurred.", sb.ToString()); Assert.Contains(@"System.IO.IOException: I/O error occurred.", sb.ToString()); // Check if the temp file have been deleted fileSubstitute.Received().Delete(filePath); }
public override void When() { FileSystem.AddFile(@"C:\website\_layouts\default.cshtml", new MockFileData(TemplateContents)); FileSystem.AddFile(@"C:\website\index.cshtml", new MockFileData(IndexContents)); var generator = new SiteContextGenerator(FileSystem, new LinkHelper(), new Configuration()); var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false); Subject.FileSystem = FileSystem; Subject.Process(context); }
public void permalink_supports_custom_slug() { generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock(string.Format("permalink: {0}", "/:year-:month-:day/:slug.html"))); fileSystem.AddFile(@"C:\TestSite\_posts\2015-03-09-foobar-baz.md", new MockFileData(@"--- slug: my-slug ---# Title")); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); var firstPost = siteContext.Posts.First(); Assert.Equal("/2015-03-09/my-slug.html", firstPost.Url); }
public void permalink_is_well_formatted(string permalink, string expectedUrl, string categories) { generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock(string.Format("permalink: {0}", permalink))); fileSystem.AddFile(@"C:\TestSite\_posts\2015-03-09-foobar-baz.md", new MockFileData(string.Format(@"--- categories: [{0}] ---# Title", categories))); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); var firstPost = siteContext.Posts.First(); Assert.Equal(expectedUrl, firstPost.Url); }
public void file_with_1_ioexception_on_ReadAllText_is_processed() { // arrange string filePath = Path.Combine(Path.GetTempPath(), "SomeFile.md"); bool alreadyOccured = false; var fileSubstitute = Substitute.For <FileBase>(); fileSubstitute.OpenText(Arg.Any <string>()).Returns(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes("---")))); fileSubstitute.ReadAllText(Arg.Any <string>()).Returns(x => { if (alreadyOccured) { return("---\r\n---# Title"); } else { alreadyOccured = true; throw new IOException(); } }); fileSubstitute.Exists(filePath).Returns(true); var directorySubstitute = Substitute.For <DirectoryBase>(); directorySubstitute.GetFiles(Arg.Any <string>(), "*.*", SearchOption.AllDirectories).Returns(new[] { @"C:\TestSite\SomeFile.md" }); var fileInfoSubstitute = Substitute.For <FileInfoBase>(); fileInfoSubstitute.Name.Returns("SomeFile.md"); var fileInfoFactorySubstitute = Substitute.For <IFileInfoFactory>(); fileInfoFactorySubstitute.FromFileName(Arg.Any <string>()).Returns(fileInfoSubstitute); var fileSystemSubstitute = Substitute.For <IFileSystem>(); fileSystemSubstitute.File.Returns(fileSubstitute); fileSystemSubstitute.Directory.Returns(directorySubstitute); fileSystemSubstitute.FileInfo.Returns(fileInfoFactorySubstitute); var generator = new SiteContextGenerator(fileSystemSubstitute, new LinkHelper(), new Configuration()); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); // assert Assert.Equal(1, siteContext.Pages.Count); Assert.Equal("---\r\n---# Title", siteContext.Pages[0].Content); // Check if the temp file have been deleted fileSubstitute.Received().Delete(filePath); }
public override void When() { FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContent)); var generator = new SiteContextGenerator(FileSystem, new LinkHelper(), new ConfigurationMock(ConfigContents)); var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false); Subject.FileSystem = FileSystem; Subject.TagFactories = new List <TagFactoryBase> { new CustomTagFactory() }; Subject.Process(context); }
public void permalink_with_folder_categories_frontmatter_only() { generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock(@"only_frontmatter_categories: true")); fileSystem.AddFile(@"C:\TestSite\foo\bar\_posts\2015-03-09-SomeFile.md", new MockFileData(@"--- categories: [cat1, cat2] ---# Title")); var outputPath = "/cat1/cat2/2015/03/09/SomeFile.html"; // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); var firstPost = siteContext.Posts.First(); Assert.Equal(outputPath, firstPost.Url); }
public override void When() { FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents)); FileSystem.AddFile(@"C:\website\index.md", new MockFileData(IndexContents)); for (var i = 1; i <= 7; i++) { FileSystem.AddFile(String.Format(@"C:\website\_posts\2012-02-0{0}-p{0}.md", i), new MockFileData(PostContents)); } var generator = new SiteContextGenerator(FileSystem, Enumerable.Empty <IContentTransform>()); var context = generator.BuildContext(@"C:\website\", false); Subject.FileSystem = FileSystem; Subject.Process(context); }
public override void When() { FileSystem.AddFile(@"C:\website\_layouts\default.cshtml", new MockFileData(TemplateContents)); FileSystem.AddFile(@"C:\website\index.md", new MockFileData(IndexContents)); for (var i = 1; i <= 7; i++) { FileSystem.AddFile(String.Format(@"C:\website\_posts\2012-02-0{0}-p{0}.md", i), new MockFileData(String.Format(PostContents, i))); } var generator = new SiteContextGenerator(FileSystem, new LinkHelper(), new Configuration()); var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false); Subject.FileSystem = FileSystem; Subject.Process(context); }
public void render_with_ContentTransformer_should_transform_content() { fileSystem.AddFile(@"C:\TestSite\SomeFile.md", new MockFileData(@"--- ---# Title [foo]")); var contentTransformer = Substitute.For <IContentTransform>(); contentTransformer.Transform(Arg.Any <string>()).Returns(s => s[0].ToString().Replace("[foo]", "bar")); var generator = new SiteContextGenerator(fileSystem, new[] { contentTransformer }, new LinkHelper()); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); Assert.Equal(1, siteContext.Pages.Count); Assert.Equal("<h1>Title</h1><p>bar</p>", siteContext.Pages[0].Content.RemoveWhiteSpace()); }
public void posts_without_front_matter_and_override_config_renders_folder() { var post = new MockFileData("# Title"); var lastmod = new DateTime(2015, 03, 14); post.LastWriteTime = lastmod; fileSystem.AddFile(@"C:\TestSite\_posts\SomeFile.md", post); var generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new ConfigurationMock("permalink: /blog/:year/:month/:day/:title.html")); var outputPath = string.Format("/blog/{0}/{1}", lastmod.ToString("yyyy'/'MM'/'dd"), "SomeFile.html"); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); var firstPost = siteContext.Posts.First(); Assert.Equal(outputPath, firstPost.Url); }
public void SiteContextGenerator_IsIncludedPath() { // arrange var fs = new MockFileSystem(new Dictionary <string, MockFileData>()); var gen = new SiteContextGenerator(fs, new LinkHelper(), new ConfigurationMock(@"include: [_folder, .something-else] exclude: [folder, test\somefile.txt, .git]")); Func <string, bool> function = gen.IsIncludedPath; // include entires and subentries are included Assert.True(function("_folder")); Assert.True(function("_folder/somefile")); Assert.True(function(".something-else")); // exluded entries are not included Assert.False(function("folder")); Assert.False(function("folder/somefile")); Assert.False(function(@"test\somefile.txt")); Assert.False(function(".git")); // other entries are not included Assert.False(function("test")); Assert.False(function("asdf")); }
public SiteContextGeneratorTests() { fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>()); generator = new SiteContextGenerator(fileSystem, new LinkHelper(), new Configuration()); }
public void RemoveDiacritics_should_return_null_if_input_null() { Assert.Equal(null, SiteContextGenerator.RemoveDiacritics(null)); }
public void GetTitle_returns_strips_timestamp() { var result = SiteContextGenerator.GetTitle(@"C:\temp\2012-01-03-foobar_baz.md"); Assert.Equal("foobar_baz", result); }
public SiteContextGeneratorTests() { fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>()); generator = new SiteContextGenerator(fileSystem, Enumerable.Empty <IContentTransform>()); }
public void GetTitle_preserves_dash_separated_values_that_arent_timestamps() { var result = SiteContextGenerator.GetTitle(@"C:\temp\foo-bar-baz-qak-foobar_baz.md"); Assert.Equal("foo-bar-baz-qak-foobar_baz", result); }
public void RemoveDiacritics_should_remove_any_accents() { Assert.Equal("the cat is running & getting away", SiteContextGenerator.RemoveDiacritics("The cát ís running & getting away")); }
public void GetTitle_returns_original_value_when_no_timestamp() { var result = SiteContextGenerator.GetTitle(@"C:\temp\foobar_baz.md"); Assert.Equal("foobar_baz", result); }