88from test import support
99from platform import win32_edition
1010
11- # Tell it we don't know about external files:
12- mimetypes .knownfiles = []
13- mimetypes .inited = False
14- mimetypes ._default_mime_types ()
11+
12+ def setUpModule ():
13+ global knownfiles
14+ knownfiles = mimetypes .knownfiles
15+
16+ # Tell it we don't know about external files:
17+ mimetypes .knownfiles = []
18+ mimetypes .inited = False
19+ mimetypes ._default_mime_types ()
20+
21+
22+ def tearDownModule ():
23+ # Restore knownfiles to its initial state
24+ mimetypes .knownfiles = knownfiles
1525
1626
1727class MimeTypesTestCase (unittest .TestCase ):
@@ -21,6 +31,7 @@ def setUp(self):
2131 def test_default_data (self ):
2232 eq = self .assertEqual
2333 eq (self .db .guess_type ("foo.html" ), ("text/html" , None ))
34+ eq (self .db .guess_type ("foo.HTML" ), ("text/html" , None ))
2435 eq (self .db .guess_type ("foo.tgz" ), ("application/x-tar" , "gzip" ))
2536 eq (self .db .guess_type ("foo.tar.gz" ), ("application/x-tar" , "gzip" ))
2637 eq (self .db .guess_type ("foo.tar.Z" ), ("application/x-tar" , "compress" ))
@@ -30,6 +41,7 @@ def test_default_data(self):
3041 def test_data_urls (self ):
3142 eq = self .assertEqual
3243 guess_type = self .db .guess_type
44+ eq (guess_type ("data:invalidDataWithoutComma" ), (None , None ))
3345 eq (guess_type ("data:,thisIsTextPlain" ), ("text/plain" , None ))
3446 eq (guess_type ("data:;base64,thisIsTextPlain" ), ("text/plain" , None ))
3547 eq (guess_type ("data:text/x-foo,thisIsTextXFoo" ), ("text/x-foo" , None ))
@@ -42,14 +54,30 @@ def test_file_parsing(self):
4254 ("x-application/x-unittest" , None ))
4355 eq (self .db .guess_extension ("x-application/x-unittest" ), ".pyunit" )
4456
57+ def test_read_mime_types (self ):
58+ eq = self .assertEqual
59+
60+ # Unreadable file returns None
61+ self .assertIsNone (mimetypes .read_mime_types ("non-existent" ))
62+
63+ with support .temp_dir () as directory :
64+ data = "x-application/x-unittest pyunit\n "
65+ file = pathlib .Path (directory , "sample.mimetype" )
66+ file .write_text (data )
67+ mime_dict = mimetypes .read_mime_types (file )
68+ eq (mime_dict [".pyunit" ], "x-application/x-unittest" )
69+
4570 def test_non_standard_types (self ):
4671 eq = self .assertEqual
4772 # First try strict
4873 eq (self .db .guess_type ('foo.xul' , strict = True ), (None , None ))
4974 eq (self .db .guess_extension ('image/jpg' , strict = True ), None )
5075 # And then non-strict
5176 eq (self .db .guess_type ('foo.xul' , strict = False ), ('text/xul' , None ))
77+ eq (self .db .guess_type ('foo.XUL' , strict = False ), ('text/xul' , None ))
78+ eq (self .db .guess_type ('foo.invalid' , strict = False ), (None , None ))
5279 eq (self .db .guess_extension ('image/jpg' , strict = False ), '.jpg' )
80+ eq (self .db .guess_extension ('image/JPG' , strict = False ), '.jpg' )
5381
5482 def test_filename_with_url_delimiters (self ):
5583 # bpo-38449: URL delimiters cases should be handled also.
@@ -200,5 +228,53 @@ def test__all__(self):
200228 support .check__all__ (self , mimetypes )
201229
202230
231+ class MimetypesCliTestCase (unittest .TestCase ):
232+
233+ def mimetypes_cmd (self , * args , ** kwargs ):
234+ support .patch (self , sys , "argv" , [sys .executable , * args ])
235+ with support .captured_stdout () as output :
236+ mimetypes ._main ()
237+ return output .getvalue ().strip ()
238+
239+ def test_help_option (self ):
240+ support .patch (self , sys , "argv" , [sys .executable , "-h" ])
241+ with support .captured_stdout () as output :
242+ with self .assertRaises (SystemExit ) as cm :
243+ mimetypes ._main ()
244+
245+ self .assertIn ("Usage: mimetypes.py" , output .getvalue ())
246+ self .assertEqual (cm .exception .code , 0 )
247+
248+ def test_invalid_option (self ):
249+ support .patch (self , sys , "argv" , [sys .executable , "--invalid" ])
250+ with support .captured_stdout () as output :
251+ with self .assertRaises (SystemExit ) as cm :
252+ mimetypes ._main ()
253+
254+ self .assertIn ("Usage: mimetypes.py" , output .getvalue ())
255+ self .assertEqual (cm .exception .code , 1 )
256+
257+ def test_guess_extension (self ):
258+ eq = self .assertEqual
259+
260+ extension = self .mimetypes_cmd ("-l" , "-e" , "image/jpg" )
261+ eq (extension , ".jpg" )
262+
263+ extension = self .mimetypes_cmd ("-e" , "image/jpg" )
264+ eq (extension , "I don't know anything about type image/jpg" )
265+
266+ extension = self .mimetypes_cmd ("-e" , "image/jpeg" )
267+ eq (extension , ".jpg" )
268+
269+ def test_guess_type (self ):
270+ eq = self .assertEqual
271+
272+ type_info = self .mimetypes_cmd ("-l" , "foo.pic" )
273+ eq (type_info , "type: image/pict encoding: None" )
274+
275+ type_info = self .mimetypes_cmd ("foo.pic" )
276+ eq (type_info , "I don't know anything about type foo.pic" )
277+
278+
203279if __name__ == "__main__" :
204280 unittest .main ()
0 commit comments