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
.tool_setup(unrar
=unrar
, unar
=unar
, bsdtar
=bsdtar
,
23 sevenzip
=sevenzip
, sevenzip2
=sevenzip2
,
26 def install_unar_tool():
30 def install_bsdtar_tool():
31 tool_setup(bsdtar
=True)
34 def install_7z_tool():
35 tool_setup(sevenzip
=True)
38 def install_7zz_tool():
39 tool_setup(sevenzip2
=True)
42 def uninstall_alt_tool():
43 rarfile
.tool_setup(force
=True)
47 with rarfile
.RarFile("test/files/seektest.rar") as rf
:
48 for fn
in rf
.namelist():
52 def test_read_rar3_old():
53 with rarfile
.RarFile("test/files/rar3-old.rar") as rf
:
54 for fn
in rf
.namelist():
58 @pytest.mark
.skipif(sys
.platform
== "win32", reason
="unar not available on Windows")
59 @pytest.mark
.skipif(not have_tool(rarfile
.UNAR_TOOL
), reason
="unar not installed")
66 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
70 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
71 rf
.setpassword("password")
77 @pytest.mark
.skipif(not have_tool(rarfile
.BSDTAR_TOOL
), reason
="bsdtar not installed")
78 def test_bsdtar_tool():
81 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
85 with pytest
.raises(rarfile
.RarCannotExec
):
86 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
87 rf
.setpassword("password")
93 @pytest.mark
.skipif(not have_tool(rarfile
.SEVENZIP_TOOL
), reason
="7z not installed")
97 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
101 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
102 rf
.setpassword("password")
108 @pytest.mark
.skipif(not have_tool(rarfile
.SEVENZIP2_TOOL
), reason
="7zz not installed")
112 with rarfile
.RarFile("test/files/rar3-comment-plain.rar") as rf
:
116 with rarfile
.RarFile("test/files/rar3-comment-psw.rar") as rf
:
117 rf
.setpassword("password")
125 def test_popen_fail():
126 with pytest
.raises(rarfile
.RarCannotExec
):
127 rarfile
.custom_popen(["missing-unrar-exe"])
129 if sys
.platform
!= "win32":
130 with pytest
.raises(rarfile
.RarCannotExec
):
131 rarfile
.custom_popen(["./test/files/rar5-blake.rar.exp"])
134 def test_check_returncode():
135 errmap
= rarfile
.UNRAR_CONFIG
["errmap"]
137 assert not rarfile
.check_returncode(0, "", errmap
)
139 with pytest
.raises(rarfile
.RarFatalError
):
140 rarfile
.check_returncode(2, "x", errmap
)
141 with pytest
.raises(rarfile
.RarUnknownError
):
142 rarfile
.check_returncode(100, "", errmap
)
143 with pytest
.raises(rarfile
.RarUserBreak
):
144 rarfile
.check_returncode(255, "", errmap
)
145 with pytest
.raises(rarfile
.RarSignalExit
):
146 rarfile
.check_returncode(-11, "", errmap
)
148 errmap
= rarfile
.UNAR_CONFIG
["errmap"]
149 with pytest
.raises(rarfile
.RarUnknownError
):
150 rarfile
.check_returncode(2, "", errmap
)
159 except SystemExit as ex
:
161 except Exception as ex
:
162 sys
.stderr
.write(str(ex
) + "\n")
166 def test_cli_list(capsys
):
167 assert cli("-l", "test/files/rar3-old.rar") == 0
168 res
= capsys
.readouterr()
169 assert "bigfile" in res
.out
172 def test_cli_testrar(capsys
):
173 assert cli("-t", "test/files/rar3-old.rar") == 0
174 res
= capsys
.readouterr()
178 def test_cli_extract(capsys
, tmp_path
):
179 assert cli("-e", "test/files/rar3-old.rar", str(tmp_path
)) == 0
180 res
= capsys
.readouterr()
184 def test_cli_help(capsys
):
185 assert cli("--help") == 0
186 res
= capsys
.readouterr()
187 assert "option" in res
.out