8
8
from test import support
9
9
from platform import win32_edition
10
10
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
15
25
16
26
17
27
class MimeTypesTestCase (unittest .TestCase ):
@@ -21,6 +31,7 @@ def setUp(self):
21
31
def test_default_data (self ):
22
32
eq = self .assertEqual
23
33
eq (self .db .guess_type ("foo.html" ), ("text/html" , None ))
34
+ eq (self .db .guess_type ("foo.HTML" ), ("text/html" , None ))
24
35
eq (self .db .guess_type ("foo.tgz" ), ("application/x-tar" , "gzip" ))
25
36
eq (self .db .guess_type ("foo.tar.gz" ), ("application/x-tar" , "gzip" ))
26
37
eq (self .db .guess_type ("foo.tar.Z" ), ("application/x-tar" , "compress" ))
@@ -30,6 +41,7 @@ def test_default_data(self):
30
41
def test_data_urls (self ):
31
42
eq = self .assertEqual
32
43
guess_type = self .db .guess_type
44
+ eq (guess_type ("data:invalidDataWithoutComma" ), (None , None ))
33
45
eq (guess_type ("data:,thisIsTextPlain" ), ("text/plain" , None ))
34
46
eq (guess_type ("data:;base64,thisIsTextPlain" ), ("text/plain" , None ))
35
47
eq (guess_type ("data:text/x-foo,thisIsTextXFoo" ), ("text/x-foo" , None ))
@@ -42,14 +54,30 @@ def test_file_parsing(self):
42
54
("x-application/x-unittest" , None ))
43
55
eq (self .db .guess_extension ("x-application/x-unittest" ), ".pyunit" )
44
56
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
+
45
70
def test_non_standard_types (self ):
46
71
eq = self .assertEqual
47
72
# First try strict
48
73
eq (self .db .guess_type ('foo.xul' , strict = True ), (None , None ))
49
74
eq (self .db .guess_extension ('image/jpg' , strict = True ), None )
50
75
# And then non-strict
51
76
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 ))
52
79
eq (self .db .guess_extension ('image/jpg' , strict = False ), '.jpg' )
80
+ eq (self .db .guess_extension ('image/JPG' , strict = False ), '.jpg' )
53
81
54
82
def test_filename_with_url_delimiters (self ):
55
83
# bpo-38449: URL delimiters cases should be handled also.
@@ -200,5 +228,53 @@ def test__all__(self):
200
228
support .check__all__ (self , mimetypes )
201
229
202
230
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
+
203
279
if __name__ == "__main__" :
204
280
unittest .main ()
0 commit comments