Example #1
0
def test_mtime(tmpdir: Path) -> None:
    (tmpdir / "foo").touch()
    item = run(Item(path=tmpdir / "foo"))
    assert item.file_mtime == item.dir_mtime

    sleep(0.01)

    (tmpdir / "bar").touch()
    new_item = run(Item(path=tmpdir / "foo"))
    assert new_item.file_mtime < new_item.dir_mtime
Example #2
0
def test_with_git_in_subdir(tmpdir: Path) -> None:
    repodir = tmpdir / "some" / "subdir"
    repodir.mkdir(parents=True)

    repo = Repo.init(str(repodir))

    item = Item(path=repodir / "xyz")
    item.path.touch()

    # before commit
    new_item = run(item)
    assert new_item.author == item.author
    assert new_item.file_date == item.file_date
    assert new_item.dir_date == item.dir_date
    assert new_item.file_mtime != item.file_mtime

    # after commit
    repo.stage([item.path.name])

    timestamp = int(time())
    repo.do_commit(
        message=b"msg",
        committer=b"foo <a@b>",
        author=b"foo bar baz <x@y>",
        author_timestamp=timestamp,
    )

    new_item = run(item)
    assert new_item.author == "foo bar baz"
    assert new_item.file_date == timestamp
    assert new_item.dir_date == timestamp
    assert new_item.file_mtime != item.file_mtime
    assert new_item.dir_mtime != item.dir_mtime
Example #3
0
def test_coderef(tmpdir: Path) -> None:
    org = dedent("""
        #+begin_src python
        print("foo")  # (ref:1)
        print("bar")  # (ref:2)
        #+end_src

        #+begin_src c
        printf("xyz"); // (ref:3)
        #+end_src
        abcd [[(1)]] [[(2)]] [[(3)]]
        """)
    (tmpdir / "xyz.org").write_text(org)
    item = run(Item(path=tmpdir / "xyz.org"))
    soup = BeautifulSoup(item.content, "html.parser")

    first, second = soup.select(".org-src-container")
    assert len(first.select("#coderef-1")) == 1
    assert len(first.select("#coderef-2")) == 1
    assert len(first.select("#coderef-3")) == 0

    assert len(second.select("#coderef-1")) == 0
    assert len(second.select("#coderef-2")) == 0
    assert len(second.select("#coderef-3")) == 1

    assert len(soup.select("[href='#coderef-1']")) == 1
    assert len(soup.select("[href='#coderef-2']")) == 1
    assert len(soup.select("[href='#coderef-3']")) == 1
Example #4
0
def test_run_absolute(tmpdir: Path) -> None:
    files = [
        (tmpdir / "x/src/index.md", "dst/index.html"),
        (tmpdir / "x/src/foo.md", "dst/foo/index.html"),
        (tmpdir / "x/src/bar/index.md", "dst/bar/index.html"),
        (tmpdir / "x/src/bar/baz.md", "dst/bar/baz/index.html"),
        (tmpdir / "x/src/abc/def/index.md", "dst/abc/def/index.html"),
        (tmpdir / "x/src/abc/def/m00.md", "dst/abc/def/m00/index.html"),
    ]

    config = {
        "build": {
            "build-dir": Path("dst"),
            "collection-dir": tmpdir / "x" / Path("src"),
        }
    }

    config["build"]["build-dir"].mkdir(parents=True)
    config["build"]["collection-dir"].mkdir(parents=True)

    for src, _ in files:
        item = Item(path=src, content=str(src))
        assert isinstance(run(item, config=config), Item)

    for src, dst in files:
        assert (tmpdir / dst).read_text() == str(src)
Example #5
0
def test_code(tmpdir: Path) -> None:
    item = Item(path=tmpdir / "code.md")
    md = dedent(
        """
        # abcd

        ```python
        print("m00")
        ```

        ```c
        exit(EXIT_SUCCESS);
        ```

        ```
        asdf
        ```

        ```foobarbazqux
        hmm
        ```
        """
    )
    item.path.write_text(md)

    content = run(item).content
    assert content.count("highlight") == 2
    assert content.count("exit") == 1
    assert content.count("hmm") == 1
