-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathtest_filetype.py
More file actions
168 lines (124 loc) · 4.72 KB
/
Copy pathtest_filetype.py
File metadata and controls
168 lines (124 loc) · 4.72 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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
import sys
# Python 2.7 workaround
try:
import pathlib
except ImportError:
pathlib = None
import filetype
from filetype.types import image
from filetype.types.base import Type
import pytest
from . import FIXTURES
class TestFileType(object):
def test_guess_file_path(self):
kind = filetype.guess(os.path.join(FIXTURES, 'sample.jpg'))
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'
def test_guess_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
kind = filetype.guess(buf)
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'
def test_guess_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
kind = filetype.guess(buf)
assert kind is None
def test_guess_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
kind = filetype.guess(buf)
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'
@pytest.mark.skipif(sys.version_info < (3,),
reason="No workarounds anymore for python 2.7")
def test_guess_bytes(self):
buf = bytes(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
kind = filetype.guess(buf)
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'
@pytest.mark.skipif(sys.version_info < (3, 6),
reason="No workarounds for unsupported versions")
def test_guess_pathlib(self):
path = pathlib.Path(os.path.join(FIXTURES, 'sample.jpg'))
kind = filetype.guess(path)
assert kind is not None
assert kind.mime == 'image/jpeg'
assert kind.extension == 'jpg'
@pytest.mark.skipif(sys.version_info < (3, 6),
reason="No workarounds for unsupported versions")
def test_invalid_input(self):
with pytest.raises(TypeError):
filetype.guess(tuple)
class TestFileTypeExtension(object):
def test_guess_extension_file_path(self):
ext = filetype.guess_extension(os.path.join(FIXTURES, 'sample.jpg'))
assert ext is not None
assert ext == 'jpg'
def test_guess_extension_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
ext = filetype.guess_extension(buf)
assert ext is not None
assert ext == 'jpg'
def test_guess_extension_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
ext = filetype.guess_extension(buf)
assert ext is None
def test_guess_extension_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
ext = filetype.guess_extension(buf)
assert ext is not None
assert ext == 'jpg'
class TestFileTypeMIME(object):
def test_guess_mime_file_path(self):
mime = filetype.guess_mime(os.path.join(FIXTURES, 'sample.jpg'))
assert mime is not None
assert mime == 'image/jpeg'
def test_guess_mime_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
mime = filetype.guess_mime(buf)
assert mime is not None
assert mime == 'image/jpeg'
def test_guess_mime_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
mime = filetype.guess_mime(buf)
assert mime is None
def test_guess_mime_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
mime = filetype.guess_mime(buf)
assert mime is not None
assert mime == 'image/jpeg'
def test_get_type_mime():
type_ = filetype.get_type(mime='image/png')
assert isinstance(type_, image.Png)
def test_get_type_ext():
type_ = filetype.get_type(ext='png')
assert isinstance(type_, image.Png)
def test_get_type():
type_ = filetype.get_type(mime='image/png', ext='png')
assert isinstance(type_, image.Png)
def test_get_type_invalid():
type_ = filetype.get_type(mime='foo', ext='bar')
assert type_ is None
def test_invalid_custom_type():
with pytest.raises(TypeError):
filetype.add_type(tuple)
@pytest.mark.skipif(sys.version_info < (3,),
reason="No workarounds anymore for python 2.7")
def test_custom_type():
mime = 'app/bar'
ext = 'bar'
class Custom(Type):
def __init__(self):
super().__init__(mime=mime, extension=ext)
def match(self, buf):
return buf == b"bar"
filetype.add_type(Custom())
kind = filetype.guess(b"bar")
assert kind.extension == ext
assert kind.mime == mime