Пример #1
0
    def test_rename_file_invalid_name(self):
        build_info = BuildInfo(files={
            "file1.txt": "contents",
        })

        with pytest.raises(ValueError):
            build_info.rename_file("bad_name", "new_name")
Пример #2
0
    def test_get_files_by_pattern(self):
        build_info = BuildInfo(files={
            "file1.html": "file1",
            "file2.css": "file2",
        })

        assert list(build_info.get_files_by_pattern("*.css")) == [
            ("file2.css", "file2"),
        ]
Пример #3
0
    def test_rename_file_success(self):
        build_info = BuildInfo(files={
            "file1.txt": "contents",
        })

        build_info.rename_file("file1.txt", "file2.txt")

        assert build_info.files == {
            "file2.txt": "contents",
        }
Пример #4
0
    def test_rename_file_same_name(self):
        mock_files = unittest.mock.MagicMock()
        mock_files.__contains__.return_value = True

        build_info = BuildInfo(files=mock_files)

        build_info.rename_file("name", "name")

        mock_files.__getitem__.assert_not_called()
        mock_files.__delitem__.assert_not_called()
        mock_files.__setitem__.assert_not_called()
Пример #5
0
def test_collections_key_defined():
    build_info = BuildInfo()
    build_info.metadata[COLLECTIONS_KEY] = {}

    collection = Collection(collection_name="test",
                            match_pattern="*.md",
                            order_by="order")
    collection.build(build_info)

    assert COLLECTIONS_KEY in build_info.metadata
    assert "test" in build_info.metadata[COLLECTIONS_KEY]
    assert build_info.metadata[COLLECTIONS_KEY]["test"] == []
Пример #6
0
def test_duplicate_collection_name():
    build_info = BuildInfo()
    build_info.metadata[COLLECTIONS_KEY] = {
        "test": [],
    }

    collection = Collection(collection_name="test",
                            match_pattern="*.md",
                            order_by="order")

    with pytest.raises(ValueError):
        collection.build(build_info)
Пример #7
0
    def test_get_files_by_regex(self):
        build_info = BuildInfo(files={
            "file1.html": "file1",
            "file2.css": "file2",
            "file3.js": "file3",
        })

        regex = re.compile(r".*\.(html|js)")
        assert list(build_info.get_files_by_regex(regex)) == [
            ("file1.html", "file1"),
            ("file3.js", "file3"),
        ]
Пример #8
0
    def build(self, build_info: BuildInfo):
        for file_name, file_info in build_info.get_files_by_pattern(
                self._match_pattern):
            try:
                permalink = self._permalink_selector(file_info)
            except Exception:
                continue

            if permalink.startswith("/"):
                permalink = permalink[1:]

            if permalink.endswith("/") or permalink == "":
                permalink += "index.html"

            build_info.rename_file(file_name, permalink)
Пример #9
0
def test_existing_metadata(mock_parse):
    files = {
        "file1":
        MockFileInfo("contents", {
            "existingKey1": "value1",
            "existingKey2": "value2",
        })
    }

    mock_parse.return_value = ({
        "existingKey1": "newValue",
        "newKey": "value"
    }, "newContents")

    frontmatter = Frontmatter()
    frontmatter.build(BuildInfo(files))

    assert files == {
        "file1":
        MockFileInfo(
            b"newContents", {
                "existingKey1": "newValue",
                "existingKey2": "value2",
                "newKey": "value",
            })
    }
Пример #10
0
def test_valid_files(mock_parse):
    files = {
        "file1": MockFileInfo("contents1"),
        "file2": MockFileInfo("contents2"),
        "file3": MockFileInfo("contents3"),
    }

    mock_parse.side_effect = (
        ({
            "key1": "value1"
        }, "newContents1"),
        ({
            "key2": "value2"
        }, "newContents2"),
        ({
            "key3": "value3"
        }, "newContents3"),
    )

    frontmatter = Frontmatter()
    frontmatter.build(BuildInfo(files))

    mock_parse.assert_has_calls(
        (call("contents1"), call("contents2"), call("contents3")))
    assert files == {
        "file1": MockFileInfo(b"newContents1", {"key1": "value1"}),
        "file2": MockFileInfo(b"newContents2", {"key2": "value2"}),
        "file3": MockFileInfo(b"newContents3", {"key3": "value3"}),
    }
