3
3
import filecmp
4
4
import os
5
5
from pathlib import Path
6
+ import shutil
6
7
from subprocess import Popen , PIPE
7
8
import sys
8
9
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 (Path (__file__ ).parent / 'tinypages' , 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,45 @@ 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
+ contents = (source_dir / 'included_plot_21.rst' ).read_text ()
67
+ contents = contents .replace ('plt.plot(range(6))' , 'plt.plot(range(4))' )
68
+ (source_dir / 'included_plot_21.rst' ).write_text (contents )
69
+ # Build the pages again and check that the modified file was updated
70
+ modification_times = [plot_directive_file (i ).stat ().st_mtime
71
+ for i in (1 , 2 , 3 , 5 )]
72
+ build_sphinx_html (source_dir , doctree_dir , html_dir )
73
+ assert filecmp .cmp (range_4 , plot_file (17 ))
74
+ # Check that the plots in the plot_directive folder weren't changed.
75
+ # (plot_directive_file(1) won't be modified, but it will be copied to html/
76
+ # upon compilation, so plot_file(1) will be modified)
77
+ assert plot_directive_file (1 ).stat ().st_mtime == modification_times [0 ]
78
+ assert plot_directive_file (2 ).stat ().st_mtime == modification_times [1 ]
79
+ assert plot_directive_file (3 ).stat ().st_mtime == modification_times [2 ]
80
+ assert filecmp .cmp (range_10 , plot_file (1 ))
81
+ assert filecmp .cmp (range_6 , plot_file (2 ))
82
+ assert filecmp .cmp (range_4 , plot_file (3 ))
83
+ # Make sure that figures marked with context are re-created (but that the
84
+ # contents are the same)
85
+ assert plot_directive_file (5 ).stat ().st_mtime > modification_times [3 ]
86
+ assert filecmp .cmp (range_6 , plot_file (5 ))
87
+
88
+
89
+ def build_sphinx_html (source_dir , doctree_dir , html_dir ):
90
+ # Build the pages with warnings turned into errors
91
+ cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' ,
92
+ '-d' , str (doctree_dir ), str (source_dir ), str (html_dir )]
93
+ proc = Popen (cmd , stdout = PIPE , stderr = PIPE , universal_newlines = True ,
94
+ env = {** os .environ , "MPLBACKEND" : "" })
95
+ out , err = proc .communicate ()
96
+
97
+ assert proc .returncode == 0 , \
98
+ f"sphinx build failed with stdout:\n { out } \n stderr:\n { err } \n "
99
+ if err :
100
+ pytest .fail (f"sphinx build emitted the following warnings:\n { err } " )
101
+
102
+ assert html_dir .is_dir ()
0 commit comments