test_sphinxext.py 2.0 KB

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