summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-02-11 07:32:40 -0800
committerGitHub <noreply@github.com>2020-02-11 07:32:40 -0800
commitd3f9fb2d28ceedb0a17a703338424ff284a578c8 (patch)
tree96697d4803be5fcc93acd3783b63c025123eb191
parent40ef4fc07e64be93522bc705c3d8a954a62b9ded (diff)
downloadcpython-git-d3f9fb2d28ceedb0a17a703338424ff284a578c8.tar.gz
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. (cherry picked from commit d8efc1495194228c3a4cd472200275d6491d8e2d) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
-rw-r--r--Lib/mimetypes.py6
-rw-r--r--Lib/test/test_mimetypes.py84
2 files changed, 85 insertions, 5 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index 9b42bf6dd2..f33b658f10 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -563,7 +563,7 @@ def _default_mime_types():
_default_mime_types()
-if __name__ == '__main__':
+def _main():
import getopt
USAGE = """\
@@ -607,3 +607,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()
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index a5a06b189d..9cac6ce022 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -8,10 +8,20 @@ import unittest
from test import support
from platform import win32_edition
-# Tell it we don't know about external files:
-mimetypes.knownfiles = []
-mimetypes.inited = False
-mimetypes._default_mime_types()
+
+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):
@@ -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()