summaryrefslogtreecommitdiff
path: root/test.py
blob: d822f6819439e3ba888f078376e3fcfd64c45e27 (plain)
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

import os.path
import unittest
import random
from StringIO import StringIO
from os import path
from magic import Magic, MagicException

testfile = [
    ("magic.pyc", "python 2.4 byte-compiled", "application/octet-stream"),
    ("test.pdf", "PDF document, version 1.2", "application/pdf"),
    ("test.gz", 'gzip compressed data, was "test", from Unix, last modified: '
     'Sat Jun 28 18:32:52 2008', "application/x-gzip"),
    ("text.txt", "ASCII text", "text/plain"),
    ]

testFileEncoding = [('text-iso8859-1.txt', 'iso-8859-1')]

class TestMagic(unittest.TestCase):

    mime = False
    
    def setUp(self):
        self.m = Magic(mime=self.mime)

    def testFileTypes(self):
        for filename, desc, mime in testfile:
            filename = path.join(path.dirname(__file__),
                                 "testdata",
                                 filename)
            if self.mime:
                target = mime
            else:
                target = desc
                
            self.assertEqual(target, self.m.from_buffer(open(filename).read(1024)))
            self.assertEqual(target, self.m.from_file(filename), filename)
        

    def testErrors(self):
        self.assertRaises(IOError, self.m.from_file, "nonexistent")
        self.assertRaises(MagicException, Magic, magic_file="noneexistent")
        os.environ['MAGIC'] = '/nonexistetn'
        self.assertRaises(MagicException, Magic)
        del os.environ['MAGIC']

class TestMagicMime(TestMagic):
    mime = True

class TestMagicMimeEncoding(unittest.TestCase):
    def setUp(self):
        self.m = Magic(mime_encoding=True)

    def testFileEncoding(self):
        for filename, encoding in testFileEncoding:
            filename = path.join(path.dirname(__file__),
                                 "testdata",
                                 filename)
            self.assertEqual(encoding, self.m.from_buffer(open(filename).read(1024)))
            self.assertEqual(encoding, self.m.from_file(filename), filename)


if __name__ == '__main__':
    unittest.main()