Coverage for tests\test_cli.py: 100%
266 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-27 21:50 -0700
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-27 21:50 -0700
1#! /usr/bin/python3
2# -*- coding: utf-8 -*-
4##############################################################################
5# Copyright (C) 2021-current alexpdev
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18##############################################################################
19"""
20Testing functions for the command line interface.
21"""
23import os
24import sys
25import datetime
26import textwrap
28import pyben
29import pytest
31from tests import dir1, dir2, file1, filemeta1, metafile1, rmpath, tempfile
32from torrentfile import execute
33from torrentfile.__main__ import main
36def test_fix():
37 """
38 Test dir1 fixture is not None.
39 """
40 assert dir1 and dir2 and metafile1 and filemeta1 and file1
43@pytest.fixture()
44def folder(dir1):
45 """
46 Yield a folder object as fixture.
47 """
48 sfolder = str(dir1)
49 torrent = sfolder + ".torrent"
50 yield (sfolder, torrent)
51 rmpath(torrent)
54def test_cli_v1(folder):
55 """
56 Basic create torrent cli command.
57 """
58 folder, torrent = folder
59 args = ["torrentfile", "create", folder, "-o", torrent]
60 sys.argv = args
61 execute()
62 assert os.path.exists(torrent)
65def test_cli_v2(folder):
66 """
67 Create torrent v2 cli command.
68 """
69 folder, torrent = folder
70 args = [
71 "torrentfile",
72 "create",
73 folder,
74 "--meta-version",
75 "2",
76 "-o",
77 torrent,
78 ]
79 sys.argv = args
80 execute()
81 assert os.path.exists(torrent)
84def test_cli_v3(folder):
85 """
86 Create hybrid torrent cli command.
87 """
88 folder, torrent = folder
89 args = [
90 "torrentfile",
91 "create",
92 folder,
93 "--meta-version",
94 "3",
95 "-o",
96 torrent,
97 ]
98 sys.argv = args
99 execute()
100 assert os.path.exists(torrent)
103def test_cli_private(folder):
104 """
105 Test private cli flag.
106 """
107 folder, torrent = folder
108 args = ["torrentfile", "create", folder, "--private", "-o", torrent]
109 sys.argv = args
110 main()
111 meta = pyben.load(torrent)
112 assert "private" in meta["info"]
115@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
116@pytest.mark.parametrize("version", ["1", "2", "3"])
117def test_cli_piece_length(folder, piece_length, version):
118 """
119 Test piece length cli flag.
120 """
121 folder, torrent = folder
122 args = [
123 "torrentfile",
124 "-v",
125 "create",
126 folder,
127 "--piece-length",
128 str(piece_length),
129 "--meta-version",
130 version,
131 "--progress",
132 "0",
133 "-o",
134 torrent,
135 ]
136 sys.argv = args
137 execute()
138 meta = pyben.load(torrent)
139 assert meta["info"]["piece length"] == piece_length
142@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
143@pytest.mark.parametrize("version", ["1", "2", "3"])
144def test_cli_announce(folder, piece_length, version):
145 """
146 Test announce cli flag.
147 """
148 folder, torrent = folder
149 args = [
150 "torrentfile",
151 "create",
152 folder,
153 "--piece-length",
154 str(piece_length),
155 "--meta-version",
156 version,
157 "--announce",
158 "https://announce.org/tracker",
159 "-o",
160 torrent,
161 ]
162 sys.argv = args
163 execute()
164 meta = pyben.load(torrent)
165 assert meta["announce"] == "https://announce.org/tracker"
168@pytest.mark.parametrize("version", ["1", "2", "3"])
169def test_cli_announce_list(folder, version):
170 """
171 Test announce-list cli flag.
172 """
173 folder, torrent = folder
174 trackers = [
175 "https://announce.org/tracker",
176 "https://announce.net/tracker",
177 "https://tracker.net/announce",
178 ]
179 args = [
180 "torrentfile",
181 "create",
182 folder,
183 "--meta-version",
184 version,
185 "-o",
186 torrent,
187 "--announce",
188 ] + trackers
189 sys.argv = args
190 execute()
191 meta = pyben.load(torrent)
192 for url in trackers:
193 assert url in [j for i in meta["announce-list"] for j in i]
196@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
197@pytest.mark.parametrize("version", ["1", "2", "3"])
198def test_cli_comment(folder, piece_length, version):
199 """
200 Test comment cli flag.
201 """
202 folder, torrent = folder
203 args = [
204 "torrentfile",
205 "create",
206 folder,
207 "--piece-length",
208 str(piece_length),
209 "--meta-version",
210 version,
211 "--magnet",
212 "--comment",
213 "this is a comment",
214 "--progress",
215 "1",
216 "-o",
217 torrent,
218 ]
219 sys.argv = args
220 execute()
221 meta = pyben.load(torrent)
222 assert meta["info"]["comment"] == "this is a comment"
225@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
226@pytest.mark.parametrize("version", ["1", "2", "3"])
227def test_cli_outfile(dir1, piece_length, version):
228 """
229 Test outfile cli flag.
230 """
231 outfile = dir1 + "test.torrent"
232 args = [
233 "torrentfile",
234 "create",
235 dir1,
236 "--piece-length",
237 str(piece_length),
238 "--meta-version",
239 version,
240 "-o",
241 outfile,
242 "--prog",
243 "1",
244 ]
245 sys.argv = args
246 execute()
247 assert os.path.exists(outfile)
248 rmpath(outfile)
251@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
252@pytest.mark.parametrize("version", ["1", "2", "3"])
253def test_cli_creation_date(folder, piece_length, version):
254 """
255 Test if torrents created get an accurate timestamp.
256 """
257 folder, torrent = folder
258 args = [
259 "torrentfile",
260 "create",
261 folder,
262 "--piece-length",
263 str(piece_length),
264 "--meta-version",
265 version,
266 "--comment",
267 "this is a comment",
268 "-o",
269 torrent,
270 ]
271 sys.argv = args
272 execute()
273 meta = pyben.load(torrent)
274 num = float(meta["creation date"])
275 date = datetime.datetime.fromtimestamp(num)
276 now = datetime.datetime.now()
277 assert date.day == now.day
278 assert date.year == now.year
279 assert date.month == now.month
282@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
283@pytest.mark.parametrize("version", ["1", "2", "3"])
284def test_cli_created_by(folder, piece_length, version):
285 """
286 Test if created torrents recieve a created by field in meta info.
287 """
288 folder, torrent = folder
289 args = [
290 "torrentfile",
291 "-q",
292 "create",
293 folder,
294 "--piece-length",
295 str(piece_length),
296 "--meta-version",
297 version,
298 "--align",
299 "--comment",
300 "this is a comment",
301 "-o",
302 torrent,
303 ]
304 sys.argv = args
305 execute()
306 meta = pyben.load(torrent)
307 assert "torrentfile" in meta["created by"]
310@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
311@pytest.mark.parametrize("version", ["1", "2", "3"])
312def test_cli_web_seeds(folder, piece_length, version):
313 """
314 Test if created torrents recieve a web seeds field in meta info.
315 """
316 folder, torrent = folder
317 args = [
318 "torrentfile",
319 "create",
320 folder,
321 "--piece-length",
322 str(piece_length),
323 "--align",
324 "--meta-version",
325 version,
326 "--web-seed",
327 "https://webseed.url/1",
328 "https://webseed.url/2",
329 "https://webseed.url/3",
330 "-o",
331 torrent,
332 ]
333 sys.argv = args
334 execute()
335 meta = pyben.load(torrent)
336 assert "https://webseed.url/1" in meta["url-list"]
339@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
340@pytest.mark.parametrize("version", ["1", "2", "3"])
341def test_cli_with_debug(folder, piece_length, version):
342 """
343 Test debug mode cli flag.
344 """
345 folder, torrent = folder
346 args = [
347 "torrentfile",
348 "-v",
349 "create",
350 folder,
351 "--piece-length",
352 str(piece_length),
353 "--meta-version",
354 version,
355 "--comment",
356 "this is a comment",
357 "-o",
358 torrent,
359 ]
360 sys.argv = args
361 execute()
362 assert os.path.exists(torrent)
365@pytest.mark.parametrize("piece_length", [2**exp for exp in range(14, 21)])
366@pytest.mark.parametrize("version", ["1", "2", "3"])
367def test_cli_with_source(folder, piece_length, version):
368 """
369 Test source cli flag.
370 """
371 folder, torrent = folder
372 args = [
373 "torrentfile",
374 "create",
375 folder,
376 "--piece-length",
377 str(piece_length),
378 "--meta-version",
379 version,
380 "--source",
381 "somesource",
382 "-o",
383 torrent,
384 ]
385 sys.argv = args
386 execute()
387 meta = pyben.load(torrent)
388 assert meta["info"]["source"] == "somesource"
391def test_cli_help():
392 """
393 Test showing help notice cli flag.
394 """
395 args = ["-h"]
396 sys.argv = args
397 try:
398 assert execute()
399 except SystemExit:
400 assert True
403@pytest.mark.parametrize("version", ["1", "2", "3"])
404@pytest.mark.parametrize("progress", ["0", "1"])
405def test_cli_empty_files(dir2, version, progress):
406 """
407 Test creating torrent with empty files.
408 """
409 outfile = str(dir2) + ".torrent"
410 args = [
411 "torrentfile",
412 "create",
413 str(dir2),
414 "--meta-version",
415 version,
416 "--source",
417 "somesource",
418 "--prog",
419 progress,
420 "-o",
421 outfile,
422 ]
423 sys.argv = args
425 def walk(root, count):
426 """
427 Traverse directory to edit files.
428 """
429 if root.is_file():
430 with open(root, "wb") as _:
431 return 1
432 elif root.is_dir():
433 for item in root.iterdir():
434 if count >= 2:
435 break
436 count += walk(item, count)
437 return count
439 walk(dir2, 0)
440 execute()
441 assert os.path.exists(outfile)
442 rmpath(outfile)
445@pytest.mark.parametrize("ending", ["/", "\\"])
446def test_cli_slash_path(dir1, ending):
447 """
448 Test if output when path ends with a /.
449 """
450 outfile = str(dir1) + ".torrent"
451 if sys.platform != "win32" and ending == "\\": # pragma: nocover
452 ending = "/"
453 args = [
454 "torrentfile",
455 "create",
456 "-o",
457 outfile,
458 "-a",
459 "https://announce1.org",
460 "--private",
461 str(dir1) + ending,
462 ]
463 sys.argv = args
464 execute()
465 assert os.path.exists(outfile)
466 rmpath(outfile)
469@pytest.mark.parametrize("sep", ["/", "\\"])
470def test_cli_slash_outpath(dir1, sep):
471 """
472 Test if output when outpath ends with a /.
473 """
474 if sys.platform != "win32":
475 sep = "/" # pragma: nocover
476 parent = os.path.dirname(dir1) + sep
477 args = [
478 "torrentfile",
479 "create",
480 "-a",
481 "https://announce1.org",
482 "--private",
483 "-o",
484 parent,
485 str(dir1),
486 ]
487 sys.argv = args
488 execute()
489 outfile = str(dir1) + ".torrent"
490 assert os.path.exists(outfile)
491 rmpath(outfile)
494@pytest.mark.parametrize("flag",
495 ["-a", "--announce", "--web-seed", "--http-seed"])
496def test_cli_announce_path(dir1, flag):
497 """
498 Test CLI when path is placed after the trackers flag.
499 """
500 outfile = str(dir1) + ".torrent"
501 args = [
502 "torrentfile",
503 "create",
504 "-o",
505 outfile,
506 flag,
507 "https://announce1.org",
508 str(dir1),
509 ]
510 sys.argv = args
511 execute()
512 assert os.path.exists(outfile)
513 rmpath(outfile)
516@pytest.fixture()
517def build(dir2):
518 """Fixture for testing the build subcommand."""
519 dest = os.path.join(os.path.dirname(__file__), "dest")
520 if os.path.exists(dest):
521 rmpath(dest)
522 try:
523 os.makedirs(dest)
524 except FileExistsError: # pragma: nocover
525 rmpath(dest)
526 return os.path.dirname(dir2), dest, dir2
529@pytest.mark.parametrize("size", list(range(15, 19)))
530@pytest.mark.parametrize("version", [1, 2])
531def test_rebuild_subcommand(build, version, size):
532 """Test the rebuild CLI subcommand."""
533 basedir, dest, content = build
534 args = [
535 "torrentfile",
536 "create",
537 str(content),
538 "--meta-version",
539 str(version),
540 "--piece-length",
541 str(size),
542 "-o",
543 str(content) + ".torrent",
544 ]
545 sys.argv = args
546 execute()
547 args = ["torrentfile", "rebuild", "-m", basedir, "-c", basedir, "-d", dest]
548 sys.argv = args
549 counter = execute()
550 assert counter > 0
553def test_empty_maker(dir1):
554 """Test empty create cli."""
555 args = ["torrentfile", "create", dir1, "-o", dir1 + ".torrent"]
556 sys.argv = args
557 execute()
558 assert os.path.exists(dir1 + ".torrent")
559 rmpath(dir1 + ".torrent")
562def test_rename():
563 """Test the rename command."""
564 tfile = str(tempfile())
565 args = ["torrentfile", "create", tfile, "-o", tfile + ".torrent"]
566 sys.argv = args
567 execute()
568 assert os.path.exists(tfile + ".torrent")
569 parent = os.path.dirname(tfile)
570 temp_path = os.path.join(parent, "renamed.torrent")
571 os.rename(tfile + ".torrent", temp_path)
572 print(os.listdir(parent))
573 args = ["torrentfile", "rename", temp_path]
574 sys.argv = args
575 execute()
576 assert os.path.exists(tfile + ".torrent")
577 assert not os.path.exists(temp_path)
578 rmpath(tfile + ".torrent", tfile)
581@pytest.mark.parametrize("version", ["1", "2", "3"])
582def test_cli_default_command(folder, version):
583 """Test default command by ommitting command."""
584 folder, torrent = folder
585 args = [
586 "torrentfile",
587 "-q",
588 folder,
589 "--meta-version",
590 version,
591 "-o",
592 torrent,
593 ]
594 sys.argv = args
595 execute()
596 assert os.path.exists(torrent)
599def test_cli_configfile(folder):
600 """Test config cli parameter."""
601 folder, _ = folder
602 args = ["torrentfile", "create", "--config", folder]
603 sys.argv = args
604 try:
605 execute()
606 except FileNotFoundError:
607 assert True
610@pytest.fixture
611def configfile():
612 """Test fixture for the configfile parameter."""
613 config = """
614 [config]
615 meta-version = 3
616 piece-length = 15
617 announce = example
618 private = true
619 source = example
620 comment = comment example
621 """
622 path = os.path.join(os.path.dirname(__file__), "torrentfile.ini")
623 with open(path, "wt", encoding="utf8") as fd:
624 fd.write(textwrap.dedent(config))
625 yield path
626 rmpath(path)
629def test_cli_configfile_params(configfile, folder):
630 """Testing parameters with config file option."""
631 folder, torrent = folder
632 args = [
633 "torrentfile",
634 "create",
635 "--config",
636 "--config-path",
637 configfile,
638 "-o",
639 torrent,
640 folder,
641 ]
642 sys.argv = args
643 execute()
644 meta = pyben.load(torrent)
645 assert meta["info"]["private"] == 1
646 assert meta["info"]["source"] == "example"
647 assert meta["info"]["piece length"] == 2**15
648 assert meta["announce"] == "example"
649 assert meta["info"]["meta version"] == 2