14
14
minversion = None if sys .version_info < (3 , 10 ) else '4.1.3' )
15
15
16
16
17
- def test_tinypages (tmpdir ):
18
- source_dir = Path (tmpdir ) / 'src'
19
- shutil . copytree ( Path ( __file__ ). parent / 'tinypages' , source_dir )
20
- html_dir = source_dir / '_build' / 'html'
21
- doctree_dir = source_dir / 'doctrees'
17
+ def test_tinypages (tmp_path ):
18
+ shutil . copytree ( Path (__file__ ). parent / 'tinypages' , tmp_path ,
19
+ dirs_exist_ok = True )
20
+ html_dir = tmp_path / '_build' / 'html'
21
+ doctree_dir = tmp_path / 'doctrees'
22
22
# Build the pages with warnings turned into errors
23
23
cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' ,
24
24
'-d' , str (doctree_dir ),
@@ -32,7 +32,7 @@ def test_tinypages(tmpdir):
32
32
out , err = proc .communicate ()
33
33
34
34
# Build the pages with warnings turned into errors
35
- build_sphinx_html (source_dir , doctree_dir , html_dir )
35
+ build_sphinx_html (tmp_path , doctree_dir , html_dir )
36
36
37
37
def plot_file (num ):
38
38
return html_dir / f'some_plots-{ num } .png'
@@ -75,13 +75,13 @@ def plot_directive_file(num):
75
75
assert filecmp .cmp (range_6 , plot_file (17 ))
76
76
77
77
# Modify the included plot
78
- contents = (source_dir / 'included_plot_21.rst' ).read_bytes ()
78
+ contents = (tmp_path / 'included_plot_21.rst' ).read_bytes ()
79
79
contents = contents .replace (b'plt.plot(range(6))' , b'plt.plot(range(4))' )
80
- (source_dir / 'included_plot_21.rst' ).write_bytes (contents )
80
+ (tmp_path / 'included_plot_21.rst' ).write_bytes (contents )
81
81
# Build the pages again and check that the modified file was updated
82
82
modification_times = [plot_directive_file (i ).stat ().st_mtime
83
83
for i in (1 , 2 , 3 , 5 )]
84
- build_sphinx_html (source_dir , doctree_dir , html_dir )
84
+ build_sphinx_html (tmp_path , doctree_dir , html_dir )
85
85
assert filecmp .cmp (range_4 , plot_file (17 ))
86
86
# Check that the plots in the plot_directive folder weren't changed.
87
87
# (plot_directive_file(1) won't be modified, but it will be copied to html/
@@ -98,35 +98,73 @@ def plot_directive_file(num):
98
98
assert filecmp .cmp (range_6 , plot_file (5 ))
99
99
100
100
101
- def test_plot_html_show_source_link (tmpdir ):
102
- source_dir = Path (tmpdir ) / 'src'
103
- source_dir .mkdir ()
101
+ def test_plot_html_show_source_link (tmp_path ):
104
102
parent = Path (__file__ ).parent
105
- shutil .copyfile (parent / 'tinypages/conf.py' , source_dir / 'conf.py' )
106
- shutil .copytree (parent / 'tinypages/_static' , source_dir / '_static' )
107
- doctree_dir = source_dir / 'doctrees'
108
- (source_dir / 'index.rst' ).write_text ("""
103
+ shutil .copyfile (parent / 'tinypages/conf.py' , tmp_path / 'conf.py' )
104
+ shutil .copytree (parent / 'tinypages/_static' , tmp_path / '_static' )
105
+ doctree_dir = tmp_path / 'doctrees'
106
+ (tmp_path / 'index.rst' ).write_text ("""
109
107
.. plot::
110
108
111
109
plt.plot(range(2))
112
110
""" )
113
111
# Make sure source scripts are created by default
114
- html_dir1 = source_dir / '_build' / 'html1'
115
- build_sphinx_html (source_dir , doctree_dir , html_dir1 )
112
+ html_dir1 = tmp_path / '_build' / 'html1'
113
+ build_sphinx_html (tmp_path , doctree_dir , html_dir1 )
116
114
assert "index-1.py" in [p .name for p in html_dir1 .iterdir ()]
117
115
# Make sure source scripts are NOT created when
118
116
# plot_html_show_source_link` is False
119
- html_dir2 = source_dir / '_build' / 'html2'
120
- build_sphinx_html (source_dir , doctree_dir , html_dir2 ,
117
+ html_dir2 = tmp_path / '_build' / 'html2'
118
+ build_sphinx_html (tmp_path , doctree_dir , html_dir2 ,
121
119
extra_args = ['-D' , 'plot_html_show_source_link=0' ])
122
120
assert "index-1.py" not in [p .name for p in html_dir2 .iterdir ()]
123
121
124
122
125
- def build_sphinx_html (source_dir , doctree_dir , html_dir , extra_args = None ):
123
+ @pytest .mark .parametrize ('plot_html_show_source_link' , [0 , 1 ])
124
+ def test_show_source_link_true (tmp_path , plot_html_show_source_link ):
125
+ # Test that a source link is generated if :show-source-link: is true,
126
+ # whether or not plot_html_show_source_link is true.
127
+ parent = Path (__file__ ).parent
128
+ shutil .copyfile (parent / 'tinypages/conf.py' , tmp_path / 'conf.py' )
129
+ shutil .copytree (parent / 'tinypages/_static' , tmp_path / '_static' )
130
+ doctree_dir = tmp_path / 'doctrees'
131
+ (tmp_path / 'index.rst' ).write_text ("""
132
+ .. plot::
133
+ :show-source-link: true
134
+
135
+ plt.plot(range(2))
136
+ """ )
137
+ html_dir = tmp_path / '_build' / 'html'
138
+ build_sphinx_html (tmp_path , doctree_dir , html_dir , extra_args = [
139
+ '-D' , f'plot_html_show_source_link={ plot_html_show_source_link } ' ])
140
+ assert "index-1.py" in [p .name for p in html_dir .iterdir ()]
141
+
142
+
143
+ @pytest .mark .parametrize ('plot_html_show_source_link' , [0 , 1 ])
144
+ def test_show_source_link_false (tmp_path , plot_html_show_source_link ):
145
+ # Test that a source link is NOT generated if :show-source-link: is false,
146
+ # whether or not plot_html_show_source_link is true.
147
+ parent = Path (__file__ ).parent
148
+ shutil .copyfile (parent / 'tinypages/conf.py' , tmp_path / 'conf.py' )
149
+ shutil .copytree (parent / 'tinypages/_static' , tmp_path / '_static' )
150
+ doctree_dir = tmp_path / 'doctrees'
151
+ (tmp_path / 'index.rst' ).write_text ("""
152
+ .. plot::
153
+ :show-source-link: false
154
+
155
+ plt.plot(range(2))
156
+ """ )
157
+ html_dir = tmp_path / '_build' / 'html'
158
+ build_sphinx_html (tmp_path , doctree_dir , html_dir , extra_args = [
159
+ '-D' , f'plot_html_show_source_link={ plot_html_show_source_link } ' ])
160
+ assert "index-1.py" not in [p .name for p in html_dir .iterdir ()]
161
+
162
+
163
+ def build_sphinx_html (tmp_path , doctree_dir , html_dir , extra_args = None ):
126
164
# Build the pages with warnings turned into errors
127
165
extra_args = [] if extra_args is None else extra_args
128
166
cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' ,
129
- '-d' , str (doctree_dir ), str (source_dir ), str (html_dir ), * extra_args ]
167
+ '-d' , str (doctree_dir ), str (tmp_path ), str (html_dir ), * extra_args ]
130
168
proc = Popen (cmd , stdout = PIPE , stderr = PIPE , universal_newlines = True ,
131
169
env = {** os .environ , "MPLBACKEND" : "" })
132
170
out , err = proc .communicate ()
0 commit comments