Converts test to pytest, nose is dead
[rarfile.git] / test / test_api.py
blob1fb2483e030964812c973386a3617974704c55d4
1 """API tests.
2 """
4 import io
5 import os
7 import pytest
9 import rarfile
11 if rarfile._have_pathlib:
12 from pathlib import Path
15 # test start
18 def test_bad_arc_mode_w():
19 with pytest.raises(NotImplementedError):
20 rarfile.RarFile('test/files/rar3-comment-plain.rar', 'w')
22 def test_bad_arc_mode_rb():
23 with pytest.raises(NotImplementedError):
24 rarfile.RarFile('test/files/rar3-comment-plain.rar', 'rb')
26 def test_bad_errs():
27 with pytest.raises(ValueError):
28 rarfile.RarFile('test/files/rar3-comment-plain.rar', 'r', errors='foo')
30 def test_bad_open_mode_w():
31 rf = rarfile.RarFile('test/files/rar3-comment-plain.rar')
32 with pytest.raises(NotImplementedError):
33 rf.open('qwe', 'w')
35 def test_bad_open_psw():
36 rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
37 with pytest.raises(rarfile.PasswordRequired):
38 rf.open('file1.txt')
40 def test_bad_filelike():
41 with pytest.raises(ValueError):
42 rarfile.is_rarfile(bytearray(10))
44 def test_open_psw_late_rar3():
45 rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
46 d1 = rf.open('file1.txt', 'r', 'password').read()
47 d2 = rf.open('file1.txt', 'r', u'password').read()
48 assert d1 == d2
50 def test_open_psw_late_rar5():
51 rf = rarfile.RarFile('test/files/rar5-psw.rar')
52 rf.open('stest1.txt', 'r', 'password').read()
53 rf.open('stest1.txt', 'r', u'password').read()
55 if rarfile._have_pathlib:
56 def test_open_pathlib_path():
57 rf = rarfile.RarFile('test/files/rar5-psw.rar')
58 rf.open(Path('stest1.txt'), 'r', 'password').read()
60 def test_read_psw_late_rar3():
61 rf = rarfile.RarFile('test/files/rar3-comment-psw.rar')
62 rf.read('file1.txt', 'password')
63 rf.read('file1.txt', u'password')
65 def test_read_psw_late_rar5():
66 rf = rarfile.RarFile('test/files/rar5-psw.rar')
67 rf.read('stest1.txt', 'password')
68 rf.read('stest1.txt', u'password')
70 def test_open_psw_late():
71 rf = rarfile.RarFile('test/files/rar5-psw.rar')
72 with pytest.raises(rarfile.BadRarFile):
73 rf.read('stest1.txt', 'password222')
75 if rarfile._have_pathlib:
76 def test_create_from_pathlib_path():
77 # Make sure we can open both relative and absolute Paths
78 rarfile.RarFile(Path('test/files/rar5-psw.rar'))
79 rarfile.RarFile(Path('test/files/rar5-psw.rar').resolve())
81 def test_detection():
82 assert rarfile.is_rarfile('test/files/ctime4.rar.exp') is False
83 assert rarfile.is_rarfile('test/files/ctime4.rar') is True
84 assert rarfile.is_rarfile('test/files/rar5-crc.rar') is True
86 if rarfile._have_pathlib:
87 assert rarfile.is_rarfile(Path('test/files/rar5-crc.rar')) is True
90 def test_signature_error():
91 with pytest.raises(rarfile.BadRarFile):
92 rarfile.RarFile('test/files/ctime4.rar.exp')
94 def test_signature_error_mem():
95 data = io.BytesIO(b'x'*40)
96 with pytest.raises(rarfile.BadRarFile):
97 rarfile.RarFile(data)
99 def test_with():
100 with rarfile.RarFile('test/files/rar5-crc.rar') as rf:
101 data = rf.read('stest1.txt')
102 with rf.open('stest1.txt') as f:
103 dst = io.BytesIO()
104 while 1:
105 buf = f.read(7)
106 if not buf:
107 break
108 dst.write(buf)
109 assert dst.getvalue() == data
111 def test_readline():
112 def load_readline(rf, fn):
113 with rf.open(fn) as f:
114 tr = io.TextIOWrapper(io.BufferedReader(f))
115 res = []
116 while 1:
117 ln = tr.readline()
118 if not ln:
119 break
120 res.append(ln)
121 return res
123 rf = rarfile.RarFile('test/files/seektest.rar')
124 v1 = load_readline(rf, 'stest1.txt')
125 v2 = load_readline(rf, 'stest2.txt')
126 assert len(v1) == 512
127 assert v1 == v2
129 def test_printdir(capsys):
130 rf = rarfile.RarFile('test/files/seektest.rar')
131 rf.printdir()
132 res = capsys.readouterr()
133 assert res.out == u'stest1.txt\nstest2.txt\n'
135 def test_testrar():
136 rf = rarfile.RarFile('test/files/seektest.rar')
137 rf.testrar()
139 def test_testrar_mem():
140 arc = open('test/files/seektest.rar', 'rb').read()
141 rf = rarfile.RarFile(io.BytesIO(arc))
142 rf.testrar()
144 def test_extract(tmp_path):
145 ex1 = tmp_path / "extract1"
146 ex2 = tmp_path / "extract2"
147 ex3 = tmp_path / "extract3"
148 os.makedirs(str(ex1))
149 os.makedirs(str(ex2))
150 os.makedirs(str(ex3))
151 rf = rarfile.RarFile('test/files/seektest.rar')
153 rf.extractall(str(ex1))
154 assert os.path.isfile(str(ex1 / 'stest1.txt')) is True
155 assert os.path.isfile(str(ex1 / 'stest2.txt')) is True
157 rf.extract('stest1.txt', str(ex2))
158 assert os.path.isfile(str(ex2 / 'stest1.txt')) is True
159 assert os.path.isfile(str(ex2 / 'stest2.txt')) is False
161 inf = rf.getinfo('stest2.txt')
162 rf.extract(inf, str(ex3))
163 assert os.path.isfile(str(ex3 / 'stest1.txt')) is False
164 assert os.path.isfile(str(ex3 / 'stest2.txt')) is True
166 rf.extractall(str(ex2), ['stest1.txt'])
167 assert os.path.isfile(str(ex2 / 'stest1.txt')) is True
169 rf.extractall(str(ex3), [rf.getinfo('stest2.txt')])
170 assert os.path.isfile(str(ex3 / 'stest2.txt')) is True
172 if rarfile._have_pathlib:
173 ex4 = tmp_path / "extract4"
174 os.makedirs(str(ex4))
175 rf.extractall(ex4)
176 assert os.path.isfile(str(ex4 / 'stest1.txt')) is True
177 assert os.path.isfile(str(ex4 / 'stest2.txt')) is True
179 def test_extract_mem(tmp_path):
180 ex1 = tmp_path / "extract11"
181 ex2 = tmp_path / "extract22"
182 ex3 = tmp_path / "extract33"
183 os.makedirs(str(ex1))
184 os.makedirs(str(ex2))
185 os.makedirs(str(ex3))
186 arc = open('test/files/seektest.rar', 'rb').read()
187 rf = rarfile.RarFile(io.BytesIO(arc))
189 rf.extractall(str(ex1))
190 assert os.path.isfile(str(ex1 / 'stest1.txt')) is True
191 assert os.path.isfile(str(ex1 / 'stest2.txt')) is True
193 rf.extract('stest1.txt', str(ex2))
194 assert os.path.isfile(str(ex2 / 'stest1.txt')) is True
195 assert os.path.isfile(str(ex2 / 'stest2.txt')) is False
197 inf = rf.getinfo('stest2.txt')
198 rf.extract(inf, str(ex3))
199 assert os.path.isfile(str(ex3 / 'stest1.txt')) is False
200 assert os.path.isfile(str(ex3 / 'stest2.txt')) is True
202 def test_infocb():
203 infos = []
204 def info_cb(info):
205 infos.append( (info.type, info.needs_password(), info.isdir(), info._must_disable_hack()) )
207 rf = rarfile.RarFile('test/files/seektest.rar', info_callback=info_cb)
208 assert infos == [
209 (rarfile.RAR_BLOCK_MAIN, False, False, False),
210 (rarfile.RAR_BLOCK_FILE, False, False, False),
211 (rarfile.RAR_BLOCK_FILE, False, False, False),
212 (rarfile.RAR_BLOCK_ENDARC, False, False, False)]
214 infos = []
215 rf = rarfile.RarFile('test/files/rar5-solid-qo.rar', info_callback=info_cb)
216 assert infos == [
217 (rarfile.RAR_BLOCK_MAIN, False, False, True),
218 (rarfile.RAR_BLOCK_FILE, False, False, False),
219 (rarfile.RAR_BLOCK_FILE, False, False, True),
220 (rarfile.RAR_BLOCK_FILE, False, False, True),
221 (rarfile.RAR_BLOCK_FILE, False, False, True),
222 (rarfile.RAR_BLOCK_SUB, False, False, False),
223 (rarfile.RAR_BLOCK_ENDARC, False, False, False)]
225 def install_alt_tool():
226 rarfile.ORIG_UNRAR_TOOL = 'x_unrar_missing'
227 rarfile._check_unrar_tool()
229 def uninstall_alt_tool():
230 rarfile.ORIG_UNRAR_TOOL = 'unrar'
231 rarfile._check_unrar_tool()
233 def test_read_rar3():
234 with rarfile.RarFile('test/files/seektest.rar') as rf:
235 for fn in rf.namelist():
236 rf.read(fn)
238 #@with_setup(install_alt_tool, uninstall_alt_tool)
239 def test_alt_tool():
240 #test_read_rar3()
241 pass