|
10 | 10 | from .. import (Spm99AnalyzeImage, Spm2AnalyzeImage,
|
11 | 11 | Nifti1Pair, Nifti1Image,
|
12 | 12 | Nifti2Pair, Nifti2Image)
|
13 |
| -from ..loadsave import load, read_img_data |
| 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 | 16 |
|
@@ -76,6 +76,45 @@ def test_load_empty_image():
|
76 | 76 | assert str(err.value).startswith('Empty file: ')
|
77 | 77 |
|
78 | 78 |
|
| 79 | +@pytest.mark.parametrize("extension", [".gz", ".bz2"]) |
| 80 | +def test_load_bad_compressed_extension(tmp_path, extension): |
| 81 | + file_path = tmp_path / f"img.nii{extension}" |
| 82 | + file_path.write_bytes(b"bad") |
| 83 | + with pytest.raises(ImageFileError, match=".*is not a .* file"): |
| 84 | + load(file_path) |
| 85 | + |
| 86 | + |
| 87 | +def test_signature_matches_extension(tmp_path): |
| 88 | + gz_signature = b"\x1f\x8b" |
| 89 | + good_file = tmp_path / "good.gz" |
| 90 | + good_file.write_bytes(gz_signature) |
| 91 | + bad_file = tmp_path / "bad.gz" |
| 92 | + bad_file.write_bytes(b"bad") |
| 93 | + matches, msg = _signature_matches_extension( |
| 94 | + tmp_path / "uncompressed.nii", None) |
| 95 | + assert matches |
| 96 | + assert msg == "" |
| 97 | + matches, msg = _signature_matches_extension(tmp_path / "missing.gz", None) |
| 98 | + assert not matches |
| 99 | + assert msg.startswith("Could not read") |
| 100 | + matches, msg = _signature_matches_extension(bad_file, None) |
| 101 | + assert not matches |
| 102 | + assert "is not a" in msg |
| 103 | + matches, msg = _signature_matches_extension(bad_file, gz_signature + b"abc") |
| 104 | + assert matches |
| 105 | + assert msg == "" |
| 106 | + matches, msg = _signature_matches_extension( |
| 107 | + good_file, gz_signature + b"abc") |
| 108 | + assert matches |
| 109 | + assert msg == "" |
| 110 | + matches, msg = _signature_matches_extension(good_file, gz_signature[:1]) |
| 111 | + assert matches |
| 112 | + assert msg == "" |
| 113 | + matches, msg = _signature_matches_extension(good_file, None) |
| 114 | + assert matches |
| 115 | + assert msg == "" |
| 116 | + |
| 117 | + |
79 | 118 | def test_read_img_data_nifti():
|
80 | 119 | shape = (2, 3, 4)
|
81 | 120 | data = np.random.normal(size=shape)
|
|
0 commit comments