doc: add readthedocs config
[rarfile.git] / test / test_tool.py
blob48dc7d5b18b057e89d34e32f79fd9f7514aa0568
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.FORCE_TOOL = True
23 rarfile.tool_setup(unrar=unrar, unar=unar, bsdtar=bsdtar,
24 sevenzip=sevenzip, sevenzip2=sevenzip2,
25 force=True)
28 def install_unrar_tool():
29 tool_setup(unrar=True)
32 def install_unar_tool():
33 tool_setup(unar=True)
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)
53 def test_read_rar3():
54 with rarfile.RarFile("test/files/seektest.rar") as rf:
55 for fn in rf.namelist():
56 rf.read(fn)
59 def test_read_vols():
60 with rarfile.RarFile("test/files/rar3-old.rar") as rf:
61 for fn in rf.namelist():
62 rf.read(fn) # old
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():
68 rf.read(fn) # rar5
71 def test_unrar_tool():
72 install_unrar_tool()
73 try:
74 test_read_rar3()
75 test_read_vols()
77 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
78 rf.read("file1.txt")
79 rf.read("file2.txt")
81 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
82 rf.setpassword("password")
83 rf.read("file1.txt")
84 finally:
85 uninstall_alt_tool()
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")
90 def test_unar_tool():
91 install_unar_tool()
92 try:
93 test_read_rar3()
94 test_read_vols()
96 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
97 rf.read("file1.txt")
98 rf.read("file2.txt")
100 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
101 rf.setpassword("password")
102 rf.read("file1.txt")
103 finally:
104 uninstall_alt_tool()
107 @pytest.mark.skipif(not have_tool(rarfile.BSDTAR_TOOL), reason="bsdtar not installed")
108 def test_bsdtar_tool():
109 install_bsdtar_tool()
110 try:
111 #test_read_rar3()
112 #test_read_vols()
114 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
115 rf.read("file1.txt")
116 rf.read("file2.txt")
118 with pytest.raises(rarfile.RarCannotExec):
119 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
120 rf.setpassword("password")
121 rf.read("file1.txt")
122 finally:
123 uninstall_alt_tool()
126 @pytest.mark.skipif(not have_tool(rarfile.SEVENZIP_TOOL), reason="7z not installed")
127 def test_7z_tool():
128 install_7z_tool()
129 try:
130 #test_read_rar3()
131 test_read_vols()
133 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
134 rf.read("file1.txt")
135 rf.read("file2.txt")
137 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
138 rf.setpassword("password")
139 rf.read("file1.txt")
140 finally:
141 uninstall_alt_tool()
144 @pytest.mark.skipif(not have_tool(rarfile.SEVENZIP2_TOOL), reason="7zz not installed")
145 def test_7zz_tool():
146 install_7zz_tool()
147 try:
148 #test_read_rar3()
149 test_read_vols()
151 with rarfile.RarFile("test/files/rar3-comment-plain.rar") as rf:
152 rf.read("file1.txt")
153 rf.read("file2.txt")
155 with rarfile.RarFile("test/files/rar3-comment-psw.rar") as rf:
156 rf.setpassword("password")
157 rf.read("file1.txt")
158 finally:
159 uninstall_alt_tool()
162 # test popen errors
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)
192 # own cli tests
194 def cli(*args):
195 try:
196 rarfile.main(args)
197 return 0
198 except SystemExit as ex:
199 return int(ex.code)
200 except Exception as ex:
201 sys.stderr.write(str(ex) + "\n")
202 return 1
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()
214 assert not res.err
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()
220 assert not res.err
223 def test_cli_help(capsys):
224 assert cli("--help") == 0
225 res = capsys.readouterr()
226 assert "option" in res.out