|
13 | 13 | from ..loadsave import load, read_img_data, _signature_matches_extension
|
14 | 14 | from ..filebasedimages import ImageFileError
|
15 | 15 | from ..tmpdirs import InTemporaryDirectory, TemporaryDirectory
|
| 16 | +from ..openers import Opener |
16 | 17 |
|
17 | 18 | from ..optpkg import optional_package
|
18 | 19 | _, have_scipy, _ = optional_package('scipy')
|
| 20 | +_, have_pyzstd, _ = optional_package('pyzstd') |
19 | 21 |
|
20 | 22 | from numpy.testing import (assert_almost_equal,
|
21 | 23 | assert_array_equal)
|
@@ -74,41 +76,46 @@ def test_load_empty_image():
|
74 | 76 | assert str(err.value).startswith('Empty file: ')
|
75 | 77 |
|
76 | 78 |
|
77 |
| -@pytest.mark.parametrize("extension", [".gz", ".bz2"]) |
| 79 | +@pytest.mark.parametrize("extension", [".gz", ".bz2", ".zst"]) |
78 | 80 | def test_load_bad_compressed_extension(tmp_path, extension):
|
| 81 | + if extension == ".zst" and not have_pyzstd: |
| 82 | + pytest.skip() |
79 | 83 | file_path = tmp_path / f"img.nii{extension}"
|
80 | 84 | file_path.write_bytes(b"bad")
|
81 | 85 | with pytest.raises(ImageFileError, match=".*is not a .* file"):
|
82 | 86 | load(file_path)
|
83 | 87 |
|
84 | 88 |
|
| 89 | +@pytest.mark.parametrize("extension", [".gz", ".bz2", ".zst"]) |
| 90 | +def test_load_good_extension_with_bad_data(tmp_path, extension): |
| 91 | + if extension == ".zst" and not have_pyzstd: |
| 92 | + pytest.skip() |
| 93 | + file_path = tmp_path / f"img.nii{extension}" |
| 94 | + with Opener(file_path, "wb") as fobj: |
| 95 | + fobj.write(b"bad") |
| 96 | + with pytest.raises(ImageFileError, match="Cannot work out file type of .*"): |
| 97 | + load(file_path) |
| 98 | + |
| 99 | + |
85 | 100 | def test_signature_matches_extension(tmp_path):
|
86 | 101 | gz_signature = b"\x1f\x8b"
|
87 | 102 | good_file = tmp_path / "good.gz"
|
88 | 103 | good_file.write_bytes(gz_signature)
|
89 | 104 | bad_file = tmp_path / "bad.gz"
|
90 | 105 | bad_file.write_bytes(b"bad")
|
91 |
| - matches, msg = _signature_matches_extension( |
92 |
| - tmp_path / "uncompressed.nii", None) |
| 106 | + matches, msg = _signature_matches_extension(tmp_path / "uncompressed.nii") |
93 | 107 | assert matches
|
94 | 108 | assert msg == ""
|
95 |
| - matches, msg = _signature_matches_extension(tmp_path / "missing.gz", None) |
| 109 | + matches, msg = _signature_matches_extension(tmp_path / "missing.gz") |
96 | 110 | assert not matches
|
97 | 111 | assert msg.startswith("Could not read")
|
98 |
| - matches, msg = _signature_matches_extension(bad_file, None) |
| 112 | + matches, msg = _signature_matches_extension(bad_file) |
99 | 113 | assert not matches
|
100 | 114 | assert "is not a" in msg
|
101 |
| - matches, msg = _signature_matches_extension(bad_file, gz_signature + b"abc") |
102 |
| - assert matches |
103 |
| - assert msg == "" |
104 |
| - matches, msg = _signature_matches_extension( |
105 |
| - good_file, gz_signature + b"abc") |
106 |
| - assert matches |
107 |
| - assert msg == "" |
108 |
| - matches, msg = _signature_matches_extension(good_file, gz_signature[:1]) |
| 115 | + matches, msg = _signature_matches_extension(good_file) |
109 | 116 | assert matches
|
110 | 117 | assert msg == ""
|
111 |
| - matches, msg = _signature_matches_extension(good_file, None) |
| 118 | + matches, msg = _signature_matches_extension(tmp_path / "missing.nii") |
112 | 119 | assert matches
|
113 | 120 | assert msg == ""
|
114 | 121 |
|
|
0 commit comments