From a0b9f316fda16b21923bb57e1de9a98789befbba Mon Sep 17 00:00:00 2001 From: Adam Hupp Date: Mon, 4 Dec 2017 11:55:27 -0800 Subject: Merge in compatability mode with libmagic The libmagic distribution uses the same package name `magic` as python-magic, but has an incompatible API. This change merges in a copy of libmagic's bindings, wrapped to give deprecation warnings. This is intended to a) mitigate the short-term pain to users and packagers who need to figure out which to use, and b) give us a path to merging the two sets of bindings. I'd be happy for libmagic to take over this package if we could find a path to it. --- test/libmagic_test.py | 35 +++++++++++++++++++++++++++++++++++ test/run.py | 1 + 2 files changed, 36 insertions(+) create mode 100644 test/libmagic_test.py (limited to 'test') diff --git a/test/libmagic_test.py b/test/libmagic_test.py new file mode 100644 index 0000000..5a0a290 --- /dev/null +++ b/test/libmagic_test.py @@ -0,0 +1,35 @@ +# coding: utf-8 + +import unittest + +import magic + + +class MagicTestCase(unittest.TestCase): + + filename = 'test/testdata/test.pdf' + expected_mime_type = 'application/pdf' + expected_encoding = 'us-ascii' + expected_name = 'PDF document, version 1.2' + + def assert_result(self, result): + self.assertEqual(result.mime_type, self.expected_mime_type) + self.assertEqual(result.encoding, self.expected_encoding) + self.assertEqual(result.name, self.expected_name) + + def test_detect_from_filename(self): + result = magic.detect_from_filename(self.filename) + self.assert_result(result) + + def test_detect_from_fobj(self): + with open(self.filename) as fobj: + result = magic.detect_from_fobj(fobj) + self.assert_result(result) + + def test_detect_from_content(self): + with open(self.filename) as fobj: + result = magic.detect_from_content(fobj.read(4096)) + self.assert_result(result) + +if __name__ == '__main__': + unittest.main() diff --git a/test/run.py b/test/run.py index c10c11f..6b37555 100644 --- a/test/run.py +++ b/test/run.py @@ -24,6 +24,7 @@ def run_test(versions): found = True print("Testing %s" % i) subprocess.run([i, os.path.join(this_dir, "test.py")], env=new_env, check=True) + subprocess.run([i, os.path.join(this_dir, "libmagic_test.py")], env=new_env, check=True) if not found: sys.exit("No versions found: " + str(versions)) -- cgit v1.2.1