15
15
16
16
17
17
def test_tinypages (tmp_path ):
18
- source_dir = tmp_path / 'src'
19
- shutil . copytree ( Path ( __file__ ). parent / 'tinypages' , source_dir )
20
- html_dir = source_dir / '_build' / 'html'
21
- doctree_dir = source_dir / 'doctrees'
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(tmp_path):
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/
@@ -99,25 +99,23 @@ def plot_directive_file(num):
99
99
100
100
101
101
def test_plot_html_show_source_link (tmp_path ):
102
- source_dir = tmp_path / 'src'
103
- source_dir .mkdir ()
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
@@ -126,20 +124,18 @@ def test_plot_html_show_source_link(tmp_path):
126
124
def test_show_source_link_true (tmp_path , plot_html_show_source_link ):
127
125
# Test that a source link is generated if :show-source-link: is true,
128
126
# whether or not plot_html_show_source_link is true.
129
- source_dir = tmp_path / 'src'
130
- source_dir .mkdir ()
131
127
parent = Path (__file__ ).parent
132
- shutil .copyfile (parent / 'tinypages/conf.py' , source_dir / 'conf.py' )
133
- shutil .copytree (parent / 'tinypages/_static' , source_dir / '_static' )
134
- doctree_dir = source_dir / 'doctrees'
135
- (source_dir / 'index.rst' ).write_text ("""
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 ("""
136
132
.. plot::
137
133
:show-source-link: true
138
134
139
135
plt.plot(range(2))
140
136
""" )
141
- html_dir = source_dir / '_build' / 'html'
142
- build_sphinx_html (source_dir , doctree_dir , html_dir , extra_args = [
137
+ html_dir = tmp_path / '_build' / 'html'
138
+ build_sphinx_html (tmp_path , doctree_dir , html_dir , extra_args = [
143
139
'-D' , f'plot_html_show_source_link={ plot_html_show_source_link } ' ])
144
140
assert "index-1.py" in [p .name for p in html_dir .iterdir ()]
145
141
@@ -148,29 +144,27 @@ def test_show_source_link_true(tmp_path, plot_html_show_source_link):
148
144
def test_show_source_link_false (tmp_path , plot_html_show_source_link ):
149
145
# Test that a source link is NOT generated if :show-source-link: is false,
150
146
# whether or not plot_html_show_source_link is true.
151
- source_dir = tmp_path / 'src'
152
- source_dir .mkdir ()
153
147
parent = Path (__file__ ).parent
154
- shutil .copyfile (parent / 'tinypages/conf.py' , source_dir / 'conf.py' )
155
- shutil .copytree (parent / 'tinypages/_static' , source_dir / '_static' )
156
- doctree_dir = source_dir / 'doctrees'
157
- (source_dir / 'index.rst' ).write_text ("""
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 ("""
158
152
.. plot::
159
153
:show-source-link: false
160
154
161
155
plt.plot(range(2))
162
156
""" )
163
- html_dir = source_dir / '_build' / 'html'
164
- build_sphinx_html (source_dir , doctree_dir , html_dir , extra_args = [
157
+ html_dir = tmp_path / '_build' / 'html'
158
+ build_sphinx_html (tmp_path , doctree_dir , html_dir , extra_args = [
165
159
'-D' , f'plot_html_show_source_link={ plot_html_show_source_link } ' ])
166
160
assert "index-1.py" not in [p .name for p in html_dir .iterdir ()]
167
161
168
162
169
- def build_sphinx_html (source_dir , doctree_dir , html_dir , extra_args = None ):
163
+ def build_sphinx_html (tmp_path , doctree_dir , html_dir , extra_args = None ):
170
164
# Build the pages with warnings turned into errors
171
165
extra_args = [] if extra_args is None else extra_args
172
166
cmd = [sys .executable , '-msphinx' , '-W' , '-b' , 'html' ,
173
- '-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 ]
174
168
proc = Popen (cmd , stdout = PIPE , stderr = PIPE , universal_newlines = True ,
175
169
env = {** os .environ , "MPLBACKEND" : "" })
176
170
out , err = proc .communicate ()
0 commit comments