Coverage for tests\test_rebuild.py: 100%
97 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 rebuild sub-action commands from command line args.
21"""
22import os
24import pyben
25import pytest
27from tests import dir1, file1, file2, filemeta1, filemeta2, rmpath, tempfile
28from torrentfile.commands import rebuild
29from torrentfile.hasher import FileHasher, HasherHybrid, HasherV2
30from torrentfile.rebuild import Assembler
31from torrentfile.torrent import TorrentAssembler, TorrentFile
34def test_fix():
35 """Test that the fixtures import properly."""
36 assert filemeta2 and dir1 and file1 and filemeta1 and file2
39def create_torrentfile(path, creator, dest, piece_length):
40 """
41 Create a new torrent metafile based on the provided parameters.
42 """
43 torrent = creator(path=path, outfile=dest, piece_length=piece_length)
44 data = torrent.write()
45 return data
48def create_dest():
49 """
50 Create the destination directory for testing the rebuild function.
51 """
52 parent = os.path.dirname(__file__)
53 dest = os.path.join(parent, "dest2")
54 if os.path.exists(dest):
55 rmpath(dest)
56 os.mkdir(dest)
57 return dest
60@pytest.mark.parametrize("creator", [TorrentFile, TorrentAssembler])
61@pytest.mark.parametrize("size", list(range(15, 19)))
62def test_rebuilder_with_dir(dir1, creator, size):
63 """
64 Test that the rebuilder works with full directories.
65 """
66 dest = create_dest()
67 contents = [os.path.dirname(dir1)]
68 outfile = dir1 + ".torrent"
69 create_torrentfile(dir1, creator, outfile, size)
70 assembler = Assembler(contents, contents, dest)
71 counter = assembler.assemble_torrents()
72 assert counter > 0
75def get_params(path):
76 """
77 Shortcut method for building the parameters provided by fixtures.
78 """
79 base = os.path.dirname(path)
80 out = os.path.join(os.path.dirname(base), "dest")
81 if os.path.exists(out):
82 rmpath(out) # pragma: nocover
83 os.mkdir(out)
84 return ([path], [base], out)
87@pytest.fixture()
88def single1(filemeta1):
89 """Test fixture for testing the build subcommand."""
90 params = get_params(filemeta1)
91 return params
94@pytest.fixture()
95def single2(filemeta2):
96 """Test fixture testing build with mismatched file size."""
97 params = get_params(filemeta2)
98 return params
101def test_single_file(single1):
102 """
103 Test functionality of single file torrent and single torrent.
104 """
105 assembler = Assembler(*single1)
106 counter = assembler.assemble_torrents()
107 assert counter > 0
110def test_single_file_smaller(single2):
111 """
112 Test functionality of single file torrent and single torrent.
113 """
114 name = pyben.load(single2[0][0])["info"]["name"]
115 contents = os.path.join(single2[1][0], name)
116 with open(contents, "rb") as content:
117 data = content.read()
118 with open(contents, "wb") as content:
119 content.write(data[:len(data) // 2])
120 assembler = Assembler(*single2)
121 counter = assembler.assemble_torrents()
122 assert counter == 0
125def test_wrong_path():
126 """Test rebuild command with incorrect paths."""
128 class Namespace:
129 """
130 Emulates the behaviour of argparse.Namespace.
131 """
133 metafiles = "/non/existent/path"
134 destination = "/non/existing/path"
135 contents = "/non/existing/path"
137 try:
138 rebuild(Namespace)
139 except FileNotFoundError:
140 assert True
143@pytest.mark.parametrize("size", [2**i for i in range(14, 21)])
144def test_file1_hashers(file2, size):
145 """Test that all three of the version2 hashers produce the same result."""
146 hasher1 = HasherHybrid(file2, size, progress=1)
147 hasher2 = HasherV2(file2, size, progress=1)
148 hasher3 = FileHasher(file2, size, progress=1)
149 _ = list(hasher3)
150 lst = [hasher1.root, hasher2.root, hasher3.root]
151 print(lst)
152 assert len(set(lst)) == 1
155@pytest.fixture(scope="package")
156def dextra():
157 """Text fixture for creating testing directory."""
158 paths = [
159 "dir3/file1.bin",
160 "dir3/subdir1/file2.xls",
161 "dir3/subdir1/file3.mov",
162 "dir3/subdir2/file4.txt",
163 "dir3/subdir2/file5.md",
164 "dir3/file6.png",
165 "dir3/file7.nfo",
166 ]
167 temps, start = [], 13
168 for path in paths:
169 temp = tempfile(path=path, exp=start)
170 start += 1
171 temps.append(temp)
172 return os.path.commonpath(temps)
175@pytest.mark.parametrize("creator", [TorrentFile, TorrentAssembler])
176@pytest.mark.parametrize("size", list(range(15, 19)))
177def test_rebuild_extra_dir(dextra, creator, size):
178 """Test rebuild with dir2 and an extra smaller file."""
179 dest = create_dest()
180 contents = [os.path.dirname(dextra)]
181 outfile = str(dextra) + ".torrent"
182 create_torrentfile(dextra, creator, outfile, size)
183 assembler = Assembler(contents, contents, dest)
184 counter = assembler.assemble_torrents()
185 assert counter > 0