Пример #11
0
    def test_process_file(self, mock_environment_constructor):
        mock_from_string = mock_environment_constructor.return_value.from_string
        mock_template = mock_from_string.return_value
        mock_template.render.return_value = "rendered"

        build_info = BuildInfo()
        build_info.metadata["key"] = "value"
        file_info = MockFileInfo(b"original")
        template = ContentTemplate()
        template.process_file(build_info, "name", file_info)

        mock_from_string.assert_called_once_with("original")
        mock_template.render.assert_called_once_with(site={
            "key": "value",
        })
        assert file_info == MockFileInfo(b"rendered")
Пример #12
0
    def test_exception_getting_layout(self, mock_environment_constructor):
        mock_get_template = mock_environment_constructor.return_value.get_template

        file_info = MockFileInfo(b"contents")
        build_info = BuildInfo()
        template = LayoutTemplate()
        output_name = template.process_file(build_info, "file.txt", file_info)

        assert output_name is None
        mock_get_template.assert_not_called()
Пример #13
0
    def test_process_file(self, mock_environment_constructor,
                          process_file_variants):
        output_extension, input_name, expected_output_name = process_file_variants

        file_info = MockFileInfo(b"contents", metadata={"layout": "test"})
        mock_get_template = mock_environment_constructor.return_value.get_template
        mock_template = mock_get_template.return_value
        mock_template.render.return_value = "rendered"

        build_info = BuildInfo()
        build_info.metadata["key"] = "value"
        template = LayoutTemplate(output_extension=output_extension)
        output_name = template.process_file(build_info, input_name, file_info)

        mock_get_template.assert_called_once_with("test")
        mock_template.render.assert_called_once_with(contents="contents",
                                                     page={"layout": "test"},
                                                     site={"key": "value"})
        assert output_name == expected_output_name
        assert file_info == MockFileInfo(b"rendered",
                                         metadata={"layout": "test"})
Пример #14
0
def test_parse_raises_exception(mock_parse):
    build_info = BuildInfo(files={
        "file1": MockFileInfo("contents"),
    })

    mock_parse.side_effect = Exception("error")

    frontmatter = Frontmatter()
    frontmatter.build(build_info)

    assert build_info.files == {
        "file1": MockFileInfo("contents"),
    }
Пример #15
0
def test_extras(mock_markdown2):
    files = {
        "test.md": MockFileInfo("contents"),
    }

    mock_markdown2.return_value = "parsedContents"

    markdown = Markdown(extras="test")
    markdown.build(BuildInfo(files))

    mock_markdown2.assert_called_once_with("contents", extras="test")
    assert files == {
        "test.md": MockFileInfo(b"parsedContents"),
    }
Пример #16
0
def test_valid_files_no_rename(mock_compile):
    files = {
        "test1.scss": MockFileInfo("contents1"),
    }

    mock_compile.return_value = "parsedContents1"

    sass = Sass(output_extension=".scss")
    sass.build(BuildInfo(files))

    mock_compile.assert_called_once_with(string="contents1")
    assert files == {
        "test1.scss": MockFileInfo(b"parsedContents1"),
    }
Пример #17
0
def test_compile_args(mock_compile):
    files = {
        "test1.scss": MockFileInfo("contents1"),
    }

    mock_compile.return_value = "parsedContents1"

    sass = Sass(compile_args={"extra_arg": "value"})
    sass.build(BuildInfo(files))

    mock_compile.assert_called_once_with(string="contents1", extra_arg="value")
    assert files == {
        "test1.css": MockFileInfo(b"parsedContents1"),
    }
Пример #18
0
def test_valid_files(mock_jsmin):
    files = {
        "test1.js": MockFileInfo(b"contents1"),
        "test2.js": MockFileInfo(b"contents2"),
    }

    mock_jsmin.side_effect = (b"parsedContents1", b"parsedContents2")

    minify = Minify()
    minify.build(BuildInfo(files))

    mock_jsmin.assert_has_calls((call(b"contents1"), call(b"contents2")))
    assert files == {
        "test1.js": MockFileInfo(b"parsedContents1"),
        "test2.js": MockFileInfo(b"parsedContents2"),
    }
Пример #19
0
def test_valid_files_rename(mock_compile):
    files = {
        "test1.scss": MockFileInfo("contents1"),
        "test2.sass": MockFileInfo("contents2"),
    }

    mock_compile.side_effect = ("parsedContents1", "parsedContents2")

    sass = Sass()
    sass.build(BuildInfo(files))

    mock_compile.assert_has_calls(
        (call(string="contents1"), call(string="contents2")))
    assert files == {
        "test1.css": MockFileInfo(b"parsedContents1"),
        "test2.css": MockFileInfo(b"parsedContents2"),
    }
