diff options
| author | Adam Hupp <adam@hupp.org> | 2015-07-29 21:54:07 -0700 |
|---|---|---|
| committer | Adam Hupp <adam@hupp.org> | 2015-07-29 21:54:50 -0700 |
| commit | a818eda7e3e163d6179f9a6a381856ffb00722ac (patch) | |
| tree | f89650a7cf74a49eecd992a8fb986070fac0239c /test | |
| parent | e479096d5b08af12f3f6eb47290932c9f813acdd (diff) | |
| download | python-magic-a818eda7e3e163d6179f9a6a381856ffb00722ac.tar.gz | |
Include tests in the sdist tarball
Diffstat (limited to 'test')
| -rwxr-xr-x | test/run.sh | 12 | ||||
| -rw-r--r-- | test/test.py | 78 | ||||
| -rw-r--r-- | test/testdata/keep-going.jpg | bin | 0 -> 124365 bytes | |||
| -rw-r--r-- | test/testdata/magic.pyc | bin | 0 -> 1797 bytes | |||
| -rw-r--r-- | test/testdata/test.gz | bin | 0 -> 40 bytes | |||
| -rw-r--r-- | test/testdata/test.pdf | 199 | ||||
| -rw-r--r-- | test/testdata/text-iso8859-1.txt | 2 | ||||
| -rw-r--r-- | test/testdata/text.txt | 2 | ||||
| -rw-r--r-- | test/testdata/λ | 1 |
9 files changed, 294 insertions, 0 deletions
diff --git a/test/run.sh b/test/run.sh new file mode 100755 index 0000000..3768497 --- /dev/null +++ b/test/run.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +set -e + +# ensure we can use unicode filenames in the test +export LC_ALL=en_US.UTF-8 +THISDIR=`dirname $0` +export PYTHONPATH=${THISDIR}/.. + +python2.6 ${THISDIR}/test.py +python2.7 ${THISDIR}/test.py +python3 ${THISDIR}/test.py diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..f9c9c41 --- /dev/null +++ b/test/test.py @@ -0,0 +1,78 @@ +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(): + try: + filename = os.path.join(self.TESTDATA_DIR, filename) + except TypeError: + filename = os.path.join(self.TESTDATA_DIR.encode('utf-8'), filename) + + with open(filename, 'rb') as f: + value = m.from_buffer(f.read()) + expected_value_bytes = expected_value.encode('utf-8') + self.assertEqual(value, expected_value_bytes) + + value = m.from_file(filename) + self.assertEqual(value, expected_value_bytes) + + def test_mime_types(self): + m = magic.Magic(mime=True) + self.assert_values(m, { + 'magic.pyc': 'application/octet-stream', + 'test.pdf': 'application/pdf', + 'test.gz': 'application/x-gzip', + 'text.txt': 'text/plain', + b'\xce\xbb'.decode('utf-8'): 'text/plain', + b'\xce\xbb': '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': 'python 2.4 byte-compiled', + 'test.pdf': 'PDF document, version 1.2', + 'test.gz': 'gzip compressed data, was "test", from Unix, ' + 'last modified: Sun Jun 29 01:32:52 2008', + 'text.txt': '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': 'iso-8859-1', + 'text.txt': '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), + 'application/octet-stream'.encode('utf-8')) + + m = magic.Magic(mime=True, keep_going=True) + self.assertEqual(m.from_file(filename), 'image/jpeg'.encode('utf-8')) + +if __name__ == '__main__': + unittest.main() diff --git a/test/testdata/keep-going.jpg b/test/testdata/keep-going.jpg Binary files differnew file mode 100644 index 0000000..c15171d --- /dev/null +++ b/test/testdata/keep-going.jpg diff --git a/test/testdata/magic.pyc b/test/testdata/magic.pyc Binary files differnew file mode 100644 index 0000000..ba801e0 --- /dev/null +++ b/test/testdata/magic.pyc diff --git a/test/testdata/test.gz b/test/testdata/test.gz Binary files differnew file mode 100644 index 0000000..5d847dd --- /dev/null +++ b/test/testdata/test.gz diff --git a/test/testdata/test.pdf b/test/testdata/test.pdf new file mode 100644 index 0000000..b986617 --- /dev/null +++ b/test/testdata/test.pdf @@ -0,0 +1,199 @@ +%PDF-1.2 +7 0 obj +[5 0 R/XYZ 111.6 757.86] +endobj +13 0 obj +<< +/Title(About this document) +/A<< +/S/GoTo +/D(subsection.1.1) +>> +/Parent 12 0 R +/Next 14 0 R +>> +endobj +15 0 obj +<< +/Title(Compiling with GHC) +/A<< +/S/GoTo +/D(subsubsection.1.2.1) +>> +/Parent 14 0 R +/Next 16 0 R +>> +endobj +16 0 obj +<< +/Title(Compiling with Hugs) +/A<< +/S/GoTo +/D(subsubsection.1.2.2) +>> +/Parent 14 0 R +/Prev 15 0 R +>> +endobj +14 0 obj +<< +/Title(Compatibility) +/A<< +/S/GoTo +/D(subsection.1.2) +>> +/Parent 12 0 R +/Prev 13 0 R +/First 15 0 R +/Last 16 0 R +/Count -2 +/Next 17 0 R +>> +endobj +17 0 obj +<< +/Title(Reporting bugs) +/A<< +/S/GoTo +/D(subsection.1.3) +>> +/Parent 12 0 R +/Prev 14 0 R +/Next 18 0 R +>> +endobj +18 0 obj +<< +/Title(History) +/A<< +/S/GoTo +/D(subsection.1.4) +>> +/Parent 12 0 R +/Prev 17 0 R +/Next 19 0 R +>> +endobj +19 0 obj +<< +/Title(License) +/A<< +/S/GoTo +/D(subsection.1.5) +>> +/Parent 12 0 R +/Prev 18 0 R +>> +endobj +12 0 obj +<< +/Title(Introduction) +/A<< +/S/GoTo +/D(section.1) +>> +/Parent 11 0 R +/First 13 0 R +/Last 19 0 R +/Count -5 +/Next 20 0 R +>> +endobj +21 0 obj +<< +/Title(Running a parser) +/A<< +/S/GoTo +/D(subsection.2.1) +>> +/Parent 20 0 R +/Next 22 0 R +>> +endobj +22 0 obj +<< +/Title(Sequence and choice) +/A<< +/S/GoTo +/D(subsection.2.2) +>> +/Parent 20 0 R +/Prev 21 0 R +/Next 23 0 R +>> +endobj +23 0 obj +<< +/Title(Predictive parsers) +/A<< +/S/GoTo +/D(subsection.2.3) +>> +/Parent 20 0 R +/Prev 22 0 R +/Next 24 0 R +>> +endobj +24 0 obj +<< +/Title(Adding semantics) +/A<< +/S/GoTo +/D(subsection.2.4) +>> +/Parent 20 0 R +/Prev 23 0 R +/Next 25 0 R +>> +endobj +25 0 obj +<< +/Title(Sequences and seperators) +/A<< +/S/GoTo +/D(subsection.2.5) +>> +/Parent 20 0 R +/Prev 24 0 R +/Next 26 0 R +>> +endobj +26 0 obj +<< +/Title(Improving error messages) +/A<< +/S/GoTo +/D(subsection.2.6) +>> +/Parent 20 0 R +/Prev 25 0 R +/Next 27 0 R +>> +endobj +27 0 obj +<< +/Title(Expressions) +/A<< +/S/GoTo +/D(subsection.2.7) +>> +/Parent 20 0 R +/Prev 26 0 R +/Next 28 0 R +>> +endobj +28 0 obj +<< +/Title(Lexical analysis) +/A<< +/S/GoTo +/D(subsection.2.8) +>> +/Parent 20 0 R +/Prev 27 0 R +/Next 29 0 R +>> +endobj +30 0 obj +<< +/Title(Lexeme parsers
\ No newline at end of file diff --git a/test/testdata/text-iso8859-1.txt b/test/testdata/text-iso8859-1.txt new file mode 100644 index 0000000..524a1d0 --- /dev/null +++ b/test/testdata/text-iso8859-1.txt @@ -0,0 +1,2 @@ +This is a web page encoded in iso-8859-1 +éèàùôâïî diff --git a/test/testdata/text.txt b/test/testdata/text.txt new file mode 100644 index 0000000..476f506 --- /dev/null +++ b/test/testdata/text.txt @@ -0,0 +1,2 @@ +Hello, World! + diff --git a/test/testdata/λ b/test/testdata/λ new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/test/testdata/λ @@ -0,0 +1 @@ +test |
