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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""Test the sphinx-gallery custom scraper."""
import os
from vispy.testing import TestingCanvas, requires_application
import pytest
try:
from sphinx_gallery.gen_gallery import DEFAULT_GALLERY_CONF
except ImportError:
pytest.skip("Skipping sphinx-gallery tests", allow_module_level=True)
from ..gallery_scraper import VisPyGalleryScraper
pytest.importorskip("PyQt5", reason="Gallery scraper only supports PyQt5")
def _create_fake_block_vars(canvas):
block_vars = {
"example_globals": {
"canvas": canvas,
},
"src_file": "example.py",
"image_path_iterator": (f"{x}.png" for x in range(10)),
}
return block_vars
def _create_fake_gallery_conf(src_dir):
gallery_conf = {}
gallery_conf.update(DEFAULT_GALLERY_CONF)
gallery_conf.update({
"compress_images": "images",
"compress_images_args": [],
"src_dir": src_dir,
"gallery_dirs": src_dir,
})
return gallery_conf
@requires_application()
@pytest.mark.parametrize("include_gallery_comment", [False, True])
def test_single_frame(include_gallery_comment, tmpdir):
canvas = TestingCanvas()
block_vars = _create_fake_block_vars(canvas)
gallery_conf = _create_fake_gallery_conf(str(tmpdir))
script = "\n# vispy: gallery 30\n" if include_gallery_comment else ""
with tmpdir.as_cwd():
with open("example.py", "w") as example_file:
example_file.write(script)
scraper = VisPyGalleryScraper()
rst = scraper(None, block_vars, gallery_conf)
if include_gallery_comment:
assert "0.png" in rst
assert os.path.isfile("0.png")
else:
assert "0.png" not in rst
assert not os.path.isfile("0.png")
assert not os.path.isfile("1.png") # only one file created
@requires_application()
def test_single_animation(tmpdir):
canvas = TestingCanvas()
block_vars = _create_fake_block_vars(canvas)
gallery_conf = _create_fake_gallery_conf(str(tmpdir))
with tmpdir.as_cwd():
with open("example.py", "w") as example_file:
example_file.write("""# vispy: gallery 10:50:5
""")
scraper = VisPyGalleryScraper()
rst = scraper(None, block_vars, gallery_conf)
assert "0.gif" in rst
assert os.path.isfile("0.gif")
assert not os.path.isfile("0.png") # only gif file created
assert not os.path.isfile("1.png") # only one file created
@requires_application()
@pytest.mark.parametrize(
"exported_files",
[
("example.png",),
("example.gif",),
("example1.png", "example2.png"),
])
def test_single_export(exported_files, tmpdir):
canvas = TestingCanvas()
block_vars = _create_fake_block_vars(canvas)
gallery_conf = _create_fake_gallery_conf(str(tmpdir))
with tmpdir.as_cwd():
# create the files that the example should have created
for fn in exported_files:
open(fn, "w").close()
with open("example.py", "w") as example_file:
example_file.write("""# vispy: gallery-exports {}
""".format(" ".join(exported_files)))
scraper = VisPyGalleryScraper()
rst = scraper(None, block_vars, gallery_conf)
for idx, fn in enumerate(exported_files):
# the original file should have been moved
assert not os.path.isfile(fn)
# the new name should from the sphinx-gallery iterator
new_name = str(idx) + os.path.splitext(fn)[1]
assert os.path.isfile(new_name)
assert new_name in rst
|