Skip to content

Commit e494db4

Browse files
committed
mock magic so our fallback can be tested on systems with fixed magic version
1 parent 4494a40 commit e494db4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

tests/filesystem/test_filesystem.py

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# -*- coding: utf-8 -*-
33
# vim: ai ts=4 sts=4 et sw=4 nu
44

5+
import magic
6+
57
from zimscraperlib.filesystem import (
68
delete_callback,
79
get_content_mimetype,
@@ -21,6 +23,17 @@ def test_content_mimetype(png_image, jpg_image, undecodable_byte_stream):
2123
with open(jpg_image, "rb") as fh:
2224
assert get_content_mimetype(fh.read(64)) == "image/jpeg"
2325

26+
27+
def test_content_mimetype_fallback(monkeypatch, undecodable_byte_stream):
28+
29+
# use raw function first to test actual code
30+
assert get_content_mimetype(undecodable_byte_stream) == "application/octet-stream"
31+
32+
# mock then so we keep coverage on systems where magic works
33+
def raising_magic(*args):
34+
raise UnicodeDecodeError("nocodec", b"", 0, 1, "noreason")
35+
36+
monkeypatch.setattr(magic, "detect_from_content", raising_magic)
2437
assert get_content_mimetype(undecodable_byte_stream) == "application/octet-stream"
2538

2639

0 commit comments

Comments
 (0)