Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ _unittests/ut__main/*.html
_unittests/ut_runpython/*.png
_unittests/ut_runpython/*.html
test_latex/*
test_latex2/*
test_api/*
test_sphinx_api_func/*
*.pdf
_unittests/ut_runpython/exescript.py
132 changes: 130 additions & 2 deletions _unittests/ut__main/test_cmd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import platform
import shutil
import sys
import tempfile
import unittest
import os
from sphinx_runpython.ext_test_case import ExtTestCase, hide_stdout
from sphinx_runpython._cmd_helper import get_parser, nb2py, latex_process
from argparse import Namespace
from sphinx_runpython.ext_test_case import ExtTestCase, hide_stdout, skipif_ci_windows
from sphinx_runpython._cmd_helper import (
get_parser,
nb2py,
latex_process,
process_args,
sphinx_api,
)


class TestCmd(ExtTestCase):
Expand Down Expand Up @@ -30,6 +40,124 @@ def test_latex(self):
expected = os.path.join(folder, "poulet.py")
self.assertExists(expected)

def test_latex_inplace(self):
data = os.path.join(os.path.dirname(__file__), "data")
with tempfile.TemporaryDirectory() as tmpdir:
shutil.copy(
os.path.join(data, "strategie_avec_alea.rst"),
os.path.join(tmpdir, "strategie_avec_alea.rst"),
)
latex_process(tmpdir, verbose=1)
self.assertExists(os.path.join(tmpdir, "strategie_avec_alea.rst"))

def test_nb2py_not_found(self):
self.assertRaise(lambda: nb2py("/nonexistent/path/xyz"), FileNotFoundError)

def test_latex_process_not_found(self):
self.assertRaise(
lambda: latex_process("/nonexistent/path/xyz"), FileNotFoundError
)

def test_process_args_nb2py_empty(self):
with tempfile.TemporaryDirectory() as tmpdir:
args = Namespace(
command="nb2py",
path=tmpdir,
recursive=False,
verbose=0,
)
process_args(args)

@skipif_ci_windows("readme processing does not work on Windows")
def test_process_args_readme(self):
readme = os.path.join(os.path.dirname(__file__), "..", "..", "README.rst")
args = Namespace(
command="readme",
path=readme,
verbose=0,
)
process_args(args)

def test_process_args_unknown_command(self):
args = Namespace(command="unknown_cmd", path=None, verbose=0)
self.assertRaise(lambda: process_args(args), ValueError)

@hide_stdout()
def test_process_args_latex(self):
data = os.path.join(os.path.dirname(__file__), "data")
folder = "test_latex2"
if not os.path.exists(folder):
os.mkdir(folder)
args = Namespace(
command="latex",
path=data,
recursive=False,
verbose=0,
output=folder,
)
process_args(args)

def test_process_args_api(self):
data = os.path.join(os.path.dirname(__file__), "..", "..", "sphinx_runpython")
folder = "test_api"
if not os.path.exists(folder):
os.mkdir(folder)
args = Namespace(
command="api",
path=data,
recursive=False,
verbose=0,
output=folder,
hidden=False,
)
process_args(args)

def test_process_args_img2pdf(self):
from PIL import Image

with tempfile.TemporaryDirectory() as tmpdir:
img_path = os.path.join(tmpdir, "test.png")
out_path = os.path.join(tmpdir, "out.pdf")
Image.new("RGB", (100, 100), "white").save(img_path)
args = Namespace(
command="img2pdf",
path=img_path,
output=out_path,
verbose=0,
zoom=1.0,
rotate=0.0,
)
process_args(args)
self.assertExists(out_path)

def test_sphinx_api_function(self):
data = os.path.join(os.path.dirname(__file__), "..", "..", "sphinx_runpython")
folder = "test_sphinx_api_func"
if not os.path.exists(folder):
os.mkdir(folder)
sphinx_api(data, folder, verbose=0)

def test_main_latex(self):
with tempfile.TemporaryDirectory() as tmpdir:
old_argv = sys.argv
try:
sys.argv = ["sphinx-runpython", "latex", "--path", tmpdir]
from sphinx_runpython._cmd_helper import main

main()
finally:
sys.argv = old_argv

def test_main_help(self):
old_argv = sys.argv
try:
sys.argv = ["sphinx-runpython", "--help"]
from sphinx_runpython._cmd_helper import main

self.assertRaise(lambda: main(), SystemExit)
finally:
sys.argv = old_argv


if __name__ == "__main__":
unittest.main(verbosity=2)
101 changes: 101 additions & 0 deletions _unittests/ut__main/test_ext_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import unittest
from docutils import nodes
from sphinx_runpython.ext_test_case import ExtTestCase
from sphinx_runpython.ext_helper import (
NodeEnter,
NodeLeave,
TinyNode,
WrappedNode,
traverse,
sphinx_lang,
)


class TestExtHelper(ExtTestCase):
def test_tiny_node(self):
parent = object()
node = TinyNode(parent)
self.assertIs(node.parent, parent)

def test_node_enter(self):
parent = object()
node = NodeEnter(parent)
self.assertIsInstance(node, TinyNode)
self.assertIs(node.parent, parent)

def test_node_leave(self):
parent = object()
node = NodeLeave(parent)
self.assertIsInstance(node, TinyNode)
self.assertIs(node.parent, parent)

def test_wrapped_node(self):
doc_node = nodes.section()
wrapped = WrappedNode(doc_node)
self.assertIs(wrapped.node, doc_node)

def test_traverse_simple(self):
root = nodes.section()
para = nodes.paragraph(text="hello")
root += para

results = list(traverse(root))
self.assertGreater(len(results), 0)
depths = [d for d, n in results]
node_types = [type(n) for d, n in results]
self.assertIn(0, depths)
self.assertIn(NodeEnter, node_types)
self.assertIn(NodeLeave, node_types)
self.assertIn(nodes.section, node_types)
self.assertIn(nodes.paragraph, node_types)

def test_traverse_with_wrapped_node(self):
root = nodes.paragraph(text="test")
wrapped = WrappedNode(root)
results = list(traverse(wrapped))
self.assertGreater(len(results), 0)
node_types = [type(n) for d, n in results]
self.assertIn(NodeEnter, node_types)
self.assertIn(NodeLeave, node_types)

def test_traverse_depth(self):
root = nodes.section()
child = nodes.paragraph(text="child")
grandchild = nodes.Text("text")
child += grandchild
root += child

results = list(traverse(root))
max_depth = max(d for d, n in results)
self.assertGreaterEqual(max_depth, 2)

def test_sphinx_lang_no_settings(self):
class FakeEnv:
pass

lang = sphinx_lang(FakeEnv())
self.assertEqual(lang, "en")

def test_sphinx_lang_with_settings_no_code(self):
class FakeSettings:
pass

class FakeEnv:
settings = FakeSettings()

lang = sphinx_lang(FakeEnv())
self.assertEqual(lang, "en")

def test_sphinx_lang_with_language_code(self):
class FakeSettings:
language_code = "fr"

class FakeEnv:
settings = FakeSettings()

lang = sphinx_lang(FakeEnv())
self.assertEqual(lang, "fr")


if __name__ == "__main__":
unittest.main(verbosity=2)
87 changes: 87 additions & 0 deletions _unittests/ut__main/test_ext_io_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import unittest
import tempfile
from sphinx_runpython.ext_test_case import ExtTestCase
from sphinx_runpython.ext_io_helper import (
_get_file_url,
ReadUrlException,
InternetException,
FileException,
MONTH_DATE,
get_url_content_timeout,
)


class TestExtIoHelper(ExtTestCase):
def test_month_date_keys(self):
self.assertEqual(MONTH_DATE["jan"], 1)
self.assertEqual(MONTH_DATE["dec"], 12)
self.assertEqual(len(MONTH_DATE), 12)

def test_get_file_url_basic(self):
result = _get_file_url("http://example.com/file.html", "/tmp/cache")
self.assertIn("/tmp/cache", result)
self.assertIn("example", result)

def test_get_file_url_png(self):
result = _get_file_url("http://example.com/image.png", "/tmp/cache")
self.assertTrue(result.endswith(".png"))

def test_get_file_url_no_extension(self):
result = _get_file_url("http://example.com/noext", "/tmp/cache")
self.assertIn("/tmp/cache", result)

def test_get_file_url_query_params(self):
result = _get_file_url("http://example.com/file?key=value.pdf", "/tmp/cache")
self.assertIn("/tmp/cache", result)

def test_get_file_url_py(self):
result = _get_file_url("http://example.com/script.py", "/tmp/cache")
self.assertTrue(result.endswith(".py"))

def test_read_url_exception_custom(self):
exc = ReadUrlException("test error")
self.assertIsInstance(exc, Exception)

def test_internet_exception_custom(self):
exc = InternetException("test error")
self.assertIsInstance(exc, Exception)

def test_file_exception_custom(self):
exc = FileException("test error")
self.assertIsInstance(exc, Exception)

def test_get_url_content_timeout_invalid_url(self):
url = "https://localhost:87777/nonexistent"
result = get_url_content_timeout(
url, timeout=2, raise_exception=False, encoding="utf-8"
)
self.assertIsNone(result)

def test_get_url_content_timeout_raises(self):
url = "https://localhost:87777/nonexistent"
self.assertRaise(
lambda: get_url_content_timeout(url, timeout=2, raise_exception=True),
InternetException,
)

def test_get_url_content_timeout_save_to_file(self):
url = "https://localhost:87777/nonexistent"
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f:
outfile = f.name
try:
result = get_url_content_timeout(
url,
timeout=2,
output=outfile,
raise_exception=False,
encoding="utf-8",
)
self.assertIsNone(result)
finally:
if os.path.exists(outfile):
os.remove(outfile)


if __name__ == "__main__":
unittest.main(verbosity=2)
Loading
Loading