Example #6
0
def test_with_git_in_cwd(tmpdir: Path) -> None:  # pylint: disable=W0613
    repo = Repo.init(".")

    item = Item(path=Path("src") / "foo")
    item.path.parent.mkdir()
    item.path.touch()

    # before commit
    new_item = run(item)
    assert new_item.author == item.author
    assert new_item.file_date == item.file_date
    assert new_item.dir_date == item.dir_date
    assert new_item.file_mtime != item.file_mtime
    assert new_item.dir_mtime != item.dir_mtime

    # after commit
    repo.stage([str(item.path)])

    timestamp = int(time())
    repo.do_commit(
        message=b"msg",
        committer=b"foo <a@b>",
        author=b"bar <x@y>",
        author_timestamp=timestamp,
    )

    new_item = run(item)
    assert new_item.author == "bar"
    assert new_item.file_date == timestamp
    assert new_item.dir_date == timestamp
    assert new_item.file_mtime != item.file_mtime
    assert new_item.dir_mtime != item.dir_mtime
Example #7
0
def test_run_without_options_or_fallback(tmpdir: Path) -> None:
    (tmpdir / "xyz.org").write_text("text123")
    item = run(Item(path=tmpdir / "xyz.org"))

    assert item.title == ""
    assert item.author == ""
    assert item.file_date == 0.0
    assert item.content.count("text123") == 1
Example #8
0
def test_no_title(tmpdir: Path) -> None:
    path = tmpdir / "foo.md"
    path.write_text("## second")
    item = Item(path=path)

    res = run(item)
    assert res.content.index("second")
    assert res != item
Example #9
0
def test_render(tmpdir: Path) -> None:
    (tmpdir / "template").write_text("x {{ item.content }} y")

    item = Item(content="abc", path=tmpdir / "foo", template="template")
    config = {"build": {"template-dir": "."}}

    new_item = run(item, items=[], config=config)
    assert new_item.content == "x abc y"
    assert new_item != item
Example #10
0
def test_run_invalid_process(tmpdir: Path, mocker: MockFixture) -> None:
    check_output = mocker.patch("luoda.plugins.org.check_output")
    check_output.side_effect = CalledProcessError(
        cmd=["foo"],
        output=b"bar",
        returncode=1,
    )

    with raises(PluginError, match="bar"):
        run(Item(path=tmpdir / "foo.org"))
Example #11
0
def test_title(tmpdir: Path) -> None:
    path = tmpdir / "foo.md"
    path.write_text("#  first \n ## second")
    item = Item(path=path)

    res = run(item)
    assert res.content.count("first") == 0
    assert res.content.count("second") == 1
    assert res.title == "first"
    assert res != item
Example #12
0
def test_run(tmpdir: Path) -> None:
    tests = [
        ("", ""),
        ("<html>\n\n\n", "<html>"),
        ("<div>\n<p>\n\n\n foo \n\n\n</p></div>", "<div><p> foo </p></div>"),
        ("<p>\n\n abcd \n</p>", "<p> abcd </p>"),
        (b"<p>\n\n abcd \n</p>", b"<p>\n\n abcd \n</p>"),
    ]

    for a, b in tests:
        assert run(Item(path=tmpdir, content=a)).content == b  # type: ignore
Example #13
0
def test_without_git(tmpdir: Path) -> None:
    path = tmpdir / "foo"
    path.touch()

    item = Item(path=path)
    new_item = run(item)

    assert new_item.author == item.author
    assert new_item.file_date == item.file_date
    assert new_item.dir_date == item.dir_date
    assert new_item.file_mtime != item.file_mtime
    assert new_item.dir_mtime != item.dir_mtime
Example #14
0
def test_run(tmpdir: Path) -> None:
    config = {
        "build": {
            "build-dir": tmpdir / "build",
            "highlight": "default",
        }
    }
    item = Item(path=Path(tmpdir / "xyz"))
    output = tmpdir / "build" / "static" / "highlight.css"

    assert run(item, config) == item
    assert output.is_file()
    assert output.read_text().count("color") > 0
