public SiteContextGeneratorTests() { fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); generator = new SiteContextGenerator(fileSystem); }
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()); // 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 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()); // 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 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 SiteContextGeneratorTests() { fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); generator = new SiteContextGenerator(fileSystem, Enumerable.Empty<IContentTransform>()); }
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 SiteContextGenerator_IsExcludedPath() { // 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.IsExcludedPath; // include entires and subentries are not excluded Assert.False(function("_folder")); Assert.False(function("_folder/somefile")); Assert.False(function(".something-else")); // exluded entries are exluded Assert.True(function("folder")); Assert.True(function("folder/somefile")); Assert.True(function(@"test\somefile.txt")); Assert.True(function(".git")); // other entries are not excluded 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 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 permalink_with_folder_categories_frontmatter_only() { var 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 void permalink_is_well_formatted(string permalink, string expectedUrl, string categories) { var 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 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 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 SetUp() { _fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); _generator = new SiteContextGenerator(_fileSystem, new LinkHelper()); }
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, @"C:\TestSite"); config.ReadFromFile(); 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 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, @"C:\TestSite"); config.ReadFromFile(); 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 }