1
1
import io
2
2
import mimetypes
3
3
import os
4
+ import shlex
4
5
import sys
5
6
import unittest .mock
6
- from os import linesep
7
-
7
+ from platform import win32_edition
8
8
from test import support
9
9
from test .support import os_helper
10
10
from test .support .script_helper import run_python_until_end
11
- from platform import win32_edition
12
11
13
12
try :
14
13
import _winapi
@@ -390,55 +389,52 @@ def test__all__(self):
390
389
support .check__all__ (self , mimetypes )
391
390
392
391
393
- class MimetypesCliTestCase (unittest .TestCase ):
394
-
395
- def mimetypes_cmd (cls , * args , ** kwargs ):
396
- result , _ = run_python_until_end ('-m' , 'mimetypes' , * args )
397
- return result .rc , result .out .decode (), result .err .decode ()
398
-
399
- def test_help_option (self ):
400
- retcode , out , err = self .mimetypes_cmd ('-h' )
401
- self .assertEqual (retcode , 0 )
402
- self .assertStartsWith (out , 'usage: ' )
403
- self .assertEqual (err , '' )
404
-
405
- def test_invalid_option (self ):
406
- retcode , out , err = self .mimetypes_cmd ('--invalid' )
407
- self .assertEqual (retcode , 2 )
408
- self .assertEqual (out , '' )
409
- self .assertStartsWith (err , 'usage: ' )
410
-
411
- def test_guess_extension (self ):
412
- retcode , out , err = self .mimetypes_cmd ('-l' , '-e' , 'image/jpg' )
413
- self .assertEqual (retcode , 0 )
414
- self .assertEqual (out , f'.jpg{ linesep } ' )
415
- self .assertEqual (err , '' )
416
-
417
- retcode , out , err = self .mimetypes_cmd ('-e' , 'image/jpg' )
418
- self .assertEqual (retcode , 1 )
419
- self .assertEqual (out , '' )
420
- self .assertEqual (err , f'error: unknown type image/jpg{ linesep } ' )
421
-
422
- retcode , out , err = self .mimetypes_cmd ('-e' , 'image/jpeg' )
423
- self .assertEqual (retcode , 0 )
424
- self .assertEqual (out , f'.jpg{ linesep } ' )
425
- self .assertEqual (err , '' )
426
-
427
- def test_guess_type (self ):
428
- retcode , out , err = self .mimetypes_cmd ('-l' , 'foo.webp' )
429
- self .assertEqual (retcode , 0 )
430
- self .assertEqual (out , f'type: image/webp encoding: None{ linesep } ' )
431
- self .assertEqual (err , '' )
432
-
433
- @unittest .skipIf (
434
- sys .platform == 'darwin' ,
435
- 'macOS lists common_types in mime.types thus making them always known'
436
- )
437
- def test_guess_type_conflicting_with_mimetypes (self ):
438
- retcode , out , err = self .mimetypes_cmd ('foo.pic' )
439
- self .assertEqual (retcode , 1 )
440
- self .assertEqual (out , '' )
441
- self .assertEqual (err , f'error: media type unknown for foo.pic{ linesep } ' )
392
+ class CommandLineTest (unittest .TestCase ):
393
+ def test_parse_args (self ):
394
+ args , help_text = mimetypes ._parse_args ("-h" )
395
+ self .assertTrue (help_text .startswith ("usage: " ))
396
+
397
+ args , help_text = mimetypes ._parse_args ("--invalid" )
398
+ self .assertTrue (help_text .startswith ("usage: " ))
399
+
400
+ args , _ = mimetypes ._parse_args (shlex .split ("-l -e image/jpg" ))
401
+ self .assertTrue (args .extension )
402
+ self .assertTrue (args .lenient )
403
+ self .assertEqual (args .type , ["image/jpg" ])
404
+
405
+ args , _ = mimetypes ._parse_args (shlex .split ("-e image/jpg" ))
406
+ self .assertTrue (args .extension )
407
+ self .assertFalse (args .lenient )
408
+ self .assertEqual (args .type , ["image/jpg" ])
409
+
410
+ args , _ = mimetypes ._parse_args (shlex .split ("-l foo.webp" ))
411
+ self .assertFalse (args .extension )
412
+ self .assertTrue (args .lenient )
413
+ self .assertEqual (args .type , ["foo.webp" ])
414
+
415
+ args , _ = mimetypes ._parse_args (shlex .split ("foo.pic" ))
416
+ self .assertFalse (args .extension )
417
+ self .assertFalse (args .lenient )
418
+ self .assertEqual (args .type , ["foo.pic" ])
419
+
420
+
421
+ def test_invocation (self ):
422
+ for command , expected in [
423
+ ("-l -e image/jpg" , ".jpg" ),
424
+ ("-e image/jpeg" , ".jpg" ),
425
+ ("-l foo.webp" , "type: image/webp encoding: None" ),
426
+ ]:
427
+ self .assertEqual (mimetypes ._main (shlex .split (command )), expected )
428
+
429
+
430
+ def test_invocation_error (self ):
431
+ for command , expected in [
432
+ ("-e image/jpg" , "error: unknown type image/jpg" ),
433
+ ("foo.pic" , "error: media type unknown for foo.pic" ),
434
+ ]:
435
+ with self .assertRaisesRegex (SystemExit , expected ):
436
+ mimetypes ._main (shlex .split (command ))
437
+
442
438
443
439
if __name__ == "__main__" :
444
440
unittest .main ()
0 commit comments