File: test_cli.py

package info (click to toggle)
myst-nb 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,452 kB
  • sloc: python: 6,066; xml: 1,434; makefile: 33
file content (60 lines) | stat: -rw-r--r-- 1,560 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Test the quickstart CLI"""

import os
from pathlib import Path

import nbformat
from sphinx import version_info as sphinx_version_info

from myst_nb.cli import md_to_nb, quickstart


def test_quickstart(tmp_path: Path, make_app):
    """Test the quickstart CLI builds a valid sphinx project."""
    project_path = tmp_path / "project"
    quickstart([str(project_path)])
    assert {p.name for p in project_path.iterdir()} == {
        ".gitignore",
        "conf.py",
        "index.md",
        "notebook1.ipynb",
        "notebook2.md",
    }

    # For compatibility with multiple versions of sphinx, convert pathlib.Path to
    # sphinx.testing.path.path here.
    if sphinx_version_info >= (7, 2):
        app_srcdir = project_path
    else:
        from sphinx.testing.path import path

        app_srcdir = path(os.fspath(project_path))

    app = make_app(srcdir=app_srcdir, buildername="html")
    app.build()
    assert app._warning.getvalue().strip() == ""
    assert (project_path / "_build/html/index.html").exists()


def test_md_to_nb(tmp_path: Path):
    """Test the md_to_nb CLI."""
    path = tmp_path / "notebook.md"
    outpath = path.with_suffix(".ipynb")
    path.write_text(
        """\
---
kernelspec:
    name: python3
---
# Title
+++
next cell
""",
        "utf-8",
    )
    md_to_nb([str(path)])
    assert path.exists()
    with outpath.open("r") as handle:
        nb = nbformat.read(handle, as_version=4)
    assert nb.metadata == {"kernelspec": {"display_name": "python3", "name": "python3"}}
    assert len(nb.cells) == 2