13 for dn
in os
.get_exec_path():
14 if os
.path
.isfile(os
.path
.join(dn
, name
)):
16 if os
.path
.isfile(os
.path
.join(dn
, name
+ ".exe")):
21 def tool_setup(unrar
=False, unar
=False, bsdtar
=False, sevenzip
=False, sevenzip2
=False):
22 rarfile
.FORCE_TOOL
= True
23 rarfile
.tool_setup(unrar
=unrar
, unar
=unar
, bsdtar
=bsdtar
,
24 sevenzip
=sevenzip
, sevenzip2
=sevenzip2
,
28 def install_unrar_tool():
29 tool_setup(unrar
=True)
32 def install_unar_tool():
36 def install_bsdtar_tool():
37 tool_setup(bsdtar
=True)
40 def install_7z_tool():
41 tool_setup(sevenzip
=True)
44 def install_7zz_tool():
45 tool_setup(sevenzip2
=True)
48 def uninstall_alt_tool():
49 rarfile
.FORCE_TOOL
= False
50 rarfile
.tool_setup(force
=True)
54 with rarfile
.RarFile("test/files/seektest.rar") as rf
:
55 for fn
in rf
.namelist():
60 with rarfile
.RarFile("test/files/rar3-old.rar") as rf
:
61 for fn
in rf
.namelist():
63 with rarfile
.RarFile("test/files/rar3-vols.part1.rar") as rf
:
64 for fn
in rf
.namelist():
65 rf
.read(fn
) # rar3-new
66 with rarfile
.RarFile("test/files/rar5-vols.part1.rar") as rf
:
67 for fn
in rf
.namelist():
71 def test_unrar_tool():
77 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
81 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
82 rf
.setpassword("password")
88 @pytest.mark
.skipif(sys
.platform
== "win32", reason
="unar not available on Windows")
89 @pytest.mark
.skipif(not have_tool(rarfile
.UNAR_TOOL
), reason
="unar not installed")
96 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
100 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
101 rf
.setpassword("password")
107 @pytest.mark
.skipif(not have_tool(rarfile
.BSDTAR_TOOL
), reason
="bsdtar not installed")
108 def test_bsdtar_tool():
109 install_bsdtar_tool()
114 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
118 with pytest
.raises(rarfile
.RarCannotExec
):
119 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
120 rf
.setpassword("password")
126 @pytest.mark
.skipif(not have_tool(rarfile
.SEVENZIP_TOOL
), reason
="7z not installed")
133 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
137 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
138 rf
.setpassword("password")
144 @pytest.mark
.skipif(not have_tool(rarfile
.SEVENZIP2_TOOL
), reason
="7zz not installed")
151 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
155 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
156 rf
.setpassword("password")
164 def test_popen_fail():
165 with pytest
.raises(rarfile
.RarCannotExec
):
166 rarfile
.custom_popen(["missing-unrar-exe"])
168 if sys
.platform
!= "win32":
169 with pytest
.raises(rarfile
.RarCannotExec
):
170 rarfile
.custom_popen(["./test/files/rar5-blake.rar.exp"])
173 def test_check_returncode():
174 errmap
= rarfile
.UNRAR_CONFIG
["errmap"]
176 assert not rarfile
.check_returncode(0, "", errmap
)
178 with pytest
.raises(rarfile
.RarFatalError
):
179 rarfile
.check_returncode(2, "x", errmap
)
180 with pytest
.raises(rarfile
.RarUnknownError
):
181 rarfile
.check_returncode(100, "", errmap
)
182 with pytest
.raises(rarfile
.RarUserBreak
):
183 rarfile
.check_returncode(255, "", errmap
)
184 with pytest
.raises(rarfile
.RarSignalExit
):
185 rarfile
.check_returncode(-11, "", errmap
)
187 errmap
= rarfile
.UNAR_CONFIG
["errmap"]
188 with pytest
.raises(rarfile
.RarUnknownError
):
189 rarfile
.check_returncode(2, "", errmap
)
198 except SystemExit as ex
:
200 except Exception as ex
:
201 sys
.stderr
.write(str(ex
) + "\n")
205 def test_cli_list(capsys
):
206 assert cli("-l", "test/files/rar3-old.rar") == 0
207 res
= capsys
.readouterr()
208 assert "bigfile" in res
.out
211 def test_cli_testrar(capsys
):
212 assert cli("-t", "test/files/rar3-old.rar") == 0
213 res
= capsys
.readouterr()
217 def test_cli_extract(capsys
, tmp_path
):
218 assert cli("-e", "test/files/rar3-old.rar", str(tmp_path
)) == 0
219 res
= capsys
.readouterr()
223 def test_cli_help(capsys
):
224 assert cli("--help") == 0
225 res
= capsys
.readouterr()
226 assert "option" in res
.out