11 if rarfile
._have
_pathlib
:
12 from pathlib
import Path
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')
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):
35 def test_bad_open_psw():
36 rf
= rarfile
.RarFile('test/files/rar3-comment-psw.rar')
37 with pytest
.raises(rarfile
.PasswordRequired
):
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()
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())
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
):
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
:
109 assert dst
.getvalue() == data
112 def load_readline(rf
, fn
):
113 with rf
.open(fn
) as f
:
114 tr
= io
.TextIOWrapper(io
.BufferedReader(f
))
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
129 def test_printdir(capsys
):
130 rf
= rarfile
.RarFile('test/files/seektest.rar')
132 res
= capsys
.readouterr()
133 assert res
.out
== u
'stest1.txt\nstest2.txt\n'
136 rf
= rarfile
.RarFile('test/files/seektest.rar')
139 def test_testrar_mem():
140 arc
= open('test/files/seektest.rar', 'rb').read()
141 rf
= rarfile
.RarFile(io
.BytesIO(arc
))
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
))
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
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
)
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)]
215 rf
= rarfile
.RarFile('test/files/rar5-solid-qo.rar', info_callback
=info_cb
)
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():
238 #@with_setup(install_alt_tool, uninstall_alt_tool)