Example #15
0
def test_date(tmpdir: Path) -> None:  # pylint: disable=W0613
    repo = Repo.init(".")

    parent = Path("src")
    parent.mkdir()

    foo = parent / "foo"
    bar = parent / "bar"

    foo.touch()
    bar.touch()

    timestamp = int(time())

    repo.stage([str(foo)])
    repo.do_commit(
        message=b"msg",
        committer=b"foo <a@b>",
        author=b"bar <x@y>",
        author_timestamp=timestamp,
    )

    first = run(Item(path=foo))
    assert first.file_date == timestamp
    assert first.dir_date == timestamp

    repo.stage([str(bar)])
    repo.do_commit(
        message=b"msg",
        committer=b"foo <a@b>",
        author=b"bar <x@y>",
        author_timestamp=timestamp + 100,
    )

    second = run(Item(path=bar))
    assert second.file_date == timestamp + 100
    assert second.dir_date == timestamp
Example #16
0
def test_run(tmpdir: Path) -> None:
    tests = [
        ("x.html", "template", False),
        ("x.html", "", True),
        ("xyz", "template", False),
        ("xyz", "", False),
    ]

    for name, template, supported in tests:
        (tmpdir / name).write_text(name)
        item = Item(path=tmpdir / name, template=template)
        if supported:
            assert run(item).content == name.encode()
        else:
            assert run(item) == item
Example #17
0
def test_run_without_options(tmpdir: Path) -> None:
    org = dedent("""
        *   first
        **  second
        text123
        """)
    (tmpdir / "xyz.org").write_text(org)
    item = run(Item(path=tmpdir / "xyz.org"))

    assert item.title == "first"
    assert item.author == ""
    assert item.file_date == 0.0
    assert item.content.count("first") == 0
    assert item.content.count("second") == 1
    assert item.content.count("text123") == 1
Example #18
0
def test_run_with_options(tmpdir: Path) -> None:
    org = dedent("""
        #+author: Foo Bar
        #+title: option title
        #+date: 1970-01-02

        *   first
        **  second
        text123

        #+begin_src python
        print("m00")
        #+end_src

        #+begin_src c
        exit(0);
        #+end_src

        #+begin_src foobarbazqux
        unknown lexer
        #+end_src

        #+begin_src
        no lang
        #+end_src
        """)
    (tmpdir / "xyz.org").write_text(org)
    item = run(Item(path=tmpdir / "xyz.org"))

    assert item.author == "Foo Bar"
    assert item.title == "option title"
    assert item.file_date == 86400.0
    assert item.content.count("option title") == 0
    assert item.content.count("first") == 1
    assert item.content.count("second") == 1
    assert item.content.count("text123") == 1
    assert item.content.count("unknown lexer") == 1
    assert item.content.count("no lang") == 1
    assert item.content.count("highlight") == 2
Example #19
0
def test_run_bytes(tmpdir: Path) -> None:
    files = [
        ("src/index.png", "dst/index.png"),
        ("src/bar/baz.svg", "dst/bar/baz.svg"),
        ("src/abc/def/m00.css", "dst/abc/def/m00.css"),
    ]

    config = {
        "build": {
            "build-dir": Path("dst"),
            "collection-dir": Path("src"),
        }
    }

    config["build"]["build-dir"].mkdir(parents=True)
    config["build"]["collection-dir"].mkdir(parents=True)

    for src, _ in files:
        path = Path(src)
        item = Item(path=path, content=src.encode())
        assert isinstance(run(item, config=config), Item)

    for src, dst in files:
        assert (tmpdir / dst).read_bytes().decode() == src
Example #20
0
def test_run_not_org(tmpdir: Path) -> None:
    path = tmpdir / "abcd"
    path.touch()

    item = Item(path=path)
    assert run(item) == item
Example #21
0
def test_template_not_found(tmpdir: Path) -> None:
    item = Item(content="abc", path=tmpdir / "foo", template="template")
    config = {"build": {"template-dir": "."}}

    assert run(item, items=[], config=config) == item
Example #22
0
def test_not_markdown() -> None:
    path = Path("foo")
    item = Item(path=path)

    assert run(item) == item