55import shutil
66from subprocess import call , Popen , PIPE
77import sys
8- import tempfile
98
109import pytest
1110
1211from matplotlib import cbook
1312
1413
15- HERE = dirname (__file__ )
16- TINY_PAGES = pjoin (HERE , 'tinypages' )
17-
18-
19- def setup_module ():
20- """Check we have a recent enough version of sphinx installed.
21- """
22- ret = call ([sys .executable , '-msphinx' , '--help' ],
23- stdout = PIPE , stderr = PIPE )
24- if ret != 0 :
25- pytest .skip ("'{} -msphinx' does not return 0" .format (sys .executable ))
14+ needs_sphinx = pytest .mark .skipif (
15+ call ([sys .executable , '-msphinx' , '--help' ], stdout = PIPE , stderr = PIPE ),
16+ reason = "'{} -msphinx' does not return 0" .format (sys .executable ))
2617
2718
2819@cbook .deprecated ("2.1" , alternative = "filecmp.cmp" )
@@ -34,58 +25,41 @@ def file_same(file1, file2):
3425 return contents1 == contents2
3526
3627
37- class TestTinyPages (object ):
38- """Test build and output of tinypages project"""
39-
40- @classmethod
41- def setup_class (cls ):
42- cls .page_build = tempfile .mkdtemp ()
43- try :
44- cls .html_dir = pjoin (cls .page_build , 'html' )
45- cls .doctree_dir = pjoin (cls .page_build , 'doctrees' )
46- # Build the pages with warnings turned into errors
47- cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' ,
48- '-d' , cls .doctree_dir ,
49- TINY_PAGES ,
50- cls .html_dir ]
51- proc = Popen (cmd , stdout = PIPE , stderr = PIPE )
52- out , err = proc .communicate ()
53- if proc .returncode != 0 :
54- raise RuntimeError (
55- "'{} -msphinx' failed with stdout:\n {}\n stderr:\n {}\n "
56- .format (sys .executable , out , err ))
57- except Exception as e :
58- shutil .rmtree (cls .page_build )
59- raise e
60-
61- @classmethod
62- def teardown_class (cls ):
63- shutil .rmtree (cls .page_build )
64-
65- def test_some_plots (self ):
66- assert isdir (self .html_dir )
67-
68- def plot_file (num ):
69- return pjoin (self .html_dir , 'some_plots-{0}.png' .format (num ))
70-
71- range_10 , range_6 , range_4 = [plot_file (i ) for i in range (1 , 4 )]
72- # Plot 5 is range(6) plot
73- assert filecmp .cmp (range_6 , plot_file (5 ))
74- # Plot 7 is range(4) plot
75- assert filecmp .cmp (range_4 , plot_file (7 ))
76- # Plot 11 is range(10) plot
77- assert filecmp .cmp (range_10 , plot_file (11 ))
78- # Plot 12 uses the old range(10) figure and the new range(6) figure
79- assert filecmp .cmp (range_10 , plot_file ('12_00' ))
80- assert filecmp .cmp (range_6 , plot_file ('12_01' ))
81- # Plot 13 shows close-figs in action
82- assert filecmp .cmp (range_4 , plot_file (13 ))
83- # Plot 14 has included source
84- with open (pjoin (self .html_dir , 'some_plots.html' ), 'rb' ) as fobj :
85- html_contents = fobj .read ()
86- assert b'# Only a comment' in html_contents
87- # check plot defined in external file.
88- assert filecmp .cmp (range_4 , pjoin (self .html_dir , 'range4.png' ))
89- assert filecmp .cmp (range_6 , pjoin (self .html_dir , 'range6.png' ))
90- # check if figure caption made it into html file
91- assert b'This is the caption for plot 15.' in html_contents
28+ def test_tinypages (tmpdir ):
29+ html_dir = pjoin (str (tmpdir ), 'html' )
30+ doctree_dir = pjoin (str (tmpdir ), 'doctrees' )
31+ # Build the pages with warnings turned into errors
32+ cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' , '-d' , doctree_dir ,
33+ pjoin (dirname (__file__ ), 'tinypages' ), html_dir ]
34+ proc = Popen (cmd , stdout = PIPE , stderr = PIPE )
35+ out , err = proc .communicate ()
36+ assert proc .returncode == 0 , \
37+ "'{} -msphinx' failed with stdout:\n {}\n stderr:\n {}\n " .format (
38+ sys .executable , out , err )
39+
40+ assert isdir (html_dir )
41+
42+ def plot_file (num ):
43+ return pjoin (html_dir , 'some_plots-{0}.png' .format (num ))
44+
45+ range_10 , range_6 , range_4 = [plot_file (i ) for i in range (1 , 4 )]
46+ # Plot 5 is range(6) plot
47+ assert filecmp .cmp (range_6 , plot_file (5 ))
48+ # Plot 7 is range(4) plot
49+ assert filecmp .cmp (range_4 , plot_file (7 ))
50+ # Plot 11 is range(10) plot
51+ assert filecmp .cmp (range_10 , plot_file (11 ))
52+ # Plot 12 uses the old range(10) figure and the new range(6) figure
53+ assert filecmp .cmp (range_10 , plot_file ('12_00' ))
54+ assert filecmp .cmp (range_6 , plot_file ('12_01' ))
55+ # Plot 13 shows close-figs in action
56+ assert filecmp .cmp (range_4 , plot_file (13 ))
57+ # Plot 14 has included source
58+ with open (pjoin (html_dir , 'some_plots.html' ), 'rb' ) as fobj :
59+ html_contents = fobj .read ()
60+ assert b'# Only a comment' in html_contents
61+ # check plot defined in external file.
62+ assert filecmp .cmp (range_4 , pjoin (html_dir , 'range4.png' ))
63+ assert filecmp .cmp (range_6 , pjoin (html_dir , 'range6.png' ))
64+ # check if figure caption made it into html file
65+ assert b'This is the caption for plot 15.' in html_contents
0 commit comments