Skip to content

Commit

Permalink
Fix tests; introduce Travis CI
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanovich committed Feb 4, 2013
1 parent d033eb4 commit 029834f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: python
python:
- "2.6"
- "2.7"
- "3.2"
script:
- python test.py
31 changes: 14 additions & 17 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@

import os.path
import unittest
import random

import magic
from os import path


testfile = [
("magic.pyc", b"python 2.4 byte-compiled", b"application/octet-stream"),
("test.pdf", b"PDF document, version 1.2", b"application/pdf"),
("test.gz", b'gzip compressed data, was "test", from Unix, last modified: '
b'Sat Jun 28 18:32:52 2008', b"application/x-gzip"),
b'Sun Jun 29 07:32:52 2008', b"application/x-gzip"),
("text.txt", b"ASCII text", b"text/plain"),
# is there no better way to encode a unicode literal across python2/3.[01]/3.3?
(b"\xce\xbb".decode('utf-8'), b"empty", b"application/x-empty")
]
# (b"\xce\xbb".decode('utf-8'), b"empty", b"application/x-empty")
]

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


class TestMagic(unittest.TestCase):

mime = False

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

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


Expand All @@ -46,22 +43,22 @@ def testErrors(self):
self.assertRaises(magic.MagicException, magic.Magic)
del os.environ['MAGIC']


class TestMagicMime(TestMagic):
mime = True


class TestMagicMimeEncoding(unittest.TestCase):
def setUp(self):
self.m = magic.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, 'rb').read(1024)))
filename = os.path.join(os.path.dirname(__file__), "testdata", filename)
with open(filename, 'rb') as f:
self.assertEqual(encoding, self.m.from_buffer(f.read(1024)))
self.assertEqual(encoding, self.m.from_file(filename), filename)


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

0 comments on commit 029834f

Please sign in to comment.