Пример #20
0
def test_valid_files(mock_markdown2):
    files = {
        "test1.md": MockFileInfo("contents1"),
        "test2.md": MockFileInfo("contents2"),
    }

    mock_markdown2.side_effect = ("parsedContents1", "parsedContents2")

    markdown = Markdown()
    markdown.build(BuildInfo(files))

    mock_markdown2.assert_has_calls(
        (call("contents1", extras=None), call("contents2", extras=None)))
    assert files == {
        "test1.md": MockFileInfo(b"parsedContents1"),
        "test2.md": MockFileInfo(b"parsedContents2"),
    }
Пример #21
0
def test_build(monkeypatch):
    build_info = BuildInfo(files={
        "file1.html": MockFileInfo("file1"),
        "file2.txt": MockFileInfo("file2"),
        "file3.html": MockFileInfo("file3", metadata={"permalink": "file3"}),
        "file4.html": MockFileInfo("file4", metadata={"permalink": "/file4"}),
        "file5.html": MockFileInfo("file5", metadata={"permalink": "/file5/"}),
        "file6.html": MockFileInfo("file6", metadata={"permalink": "/"}),
        "file7.html": MockFileInfo("file6", metadata={"permalink": ""}),
    })

    mock_rename_file = unittest.mock.Mock()
    with unittest.mock.patch("pysmith.BuildInfo.rename_file", mock_rename_file):
        permalink = Permalink()
        permalink.build(build_info)

    mock_rename_file.assert_has_calls((
        call("file3.html", "file3"),
        call("file4.html", "file4"),
        call("file5.html", "file5/index.html"),
        call("file6.html", "index.html"),
        call("file7.html", "index.html"),
    ))
Пример #22
0
    def test_build_with_some_renames(self, mock_environment_constructor):
        mock_process_file = unittest.mock.Mock()
        mock_process_file.side_effect = [None, "renamed.md"]
        mock_file_1 = MockFileInfo("contents1")
        mock_file_2 = MockFileInfo("contents2")
        files = {
            "test1.md": mock_file_1,
            "test2.md": mock_file_2,
        }

        build_info = BuildInfo(files)
        template = _BaseTemplate(match_pattern="*.md")
        template.process_file = mock_process_file
        template.build(build_info)

        mock_environment_constructor.assert_called_once_with()
        mock_process_file.assert_has_calls((
            call(build_info, "test1.md", mock_file_1),
            call(build_info, "test2.md", mock_file_2),
        ))
        assert files == {
            "test1.md": MockFileInfo("contents1"),
            "renamed.md": MockFileInfo("contents2"),
        }
Пример #23
0
def test_build(reverse):
    ordered_file_1 = MockFileInfo("contents1", metadata={"order": 1})
    ordered_file_2 = MockFileInfo("contents2", metadata={"order": 2})
    ordered_file_3 = MockFileInfo("contents3", metadata={"order": 3})
    skipped_file_1 = MockFileInfo("contents4")
    skipped_file_2 = MockFileInfo("contents5")
    files = {
        "a.md": ordered_file_3,
        "x.js": skipped_file_1,
        "b.md": ordered_file_2,
        "y.js": skipped_file_2,
        "z.md": ordered_file_1,
    }

    build_info = BuildInfo(files)
    collection = Collection(collection_name="test",
                            match_pattern="*.md",
                            order_by="order",
                            reverse=reverse)
    collection.build(build_info)

    assert COLLECTIONS_KEY in build_info.metadata
    assert "test" in build_info.metadata[COLLECTIONS_KEY]

    if reverse:
        assert build_info.metadata[COLLECTIONS_KEY]["test"] == [
            ordered_file_3,
            ordered_file_2,
            ordered_file_1,
        ]
    else:
        assert build_info.metadata[COLLECTIONS_KEY]["test"] == [
            ordered_file_1,
            ordered_file_2,
            ordered_file_3,
        ]
Пример #24
0
 def build(self, build_info: BuildInfo):
     build_info.metadata["title"] = "Dave Korhumel"
Пример #25
0
 def test_constructor(self):
     build_info = BuildInfo()
     assert build_info.metadata == {}