5
5
from pathlib import Path
6
6
from subprocess import Popen , PIPE
7
7
import sys
8
+ import shutil
8
9
9
10
import pytest
10
11
13
14
14
15
15
16
def 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 ()
17
+ source_dir = Path (tmpdir ) / 'src'
18
+ shutil .copytree (str (Path (__file__ ).parent / 'tinypages' ), str (source_dir ))
19
+ html_dir = source_dir / '_build' / 'html'
20
+ doctree_dir = source_dir / 'doctrees'
26
21
27
- assert proc .returncode == 0 , \
28
- f"sphinx build failed with stdout:\n { out } \n stderr:\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 ()
22
+ # Build the pages with warnings turned into errors
23
+ build_sphinx_html (source_dir , doctree_dir , html_dir )
33
24
34
25
def plot_file (num ):
35
26
return html_dir / f'some_plots-{ num } .png'
36
27
28
+ def plot_directive_file (num ):
29
+ # This is always next to the doctree dir.
30
+ return doctree_dir .parent / 'plot_directive' / f'some_plots-{ num } .png'
31
+
37
32
range_10 , range_6 , range_4 = [plot_file (i ) for i in range (1 , 4 )]
38
33
# Plot 5 is range(6) plot
39
34
assert filecmp .cmp (range_6 , plot_file (5 ))
@@ -48,6 +43,7 @@ def plot_file(num):
48
43
assert filecmp .cmp (range_4 , plot_file (13 ))
49
44
# Plot 14 has included source
50
45
html_contents = (html_dir / 'some_plots.html' ).read_bytes ()
46
+
51
47
assert b'# Only a comment' in html_contents
52
48
# check plot defined in external file.
53
49
assert filecmp .cmp (range_4 , html_dir / 'range4.png' )
@@ -62,3 +58,47 @@ def plot_file(num):
62
58
assert b'plot-directive my-class my-other-class' in html_contents
63
59
# check that the multi-image caption is applied twice
64
60
assert html_contents .count (b'This caption applies to both plots.' ) == 2
61
+ # Plot 21 is range(6) plot via an include directive. But because some of
62
+ # the previous plots are repeated, the argument to plot_file() is only 17.
63
+ assert filecmp .cmp (range_6 , plot_file (17 ))
64
+
65
+ # Modify the included plot
66
+ with open (str (source_dir / 'included_plot_21.rst' ), 'r' ) as file :
67
+ contents = file .read ()
68
+ contents = contents .replace ('plt.plot(range(6))' , 'plt.plot(range(4))' )
69
+ with open (str (source_dir / 'included_plot_21.rst' ), 'w' ) as file :
70
+ file .write (contents )
71
+ # Build the pages again and check that the modified file was updated
72
+ modification_times = [plot_directive_file (i ).stat ().st_mtime
73
+ for i in (1 , 2 , 3 , 5 )]
74
+ build_sphinx_html (source_dir , doctree_dir , html_dir )
75
+ assert filecmp .cmp (range_4 , plot_file (17 ))
76
+ # Check that the plots in the plot_directive folder weren't changed.
77
+ # (plot_directive_file(1) won't be modified, but it will be copied to html/
78
+ # upon compilation, so plot_file(1) will be modified)
79
+ assert plot_directive_file (1 ).stat ().st_mtime == modification_times [0 ]
80
+ assert plot_directive_file (2 ).stat ().st_mtime == modification_times [1 ]
81
+ assert plot_directive_file (3 ).stat ().st_mtime == modification_times [2 ]
82
+ assert filecmp .cmp (range_10 , plot_file (1 ))
83
+ assert filecmp .cmp (range_6 , plot_file (2 ))
84
+ assert filecmp .cmp (range_4 , plot_file (3 ))
85
+ # Make sure that figures marked with context are re-created (but that the
86
+ # contents are the same)
87
+ assert plot_directive_file (5 ).stat ().st_mtime > modification_times [3 ]
88
+ assert filecmp .cmp (range_6 , plot_file (5 ))
89
+
90
+
91
+ def build_sphinx_html (source_dir , doctree_dir , html_dir ):
92
+ # Build the pages with warnings turned into errors
93
+ cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' ,
94
+ '-d' , str (doctree_dir ), str (source_dir ), str (html_dir )]
95
+ proc = Popen (cmd , stdout = PIPE , stderr = PIPE , universal_newlines = True ,
96
+ env = {** os .environ , "MPLBACKEND" : "" })
97
+ out , err = proc .communicate ()
98
+
99
+ assert proc .returncode == 0 , \
100
+ f"sphinx build failed with stdout:\n { out } \n stderr:\n { err } \n "
101
+ if err :
102
+ pytest .fail (f"sphinx build emitted the following warnings:\n { err } " )
103
+
104
+ assert html_dir .is_dir ()
0 commit comments