forked from ahupp/python-magic
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
74 lines (59 loc) · 2.43 KB
/
test.py
File metadata and controls
74 lines (59 loc) · 2.43 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
import os.path
import unittest
import magic
class MagicTest(unittest.TestCase):
TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata')
def assert_values(self, m, expected_values):
for filename, expected_value in expected_values.items():
filename = os.path.join(self.TESTDATA_DIR, filename)
with open(filename, 'rb') as f:
value = m.from_buffer(f.read())
self.assertEqual(value, expected_value)
value = m.from_file(filename)
self.assertEqual(value, expected_value)
def test_mime_types(self):
m = magic.Magic(mime=True)
self.assert_values(m, {
'magic.pyc': b'application/octet-stream',
'test.pdf': b'application/pdf',
'test.gz': b'application/x-gzip',
'text.txt': b'text/plain',
b'\xce\xbb'.decode('utf-8'): b'text/plain',
})
def test_descriptions(self):
m = magic.Magic()
os.environ['TZ'] = 'UTC' # To get the last modified date of test.gz in UTC
try:
self.assert_values(m, {
'magic.pyc': b'python 2.4 byte-compiled',
'test.pdf': b'PDF document, version 1.2',
'test.gz': b'gzip compressed data, was "test", from Unix, '
b'last modified: Sun Jun 29 01:32:52 2008',
'text.txt': b'ASCII text',
})
finally:
del os.environ['TZ']
def test_mime_encodings(self):
m = magic.Magic(mime_encoding=True)
self.assert_values(m, {
'text-iso8859-1.txt': b'iso-8859-1',
'text.txt': b'us-ascii',
})
def test_errors(self):
m = magic.Magic()
self.assertRaises(IOError, m.from_file, 'nonexistent')
self.assertRaises(magic.MagicException, magic.Magic,
magic_file='nonexistent')
os.environ['MAGIC'] = 'nonexistent'
try:
self.assertRaises(magic.MagicException, magic.Magic)
finally:
del os.environ['MAGIC']
def test_keep_going(self):
filename = os.path.join(self.TESTDATA_DIR, 'keep-going.jpg')
m = magic.Magic(mime=True)
self.assertEqual(m.from_file(filename), b'application/octet-stream')
m = magic.Magic(mime=True, keep_going=True)
self.assertEqual(m.from_file(filename), b'image/jpeg')
if __name__ == '__main__':
unittest.main()