1"""Tests for tinypages build using sphinx extensions."""
2
3import filecmp
4import os
5from pathlib import Path
6from subprocess import Popen, PIPE
7import sys
8
9import pytest
10
11
12pytest.importorskip('sphinx')
13
14
15def test_tinypages(tmpdir):
16    tmp_path = Path(tmpdir)
17    html_dir = tmp_path / 'html'
18    doctree_dir = tmp_path / 'doctrees'
19    # Build the pages with warnings turned into errors
20    cmd = [sys.executable, '-msphinx', '-W', '-b', 'html',
21           '-d', str(doctree_dir),
22           str(Path(__file__).parent / 'tinypages'), str(html_dir)]
23    proc = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True,
24                 env={**os.environ, "MPLBACKEND": ""})
25    out, err = proc.communicate()
26
27    assert proc.returncode == 0, \
28        f"sphinx build failed with stdout:\n{out}\nstderr:\n{err}\n"
29    if err:
30        pytest.fail(f"sphinx build emitted the following warnings:\n{err}")
31
32    assert html_dir.is_dir()
33
34    def plot_file(num):
35        return html_dir / f'some_plots-{num}.png'
36
37    range_10, range_6, range_4 = [plot_file(i) for i in range(1, 4)]
38    # Plot 5 is range(6) plot
39    assert filecmp.cmp(range_6, plot_file(5))
40    # Plot 7 is range(4) plot
41    assert filecmp.cmp(range_4, plot_file(7))
42    # Plot 11 is range(10) plot
43    assert filecmp.cmp(range_10, plot_file(11))
44    # Plot 12 uses the old range(10) figure and the new range(6) figure
45    assert filecmp.cmp(range_10, plot_file('12_00'))
46    assert filecmp.cmp(range_6, plot_file('12_01'))
47    # Plot 13 shows close-figs in action
48    assert filecmp.cmp(range_4, plot_file(13))
49    # Plot 14 has included source
50    html_contents = (html_dir / 'some_plots.html').read_bytes()
51    assert b'# Only a comment' in html_contents
52    # check plot defined in external file.
53    assert filecmp.cmp(range_4, html_dir / 'range4.png')
54    assert filecmp.cmp(range_6, html_dir / 'range6.png')
55    # check if figure caption made it into html file
56    assert b'This is the caption for plot 15.' in html_contents
57    # check if figure caption using :caption: made it into html file
58    assert b'Plot 17 uses the caption option.' in html_contents
59    # check if figure caption made it into html file
60    assert b'This is the caption for plot 18.' in html_contents
61    # check if the custom classes made it into the html file
62    assert b'plot-directive my-class my-other-class' in html_contents
63    # check that the multi-image caption is applied twice
64    assert html_contents.count(b'This caption applies to both plots.') == 2
65