|
1 | 1 | import os |
2 | 2 | from os import path as op |
| 3 | +import re |
3 | 4 | import shutil |
4 | 5 | import zipfile |
5 | 6 | import sys |
|
11 | 12 | from mne.datasets._fsaverage.base import _set_montage_coreg_path |
12 | 13 | from mne.datasets.utils import _manifest_check_download |
13 | 14 |
|
14 | | -from mne.utils import (run_tests_if_main, requires_good_network, modified_env, |
| 15 | +from mne.utils import (requires_good_network, modified_env, |
15 | 16 | get_subjects_dir, ArgvSetter, _pl, use_log_level, |
16 | 17 | catch_logging, hashfunc) |
17 | 18 |
|
@@ -52,18 +53,62 @@ def test_datasets_basic(tmpdir): |
52 | 53 | assert sd.endswith('MNE-fsaverage-data') |
53 | 54 |
|
54 | 55 |
|
55 | | -def _fake_fetch_file(url, destination, print_destination=False): |
56 | | - with open(destination, 'w') as fid: |
57 | | - fid.write(url) |
58 | | - |
59 | | - |
60 | 56 | @requires_good_network |
61 | | -def test_downloads(tmpdir): |
62 | | - """Test dataset URL handling.""" |
| 57 | +def test_downloads(tmpdir, monkeypatch, capsys): |
| 58 | + """Test dataset URL and version handling.""" |
63 | 59 | # Try actually downloading a dataset |
64 | | - path = datasets._fake.data_path(path=str(tmpdir), update_path=False) |
| 60 | + kwargs = dict(path=str(tmpdir), verbose=True) |
| 61 | + path = datasets._fake.data_path(update_path=False, **kwargs) |
| 62 | + out, _ = capsys.readouterr() |
| 63 | + assert 'Downloading' in out |
| 64 | + assert op.isdir(path) |
65 | 65 | assert op.isfile(op.join(path, 'bar')) |
| 66 | + assert not datasets.utils.has_dataset('fake') # not in the desired path |
66 | 67 | assert datasets._fake.get_version() is None |
| 68 | + assert datasets.utils._get_version('fake') is None |
| 69 | + monkeypatch.setenv('_MNE_FAKE_HOME_DIR', str(tmpdir)) |
| 70 | + with pytest.warns(RuntimeWarning, match='non-standard config'): |
| 71 | + new_path = datasets._fake.data_path(update_path=True, **kwargs) |
| 72 | + assert path == new_path |
| 73 | + out, _ = capsys.readouterr() |
| 74 | + assert 'Downloading' not in out |
| 75 | + # No version: shown as existing but unknown version |
| 76 | + assert datasets.utils.has_dataset('fake') |
| 77 | + # XXX logic bug, should be "unknown" |
| 78 | + assert datasets._fake.get_version() == '0.7' |
| 79 | + # With a version but no required one: shown as existing and gives version |
| 80 | + fname = tmpdir / 'foo' / 'version.txt' |
| 81 | + with open(fname, 'w') as fid: |
| 82 | + fid.write('0.1') |
| 83 | + assert datasets.utils.has_dataset('fake') |
| 84 | + assert datasets._fake.get_version() == '0.1' |
| 85 | + datasets._fake.data_path(download=False, **kwargs) |
| 86 | + out, _ = capsys.readouterr() |
| 87 | + assert 'out of date' not in out |
| 88 | + # With the required version: shown as existing with the required version |
| 89 | + monkeypatch.setattr(datasets.utils, '_FAKE_VERSION', '0.1') |
| 90 | + assert datasets.utils.has_dataset('fake') |
| 91 | + assert datasets._fake.get_version() == '0.1' |
| 92 | + datasets._fake.data_path(download=False, **kwargs) |
| 93 | + out, _ = capsys.readouterr() |
| 94 | + assert 'out of date' not in out |
| 95 | + monkeypatch.setattr(datasets.utils, '_FAKE_VERSION', '0.2') |
| 96 | + # With an older version: |
| 97 | + # 1. Marked as not actually being present |
| 98 | + assert not datasets.utils.has_dataset('fake') |
| 99 | + # 2. Will try to update when `data_path` gets called, with logged message |
| 100 | + want_msg = 'Correctly trying to download newer version' |
| 101 | + |
| 102 | + def _error_download(url, full_name, print_destination, hash_, hash_type): |
| 103 | + assert 'foo.tgz' in url |
| 104 | + assert str(tmpdir) in full_name |
| 105 | + raise RuntimeError(want_msg) |
| 106 | + |
| 107 | + monkeypatch.setattr(datasets.utils, '_fetch_file', _error_download) |
| 108 | + with pytest.raises(RuntimeError, match=want_msg): |
| 109 | + datasets._fake.data_path(**kwargs) |
| 110 | + out, _ = capsys.readouterr() |
| 111 | + assert re.match(r'.* 0\.1 .*out of date.* 0\.2.*', out, re.MULTILINE), out |
67 | 112 |
|
68 | 113 |
|
69 | 114 | @pytest.mark.slowtest |
@@ -151,6 +196,3 @@ def test_manifest_check_download(tmpdir, n_have, monkeypatch): |
151 | 196 | assert op.isdir(destination) |
152 | 197 | for fname in _zip_fnames: |
153 | 198 | assert op.isfile(op.join(destination, fname)) |
154 | | - |
155 | | - |
156 | | -run_tests_if_main() |
|
0 commit comments