File: test_File__archives.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (126 lines) | stat: -rw-r--r-- 3,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import shutil
import sys
from unittest.mock import MagicMock

import pytest


@pytest.fixture(scope="module")
def custom_packing_format():
    shutil.register_unpack_format(
        "custom_packing",
        [".archive", ".archive.ext"],
        lambda x, y: True,
    )
    yield
    shutil.unregister_unpack_format("custom_packing")


@pytest.mark.parametrize(
    "filename, outcome",
    [
        ("filename.tar", True),
        ("filename.zip", True),
        ("filename.archive", True),
        ("filename.part.archive", True),
        ("filename.archive.ext", True),
        ("filename.part.archive.ext", True),
        ("filename.part.archive.ext", True),
        ("filename", False),
        ("filename.doc", False),
        ("filename.archive.doc", False),
        ("filename.archive.ext.doc", False),
    ],
)
@pytest.mark.usefixtures("custom_packing_format")
def test_is_archive(mock_tools, filename, outcome, monkeypatch):
    """Archive filenames are properly detected."""
    assert mock_tools.file.is_archive(filename) is outcome


def test_unpack_archive(mock_tools):
    """Archive unpacking is deferred correctly for an arbitrary archive."""
    mock_tools.shutil = MagicMock(spec=shutil)

    mock_tools.file.unpack_archive(
        "test_archive",
        extract_dir="destination",
    )

    mock_tools.shutil.unpack_archive.assert_called_once_with(
        filename="test_archive",
        extract_dir="destination",
        **({"filter": "data"} if sys.version_info >= (3, 12) else {}),
    )


def test_unpack_archive_kwargs(mock_tools):
    """Archive unpacking is deferred correctly with kwargs."""
    mock_tools.shutil = MagicMock(spec=shutil)

    mock_tools.file.unpack_archive(
        "test_archive",
        extract_dir="destination",
        extra_arg="arg this",
    )

    mock_tools.shutil.unpack_archive.assert_called_once_with(
        filename="test_archive",
        extract_dir="destination",
        **(
            {"filter": "data", "extra_arg": "arg this"}
            if sys.version_info >= (3, 12)
            else {"extra_arg": "arg this"}
        ),
    )


def test_unpack_archive_override_filter(mock_tools):
    """Archive unpacking is deferred correctly while overriding `filter`."""
    mock_tools.shutil = MagicMock(spec=shutil)

    mock_tools.file.unpack_archive(
        "test_archive",
        extract_dir="destination",
        filter="onlycatpics",
        extra_arg="arg this",
    )

    mock_tools.shutil.unpack_archive.assert_called_once_with(
        filename="test_archive",
        extract_dir="destination",
        filter="onlycatpics",
        extra_arg="arg this",
    )


def test_unpack_zip_archive(mock_tools):
    """Archive unpacking is deferred correctly for ZIP archives."""
    mock_tools.shutil = MagicMock(spec=shutil)

    mock_tools.file.unpack_archive(
        "test_archive.zip",
        extract_dir="destination",
    )

    mock_tools.shutil.unpack_archive.assert_called_once_with(
        filename="test_archive.zip",
        extract_dir="destination",
    )


def test_unpack_zip_archive_kwargs(mock_tools):
    """Archive unpacking is deferred correctly for ZIP archives with kwargs."""
    mock_tools.shutil = MagicMock(spec=shutil)

    mock_tools.file.unpack_archive(
        "test_archive.zip",
        extract_dir="destination",
        extra_arg="arg this",
    )

    mock_tools.shutil.unpack_archive.assert_called_once_with(
        filename="test_archive.zip",
        extract_dir="destination",
        extra_arg="arg this",
    )