File: test_tool.py

package info (click to toggle)
python-rarfile 4.2-3~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports-sloppy
  • size: 2,312 kB
  • sloc: python: 4,180; makefile: 186; sh: 59; awk: 14
file content (233 lines) | stat: -rw-r--r-- 6,296 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Alt tool tests
"""

import sys
import os

import pytest

import rarfile


def have_tool(name):
    for dn in os.get_exec_path():
        if os.path.isfile(os.path.join(dn, name)):
            return True
        if os.path.isfile(os.path.join(dn, name + ".exe")):
            return True
    return False


def tool_setup(unrar=False, unar=False, bsdtar=False, sevenzip=False, sevenzip2=False):
    rarfile.FORCE_TOOL = True
    rarfile.tool_setup(unrar=unrar, unar=unar, bsdtar=bsdtar,
                       sevenzip=sevenzip, sevenzip2=sevenzip2,
                       force=True)


def install_unrar_tool():
    tool_setup(unrar=True)


def install_unar_tool():
    tool_setup(unar=True)


def install_bsdtar_tool():
    tool_setup(bsdtar=True)


def install_7z_tool():
    tool_setup(sevenzip=True)


def install_7zz_tool():
    tool_setup(sevenzip2=True)


def uninstall_alt_tool():
    rarfile.FORCE_TOOL = False
    rarfile.tool_setup(force=True)


def test_read_rar3():
    with rarfile.RarFile("test/files/seektest.rar") as rf:
        for fn in rf.namelist():
            rf.read(fn)


def test_read_vols():
    with rarfile.RarFile("test/files/rar3-old.rar") as rf:
        for fn in rf.namelist():
            rf.read(fn) # old
    with rarfile.RarFile("test/files/rar3-vols.part1.rar") as rf:
        for fn in rf.namelist():
            rf.read(fn) # rar3-new
    with rarfile.RarFile("test/files/rar5-vols.part1.rar") as rf:
        for fn in rf.namelist():
            rf.read(fn) # rar5


def test_unrar_tool():
    install_unrar_tool()
    try:
        test_read_rar3()
        test_read_vols()

        with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
            rf.read("file1.txt")
            rf.read("file2.txt")

        with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
            rf.setpassword("password")
            rf.read("file1.txt")
    finally:
        uninstall_alt_tool()


@pytest.mark.skipif(sys.platform == "win32", reason="unar not available on Windows")
@pytest.mark.skipif(not have_tool(rarfile.UNAR_TOOL), reason="unar not installed")
def test_unar_tool():
    install_unar_tool()
    try:
        test_read_rar3()
        test_read_vols()

        with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
            rf.read("file1.txt")
            rf.read("file2.txt")

        with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
            rf.setpassword("password")
            rf.read("file1.txt")
    except rarfile.BadRarFile as e:
        assert any("Corrupt file - CRC check failed:" in arg for arg in e.args)
        pytest.xfail("caught crc check failure in test_unar_tool")
    finally:
        uninstall_alt_tool()


@pytest.mark.skipif(not have_tool(rarfile.BSDTAR_TOOL), reason="bsdtar not installed")
def test_bsdtar_tool():
    install_bsdtar_tool()
    try:
        #test_read_rar3()
        #test_read_vols()

        with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
            rf.read("file1.txt")
            rf.read("file2.txt")

        with pytest.raises(rarfile.RarCannotExec):
            with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
                rf.setpassword("password")
                rf.read("file1.txt")
    finally:
        uninstall_alt_tool()


@pytest.mark.skipif(not have_tool(rarfile.SEVENZIP_TOOL), reason="7z not installed")
def test_7z_tool():
    install_7z_tool()
    try:
        #test_read_rar3()
        test_read_vols()

        with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
            rf.read("file1.txt")
            rf.read("file2.txt")

        with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
            rf.setpassword("password")
            rf.read("file1.txt")
    finally:
        uninstall_alt_tool()


@pytest.mark.skipif(not have_tool(rarfile.SEVENZIP2_TOOL), reason="7zz not installed")
def test_7zz_tool():
    install_7zz_tool()
    try:
        #test_read_rar3()
        test_read_vols()

        with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
            rf.read("file1.txt")
            rf.read("file2.txt")

        with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
            rf.setpassword("password")
            rf.read("file1.txt")
    except rarfile.BadRarFile as e:
        assert any("Failed the read enough data" in arg for arg in e.args)
        pytest.xfail("caught not enough data read failure in test_7zz_tool")
    finally:
        uninstall_alt_tool()


# test popen errors

def test_popen_fail():
    with pytest.raises(rarfile.RarCannotExec):
        rarfile.custom_popen(["missing-unrar-exe"])

    if sys.platform != "win32":
        with pytest.raises(rarfile.RarCannotExec):
            rarfile.custom_popen(["./test/files/rar5-blake.rar.exp"])


def test_check_returncode():
    errmap = rarfile.UNRAR_CONFIG["errmap"]

    assert not rarfile.check_returncode(0, "", errmap)

    with pytest.raises(rarfile.RarFatalError):
        rarfile.check_returncode(2, "x", errmap)
    with pytest.raises(rarfile.RarUnknownError):
        rarfile.check_returncode(100, "", errmap)
    with pytest.raises(rarfile.RarUserBreak):
        rarfile.check_returncode(255, "", errmap)
    with pytest.raises(rarfile.RarSignalExit):
        rarfile.check_returncode(-11, "", errmap)

    errmap = rarfile.UNAR_CONFIG["errmap"]
    with pytest.raises(rarfile.RarUnknownError):
        rarfile.check_returncode(2, "", errmap)


# own cli tests

def cli(*args):
    try:
        rarfile.main(args)
        return 0
    except SystemExit as ex:
        return int(ex.code)
    except Exception as ex:
        sys.stderr.write(str(ex) + "\n")
        return 1


def test_cli_list(capsys):
    assert cli("-l", "test/files/rar3-old.rar") == 0
    res = capsys.readouterr()
    assert "bigfile" in res.out


def test_cli_testrar(capsys):
    assert cli("-t", "test/files/rar3-old.rar") == 0
    res = capsys.readouterr()
    assert not res.err


def test_cli_extract(capsys, tmp_path):
    assert cli("-e", "test/files/rar3-old.rar", str(tmp_path)) == 0
    res = capsys.readouterr()
    assert not res.err


def test_cli_help(capsys):
    assert cli("--help") == 0
    res = capsys.readouterr()
    assert "option" in res.out