bpo-39299: Add more tests for mimetypes and its cli. (GH-17949)

* Add tests for case insensitive check of types and extensions as fallback.
* Add tests for data url with no comma.
* Add tests for read_mime_types.
* Add tests for the mimetypes cli and refactor __main__ code to private function.
* Restore mimetypes.knownfiles value at the end of the test.
This commit is contained in:
Karthikeyan Singaravelan 2020-01-13 20:09:36 +05:30 committed by GitHub
parent c1ee6e5e9b
commit d8efc14951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 5 deletions

View File

@ -564,7 +564,7 @@ def _default_mime_types():
_default_mime_types()
if __name__ == '__main__':
def _main():
import getopt
USAGE = """\
@ -608,3 +608,7 @@ More than one type argument may be given.
guess, encoding = guess_type(gtype, strict)
if not guess: print("I don't know anything about type", gtype)
else: print('type:', guess, 'encoding:', encoding)
if __name__ == '__main__':
_main()

View File

@ -8,12 +8,22 @@ import unittest
from test import support
from platform import win32_edition
def setUpModule():
global knownfiles
knownfiles = mimetypes.knownfiles
# Tell it we don't know about external files:
mimetypes.knownfiles = []
mimetypes.inited = False
mimetypes._default_mime_types()
def tearDownModule():
# Restore knownfiles to its initial state
mimetypes.knownfiles = knownfiles
class MimeTypesTestCase(unittest.TestCase):
def setUp(self):
self.db = mimetypes.MimeTypes()
@ -21,6 +31,7 @@ class MimeTypesTestCase(unittest.TestCase):
def test_default_data(self):
eq = self.assertEqual
eq(self.db.guess_type("foo.html"), ("text/html", None))
eq(self.db.guess_type("foo.HTML"), ("text/html", None))
eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
@ -30,6 +41,7 @@ class MimeTypesTestCase(unittest.TestCase):
def test_data_urls(self):
eq = self.assertEqual
guess_type = self.db.guess_type
eq(guess_type("data:invalidDataWithoutComma"), (None, None))
eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
@ -42,6 +54,19 @@ class MimeTypesTestCase(unittest.TestCase):
("x-application/x-unittest", None))
eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
def test_read_mime_types(self):
eq = self.assertEqual
# Unreadable file returns None
self.assertIsNone(mimetypes.read_mime_types("non-existent"))
with support.temp_dir() as directory:
data = "x-application/x-unittest pyunit\n"
file = pathlib.Path(directory, "sample.mimetype")
file.write_text(data)
mime_dict = mimetypes.read_mime_types(file)
eq(mime_dict[".pyunit"], "x-application/x-unittest")
def test_non_standard_types(self):
eq = self.assertEqual
# First try strict
@ -49,7 +74,10 @@ class MimeTypesTestCase(unittest.TestCase):
eq(self.db.guess_extension('image/jpg', strict=True), None)
# And then non-strict
eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
eq(self.db.guess_type('foo.XUL', strict=False), ('text/xul', None))
eq(self.db.guess_type('foo.invalid', strict=False), (None, None))
eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
eq(self.db.guess_extension('image/JPG', strict=False), '.jpg')
def test_filename_with_url_delimiters(self):
# bpo-38449: URL delimiters cases should be handled also.
@ -200,5 +228,53 @@ class MiscTestCase(unittest.TestCase):
support.check__all__(self, mimetypes)
class MimetypesCliTestCase(unittest.TestCase):
def mimetypes_cmd(self, *args, **kwargs):
support.patch(self, sys, "argv", [sys.executable, *args])
with support.captured_stdout() as output:
mimetypes._main()
return output.getvalue().strip()
def test_help_option(self):
support.patch(self, sys, "argv", [sys.executable, "-h"])
with support.captured_stdout() as output:
with self.assertRaises(SystemExit) as cm:
mimetypes._main()
self.assertIn("Usage: mimetypes.py", output.getvalue())
self.assertEqual(cm.exception.code, 0)
def test_invalid_option(self):
support.patch(self, sys, "argv", [sys.executable, "--invalid"])
with support.captured_stdout() as output:
with self.assertRaises(SystemExit) as cm:
mimetypes._main()
self.assertIn("Usage: mimetypes.py", output.getvalue())
self.assertEqual(cm.exception.code, 1)
def test_guess_extension(self):
eq = self.assertEqual
extension = self.mimetypes_cmd("-l", "-e", "image/jpg")
eq(extension, ".jpg")
extension = self.mimetypes_cmd("-e", "image/jpg")
eq(extension, "I don't know anything about type image/jpg")
extension = self.mimetypes_cmd("-e", "image/jpeg")
eq(extension, ".jpg")
def test_guess_type(self):
eq = self.assertEqual
type_info = self.mimetypes_cmd("-l", "foo.pic")
eq(type_info, "type: image/pict encoding: None")
type_info = self.mimetypes_cmd("foo.pic")
eq(type_info, "I don't know anything about type foo.pic")
if __name__ == "__main__":
unittest.main()