|
1 | 1 | import platform |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | +import tempfile |
2 | 5 | import unittest |
3 | 6 | import os |
4 | | -from sphinx_runpython.ext_test_case import ExtTestCase, hide_stdout |
5 | | -from sphinx_runpython._cmd_helper import get_parser, nb2py, latex_process |
| 7 | +from argparse import Namespace |
| 8 | +from sphinx_runpython.ext_test_case import ExtTestCase, hide_stdout, skipif_ci_windows |
| 9 | +from sphinx_runpython._cmd_helper import ( |
| 10 | + get_parser, |
| 11 | + nb2py, |
| 12 | + latex_process, |
| 13 | + process_args, |
| 14 | + sphinx_api, |
| 15 | +) |
6 | 16 |
|
7 | 17 |
|
8 | 18 | class TestCmd(ExtTestCase): |
@@ -30,6 +40,124 @@ def test_latex(self): |
30 | 40 | expected = os.path.join(folder, "poulet.py") |
31 | 41 | self.assertExists(expected) |
32 | 42 |
|
| 43 | + def test_latex_inplace(self): |
| 44 | + data = os.path.join(os.path.dirname(__file__), "data") |
| 45 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 46 | + shutil.copy( |
| 47 | + os.path.join(data, "strategie_avec_alea.rst"), |
| 48 | + os.path.join(tmpdir, "strategie_avec_alea.rst"), |
| 49 | + ) |
| 50 | + latex_process(tmpdir, verbose=1) |
| 51 | + self.assertExists(os.path.join(tmpdir, "strategie_avec_alea.rst")) |
| 52 | + |
| 53 | + def test_nb2py_not_found(self): |
| 54 | + self.assertRaise(lambda: nb2py("/nonexistent/path/xyz"), FileNotFoundError) |
| 55 | + |
| 56 | + def test_latex_process_not_found(self): |
| 57 | + self.assertRaise( |
| 58 | + lambda: latex_process("/nonexistent/path/xyz"), FileNotFoundError |
| 59 | + ) |
| 60 | + |
| 61 | + def test_process_args_nb2py_empty(self): |
| 62 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 63 | + args = Namespace( |
| 64 | + command="nb2py", |
| 65 | + path=tmpdir, |
| 66 | + recursive=False, |
| 67 | + verbose=0, |
| 68 | + ) |
| 69 | + process_args(args) |
| 70 | + |
| 71 | + @skipif_ci_windows("readme processing does not work on Windows") |
| 72 | + def test_process_args_readme(self): |
| 73 | + readme = os.path.join(os.path.dirname(__file__), "..", "..", "README.rst") |
| 74 | + args = Namespace( |
| 75 | + command="readme", |
| 76 | + path=readme, |
| 77 | + verbose=0, |
| 78 | + ) |
| 79 | + process_args(args) |
| 80 | + |
| 81 | + def test_process_args_unknown_command(self): |
| 82 | + args = Namespace(command="unknown_cmd", path=None, verbose=0) |
| 83 | + self.assertRaise(lambda: process_args(args), ValueError) |
| 84 | + |
| 85 | + @hide_stdout() |
| 86 | + def test_process_args_latex(self): |
| 87 | + data = os.path.join(os.path.dirname(__file__), "data") |
| 88 | + folder = "test_latex2" |
| 89 | + if not os.path.exists(folder): |
| 90 | + os.mkdir(folder) |
| 91 | + args = Namespace( |
| 92 | + command="latex", |
| 93 | + path=data, |
| 94 | + recursive=False, |
| 95 | + verbose=0, |
| 96 | + output=folder, |
| 97 | + ) |
| 98 | + process_args(args) |
| 99 | + |
| 100 | + def test_process_args_api(self): |
| 101 | + data = os.path.join(os.path.dirname(__file__), "..", "..", "sphinx_runpython") |
| 102 | + folder = "test_api" |
| 103 | + if not os.path.exists(folder): |
| 104 | + os.mkdir(folder) |
| 105 | + args = Namespace( |
| 106 | + command="api", |
| 107 | + path=data, |
| 108 | + recursive=False, |
| 109 | + verbose=0, |
| 110 | + output=folder, |
| 111 | + hidden=False, |
| 112 | + ) |
| 113 | + process_args(args) |
| 114 | + |
| 115 | + def test_process_args_img2pdf(self): |
| 116 | + from PIL import Image |
| 117 | + |
| 118 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 119 | + img_path = os.path.join(tmpdir, "test.png") |
| 120 | + out_path = os.path.join(tmpdir, "out.pdf") |
| 121 | + Image.new("RGB", (100, 100), "white").save(img_path) |
| 122 | + args = Namespace( |
| 123 | + command="img2pdf", |
| 124 | + path=img_path, |
| 125 | + output=out_path, |
| 126 | + verbose=0, |
| 127 | + zoom=1.0, |
| 128 | + rotate=0.0, |
| 129 | + ) |
| 130 | + process_args(args) |
| 131 | + self.assertExists(out_path) |
| 132 | + |
| 133 | + def test_sphinx_api_function(self): |
| 134 | + data = os.path.join(os.path.dirname(__file__), "..", "..", "sphinx_runpython") |
| 135 | + folder = "test_sphinx_api_func" |
| 136 | + if not os.path.exists(folder): |
| 137 | + os.mkdir(folder) |
| 138 | + sphinx_api(data, folder, verbose=0) |
| 139 | + |
| 140 | + def test_main_latex(self): |
| 141 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 142 | + old_argv = sys.argv |
| 143 | + try: |
| 144 | + sys.argv = ["sphinx-runpython", "latex", "--path", tmpdir] |
| 145 | + from sphinx_runpython._cmd_helper import main |
| 146 | + |
| 147 | + main() |
| 148 | + finally: |
| 149 | + sys.argv = old_argv |
| 150 | + |
| 151 | + def test_main_help(self): |
| 152 | + old_argv = sys.argv |
| 153 | + try: |
| 154 | + sys.argv = ["sphinx-runpython", "--help"] |
| 155 | + from sphinx_runpython._cmd_helper import main |
| 156 | + |
| 157 | + self.assertRaise(lambda: main(), SystemExit) |
| 158 | + finally: |
| 159 | + sys.argv = old_argv |
| 160 | + |
33 | 161 |
|
34 | 162 | if __name__ == "__main__": |
35 | 163 | unittest.main(verbosity=2) |
0 commit comments