Skip to content

Commit 53908bd

Browse files
authored
gh-93096: fix test_mimetypes.test_guess_type_conflicting_with_mimetypes (#131408)
1 parent 8c9ef8f commit 53908bd

File tree

2 files changed

+64
-63
lines changed

2 files changed

+64
-63
lines changed

Lib/mimetypes.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,7 @@ def _default_mime_types():
669669
_default_mime_types()
670670

671671

672-
def _main():
673-
"""Run the mimetypes command-line interface."""
674-
import sys
672+
def _parse_args(args):
675673
from argparse import ArgumentParser
676674

677675
parser = ArgumentParser(description='map filename extensions to MIME types')
@@ -686,23 +684,30 @@ def _main():
686684
help='additionally search for common but non-standard types'
687685
)
688686
parser.add_argument('type', nargs='+', help='a type to search')
689-
args = parser.parse_args()
687+
args = parser.parse_args(args)
688+
return args, parser.format_help()
689+
690+
691+
def _main(args=None):
692+
"""Run the mimetypes command-line interface and return a text to print."""
693+
import sys
694+
695+
args, help_text = _parse_args(args)
690696

691697
if args.extension:
692698
for gtype in args.type:
693699
guess = guess_extension(gtype, not args.lenient)
694700
if guess:
695-
print(guess)
696-
else:
697-
sys.exit(f"error: unknown type {gtype}")
701+
return str(guess)
702+
sys.exit(f"error: unknown type {gtype}")
698703
else:
699704
for gtype in args.type:
700705
guess, encoding = guess_type(gtype, not args.lenient)
701706
if guess:
702-
print('type:', guess, 'encoding:', encoding)
703-
else:
704-
sys.exit(f"error: media type unknown for {gtype}")
707+
return f"type: {guess} encoding: {encoding}"
708+
sys.exit(f"error: media type unknown for {gtype}")
709+
return parser.format_help()
705710

706711

707712
if __name__ == '__main__':
708-
_main()
713+
print(_main())

Lib/test/test_mimetypes.py

+48-52
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import io
22
import mimetypes
33
import os
4+
import shlex
45
import sys
56
import unittest.mock
6-
from os import linesep
7-
7+
from platform import win32_edition
88
from test import support
99
from test.support import os_helper
1010
from test.support.script_helper import run_python_until_end
11-
from platform import win32_edition
1211

1312
try:
1413
import _winapi
@@ -390,55 +389,52 @@ def test__all__(self):
390389
support.check__all__(self, mimetypes)
391390

392391

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+
442438

443439
if __name__ == "__main__":
444440
unittest.main()

0 commit comments

Comments
 (0)