|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | from io import StringIO |
| 6 | +from unittest.mock import Mock |
6 | 7 |
|
7 | | -from sphinx.testing.util import SphinxTestApp, path |
| 8 | +from sphinx.addnodes import download_reference |
| 9 | +from sphinx.testing.util import assert_node, SphinxTestApp, path |
8 | 10 | from sphinx.errors import ExtensionError |
9 | | -from docutils.nodes import raw |
| 11 | +from docutils.nodes import literal, raw |
10 | 12 | from nbformat import from_dict |
11 | 13 |
|
12 | 14 | import pytest |
|
18 | 20 | JupyterWidgetViewNode, |
19 | 21 | JupyterWidgetStateNode, |
20 | 22 | cell_output_to_nodes, |
| 23 | + JupyterDownloadRole, |
21 | 24 | ) |
22 | 25 | from jupyter_sphinx.thebelab import ThebeSourceNode, ThebeOutputNode, ThebeButtonNode |
23 | 26 |
|
@@ -564,3 +567,25 @@ def test_image_mimetype_uri(doctree): |
564 | 567 | cell = from_dict(cell) |
565 | 568 | output_node = cell_output_to_nodes(cell["outputs"], priority, True, output_dir, None) |
566 | 569 | assert output_node[0].attributes['uri'] == img_locs[index] |
| 570 | + |
| 571 | + |
| 572 | +@pytest.mark.parametrize('text,reftarget,caption', ( |
| 573 | + ('nb_name', '/../jupyter_execute/path/to/nb_name.ipynb', 'nb_name.ipynb'), |
| 574 | + ('../nb_name', '/../jupyter_execute/path/nb_name.ipynb', '../nb_name.ipynb'), |
| 575 | + ('text <nb_name>', '/../jupyter_execute/path/to/nb_name.ipynb', 'text'), |
| 576 | +)) |
| 577 | +def test_download_role(text, reftarget, caption, tmp_path): |
| 578 | + role = JupyterDownloadRole() |
| 579 | + mock_inliner = Mock() |
| 580 | + config = { |
| 581 | + 'document.settings.env.app.outdir': str(tmp_path), |
| 582 | + 'document.settings.env.docname': 'path/to/docname', |
| 583 | + 'document.settings.env.srcdir': str(tmp_path), |
| 584 | + 'document.settings.env.app.srcdir': str(tmp_path), |
| 585 | + 'reporter.get_source_and_line': lambda l: ('source', l) |
| 586 | + } |
| 587 | + mock_inliner.configure_mock(**config) |
| 588 | + ret, msg = role('jupyter-download:notebook', text, text, 0, mock_inliner) |
| 589 | + assert_node(ret[0], [download_reference], reftarget=reftarget) |
| 590 | + assert_node(ret[0][0], [literal, caption]) |
| 591 | + assert msg == [] |
0 commit comments