test_sphinxext.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Tests for tinypages build using sphinx extensions."""
  2. import filecmp
  3. from pathlib import Path
  4. from subprocess import Popen, PIPE
  5. import sys
  6. import pytest
  7. pytest.importorskip('sphinx')
  8. def test_tinypages(tmpdir):
  9. tmp_path = Path(tmpdir)
  10. html_dir = tmp_path / 'html'
  11. doctree_dir = tmp_path / 'doctrees'
  12. # Build the pages with warnings turned into errors
  13. cmd = [sys.executable, '-msphinx', '-W', '-b', 'html',
  14. '-d', str(doctree_dir),
  15. str(Path(__file__).parent / 'tinypages'), str(html_dir)]
  16. proc = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
  17. out, err = proc.communicate()
  18. assert proc.returncode == 0, \
  19. "sphinx build failed with stdout:\n{}\nstderr:\n{}\n".format(out, err)
  20. if err:
  21. pytest.fail("sphinx build emitted the following warnings:\n{}"
  22. .format(err))
  23. assert html_dir.is_dir()
  24. def plot_file(num):
  25. return html_dir / f'some_plots-{num}.png'
  26. range_10, range_6, range_4 = [plot_file(i) for i in range(1, 4)]
  27. # Plot 5 is range(6) plot
  28. assert filecmp.cmp(range_6, plot_file(5))
  29. # Plot 7 is range(4) plot
  30. assert filecmp.cmp(range_4, plot_file(7))
  31. # Plot 11 is range(10) plot
  32. assert filecmp.cmp(range_10, plot_file(11))
  33. # Plot 12 uses the old range(10) figure and the new range(6) figure
  34. assert filecmp.cmp(range_10, plot_file('12_00'))
  35. assert filecmp.cmp(range_6, plot_file('12_01'))
  36. # Plot 13 shows close-figs in action
  37. assert filecmp.cmp(range_4, plot_file(13))
  38. # Plot 14 has included source
  39. html_contents = (html_dir / 'some_plots.html').read_bytes()
  40. assert b'# Only a comment' in html_contents
  41. # check plot defined in external file.
  42. assert filecmp.cmp(range_4, html_dir / 'range4.png')
  43. assert filecmp.cmp(range_6, html_dir / 'range6.png')
  44. # check if figure caption made it into html file
  45. assert b'This is the caption for plot 15.' in html_contents