Cleanup check-psw, change api
[rarfile.git] / test / test_tool.py
blob42f208adff9f5fc02ec1e3967fd6b90518416626
1 """Alt tool tests
2 """
4 import sys
5 import os
7 import pytest
9 import rarfile
12 def have_tool(name):
13 for dn in os.get_exec_path():
14 if os.path.isfile(os.path.join(dn, name)):
15 return True
16 if os.path.isfile(os.path.join(dn, name + ".exe")):
17 return True
18 return False
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,
24 force=True)
26 def install_unar_tool():
27 tool_setup(unar=True)
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)
46 def test_read_rar3():
47 with rarfile.RarFile("test/files/seektest.rar") as rf:
48 for fn in rf.namelist():
49 rf.read(fn)
52 def test_read_rar3_old():
53 with rarfile.RarFile("test/files/rar3-old.rar") as rf:
54 for fn in rf.namelist():
55 rf.read(fn)
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")
60 def test_unar_tool():
61 install_unar_tool()
62 try:
63 test_read_rar3()
64 test_read_rar3_old()
66 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
67 rf.read("file1.txt")
68 rf.read("file2.txt")
70 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
71 rf.setpassword("password")
72 rf.read("file1.txt")
73 finally:
74 uninstall_alt_tool()
77 @pytest.mark.skipif(not have_tool(rarfile.BSDTAR_TOOL), reason="bsdtar not installed")
78 def test_bsdtar_tool():
79 install_bsdtar_tool()
80 try:
81 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
82 rf.read("file1.txt")
83 rf.read("file2.txt")
85 with pytest.raises(rarfile.RarCannotExec):
86 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
87 rf.setpassword("password")
88 rf.read("file1.txt")
89 finally:
90 uninstall_alt_tool()
93 @pytest.mark.skipif(not have_tool(rarfile.SEVENZIP_TOOL), reason="7z not installed")
94 def test_7z_tool():
95 install_7z_tool()
96 try:
97 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
98 rf.read("file1.txt")
99 rf.read("file2.txt")
101 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
102 rf.setpassword("password")
103 rf.read("file1.txt")
104 finally:
105 uninstall_alt_tool()
108 @pytest.mark.skipif(not have_tool(rarfile.SEVENZIP2_TOOL), reason="7zz not installed")
109 def test_7zz_tool():
110 install_7zz_tool()
111 try:
112 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
113 rf.read("file1.txt")
114 rf.read("file2.txt")
116 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
117 rf.setpassword("password")
118 rf.read("file1.txt")
119 finally:
120 uninstall_alt_tool()
123 # test popen errors
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)
153 # own cli tests
155 def cli(*args):
156 try:
157 rarfile.main(args)
158 return 0
159 except SystemExit as ex:
160 return int(ex.code)
161 except Exception as ex:
162 sys.stderr.write(str(ex) + "\n")
163 return 1
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()
175 assert not res.err
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()
181 assert not res.err
184 def test_cli_help(capsys):
185 assert cli("--help") == 0
186 res = capsys.readouterr()
187 assert "option" in res.out