| 1 |
import shutil |
| 2 |
import sys |
| 3 |
from unittest.mock import MagicMock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture(scope="module") |
| 9 |
def custom_packing_format(): |
| 10 |
shutil.register_unpack_format( |
| 11 |
"custom_packing", |
| 12 |
[".archive", ".archive.ext"], |
| 13 |
lambda x, y: True, |
| 14 |
) |
| 15 |
yield |
| 16 |
shutil.unregister_unpack_format("custom_packing") |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize( |
| 20 |
("filename", "outcome"), |
| 21 |
[ |
| 22 |
("filename.tar", True), |
| 23 |
("filename.zip", True), |
| 24 |
("filename.archive", True), |
| 25 |
("filename.part.archive", True), |
| 26 |
("filename.archive.ext", True), |
| 27 |
("filename.part.archive.ext", True), |
| 28 |
("filename", False), |
| 29 |
("filename.doc", False), |
| 30 |
("filename.archive.doc", False), |
| 31 |
("filename.archive.ext.doc", False), |
| 32 |
], |
| 33 |
) |
| 34 |
@pytest.mark.usefixtures("custom_packing_format") |
| 35 |
def test_is_archive(mock_tools, filename, outcome, monkeypatch): |
| 36 |
"""Archive filenames are properly detected.""" |
| 37 |
assert mock_tools.file.is_archive(filename) is outcome |
| 38 |
|
| 39 |
|
| 40 |
def test_unpack_archive(mock_tools): |
| 41 |
"""Archive unpacking is deferred correctly for an arbitrary archive.""" |
| 42 |
mock_tools.shutil = MagicMock(spec=shutil) |
| 43 |
|
| 44 |
mock_tools.file.unpack_archive( |
| 45 |
"test_archive", |
| 46 |
extract_dir="destination", |
| 47 |
) |
| 48 |
|
| 49 |
mock_tools.shutil.unpack_archive.assert_called_once_with( |
| 50 |
filename="test_archive", |
| 51 |
extract_dir="destination", |
| 52 |
**({"filter": "data"} if sys.version_info >= (3, 12) else {}), |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def test_unpack_archive_kwargs(mock_tools): |
| 57 |
"""Archive unpacking is deferred correctly with kwargs.""" |
| 58 |
mock_tools.shutil = MagicMock(spec=shutil) |
| 59 |
|
| 60 |
mock_tools.file.unpack_archive( |
| 61 |
"test_archive", |
| 62 |
extract_dir="destination", |
| 63 |
extra_arg="arg this", |
| 64 |
) |
| 65 |
|
| 66 |
mock_tools.shutil.unpack_archive.assert_called_once_with( |
| 67 |
filename="test_archive", |
| 68 |
extract_dir="destination", |
| 69 |
**( |
| 70 |
{"filter": "data", "extra_arg": "arg this"} |
| 71 |
if sys.version_info >= (3, 12) |
| 72 |
else {"extra_arg": "arg this"} |
| 73 |
), |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
def test_unpack_archive_override_filter(mock_tools): |
| 78 |
"""Archive unpacking is deferred correctly while overriding `filter`.""" |
| 79 |
mock_tools.shutil = MagicMock(spec=shutil) |
| 80 |
|
| 81 |
mock_tools.file.unpack_archive( |
| 82 |
"test_archive", |
| 83 |
extract_dir="destination", |
| 84 |
filter="onlycatpics", |
| 85 |
extra_arg="arg this", |
| 86 |
) |
| 87 |
|
| 88 |
mock_tools.shutil.unpack_archive.assert_called_once_with( |
| 89 |
filename="test_archive", |
| 90 |
extract_dir="destination", |
| 91 |
filter="onlycatpics", |
| 92 |
extra_arg="arg this", |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def test_unpack_zip_archive(mock_tools): |
| 97 |
"""Archive unpacking is deferred correctly for ZIP archives.""" |
| 98 |
mock_tools.shutil = MagicMock(spec=shutil) |
| 99 |
|
| 100 |
mock_tools.file.unpack_archive( |
| 101 |
"test_archive.zip", |
| 102 |
extract_dir="destination", |
| 103 |
) |
| 104 |
|
| 105 |
mock_tools.shutil.unpack_archive.assert_called_once_with( |
| 106 |
filename="test_archive.zip", |
| 107 |
extract_dir="destination", |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
def test_unpack_zip_archive_kwargs(mock_tools): |
| 112 |
"""Archive unpacking is deferred correctly for ZIP archives with kwargs.""" |
| 113 |
mock_tools.shutil = MagicMock(spec=shutil) |
| 114 |
|
| 115 |
mock_tools.file.unpack_archive( |
| 116 |
"test_archive.zip", |
| 117 |
extract_dir="destination", |
| 118 |
extra_arg="arg this", |
| 119 |
) |
| 120 |
|
| 121 |
mock_tools.shutil.unpack_archive.assert_called_once_with( |
| 122 |
filename="test_archive.zip", |
| 123 |
extract_dir="destination", |
| 124 |
extra_arg="arg this", |
| 125 |
) |