forked from piejanssens/Extension-ExtendedUnpacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
295 lines (217 loc) · 7.92 KB
/
tests.py
File metadata and controls
295 lines (217 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
#
# Copyright (C) 2024-2025 Denis <denis@nzbget.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the program. If not, see <https://www.gnu.org/licenses/>.
#
import sys
from os.path import dirname
import os
import subprocess
import unittest
import shutil
import json
SUCCESS = 93
ERROR = 94
NONE = 95
sevenzip = os.environ.get("7z", "7z")
sevenzip_args = "e -aos"
unrar = os.environ.get("unrar", "unrar")
unrar_args = "e -idp -ai -o-"
root = dirname(__file__)
test_data_dir = root + "/test_data"
tmp_dir = root + "/tmp"
test_zips = ["test1.zip", "test2.zip", "test3.zip"]
test_partitioned_zips = ["test4.zip", "test4.z01", "test4.z02"]
test_rars = ["test1.rar", "test2.rar", "test3.rar"]
test_partitioned_rars = ["test4.r01", "test4.r02", "test4.r03"]
result_files = [tmp_dir + "/test1.txt", tmp_dir + "/test2.txt", tmp_dir + "/test3.txt"]
test_partitioned_result_files = ["test4.bin"]
test_partitioned_result_files = [tmp_dir + "/test4.bin"]
host = "127.0.0.1"
username = "TestUser"
password = "TestPassword"
port = "6789"
def get_python():
if os.name == "nt":
return "python"
return "python3"
def run_script():
sys.stdout.flush()
proc = subprocess.Popen(
[get_python(), root + "/main.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=os.environ.copy(),
)
out, err = proc.communicate()
proc.pid
ret_code = proc.returncode
return (out.decode(), int(ret_code), err.decode())
def set_default_env():
# NZBGet global options
os.environ["NZBNA_EVENT"] = "FILE_DOWNLOADED"
os.environ["NZBPP_DIRECTORY"] = tmp_dir
os.environ["NZBOP_ARTICLECACHE"] = "8"
os.environ["NZBPO_PASSACTION"] = "PASSACTION"
os.environ["NZBOP_CONTROLPORT"] = port
os.environ["NZBOP_CONTROLIP"] = host
os.environ["NZBOP_CONTROLUSERNAME"] = username
os.environ["NZBOP_CONTROLPASSWORD"] = password
os.environ["NZBPR_PASSWORDDETECTOR_HASPASSWORD"] = "no"
os.environ["NZBOP_EXTENSIONS"] = ""
os.environ["NZBOP_UNPACK"] = "yes"
os.environ["NZBPP_TOTALSTATUS"] = "SUCCESS"
# script options
os.environ["NZBNA_CATEGORY"] = "Movies"
os.environ["NZBNA_DIRECTORY"] = tmp_dir
os.environ["NZBNA_NZBNAME"] = "TestNZB"
os.environ["NZBPR_FAKEDETECTOR_SORTED"] = "yes"
os.environ["NZBOP_TEMPDIR"] = tmp_dir
os.environ["NZBOP_SEVENZIPCMD"] = sevenzip
os.environ["NZBPO_SEVENZIPCMD"] = sevenzip
os.environ["NZBPO_SEVENZIPARGS"] = sevenzip_args
os.environ["NZBOP_UNRARCMD"] = unrar
os.environ["NZBPO_UNRARCMD"] = unrar
os.environ["NZBPO_UNRARARGS"] = unrar_args
os.environ["NZBPO_WAITTIME"] = "0"
os.environ["NZBPO_DELETELEFTOVER"] = "no"
os.environ["NZBOP_UNPACKCLEANUPDISK"] = "no"
class Tests(unittest.TestCase):
def test_sevenzip(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
set_default_env()
shutil.copytree(
test_data_dir,
tmp_dir,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns("*.r*"),
)
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_unrar(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
set_default_env()
shutil.copytree(
test_data_dir,
tmp_dir,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns("*.z*"),
)
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_sevenzip_with_empty_sevenzipcmd_option(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
set_default_env()
os.environ["NZBPO_SEVENZIPCMD"] = ""
shutil.copytree(
test_data_dir,
tmp_dir,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns("*.r*"),
)
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_unrar_with_empty_unrarcmd_option(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
set_default_env()
os.environ["NZBPO_UNRARCMD"] = ""
shutil.copytree(
test_data_dir,
tmp_dir,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns("*.z*"),
)
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_multiple_folders_zips(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
os.mkdir(tmp_dir)
set_default_env()
for index, zip in enumerate(test_zips):
os.mkdir(str(f"{tmp_dir}/{index}"))
shutil.copyfile(f"{test_data_dir}/{zip}", f"{tmp_dir}/{index}/{zip}")
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_multiple_folders_rars(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
os.mkdir(tmp_dir)
set_default_env()
for index, rar in enumerate(test_rars):
os.mkdir(str(f"{tmp_dir}/{index}"))
shutil.copyfile(f"{test_data_dir}/{rar}", f"{tmp_dir}/{index}/{rar}")
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_delete_leftovers_zips(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
os.mkdir(tmp_dir)
set_default_env()
os.environ["NZBPO_DELETELEFTOVER"] = "yes"
for zip in test_partitioned_zips:
shutil.copyfile(f"{test_data_dir}/{zip}", f"{tmp_dir}/{zip}")
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in test_partitioned_zips:
self.assertFalse(os.path.exists(file))
for file in test_partitioned_result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_delete_leftovers_rars(self):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
os.mkdir(tmp_dir)
set_default_env()
os.environ["NZBPO_DELETELEFTOVER"] = "yes"
for rar in test_partitioned_rars:
shutil.copyfile(f"{test_data_dir}/{rar}", f"{tmp_dir}/{rar}")
[_, code, _] = run_script()
self.assertEqual(code, SUCCESS)
for file in test_partitioned_rars:
self.assertFalse(os.path.exists(file))
for file in test_partitioned_result_files:
self.assertTrue(os.path.exists(file))
shutil.rmtree(tmp_dir)
def test_manifest(self):
with open(root + "/manifest.json", encoding="utf-8") as file:
try:
json.loads(file.read())
except ValueError as e:
self.fail("manifest.json is not valid.")
if __name__ == "__main__":
unittest.main()