2018-07-29 05:53:49 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import magic
|
|
|
|
|
|
|
|
|
|
|
|
class MagicTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
filename = 'magic.py'
|
2022-09-24 04:20:34 +00:00
|
|
|
expected_mime_type = 'text/x-script.python'
|
2018-07-29 05:53:49 +00:00
|
|
|
expected_encoding = 'us-ascii'
|
|
|
|
expected_name = 'Python script, ASCII text executable'
|
|
|
|
|
|
|
|
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):
|
2022-09-24 04:20:34 +00:00
|
|
|
with open(self.filename, "rb") as fobj:
|
2018-07-29 05:53:49 +00:00
|
|
|
result = magic.detect_from_fobj(fobj)
|
|
|
|
self.assert_result(result)
|
|
|
|
|
|
|
|
def test_detect_from_content(self):
|
2022-09-24 04:20:34 +00:00
|
|
|
with open(self.filename, "rb") as fobj:
|
|
|
|
result = magic.detect_from_content(fobj.read(8192))
|
2018-07-29 05:53:49 +00:00
|
|
|
self.assert_result(result)
|