BuildContext() public method

public BuildContext ( string path, string destinationPath, bool includeDrafts ) : Pretzel.Logic.Templating.Context.SiteContext
path string
destinationPath string
includeDrafts bool
return Pretzel.Logic.Templating.Context.SiteContext
コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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());
        }
コード例 #7
0
        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());
        }
コード例 #8
0
        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"]);
        }
コード例 #9
0
        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
        }