From 029834f4d866f75dfa897bbfcafe2482a3675662 Mon Sep 17 00:00:00 2001 From: Anton Romanovich Date: Mon, 4 Feb 2013 22:39:42 +0600 Subject: Fix tests; introduce Travis CI --- .travis.yml | 7 +++++++ test.py | 31 ++++++++++++++----------------- 2 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0ddbd99 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: python +python: + - "2.6" + - "2.7" + - "3.2" +script: + - python test.py diff --git a/test.py b/test.py index 146097a..0a14b65 100644 --- a/test.py +++ b/test.py @@ -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) @@ -46,22 +43,22 @@ class TestMagic(unittest.TestCase): 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() - -- cgit v1.2.1 From f31da1caac95a61af88dea31cb41b5cca3d7916b Mon Sep 17 00:00:00 2001 From: Anton Romanovich Date: Mon, 4 Feb 2013 23:45:11 +0600 Subject: Refactor tests --- test.py | 96 ++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/test.py b/test.py index 0a14b65..a1d450d 100644 --- a/test.py +++ b/test.py @@ -4,61 +4,59 @@ import unittest import magic -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'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") -] +class MagicTest(unittest.TestCase): + TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata') -testFileEncoding = [('text-iso8859-1.txt', b'iso-8859-1')] + def assert_values(self, m, expected_values): + for filename, expected_value in expected_values.items(): + filename = os.path.join(self.TESTDATA_DIR, filename) - -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 = os.path.join(os.path.dirname(__file__), "testdata", filename) - if self.mime: - target = mime - else: - target = desc 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) - - - def testErrors(self): - self.assertRaises(IOError, self.m.from_file, "nonexistent") - self.assertRaises(magic.MagicException, magic.Magic, magic_file="noneexistent") - os.environ['MAGIC'] = '/nonexistetn' + value = m.from_buffer(f.read(1024)) + 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', + }) + + 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' 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 = 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() -- cgit v1.2.1 From 410dd606f370360acb6b8137256e3261aaebae40 Mon Sep 17 00:00:00 2001 From: Anton Romanovich Date: Tue, 5 Feb 2013 10:40:58 +0600 Subject: Fix tests --- test.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index a1d450d..6efbb68 100644 --- a/test.py +++ b/test.py @@ -12,7 +12,7 @@ class MagicTest(unittest.TestCase): filename = os.path.join(self.TESTDATA_DIR, filename) with open(filename, 'rb') as f: - value = m.from_buffer(f.read(1024)) + value = m.from_buffer(f.read()) self.assertEqual(value, expected_value) value = m.from_file(filename) @@ -53,9 +53,11 @@ class MagicTest(unittest.TestCase): self.assertRaises(IOError, m.from_file, 'nonexistent') self.assertRaises(magic.MagicException, magic.Magic, magic_file='nonexistent') - os.environ['MAGIC'] = '/nonexistent' - self.assertRaises(magic.MagicException, magic.Magic) - del os.environ['MAGIC'] + os.environ['MAGIC'] = 'nonexistent' + try: + self.assertRaises(magic.MagicException, magic.Magic) + finally: + del os.environ['MAGIC'] if __name__ == '__main__': -- cgit v1.2.1 From d529597e3d26e89f7f86c39a7959f582fd1b3c9e Mon Sep 17 00:00:00 2001 From: Anton Romanovich Date: Tue, 5 Feb 2013 23:05:55 +0600 Subject: Add keep going functionality --- magic.py | 7 +++++-- test.py | 10 ++++++++++ testdata/keep-going.jpg | Bin 0 -> 124365 bytes "testdata/\316\273" | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 testdata/keep-going.jpg create mode 100644 "testdata/\316\273" diff --git a/magic.py b/magic.py index 9df32a1..cb1ca6f 100644 --- a/magic.py +++ b/magic.py @@ -32,20 +32,23 @@ class Magic: """ - def __init__(self, mime=False, magic_file=None, mime_encoding=False): + def __init__(self, mime=False, magic_file=None, mime_encoding=False, + keep_going=False): """ Create a new libmagic wrapper. mime - if True, mimetypes are returned instead of textual descriptions mime_encoding - if True, codec is returned magic_file - use a mime database other than the system default - + keep_going - don't stop at the first match, keep going """ flags = MAGIC_NONE if mime: flags |= MAGIC_MIME elif mime_encoding: flags |= MAGIC_MIME_ENCODING + if keep_going: + flags |= MAGIC_CONTINUE self.cookie = magic_open(flags) diff --git a/test.py b/test.py index 6efbb68..7feb2e4 100644 --- a/test.py +++ b/test.py @@ -25,6 +25,7 @@ class MagicTest(unittest.TestCase): '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): @@ -59,6 +60,15 @@ class MagicTest(unittest.TestCase): 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() diff --git a/testdata/keep-going.jpg b/testdata/keep-going.jpg new file mode 100644 index 0000000..c15171d Binary files /dev/null and b/testdata/keep-going.jpg differ diff --git "a/testdata/\316\273" "b/testdata/\316\273" new file mode 100644 index 0000000..9daeafb --- /dev/null +++ "b/testdata/\316\273" @@ -0,0 +1 @@ +test -- cgit v1.2.1