Skip to content

Commit a38e478

Browse files
authored
Merge pull request #59 from astrofrog/mirror
Allow baseline_dir to be comma-separated URL list to allow mirrors to be specified
2 parents 7f5f246 + 052f0d7 commit a38e478

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ install:
3636

3737
script:
3838
- python -c 'import pytest_mpl.plugin'
39-
- py.test --mpl --cov pytest_mpl tests
39+
- pytest -vv --mpl --cov pytest_mpl tests
4040
- python setup.py check --restructuredtext
4141

4242
after_success:

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
0.9 (unreleased)
22
----------------
33

4-
- No changes yet.
4+
- Allow baseline_dir to be comma-separated URL list to allow mirrors to
5+
be specified. [#59]
56

67
0.8 (2017-07-19)
78
----------------

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ filename of the plot (which defaults to the name of the test with a
140140
The baseline directory in the decorator above will be interpreted as
141141
being relative to the test file. Note that the baseline directory can
142142
also be a URL (which should start with ``http://`` or ``https://`` and
143-
end in a slash).
143+
end in a slash). If you want to specify mirrors, set ``baseline_dir`` to
144+
a comma-separated list of URLs (real commas in the URL should be encoded
145+
as ``%2C``).
144146

145147
Finally, you can also set a custom baseline directory globally when
146148
running tests by running ``py.test`` with:

pytest_mpl/plugin.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,30 @@
4242

4343
if sys.version_info[0] == 2:
4444
from urllib import urlopen
45+
string_types = basestring
4546
else:
4647
from urllib.request import urlopen
47-
48-
49-
def _download_file(url):
50-
u = urlopen(url)
48+
string_types = str
49+
50+
51+
def _download_file(baseline, filename):
52+
# Note that baseline can be a comma-separated list of URLs that we can
53+
# then treat as mirrors
54+
for base_url in baseline.split(','):
55+
try:
56+
u = urlopen(base_url + filename)
57+
content = u.read()
58+
except Exception as exc:
59+
warnings.warn('Downloading {0} failed'.format(base_url + filename))
60+
else:
61+
break
62+
else:
63+
raise Exception("Could not download baseline image from any of the "
64+
"available URLs")
5165
result_dir = tempfile.mkdtemp()
5266
filename = os.path.join(result_dir, 'downloaded')
5367
with open(filename, 'wb') as tmpfile:
54-
tmpfile.write(u.read())
68+
tmpfile.write(content)
5569
return filename
5670

5771

@@ -60,9 +74,13 @@ def pytest_addoption(parser):
6074
group.addoption('--mpl', action='store_true',
6175
help="Enable comparison of matplotlib figures to reference files")
6276
group.addoption('--mpl-generate-path',
63-
help="directory to generate reference images in, relative to location where py.test is run", action='store')
77+
help="directory to generate reference images in, relative "
78+
"to location where py.test is run", action='store')
6479
group.addoption('--mpl-baseline-path',
65-
help="directory containing baseline images, relative to location where py.test is run", action='store')
80+
help="directory containing baseline images, relative to "
81+
"location where py.test is run. This can also be a URL or a "
82+
"set of comma-separated URLs (in case mirrors are "
83+
"specified)", action='store')
6684

6785
results_path_help = "directory for test results, relative to location where py.test is run"
6886
group.addoption('--mpl-results-path', help=results_path_help, action='store')
@@ -157,12 +175,12 @@ def item_function_wrapper(*args, **kwargs):
157175
baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), 'baseline')
158176
else:
159177
baseline_dir = self.baseline_dir
178+
baseline_remote = False
160179
else:
161-
if not baseline_dir.startswith(('http://', 'https://')):
180+
baseline_remote = baseline_dir.startswith(('http://', 'https://'))
181+
if not baseline_remote:
162182
baseline_dir = os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir)
163183

164-
baseline_remote = baseline_dir.startswith('http')
165-
166184
with plt.style.context(style), switch_backend(backend):
167185

168186
# Run test and get figure object
@@ -201,7 +219,7 @@ def item_function_wrapper(*args, **kwargs):
201219

202220
# Find path to baseline image
203221
if baseline_remote:
204-
baseline_image_ref = _download_file(baseline_dir + filename)
222+
baseline_image_ref = _download_file(baseline_dir, filename)
205223
else:
206224
baseline_image_ref = os.path.abspath(os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir, filename))
207225

tests/test_pytest_mpl.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def test_succeeds_remote():
3939
return fig
4040

4141

42+
# The following tries an invalid URL first (or at least a URL where the baseline
43+
# image won't exist), but should succeed with the second mirror.
44+
@pytest.mark.mpl_image_compare(baseline_dir='http://www.python.org,' + baseline_dir_remote,
45+
filename='test_succeeds_remote.png')
46+
def test_succeeds_faulty_mirror():
47+
fig = plt.figure()
48+
ax = fig.add_subplot(1, 1, 1)
49+
ax.plot([1, 2, 3])
50+
return fig
51+
52+
4253
class TestClass(object):
4354

4455
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local)

0 commit comments

Comments
 (0)