| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright 2019-2026 Mike Fährmann |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License version 2 as |
| 8 |
# published by the Free Software Foundation. |
| 9 |
|
| 10 |
import os |
| 11 |
import sys |
| 12 |
import unittest |
| 13 |
from unittest.mock import Mock, mock_open, patch, call |
| 14 |
|
| 15 |
import shutil |
| 16 |
import logging |
| 17 |
import zipfile |
| 18 |
import tempfile |
| 19 |
import collections |
| 20 |
from datetime import datetime |
| 21 |
|
| 22 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 23 |
from gallery_dl import extractor, output, path, util, exception # noqa E402 |
| 24 |
from gallery_dl import postprocessor, config, archive, dt # noqa E402 |
| 25 |
from gallery_dl.postprocessor.common import PostProcessor # noqa E402 |
| 26 |
|
| 27 |
|
| 28 |
class MockPostprocessorModule(Mock): |
| 29 |
__postprocessor__ = "mock" |
| 30 |
|
| 31 |
|
| 32 |
class FakeJob(): |
| 33 |
|
| 34 |
def __init__(self, extr=extractor.find("generic:https://example.org/")): |
| 35 |
extr.directory_fmt = ("{category}",) |
| 36 |
self.extractor = extr |
| 37 |
self.pathfmt = path.PathFormat(extr) |
| 38 |
self.out = output.NullOutput() |
| 39 |
self.get_logger = logging.getLogger |
| 40 |
self.hooks = collections.defaultdict(list) |
| 41 |
self.status = 0 |
| 42 |
|
| 43 |
def register_hooks(self, hooks, options=None): |
| 44 |
for hook, callback in hooks.items(): |
| 45 |
self.hooks[hook].append(callback) |
| 46 |
|
| 47 |
|
| 48 |
class TestPostprocessorModule(unittest.TestCase): |
| 49 |
|
| 50 |
def setUp(self): |
| 51 |
postprocessor._cache.clear() |
| 52 |
|
| 53 |
def test_find(self): |
| 54 |
for name in (postprocessor.modules): |
| 55 |
if name == "fs": |
| 56 |
name = "filesystem" |
| 57 |
cls = postprocessor.find(name) |
| 58 |
self.assertEqual(cls.__name__, f"{name.capitalize()}PP") |
| 59 |
self.assertIs(cls.__base__, PostProcessor) |
| 60 |
|
| 61 |
self.assertEqual(postprocessor.find("foo"), None) |
| 62 |
self.assertEqual(postprocessor.find(1234) , None) |
| 63 |
self.assertEqual(postprocessor.find(None) , None) |
| 64 |
|
| 65 |
@patch("builtins.__import__") |
| 66 |
def test_cache(self, import_module): |
| 67 |
import_module.return_value = MockPostprocessorModule() |
| 68 |
|
| 69 |
for name in (postprocessor.modules): |
| 70 |
postprocessor.find(name) |
| 71 |
self.assertEqual(import_module.call_count, len(postprocessor.modules)) |
| 72 |
|
| 73 |
# no new calls to import_module |
| 74 |
for name in (postprocessor.modules): |
| 75 |
postprocessor.find(name) |
| 76 |
self.assertEqual(import_module.call_count, len(postprocessor.modules)) |
| 77 |
|
| 78 |
|
| 79 |
class BasePostprocessorTest(unittest.TestCase): |
| 80 |
|
| 81 |
@classmethod |
| 82 |
def setUpClass(cls): |
| 83 |
cls.dir = tempfile.TemporaryDirectory() |
| 84 |
config.clear() |
| 85 |
config.set((), "base-directory", cls.dir.name) |
| 86 |
cls.job = FakeJob() |
| 87 |
|
| 88 |
@classmethod |
| 89 |
def tearDownClass(cls): |
| 90 |
cls.dir.cleanup() |
| 91 |
config.clear() |
| 92 |
|
| 93 |
def tearDown(self): |
| 94 |
self.job.hooks.clear() |
| 95 |
self.job.status = 0 |
| 96 |
|
| 97 |
def _create(self, options=None, data=None): |
| 98 |
kwdict = {"category": "test", "filename": "file", "extension": "ext"} |
| 99 |
if options is None: |
| 100 |
options = {} |
| 101 |
if data is not None: |
| 102 |
kwdict.update(data) |
| 103 |
|
| 104 |
self.pathfmt = self.job.pathfmt |
| 105 |
self.pathfmt.set_directory(kwdict) |
| 106 |
self.pathfmt.set_filename(kwdict) |
| 107 |
self.pathfmt.build_path() |
| 108 |
|
| 109 |
pp = postprocessor.find(self.__class__.__name__[:-4].lower()) |
| 110 |
return pp(self.job, options) |
| 111 |
|
| 112 |
def _trigger(self, events=None): |
| 113 |
for event in (events or ("prepare", "file")): |
| 114 |
for callback in self.job.hooks[event]: |
| 115 |
callback(self.pathfmt) |
| 116 |
|
| 117 |
def _output(self, mock): |
| 118 |
return "".join( |
| 119 |
call[1][0] |
| 120 |
for call in mock.mock_calls |
| 121 |
if call[0].endswith("write") |
| 122 |
) |
| 123 |
|
| 124 |
|
| 125 |
class ActionsTest(BasePostprocessorTest): |
| 126 |
|
| 127 |
def tearDown(self): |
| 128 |
super().tearDown() |
| 129 |
util.FLAGS.clear() |
| 130 |
|
| 131 |
def test_raises(self): |
| 132 |
self._create({"action": "raise AbortExtraction foobar"}) |
| 133 |
|
| 134 |
with self.assertRaises(exception.AbortExtraction) as cm: |
| 135 |
self._trigger() |
| 136 |
|
| 137 |
self.assertEqual(str(cm.exception), "foobar") |
| 138 |
|
| 139 |
def test_print(self): |
| 140 |
self._create({"action": "print Hello World"}) |
| 141 |
|
| 142 |
with patch("sys.stdout") as m: |
| 143 |
self._trigger() |
| 144 |
|
| 145 |
self.assertEqual(self._output(m), "Hello World\n") |
| 146 |
|
| 147 |
def test_status(self): |
| 148 |
self._create({"action": "status = 123"}) |
| 149 |
|
| 150 |
self.assertEqual(self.job.status, 0) |
| 151 |
self._trigger() |
| 152 |
self.assertEqual(self.job.status, 123) |
| 153 |
|
| 154 |
def test_flag_clear(self): |
| 155 |
self._create({"action": "flag download clear"}) |
| 156 |
|
| 157 |
util.FLAGS.DOWNLOAD = "stop" |
| 158 |
self._trigger() |
| 159 |
self.assertEqual(util.FLAGS.DOWNLOAD, None) |
| 160 |
self._trigger() |
| 161 |
self.assertEqual(util.FLAGS.DOWNLOAD, None) |
| 162 |
|
| 163 |
def test_flag_toggle(self): |
| 164 |
self._create({"action": "flag download toggle"}) |
| 165 |
|
| 166 |
self.assertEqual(util.FLAGS.DOWNLOAD, None) |
| 167 |
self._trigger() |
| 168 |
self.assertEqual(util.FLAGS.DOWNLOAD, "stop") |
| 169 |
self._trigger() |
| 170 |
self.assertEqual(util.FLAGS.DOWNLOAD, None) |
| 171 |
self._trigger() |
| 172 |
self.assertEqual(util.FLAGS.DOWNLOAD, "stop") |
| 173 |
|
| 174 |
|
| 175 |
class ClassifyTest(BasePostprocessorTest): |
| 176 |
|
| 177 |
def test_classify_default(self): |
| 178 |
pp = self._create() |
| 179 |
|
| 180 |
self.assertEqual(pp.mapping, { |
| 181 |
ext: directory |
| 182 |
for directory, exts in pp.DEFAULT_MAPPING.items() |
| 183 |
for ext in exts |
| 184 |
}) |
| 185 |
|
| 186 |
self.assertEqual(pp.directory, "") |
| 187 |
self._trigger(("post",)) |
| 188 |
self.assertEqual(pp.directory, self.pathfmt.directory) |
| 189 |
|
| 190 |
self.pathfmt.set_extension("jpg") |
| 191 |
self._trigger(("prepare",)) |
| 192 |
self.pathfmt.build_path() |
| 193 |
path = os.path.join(self.dir.name, "test", "Pictures") |
| 194 |
self.assertEqual(self.pathfmt.path, f"{path}/file.jpg") |
| 195 |
self.assertEqual(self.pathfmt.realpath, f"{path}/file.jpg") |
| 196 |
|
| 197 |
self.pathfmt.set_extension("mp4") |
| 198 |
self._trigger(("prepare",)) |
| 199 |
self.pathfmt.build_path() |
| 200 |
path = os.path.join(self.dir.name, "test", "Video") |
| 201 |
self.assertEqual(self.pathfmt.path, f"{path}/file.mp4") |
| 202 |
self.assertEqual(self.pathfmt.realpath, f"{path}/file.mp4") |
| 203 |
|
| 204 |
def test_classify_noop(self): |
| 205 |
pp = self._create() |
| 206 |
rp = self.pathfmt.realpath |
| 207 |
|
| 208 |
self.assertEqual(pp.directory, "") |
| 209 |
self._trigger(("post",)) |
| 210 |
self._trigger(("prepare",)) |
| 211 |
|
| 212 |
self.assertEqual(pp.directory, self.pathfmt.directory) |
| 213 |
self.assertEqual(self.pathfmt.path, rp) |
| 214 |
self.assertEqual(self.pathfmt.realpath, rp) |
| 215 |
|
| 216 |
def test_classify_custom(self): |
| 217 |
pp = self._create({"mapping": { |
| 218 |
"foo/bar": ["foo", "bar"], |
| 219 |
}}) |
| 220 |
|
| 221 |
self.assertEqual(pp.mapping, { |
| 222 |
"foo": "foo/bar", |
| 223 |
"bar": "foo/bar", |
| 224 |
}) |
| 225 |
|
| 226 |
self.assertEqual(pp.directory, "") |
| 227 |
self._trigger(("post",)) |
| 228 |
self.assertEqual(pp.directory, self.pathfmt.directory) |
| 229 |
|
| 230 |
self.pathfmt.set_extension("foo") |
| 231 |
self._trigger(("prepare",)) |
| 232 |
self.pathfmt.build_path() |
| 233 |
path = os.path.join(self.dir.name, "test", "foo", "bar") |
| 234 |
self.assertEqual(self.pathfmt.path, f"{path}/file.foo") |
| 235 |
self.assertEqual(self.pathfmt.realpath, f"{path}/file.foo") |
| 236 |
|
| 237 |
|
| 238 |
class DirectoryTest(BasePostprocessorTest): |
| 239 |
|
| 240 |
def test_default(self): |
| 241 |
self._create() |
| 242 |
|
| 243 |
path = os.path.join(self.dir.name, "test") |
| 244 |
self.assertEqual(self.pathfmt.realdirectory, f"{path}/") |
| 245 |
self.assertEqual(self.pathfmt.realpath, f"{path}/file.ext") |
| 246 |
|
| 247 |
self.pathfmt.kwdict["category"] = "custom" |
| 248 |
self._trigger() |
| 249 |
|
| 250 |
path = os.path.join(self.dir.name, "custom") |
| 251 |
self.assertEqual(self.pathfmt.realdirectory, f"{path}/") |
| 252 |
self.pathfmt.build_path() |
| 253 |
self.assertEqual(self.pathfmt.realpath, f"{path}/file.ext") |
| 254 |
|
| 255 |
|
| 256 |
class ExecTest(BasePostprocessorTest): |
| 257 |
|
| 258 |
def test_command_string(self): |
| 259 |
self._create({ |
| 260 |
"command": "echo {} {_path} {_path_unc} {_temppath} " |
| 261 |
"{_directory} {_directory_unc} {_filename} " |
| 262 |
"&& rm {};", |
| 263 |
}) |
| 264 |
|
| 265 |
with patch("gallery_dl.util.Popen") as p: |
| 266 |
i = Mock() |
| 267 |
i.wait.return_value = 0 |
| 268 |
p.return_value = i |
| 269 |
self._trigger(("after",)) |
| 270 |
|
| 271 |
p.assert_called_once_with( |
| 272 |
(f"echo " |
| 273 |
f"{self.pathfmt.path} " |
| 274 |
f"{self.pathfmt.path} " |
| 275 |
f"{self.pathfmt.realpath} " |
| 276 |
f"{self.pathfmt.temppath} " |
| 277 |
f"{self.pathfmt.directory} " |
| 278 |
f"{self.pathfmt.realdirectory} " |
| 279 |
f"{self.pathfmt.filename} " |
| 280 |
f"&& rm {self.pathfmt.path};"), |
| 281 |
shell=True, |
| 282 |
creationflags=0, |
| 283 |
start_new_session=False, |
| 284 |
stdout=None, stderr=None, |
| 285 |
) |
| 286 |
i.wait.assert_called_once_with() |
| 287 |
|
| 288 |
def test_command_list(self): |
| 289 |
self._create({ |
| 290 |
"command": ["~/script.sh", "{category}", |
| 291 |
"\fE _directory.upper()"], |
| 292 |
}) |
| 293 |
|
| 294 |
with patch("gallery_dl.util.Popen") as p: |
| 295 |
i = Mock() |
| 296 |
i.wait.return_value = 0 |
| 297 |
p.return_value = i |
| 298 |
self._trigger(("after",)) |
| 299 |
|
| 300 |
p.assert_called_once_with( |
| 301 |
[ |
| 302 |
os.path.expanduser("~/script.sh"), |
| 303 |
self.pathfmt.kwdict["category"], |
| 304 |
self.pathfmt.realdirectory.upper(), |
| 305 |
], |
| 306 |
shell=False, |
| 307 |
creationflags=0, |
| 308 |
start_new_session=False, |
| 309 |
stdout=None, stderr=None, |
| 310 |
) |
| 311 |
|
| 312 |
def test_command_many(self): |
| 313 |
self._create({ |
| 314 |
"commands": [ |
| 315 |
"echo {} {_path} {_temppath} {_directory} {_filename} " |
| 316 |
"&& rm {};", |
| 317 |
["~/script.sh", "{category}", "\fE _directory.upper()"], |
| 318 |
] |
| 319 |
}) |
| 320 |
|
| 321 |
with patch("gallery_dl.util.Popen") as p: |
| 322 |
i = Mock() |
| 323 |
i.wait.return_value = 0 |
| 324 |
p.return_value = i |
| 325 |
self._trigger(("after",)) |
| 326 |
|
| 327 |
self.assertEqual(p.call_args_list, [ |
| 328 |
call( |
| 329 |
(f"echo " |
| 330 |
f"{self.pathfmt.realpath} " |
| 331 |
f"{self.pathfmt.realpath} " |
| 332 |
f"{self.pathfmt.temppath} " |
| 333 |
f"{self.pathfmt.realdirectory} " |
| 334 |
f"{self.pathfmt.filename} " |
| 335 |
f"&& rm {self.pathfmt.realpath};"), |
| 336 |
shell=True, |
| 337 |
creationflags=0, |
| 338 |
start_new_session=False, |
| 339 |
stdout=None, stderr=None, |
| 340 |
), |
| 341 |
call( |
| 342 |
[ |
| 343 |
os.path.expanduser("~/script.sh"), |
| 344 |
self.pathfmt.kwdict["category"], |
| 345 |
self.pathfmt.realdirectory.upper(), |
| 346 |
], |
| 347 |
shell=False, |
| 348 |
creationflags=0, |
| 349 |
start_new_session=False, |
| 350 |
stdout=None, stderr=None, |
| 351 |
), |
| 352 |
]) |
| 353 |
|
| 354 |
def test_command_returncode(self): |
| 355 |
self._create({ |
| 356 |
"command": "echo {}", |
| 357 |
}) |
| 358 |
|
| 359 |
with patch("gallery_dl.util.Popen") as p: |
| 360 |
i = Mock() |
| 361 |
i.wait.return_value = 123 |
| 362 |
p.return_value = i |
| 363 |
|
| 364 |
with self.assertLogs() as log: |
| 365 |
self._trigger(("after",)) |
| 366 |
|
| 367 |
msg = (f"WARNING:postprocessor.exec:" |
| 368 |
f"'echo {self.pathfmt.realpath}' " |
| 369 |
f"returned with non-zero exit status (123)") |
| 370 |
self.assertEqual(log.output[0], msg) |
| 371 |
|
| 372 |
def test_async(self): |
| 373 |
self._create({ |
| 374 |
"async" : True, |
| 375 |
"command": "echo {}", |
| 376 |
}) |
| 377 |
|
| 378 |
with patch("gallery_dl.util.Popen") as p: |
| 379 |
i = Mock() |
| 380 |
p.return_value = i |
| 381 |
self._trigger(("after",)) |
| 382 |
|
| 383 |
self.assertTrue(p.called) |
| 384 |
self.assertFalse(i.wait.called) |
| 385 |
|
| 386 |
@unittest.skipIf(util.WINDOWS, "not POSIX") |
| 387 |
def test_session_posix(self): |
| 388 |
self._create({ |
| 389 |
"session": True, |
| 390 |
"command": ["echo", "foobar"], |
| 391 |
}) |
| 392 |
|
| 393 |
with patch("gallery_dl.util.Popen") as p: |
| 394 |
i = Mock() |
| 395 |
i.wait.return_value = 0 |
| 396 |
p.return_value = i |
| 397 |
self._trigger(("after",)) |
| 398 |
|
| 399 |
p.assert_called_once_with( |
| 400 |
["echo", "foobar"], |
| 401 |
shell=False, |
| 402 |
creationflags=0, |
| 403 |
start_new_session=True, |
| 404 |
stdout=None, stderr=None, |
| 405 |
) |
| 406 |
i.wait.assert_called_once_with() |
| 407 |
|
| 408 |
@unittest.skipIf(not util.WINDOWS, "not Windows") |
| 409 |
def test_session_windows(self): |
| 410 |
self._create({ |
| 411 |
"session": True, |
| 412 |
"command": ["echo", "foobar"], |
| 413 |
}) |
| 414 |
|
| 415 |
with patch("gallery_dl.util.Popen") as p: |
| 416 |
i = Mock() |
| 417 |
i.wait.return_value = 0 |
| 418 |
p.return_value = i |
| 419 |
self._trigger(("after",)) |
| 420 |
|
| 421 |
import subprocess |
| 422 |
p.assert_called_once_with( |
| 423 |
["echo", "foobar"], |
| 424 |
shell=False, |
| 425 |
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, |
| 426 |
start_new_session=False, |
| 427 |
stdout=None, stderr=None, |
| 428 |
) |
| 429 |
i.wait.assert_called_once_with() |
| 430 |
|
| 431 |
def test_archive(self): |
| 432 |
pp = self._create({ |
| 433 |
"command": ["echo", "foobar"], |
| 434 |
"archive": ":memory:", |
| 435 |
"event" : "finalize", |
| 436 |
}) |
| 437 |
|
| 438 |
self.assertIsInstance(pp.archive, archive.DownloadArchive) |
| 439 |
|
| 440 |
with patch.object(pp.archive, "add") as m_aa, \ |
| 441 |
patch.object(pp.archive, "close") as m_ac: |
| 442 |
self._trigger(("finalize",)) |
| 443 |
pp.archive.close() |
| 444 |
|
| 445 |
m_aa.assert_called_once_with(self.pathfmt.kwdict) |
| 446 |
m_ac.assert_called_once() |
| 447 |
|
| 448 |
def test_archive_fail(self): |
| 449 |
pp = self._create({ |
| 450 |
"command": ["echo", "failure"], |
| 451 |
"archive": ":memory:", |
| 452 |
"event" : "finalize", |
| 453 |
}) |
| 454 |
|
| 455 |
self.assertIsInstance(pp.archive, archive.DownloadArchive) |
| 456 |
|
| 457 |
with patch.object(pp.archive, "add") as m_aa, \ |
| 458 |
patch.object(pp.archive, "close") as m_ac, \ |
| 459 |
patch("gallery_dl.util.Popen") as p: |
| 460 |
p.return_value = i = Mock() |
| 461 |
i.wait.return_value = 123 |
| 462 |
with self.assertLogs(): |
| 463 |
self._trigger(("finalize",)) |
| 464 |
pp.archive.close() |
| 465 |
|
| 466 |
m_aa.assert_not_called() |
| 467 |
m_ac.assert_called_once() |
| 468 |
|
| 469 |
def test_verbose_string(self): |
| 470 |
self._create({ |
| 471 |
"command": "echo foo bar", |
| 472 |
"verbose": False, |
| 473 |
}) |
| 474 |
|
| 475 |
with patch("gallery_dl.util.Popen") as p, \ |
| 476 |
self.assertLogs(level=10) as log_info: |
| 477 |
i = Mock() |
| 478 |
i.wait.return_value = 123 |
| 479 |
p.return_value = i |
| 480 |
self._trigger(("after",)) |
| 481 |
|
| 482 |
msg = "DEBUG:postprocessor.exec:Running 'echo'" |
| 483 |
self.assertEqual(log_info.output[0], msg) |
| 484 |
self.assertIn("'echo' returned with non-zero ", log_info.output[1]) |
| 485 |
|
| 486 |
def test_verbose_list(self): |
| 487 |
self._create({ |
| 488 |
"command": ["echo", "foo", "bar"], |
| 489 |
"verbose": False, |
| 490 |
}) |
| 491 |
|
| 492 |
with patch("gallery_dl.util.Popen") as p, \ |
| 493 |
self.assertLogs(level=10) as log_info: |
| 494 |
i = Mock() |
| 495 |
i.wait.return_value = 123 |
| 496 |
p.return_value = i |
| 497 |
self._trigger(("after",)) |
| 498 |
|
| 499 |
msg = "DEBUG:postprocessor.exec:Running 'echo'" |
| 500 |
self.assertEqual(log_info.output[0], msg) |
| 501 |
self.assertIn("'echo' returned with non-zero ", log_info.output[1]) |
| 502 |
|
| 503 |
def test_opt_success(self): |
| 504 |
self._create({ |
| 505 |
"command": "echo foo bar", |
| 506 |
"success": "status = 11", |
| 507 |
}) |
| 508 |
|
| 509 |
self.assertEqual(self.job.status, 0) |
| 510 |
with patch("gallery_dl.util.Popen") as p: |
| 511 |
p.return_value = i = Mock() |
| 512 |
i.wait.return_value = 0 |
| 513 |
self._trigger(("after",)) |
| 514 |
self.assertEqual(self.job.status, 11) |
| 515 |
|
| 516 |
def test_opt_error(self): |
| 517 |
self._create({ |
| 518 |
"command": "echo foo bar", |
| 519 |
"success": "status = 11", |
| 520 |
"error" : "status = 23", |
| 521 |
}) |
| 522 |
|
| 523 |
self.assertEqual(self.job.status, 0) |
| 524 |
with patch("gallery_dl.util.Popen") as p, \ |
| 525 |
self.assertLogs(level=10) as log_info: |
| 526 |
p.return_value = i = Mock() |
| 527 |
i.wait.return_value = 1 # non-zero exit status |
| 528 |
self._trigger(("after",)) |
| 529 |
self.assertEqual(self.job.status, 23) |
| 530 |
self.assertIn("'echo foo bar' returned with non-zero ", |
| 531 |
log_info.output[1]) |
| 532 |
|
| 533 |
def test_opt_output(self): |
| 534 |
self._create({ |
| 535 |
"command": ["echo", "foobar"], |
| 536 |
"output" : False, |
| 537 |
}) |
| 538 |
|
| 539 |
with patch("gallery_dl.util.Popen") as p: |
| 540 |
p.return_value = i = Mock() |
| 541 |
i.wait.return_value = 0 |
| 542 |
self._trigger(("after",)) |
| 543 |
|
| 544 |
import subprocess |
| 545 |
p.assert_called_once_with( |
| 546 |
["echo", "foobar"], |
| 547 |
shell=False, |
| 548 |
stdout=subprocess.DEVNULL, |
| 549 |
stderr=subprocess.DEVNULL, |
| 550 |
creationflags=0, |
| 551 |
start_new_session=False, |
| 552 |
) |
| 553 |
|
| 554 |
|
| 555 |
class HashTest(BasePostprocessorTest): |
| 556 |
|
| 557 |
def test_default(self): |
| 558 |
self._create({}) |
| 559 |
|
| 560 |
with self.pathfmt.open() as fp: |
| 561 |
fp.write(b"Foo Bar\n") |
| 562 |
|
| 563 |
self._trigger() |
| 564 |
|
| 565 |
kwdict = self.pathfmt.kwdict |
| 566 |
self.assertEqual( |
| 567 |
"35c9c9c7c90ad764bae9e2623f522c24", kwdict["md5"], "md5") |
| 568 |
self.assertEqual( |
| 569 |
"14d3d804494ef4e57d72de63e4cfee761240471a", kwdict["sha1"], "sha1") |
| 570 |
|
| 571 |
def test_custom_hashes(self): |
| 572 |
self._create({"hashes": "sha256:a,sha512:b"}) |
| 573 |
|
| 574 |
with self.pathfmt.open() as fp: |
| 575 |
fp.write(b"Foo Bar\n") |
| 576 |
|
| 577 |
self._trigger() |
| 578 |
|
| 579 |
kwdict = self.pathfmt.kwdict |
| 580 |
self.assertEqual( |
| 581 |
"4775b55be17206445d7015a5fc7656f38a74b880670523c3b175455f885f2395", |
| 582 |
kwdict["a"], "sha256") |
| 583 |
self.assertEqual( |
| 584 |
"6028f9e6957f4ca929941318c4bba6258713fd5162f9e33bd10e1c456d252700" |
| 585 |
"3e1095b50736c4fd1e2deea152e3c8ecd5993462a747208e4d842659935a1c62", |
| 586 |
kwdict["b"], "sha512") |
| 587 |
|
| 588 |
def test_custom_hashes_dict(self): |
| 589 |
self._create({"hashes": {"a": "sha256", "b": "sha512"}}) |
| 590 |
|
| 591 |
with self.pathfmt.open() as fp: |
| 592 |
fp.write(b"Foo Bar\n") |
| 593 |
|
| 594 |
self._trigger() |
| 595 |
|
| 596 |
kwdict = self.pathfmt.kwdict |
| 597 |
self.assertEqual( |
| 598 |
"4775b55be17206445d7015a5fc7656f38a74b880670523c3b175455f885f2395", |
| 599 |
kwdict["a"], "sha256") |
| 600 |
self.assertEqual( |
| 601 |
"6028f9e6957f4ca929941318c4bba6258713fd5162f9e33bd10e1c456d252700" |
| 602 |
"3e1095b50736c4fd1e2deea152e3c8ecd5993462a747208e4d842659935a1c62", |
| 603 |
kwdict["b"], "sha512") |
| 604 |
|
| 605 |
def test_mode(self): |
| 606 |
self._create({"mode": "sha256,sha512"}) |
| 607 |
|
| 608 |
with self.pathfmt.open() as fp: |
| 609 |
fp.write(b"Foo Bar\n") |
| 610 |
|
| 611 |
self._trigger() |
| 612 |
|
| 613 |
kwdict = self.pathfmt.kwdict |
| 614 |
self.assertEqual( |
| 615 |
"4775b55be17206445d7015a5fc7656f38a74b880670523c3b175455f885f2395", |
| 616 |
kwdict["sha256"], "sha256") |
| 617 |
self.assertEqual( |
| 618 |
"6028f9e6957f4ca929941318c4bba6258713fd5162f9e33bd10e1c456d252700" |
| 619 |
"3e1095b50736c4fd1e2deea152e3c8ecd5993462a747208e4d842659935a1c62", |
| 620 |
kwdict["sha512"], "sha512") |
| 621 |
|
| 622 |
|
| 623 |
class MetadataTest(BasePostprocessorTest): |
| 624 |
|
| 625 |
def test_metadata_default(self): |
| 626 |
pp = self._create() |
| 627 |
|
| 628 |
# default arguments |
| 629 |
self.assertEqual(pp.write , pp._write_json) |
| 630 |
self.assertEqual(pp.extension, "json") |
| 631 |
self.assertTrue(callable(pp._json_encode)) |
| 632 |
|
| 633 |
def test_metadata_json(self): |
| 634 |
pp = self._create({ |
| 635 |
"mode" : "json", |
| 636 |
"extension" : "JSON", |
| 637 |
}, { |
| 638 |
"public" : "hello ワールド", |
| 639 |
"_private" : "foo バー", |
| 640 |
}) |
| 641 |
|
| 642 |
self.assertEqual(pp.write , pp._write_json) |
| 643 |
self.assertEqual(pp.extension, "JSON") |
| 644 |
self.assertTrue(callable(pp._json_encode)) |
| 645 |
|
| 646 |
with patch("builtins.open", mock_open()) as m: |
| 647 |
self._trigger() |
| 648 |
|
| 649 |
path = f"{self.pathfmt.realpath}.JSON" |
| 650 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 651 |
|
| 652 |
self.assertEqual(self._output(m), """{ |
| 653 |
"category": "test", |
| 654 |
"filename": "file", |
| 655 |
"extension": "ext", |
| 656 |
"public": "hello ワールド" |
| 657 |
} |
| 658 |
""") |
| 659 |
|
| 660 |
def test_metadata_json_options(self): |
| 661 |
pp = self._create({ |
| 662 |
"mode" : "json", |
| 663 |
"ascii" : True, |
| 664 |
"sort" : True, |
| 665 |
"separators": [",", " : "], |
| 666 |
"private" : True, |
| 667 |
"indent" : None, |
| 668 |
"open" : "a", |
| 669 |
"encoding" : "UTF-8", |
| 670 |
"newline" : "\r\n", |
| 671 |
"extension" : "JSON", |
| 672 |
}, { |
| 673 |
"public" : "hello ワールド", |
| 674 |
"_private" : "foo バー", |
| 675 |
}) |
| 676 |
|
| 677 |
self.assertEqual(pp.write , pp._write_json) |
| 678 |
self.assertEqual(pp.extension, "JSON") |
| 679 |
self.assertTrue(callable(pp._json_encode)) |
| 680 |
|
| 681 |
with patch("builtins.open", mock_open()) as m: |
| 682 |
self._trigger() |
| 683 |
|
| 684 |
path = f"{self.pathfmt.realpath}.JSON" |
| 685 |
m.assert_called_once_with(path, "a", encoding="UTF-8", newline='\r\n') |
| 686 |
# Since we mocked the call to open, |
| 687 |
# we don't actually see the effect of setting newline. |
| 688 |
self.assertEqual(self._output(m), """{\ |
| 689 |
"_private" : "foo \\u30d0\\u30fc",\ |
| 690 |
"category" : "test",\ |
| 691 |
"extension" : "ext",\ |
| 692 |
"filename" : "file",\ |
| 693 |
"public" : "hello \\u30ef\\u30fc\\u30eb\\u30c9"} |
| 694 |
""") |
| 695 |
|
| 696 |
def test_metadata_tags(self): |
| 697 |
pp = self._create( |
| 698 |
{"mode": "tags"}, |
| 699 |
{"tags": ["foo", "bar", "baz"]}, |
| 700 |
) |
| 701 |
self.assertEqual(pp.write, pp._write_tags) |
| 702 |
self.assertEqual(pp.extension, "txt") |
| 703 |
|
| 704 |
with patch("builtins.open", mock_open()) as m: |
| 705 |
self._trigger() |
| 706 |
|
| 707 |
path = f"{self.pathfmt.realpath}.txt" |
| 708 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 709 |
self.assertEqual(self._output(m), "foo\nbar\nbaz\n") |
| 710 |
|
| 711 |
def test_metadata_tags_split_1(self): |
| 712 |
self._create( |
| 713 |
{"mode": "tags"}, |
| 714 |
{"tags": "foo, bar, baz"}, |
| 715 |
) |
| 716 |
with patch("builtins.open", mock_open()) as m: |
| 717 |
self._trigger() |
| 718 |
self.assertEqual(self._output(m), "foo\nbar\nbaz\n") |
| 719 |
|
| 720 |
def test_metadata_tags_split_2(self): |
| 721 |
self._create( |
| 722 |
{"mode": "tags"}, |
| 723 |
{"tags": "foobar1 foobar2 foobarbaz"}, |
| 724 |
) |
| 725 |
with patch("builtins.open", mock_open()) as m: |
| 726 |
self._trigger() |
| 727 |
self.assertEqual(self._output(m), "foobar1\nfoobar2\nfoobarbaz\n") |
| 728 |
|
| 729 |
def test_metadata_tags_tagstring(self): |
| 730 |
self._create( |
| 731 |
{"mode": "tags"}, |
| 732 |
{"tag_string": "foo, bar, baz"}, |
| 733 |
) |
| 734 |
with patch("builtins.open", mock_open()) as m: |
| 735 |
self._trigger() |
| 736 |
self.assertEqual(self._output(m), "foo\nbar\nbaz\n") |
| 737 |
|
| 738 |
def test_metadata_tags_list_of_dict(self): |
| 739 |
self._create( |
| 740 |
{"mode": "tags"}, |
| 741 |
{"tags": [ |
| 742 |
{"g": "foobar1", "m": "foobar2", "u": True}, |
| 743 |
{"g": None, "m": "foobarbaz", "u": [3, 4]}, |
| 744 |
]}, |
| 745 |
) |
| 746 |
with patch("builtins.open", mock_open()) as m: |
| 747 |
self._trigger() |
| 748 |
self.assertEqual(self._output(m), "foobar1\nfoobar2\nfoobarbaz\n") |
| 749 |
|
| 750 |
def test_metadata_custom(self): |
| 751 |
def test(pp_info): |
| 752 |
pp = self._create(pp_info, {"foo": "bar"}) |
| 753 |
self.assertEqual(pp.write, pp._write_custom) |
| 754 |
self.assertEqual(pp.extension, "txt") |
| 755 |
self.assertTrue(pp._content_fmt) |
| 756 |
|
| 757 |
with patch("builtins.open", mock_open()) as m: |
| 758 |
self._trigger() |
| 759 |
self.assertEqual(self._output(m), "bar\nNone\n") |
| 760 |
self.job.hooks.clear() |
| 761 |
|
| 762 |
test({"mode": "custom", "content-format": "{foo}\n{missing}\n"}) |
| 763 |
test({"mode": "custom", "content-format": ["{foo}", "{missing}"]}) |
| 764 |
test({"mode": "custom", "format": "{foo}\n{missing}\n"}) |
| 765 |
test({"format": "{foo}\n{missing}\n"}) |
| 766 |
|
| 767 |
def test_metadata_mode_print(self): |
| 768 |
self._create( |
| 769 |
{"mode": "print", "format": "{foo}\n{missing}"}, |
| 770 |
{"foo": "bar"}, |
| 771 |
) |
| 772 |
|
| 773 |
with patch("sys.stdout") as m: |
| 774 |
self._trigger() |
| 775 |
|
| 776 |
self.assertEqual(self._output(m), "bar\nNone\n") |
| 777 |
|
| 778 |
def test_metadata_extfmt(self): |
| 779 |
pp = self._create({ |
| 780 |
"extension" : "ignored", |
| 781 |
"extension-format": "json", |
| 782 |
}) |
| 783 |
|
| 784 |
self.assertEqual(pp._filename, pp._filename_extfmt) |
| 785 |
|
| 786 |
with patch("builtins.open", mock_open()) as m: |
| 787 |
self._trigger() |
| 788 |
|
| 789 |
path = f"{self.pathfmt.realdirectory}file.json" |
| 790 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 791 |
|
| 792 |
def test_metadata_extfmt_2(self): |
| 793 |
self._create({ |
| 794 |
"extension-format": "{extension!u}-data:{category:Res/ES/}", |
| 795 |
}) |
| 796 |
|
| 797 |
self.pathfmt.prefix = "2." |
| 798 |
with patch("builtins.open", mock_open()) as m: |
| 799 |
self._trigger() |
| 800 |
|
| 801 |
path = f"{self.pathfmt.realdirectory}file.2.EXT-data:tESt" |
| 802 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 803 |
|
| 804 |
def test_metadata_directory(self): |
| 805 |
self._create({ |
| 806 |
"directory": "metadata", |
| 807 |
}) |
| 808 |
|
| 809 |
with patch("builtins.open", mock_open()) as m: |
| 810 |
self._trigger() |
| 811 |
|
| 812 |
path = f"{self.pathfmt.realdirectory}metadata/file.ext.json" |
| 813 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 814 |
|
| 815 |
def test_metadata_directory_2(self): |
| 816 |
self._create({ |
| 817 |
"directory" : "metadata////", |
| 818 |
"extension-format": "json", |
| 819 |
}) |
| 820 |
|
| 821 |
with patch("builtins.open", mock_open()) as m: |
| 822 |
self._trigger() |
| 823 |
|
| 824 |
path = f"{self.pathfmt.realdirectory}metadata/file.json" |
| 825 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 826 |
|
| 827 |
def test_metadata_directory_format(self): |
| 828 |
self._create( |
| 829 |
{"directory": ["..", "json", "\fE str(id // 500 * 500 + 500)"]}, |
| 830 |
{"id": 12345}, |
| 831 |
) |
| 832 |
|
| 833 |
with patch("builtins.open", mock_open()) as m: |
| 834 |
self._trigger() |
| 835 |
|
| 836 |
path = f"{self.pathfmt.realdirectory}../json/12500/file.ext.json" |
| 837 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 838 |
|
| 839 |
def test_metadata_directory_empty(self): |
| 840 |
self._create( |
| 841 |
{"directory": []}, |
| 842 |
) |
| 843 |
|
| 844 |
with patch("builtins.open", mock_open()) as m: |
| 845 |
self._trigger() |
| 846 |
|
| 847 |
path = f"{self.pathfmt.realdirectory}./file.ext.json" |
| 848 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 849 |
|
| 850 |
def test_metadata_basedirectory(self): |
| 851 |
self._create({"base-directory": True}) |
| 852 |
|
| 853 |
with patch("builtins.open", mock_open()) as m: |
| 854 |
self._trigger() |
| 855 |
|
| 856 |
path = f"{self.pathfmt.basedirectory}file.ext.json" |
| 857 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 858 |
|
| 859 |
def test_metadata_basedirectory_custom(self): |
| 860 |
self._create({ |
| 861 |
"base-directory": "/home/test", |
| 862 |
"directory": "meta", |
| 863 |
}) |
| 864 |
|
| 865 |
with patch("builtins.open", mock_open()) as m: |
| 866 |
self._trigger() |
| 867 |
|
| 868 |
path = "/home/test/meta/file.ext.json" |
| 869 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 870 |
|
| 871 |
def test_metadata_filename(self): |
| 872 |
self._create({ |
| 873 |
"filename" : "{category}_{filename}_/meta/\n\r.data", |
| 874 |
"extension-format": "json", |
| 875 |
}) |
| 876 |
|
| 877 |
with patch("builtins.open", mock_open()) as m: |
| 878 |
self._trigger() |
| 879 |
|
| 880 |
path = f"{self.pathfmt.realdirectory}test_file__meta_.data" |
| 881 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 882 |
|
| 883 |
def test_metadata_meta_path(self): |
| 884 |
self._create({ |
| 885 |
"metadata-path": "_meta_path", |
| 886 |
}) |
| 887 |
|
| 888 |
self._trigger() |
| 889 |
|
| 890 |
self.assertEqual(self.pathfmt.kwdict["_meta_path"], |
| 891 |
f"{self.pathfmt.realpath}.json") |
| 892 |
|
| 893 |
def test_metadata_stdout(self): |
| 894 |
self._create({"filename": "-", "indent": None, "sort": True}) |
| 895 |
|
| 896 |
with patch("sys.stdout") as m: |
| 897 |
self._trigger() |
| 898 |
|
| 899 |
self.assertEqual(self._output(m), """\ |
| 900 |
{"category": "test", "extension": "ext", "filename": "file"} |
| 901 |
""") |
| 902 |
|
| 903 |
def test_metadata_modify(self): |
| 904 |
kwdict = {"foo": 0, "bar": {"bax": 1, "bay": 2, "baz": 3, "ba2": {}}} |
| 905 |
self._create({ |
| 906 |
"mode": "modify", |
| 907 |
"fields": { |
| 908 |
"foo" : "{filename}-{foo!s}", |
| 909 |
"foo2" : "\fE bar['bax'] + 122", |
| 910 |
"bar[\"baz\"]" : "{_now}", |
| 911 |
"bar['ba2'][a]": "test", |
| 912 |
}, |
| 913 |
}, kwdict) |
| 914 |
|
| 915 |
pdict = self.pathfmt.kwdict |
| 916 |
self.assertIsNot(kwdict, pdict) |
| 917 |
self.assertEqual(pdict["foo"], kwdict["foo"]) |
| 918 |
self.assertEqual(pdict["bar"], kwdict["bar"]) |
| 919 |
|
| 920 |
self._trigger() |
| 921 |
|
| 922 |
self.assertEqual(pdict["foo"] , "file-0") |
| 923 |
self.assertEqual(pdict["foo2"], 123) |
| 924 |
self.assertEqual(pdict["bar"]["ba2"]["a"], "test") |
| 925 |
self.assertIsInstance(pdict["bar"]["baz"], datetime) |
| 926 |
|
| 927 |
def test_metadata_delete(self): |
| 928 |
kwdict = { |
| 929 |
"foo": 0, |
| 930 |
"bar": { |
| 931 |
"bax": 1, |
| 932 |
"bay": 2, |
| 933 |
"baz": {"a": 3, "b": 4}, |
| 934 |
}, |
| 935 |
} |
| 936 |
self._create({ |
| 937 |
"mode": "delete", |
| 938 |
"fields": ["foo", "bar['bax']", "bar[\"baz\"][a]"], |
| 939 |
}, kwdict) |
| 940 |
|
| 941 |
pdict = self.pathfmt.kwdict |
| 942 |
self.assertIsNot(kwdict, pdict) |
| 943 |
|
| 944 |
self.assertEqual(pdict["foo"], kwdict["foo"]) |
| 945 |
self.assertEqual(pdict["bar"], kwdict["bar"]) |
| 946 |
|
| 947 |
self._trigger() |
| 948 |
|
| 949 |
self.assertNotIn("foo", pdict) |
| 950 |
self.assertNotIn("bax", pdict["bar"]) |
| 951 |
self.assertNotIn("a", pdict["bar"]["baz"]) |
| 952 |
|
| 953 |
# no errors for deleted/undefined fields |
| 954 |
self._trigger() |
| 955 |
self.assertNotIn("foo", pdict) |
| 956 |
self.assertNotIn("bax", pdict["bar"]) |
| 957 |
self.assertNotIn("a", pdict["bar"]["baz"]) |
| 958 |
|
| 959 |
def test_metadata_option_skip(self): |
| 960 |
self._create({"skip": True}) |
| 961 |
|
| 962 |
with patch("builtins.open", mock_open()) as m, \ |
| 963 |
patch("os.path.exists") as e: |
| 964 |
e.return_value = True |
| 965 |
self._trigger() |
| 966 |
|
| 967 |
self.assertTrue(e.called) |
| 968 |
self.assertTrue(not m.called) |
| 969 |
self.assertTrue(not len(self._output(m))) |
| 970 |
|
| 971 |
with patch("builtins.open", mock_open()) as m, \ |
| 972 |
patch("os.path.exists") as e: |
| 973 |
e.return_value = False |
| 974 |
self._trigger() |
| 975 |
|
| 976 |
self.assertTrue(e.called) |
| 977 |
self.assertTrue(m.called) |
| 978 |
self.assertGreater(len(self._output(m)), 0) |
| 979 |
|
| 980 |
path = f"{self.pathfmt.realdirectory}file.ext.json" |
| 981 |
m.assert_called_once_with(path, "w", encoding="utf-8", newline=None) |
| 982 |
|
| 983 |
def test_metadata_option_skip_false(self): |
| 984 |
self._create({"skip": False}) |
| 985 |
|
| 986 |
with patch("builtins.open", mock_open()) as m, \ |
| 987 |
patch("os.path.exists") as e: |
| 988 |
self._trigger() |
| 989 |
|
| 990 |
self.assertTrue(not e.called) |
| 991 |
self.assertTrue(m.called) |
| 992 |
|
| 993 |
def test_metadata_option_newline(self): |
| 994 |
self._create({ |
| 995 |
"newline": "\r\n", |
| 996 |
"filename" : "data.json", |
| 997 |
"directory" : "", |
| 998 |
"base-directory": self.dir.name, |
| 999 |
}) |
| 1000 |
|
| 1001 |
self._trigger() |
| 1002 |
|
| 1003 |
path = os.path.join(self.dir.name, "data.json") |
| 1004 |
with open(path, newline="") as fp: |
| 1005 |
content = fp.read() |
| 1006 |
|
| 1007 |
self.assertEqual(content, """\ |
| 1008 |
{\r\n\ |
| 1009 |
"category": "test",\r\n\ |
| 1010 |
"filename": "file",\r\n\ |
| 1011 |
"extension": "ext"\r\n\ |
| 1012 |
}\r\n\ |
| 1013 |
""") |
| 1014 |
|
| 1015 |
def test_metadata_option_include(self): |
| 1016 |
self._create( |
| 1017 |
{"include": ["_private", "filename", "foo"], "sort": True}, |
| 1018 |
{"public": "hello ワールド", "_private": "foo バー"}, |
| 1019 |
) |
| 1020 |
|
| 1021 |
with patch("builtins.open", mock_open()) as m: |
| 1022 |
self._trigger() |
| 1023 |
|
| 1024 |
self.assertEqual(self._output(m), """{ |
| 1025 |
"_private": "foo バー", |
| 1026 |
"filename": "file" |
| 1027 |
} |
| 1028 |
""") |
| 1029 |
|
| 1030 |
def test_metadata_option_exclude(self): |
| 1031 |
self._create( |
| 1032 |
{"exclude": ["category", "filename", "foo"], "sort": True}, |
| 1033 |
{"public": "hello ワールド", "_private": "foo バー"}, |
| 1034 |
) |
| 1035 |
|
| 1036 |
with patch("builtins.open", mock_open()) as m: |
| 1037 |
self._trigger() |
| 1038 |
|
| 1039 |
self.assertEqual(self._output(m), """{ |
| 1040 |
"extension": "ext", |
| 1041 |
"public": "hello ワールド" |
| 1042 |
} |
| 1043 |
""") |
| 1044 |
|
| 1045 |
def test_metadata_option_empty_json(self): |
| 1046 |
self._create({"mode": "json", "include": "_"}) |
| 1047 |
|
| 1048 |
with patch("builtins.open", mock_open()) as m: |
| 1049 |
self._trigger() |
| 1050 |
|
| 1051 |
m.assert_not_called() |
| 1052 |
self.assertEqual(self._output(m), "") |
| 1053 |
|
| 1054 |
def test_metadata_option_empty_tags(self): |
| 1055 |
self._create({"mode": "tags"}) |
| 1056 |
|
| 1057 |
with patch("builtins.open", mock_open()) as m: |
| 1058 |
self._trigger() |
| 1059 |
|
| 1060 |
m.assert_not_called() |
| 1061 |
self.assertEqual(self._output(m), "") |
| 1062 |
|
| 1063 |
def test_metadata_option_empty_fmt(self): |
| 1064 |
self._create({"format": "{''}"}) |
| 1065 |
|
| 1066 |
with patch("builtins.open", mock_open()) as m: |
| 1067 |
self._trigger() |
| 1068 |
|
| 1069 |
m.assert_not_called() |
| 1070 |
self.assertEqual(self._output(m), "") |
| 1071 |
|
| 1072 |
def test_metadata_option_empty_true(self): |
| 1073 |
self._create({"empty": True, "format": "{''}"}) |
| 1074 |
|
| 1075 |
with patch("builtins.open", mock_open()) as m: |
| 1076 |
self._trigger() |
| 1077 |
|
| 1078 |
m.assert_called_once() |
| 1079 |
self.assertEqual(self._output(m), "") |
| 1080 |
|
| 1081 |
def test_archive(self): |
| 1082 |
pp = self._create({ |
| 1083 |
"archive": ":memory:", |
| 1084 |
"event" : "finalize", |
| 1085 |
}) |
| 1086 |
|
| 1087 |
self.assertIsInstance(pp.archive, archive.DownloadArchive) |
| 1088 |
|
| 1089 |
with patch.object(pp.archive, "add") as m_aa, \ |
| 1090 |
patch.object(pp.archive, "close") as m_ac: |
| 1091 |
self._trigger(("finalize",)) |
| 1092 |
pp.archive.close() |
| 1093 |
|
| 1094 |
m_aa.assert_called_once_with(self.pathfmt.kwdict) |
| 1095 |
m_ac.assert_called_once() |
| 1096 |
|
| 1097 |
|
| 1098 |
class MtimeTest(BasePostprocessorTest): |
| 1099 |
|
| 1100 |
def test_mtime_datetime(self): |
| 1101 |
self._create(None, {"date": datetime(1980, 1, 1)}) |
| 1102 |
self._trigger() |
| 1103 |
self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800) |
| 1104 |
|
| 1105 |
def test_mtime_timestamp(self): |
| 1106 |
self._create(None, {"date": 315532800}) |
| 1107 |
self._trigger() |
| 1108 |
self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800) |
| 1109 |
|
| 1110 |
def test_mtime_none(self): |
| 1111 |
self._create(None, {"date": None}) |
| 1112 |
self._trigger() |
| 1113 |
self.assertFalse(self.pathfmt.kwdict["_mtime_meta"]) |
| 1114 |
|
| 1115 |
def test_mtime_none_dt(self): |
| 1116 |
self._create(None, {"date": dt.NONE}) |
| 1117 |
self._trigger() |
| 1118 |
self.assertFalse(self.pathfmt.kwdict["_mtime_meta"]) |
| 1119 |
|
| 1120 |
def test_mtime_undefined(self): |
| 1121 |
self._create(None, {}) |
| 1122 |
self._trigger() |
| 1123 |
self.assertFalse(self.pathfmt.kwdict["_mtime_meta"]) |
| 1124 |
|
| 1125 |
def test_mtime_invalid(self): |
| 1126 |
self._create(None, {"date": "foobar"}) |
| 1127 |
self._trigger() |
| 1128 |
self.assertFalse(self.pathfmt.kwdict["_mtime_meta"]) |
| 1129 |
|
| 1130 |
def test_mtime_key(self): |
| 1131 |
self._create({"key": "foo"}, {"foo": 315532800}) |
| 1132 |
self._trigger() |
| 1133 |
self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800) |
| 1134 |
|
| 1135 |
def test_mtime_value(self): |
| 1136 |
self._create({"value": "{foo}"}, {"foo": 315532800}) |
| 1137 |
self._trigger() |
| 1138 |
self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800) |
| 1139 |
|
| 1140 |
def test_mtime_directory(self): |
| 1141 |
self._create({"target": "directory"}, {"date": 315532800}) |
| 1142 |
|
| 1143 |
with patch("os.utime") as ut: |
| 1144 |
self._trigger(("post-after",)) |
| 1145 |
|
| 1146 |
ut.assert_called_once() |
| 1147 |
args = ut.mock_calls[0].args |
| 1148 |
self.assertEqual(args[0], self.pathfmt.realdirectory) |
| 1149 |
self.assertEqual(args[1][1], 315532800) |
| 1150 |
|
| 1151 |
|
| 1152 |
class PythonTest(BasePostprocessorTest): |
| 1153 |
|
| 1154 |
def test_module(self): |
| 1155 |
path = os.path.join(self.dir.name, "module.py") |
| 1156 |
self._write_module(path) |
| 1157 |
|
| 1158 |
sys.path.insert(0, self.dir.name) |
| 1159 |
try: |
| 1160 |
self._create({"function": "module:calc"}, {"_value": 123}) |
| 1161 |
finally: |
| 1162 |
del sys.path[0] |
| 1163 |
|
| 1164 |
self.assertNotIn("_result", self.pathfmt.kwdict) |
| 1165 |
self._trigger() |
| 1166 |
self.assertEqual(self.pathfmt.kwdict["_result"], 246) |
| 1167 |
|
| 1168 |
def test_path(self): |
| 1169 |
path = os.path.join(self.dir.name, "module.py") |
| 1170 |
self._write_module(path) |
| 1171 |
|
| 1172 |
self._create({"function": f"{path}:calc"}, {"_value": 12}) |
| 1173 |
|
| 1174 |
self.assertNotIn("_result", self.pathfmt.kwdict) |
| 1175 |
self._trigger() |
| 1176 |
self.assertEqual(self.pathfmt.kwdict["_result"], 24) |
| 1177 |
|
| 1178 |
def test_eval(self): |
| 1179 |
self._create({"mode": "eval", "expression": "abort()"}) |
| 1180 |
|
| 1181 |
with self.assertRaises(exception.StopExtraction): |
| 1182 |
self._trigger() |
| 1183 |
|
| 1184 |
def test_eval_auto(self): |
| 1185 |
self._create({"expression": "abort()"}) |
| 1186 |
|
| 1187 |
with self.assertRaises(exception.StopExtraction): |
| 1188 |
self._trigger() |
| 1189 |
|
| 1190 |
def test_archive(self): |
| 1191 |
pp = self._create({ |
| 1192 |
"expression": "True", |
| 1193 |
"archive" : ":memory:", |
| 1194 |
"event" : "finalize", |
| 1195 |
}) |
| 1196 |
|
| 1197 |
self.assertIsInstance(pp.archive, archive.DownloadArchive) |
| 1198 |
|
| 1199 |
with patch.object(pp.archive, "add") as m_aa, \ |
| 1200 |
patch.object(pp.archive, "close") as m_ac: |
| 1201 |
self._trigger(("finalize",)) |
| 1202 |
pp.archive.close() |
| 1203 |
|
| 1204 |
m_aa.assert_called_once_with(self.pathfmt.kwdict) |
| 1205 |
m_ac.assert_called_once() |
| 1206 |
|
| 1207 |
def _write_module(self, path): |
| 1208 |
with open(path, "w") as fp: |
| 1209 |
fp.write(""" |
| 1210 |
def calc(kwdict): |
| 1211 |
kwdict["_result"] = kwdict["_value"] * 2 |
| 1212 |
""") |
| 1213 |
|
| 1214 |
|
| 1215 |
class RenameTest(BasePostprocessorTest): |
| 1216 |
|
| 1217 |
def _prepare(self, filename): |
| 1218 |
path = self.pathfmt.realdirectory |
| 1219 |
shutil.rmtree(path, ignore_errors=True) |
| 1220 |
os.makedirs(path, exist_ok=True) |
| 1221 |
|
| 1222 |
with open(path + filename, "w"): |
| 1223 |
pass |
| 1224 |
|
| 1225 |
return path |
| 1226 |
|
| 1227 |
def test_rename_from(self): |
| 1228 |
self._create({"from": "{id}.{extension}"}, {"id": 12345}) |
| 1229 |
path = self._prepare("12345.ext") |
| 1230 |
|
| 1231 |
self._trigger() |
| 1232 |
|
| 1233 |
self.assertEqual(os.listdir(path), ["file.ext"]) |
| 1234 |
|
| 1235 |
def test_rename_to(self): |
| 1236 |
self._create({"to": "{id}.{extension}"}, {"id": 12345}) |
| 1237 |
path = self._prepare("file.ext") |
| 1238 |
|
| 1239 |
self._trigger(("skip",)) |
| 1240 |
|
| 1241 |
self.assertEqual(os.listdir(path), ["12345.ext"]) |
| 1242 |
|
| 1243 |
def test_rename_from_to(self): |
| 1244 |
self._create({"from": "name", "to": "{id}"}, {"id": 12345}) |
| 1245 |
path = self._prepare("name") |
| 1246 |
|
| 1247 |
self._trigger() |
| 1248 |
|
| 1249 |
self.assertEqual(os.listdir(path), ["12345"]) |
| 1250 |
|
| 1251 |
def test_rename_noopt(self): |
| 1252 |
with self.assertRaises(ValueError): |
| 1253 |
self._create({}) |
| 1254 |
|
| 1255 |
def test_rename_skip(self): |
| 1256 |
self._create({"from": "{id}.{extension}"}, {"id": 12345}) |
| 1257 |
path = self._prepare("12345.ext") |
| 1258 |
with open(f"{path}file.ext", "w"): |
| 1259 |
pass |
| 1260 |
|
| 1261 |
with self.assertLogs("postprocessor.rename", level="WARNING") as cm: |
| 1262 |
self._trigger() |
| 1263 |
self.assertTrue(cm.output[0].startswith( |
| 1264 |
"WARNING:postprocessor.rename:Not renaming " |
| 1265 |
"'12345.ext' to 'file.ext'")) |
| 1266 |
self.assertEqual(sorted(os.listdir(path)), ["12345.ext", "file.ext"]) |
| 1267 |
|
| 1268 |
|
| 1269 |
class ZipTest(BasePostprocessorTest): |
| 1270 |
|
| 1271 |
def test_zip_default(self): |
| 1272 |
pp = self._create() |
| 1273 |
self.assertEqual(self.job.hooks["file"][0], pp.write_fast) |
| 1274 |
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1]) |
| 1275 |
self.assertEqual(pp.delete, True) |
| 1276 |
self.assertEqual(pp.args, ( |
| 1277 |
f"{pp.path}.zip", "a", zipfile.ZIP_STORED, True, |
| 1278 |
)) |
| 1279 |
self.assertTrue(pp.args[0].endswith("/test.zip")) |
| 1280 |
|
| 1281 |
def test_zip_safe(self): |
| 1282 |
pp = self._create({"mode": "safe"}) |
| 1283 |
self.assertEqual(self.job.hooks["file"][0], pp.write_safe) |
| 1284 |
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1]) |
| 1285 |
self.assertEqual(pp.delete, True) |
| 1286 |
self.assertEqual(pp.args, ( |
| 1287 |
f"{pp.path}.zip", "a", zipfile.ZIP_STORED, True, |
| 1288 |
)) |
| 1289 |
self.assertTrue(pp.args[0].endswith("/test.zip")) |
| 1290 |
|
| 1291 |
def test_zip_options(self): |
| 1292 |
pp = self._create({ |
| 1293 |
"keep-files": True, |
| 1294 |
"compression": "zip", |
| 1295 |
"extension": "cbz", |
| 1296 |
}) |
| 1297 |
self.assertEqual(pp.delete, False) |
| 1298 |
self.assertEqual(pp.args, ( |
| 1299 |
f"{pp.path}.cbz", "a", zipfile.ZIP_DEFLATED, True, |
| 1300 |
)) |
| 1301 |
self.assertTrue(pp.args[0].endswith("/test.cbz")) |
| 1302 |
|
| 1303 |
def test_zip_write(self): |
| 1304 |
with tempfile.NamedTemporaryFile("w", dir=self.dir.name) as file: |
| 1305 |
pp = self._create({"files": [file.name, "_info_.json"], |
| 1306 |
"keep-files": True}) |
| 1307 |
|
| 1308 |
filename = os.path.basename(file.name) |
| 1309 |
file.write("foobar\n") |
| 1310 |
|
| 1311 |
# write dummy file with 3 different names |
| 1312 |
for i in range(3): |
| 1313 |
name = f"file{i}.ext" |
| 1314 |
self.pathfmt.temppath = file.name |
| 1315 |
self.pathfmt.filename = name |
| 1316 |
|
| 1317 |
self._trigger() |
| 1318 |
|
| 1319 |
nti = pp.zfile.NameToInfo |
| 1320 |
self.assertEqual(len(nti), i+2) |
| 1321 |
self.assertIn(name, nti) |
| 1322 |
|
| 1323 |
# check file contents |
| 1324 |
self.assertEqual(len(nti), 4) |
| 1325 |
self.assertIn("file0.ext", nti) |
| 1326 |
self.assertIn("file1.ext", nti) |
| 1327 |
self.assertIn("file2.ext", nti) |
| 1328 |
self.assertIn(filename, nti) |
| 1329 |
|
| 1330 |
# write the last file a second time (will be skipped) |
| 1331 |
self._trigger() |
| 1332 |
self.assertEqual(len(pp.zfile.NameToInfo), 4) |
| 1333 |
|
| 1334 |
# close file |
| 1335 |
self._trigger(("finalize",)) |
| 1336 |
|
| 1337 |
# reopen to check persistence |
| 1338 |
with zipfile.ZipFile(pp.zfile.filename) as file: |
| 1339 |
nti = file.NameToInfo |
| 1340 |
self.assertEqual(len(pp.zfile.NameToInfo), 4) |
| 1341 |
self.assertIn("file0.ext", nti) |
| 1342 |
self.assertIn("file1.ext", nti) |
| 1343 |
self.assertIn("file2.ext", nti) |
| 1344 |
self.assertIn(filename, nti) |
| 1345 |
|
| 1346 |
os.unlink(pp.zfile.filename) |
| 1347 |
|
| 1348 |
def test_zip_write_mock(self): |
| 1349 |
|
| 1350 |
def side_effect(_, name): |
| 1351 |
pp.zfile.NameToInfo.add(name) |
| 1352 |
|
| 1353 |
pp = self._create() |
| 1354 |
pp.zfile = Mock() |
| 1355 |
pp.zfile.NameToInfo = set() |
| 1356 |
pp.zfile.write.side_effect = side_effect |
| 1357 |
|
| 1358 |
# write 3 files |
| 1359 |
for i in range(3): |
| 1360 |
self.pathfmt.temppath = f"{self.pathfmt.realdirectory}file.ext" |
| 1361 |
self.pathfmt.filename = f"file{i}.ext" |
| 1362 |
self._trigger() |
| 1363 |
|
| 1364 |
# write the last file a second time (should be skipped) |
| 1365 |
self._trigger() |
| 1366 |
|
| 1367 |
# close file |
| 1368 |
self._trigger(("finalize",)) |
| 1369 |
|
| 1370 |
self.assertEqual(pp.zfile.write.call_count, 3) |
| 1371 |
for call_args in pp.zfile.write.call_args_list: |
| 1372 |
args, kwargs = call_args |
| 1373 |
self.assertEqual(len(args), 2) |
| 1374 |
self.assertEqual(len(kwargs), 0) |
| 1375 |
self.assertEqual(args[0], self.pathfmt.temppath) |
| 1376 |
self.assertRegex(args[1], r"file\d\.ext") |
| 1377 |
self.assertEqual(pp.zfile.close.call_count, 1) |
| 1378 |
|
| 1379 |
|
| 1380 |
if __name__ == "__main__": |
| 1381 |
unittest.main() |