Repository: executablebooks/jupyter-book
File: tests/test_build.py (Python)

Code before:

def test_build_from_template(tmpdir, cli):
    """Test building the book template and a few test configs."""
    # Create the book from the template
    book = Path(tmpdir) / "new_book"
    _ = cli.invoke(commands.create, str(book))
    build_result = cli.invoke(commands.build, str(book))
    assert build_result.exit_code == 0
    html = book.joinpath("_build", "html")
    assert html.joinpath("index.html").exists()
    assert html.joinpath("intro.html").exists()


def test_custom_config(cli, build_resources):
    books, _ = build_resources
    config = books.joinpath("config")
    result = cli.invoke(commands.build, str(config))
    assert result.exit_code == 0
    html = config.joinpath("_build", "html", "index.html").read_text()
    assert '<h1 class="site-logo" id="site-title">TEST PROJECT NAME</h1>' in html
    assert '<div class="sphinx-tabs docutils container">' in html
    assert '<link rel="stylesheet" type="text/css" href="_static/mycss.css" />' in html
    assert '<script src="_static/js/myjs.js"></script>' in html


@pytest.mark.parametrize("toc", ["_toc.yml", "_toc_startwithlist.yml"])
def test_toc_builds(cli, build_resources, toc):
    """Test building the book template with several different TOC files."""
    books, tocs = build_resources
    toc = str(tocs / toc)
    result = cli.invoke(commands.build, [str(tocs), "--toc", toc, "-W"])
    assert result.exit_code == 0


def test_toc_rebuild(cli, build_resources):
    """Changes to the TOC should force a re-build of pages. Also tests for changes
    to the relative ordering of content pages.
    """
    _, tocs = build_resources
    toc = tocs / "_toc_simple.yml"
    index_html = tocs.joinpath("_build", "html", "index.html")

    # Not using -W because we expect warnings for pages not listed in TOC
    result = cli.invoke(commands.build, [str(tocs), "--toc", str(toc)])
    html = BeautifulSoup(index_html.read_text(), "html.parser")
    tags = html.find_all("a", "reference internal")
    assert tags[1].attrs["href"] == "content1.html"
    assert tags[2].attrs["href"] == "content2.html"

    toc.write_text("- file: index\n- file: content2\n- file: content1\n")
    result = cli.invoke(commands.build, [str(tocs), "--toc", str(toc)])

Review comment (bug):
@choldgraf , there is one expected test failure caused by this ToC file.  Is disabling warnings here OK? The warning is `Found a content page that is not in _toc.yml: content3.rst.`

Code after:

def test_build_from_template(tmpdir, cli):
    """Test building the book template and a few test configs."""
    # Create the book from the template
    book = Path(tmpdir) / "new_book"
    _ = cli.invoke(commands.create, str(book))
    build_result = cli.invoke(commands.build, str(book))
    assert build_result.exit_code == 0
    html = book.joinpath("_build", "html")
    assert html.joinpath("index.html").exists()
    assert html.joinpath("intro.html").exists()


def test_custom_config(cli, build_resources):
    books, _ = build_resources
    config = books.joinpath("config")
    result = cli.invoke(commands.build, str(config))
    assert result.exit_code == 0
    html = config.joinpath("_build", "html", "index.html").read_text()
    assert '<h1 class="site-logo" id="site-title">TEST PROJECT NAME</h1>' in html
    assert '<div class="sphinx-tabs docutils container">' in html
    assert '<link rel="stylesheet" type="text/css" href="_static/mycss.css" />' in html
    assert '<script src="_static/js/myjs.js"></script>' in html


@pytest.mark.parametrize("toc", ["_toc.yml", "_toc_startwithlist.yml"])
def test_toc_builds(cli, build_resources, toc, tmpdir):
    """Test building the book template with several different TOC files."""
    books, tocs = build_resources
    toc = str(tocs / toc)
    result = cli.invoke(commands.build, [str(tocs), "--toc", toc, "-W"])
    assert result.exit_code == 0


def test_toc_rebuild(cli, build_resources):
    """Changes to the TOC should force a re-build of pages. Also tests for changes
    to the relative ordering of content pages.
    """
    _, tocs = build_resources
    toc = tocs / "_toc_simple.yml"
    index_html = tocs.joinpath("_build", "html", "index.html")

    # Not using -W because we expect warnings for pages not listed in TOC
    result = cli.invoke(commands.build, [str(tocs), "--toc", str(toc)])
    html = BeautifulSoup(index_html.read_text(), "html.parser")
    tags = html.find_all("a", "reference internal")
    assert result.exit_code == 0
    assert tags[1].attrs["href"] == "content1.html"
    assert tags[2].attrs["href"] == "content2.html"

    toc.write_text("- file: index\n- file: content2\n- file: content1\n")