gh-93096: fix test_mimetypes.test_guess_type_conflicting_with_mimetypes (#131408)
This commit is contained in:
@@ -669,9 +669,7 @@ def _default_mime_types():
|
|||||||
_default_mime_types()
|
_default_mime_types()
|
||||||
|
|
||||||
|
|
||||||
def _main():
|
def _parse_args(args):
|
||||||
"""Run the mimetypes command-line interface."""
|
|
||||||
import sys
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
parser = ArgumentParser(description='map filename extensions to MIME types')
|
parser = ArgumentParser(description='map filename extensions to MIME types')
|
||||||
@@ -686,23 +684,30 @@ def _main():
|
|||||||
help='additionally search for common but non-standard types'
|
help='additionally search for common but non-standard types'
|
||||||
)
|
)
|
||||||
parser.add_argument('type', nargs='+', help='a type to search')
|
parser.add_argument('type', nargs='+', help='a type to search')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args(args)
|
||||||
|
return args, parser.format_help()
|
||||||
|
|
||||||
|
|
||||||
|
def _main(args=None):
|
||||||
|
"""Run the mimetypes command-line interface and return a text to print."""
|
||||||
|
import sys
|
||||||
|
|
||||||
|
args, help_text = _parse_args(args)
|
||||||
|
|
||||||
if args.extension:
|
if args.extension:
|
||||||
for gtype in args.type:
|
for gtype in args.type:
|
||||||
guess = guess_extension(gtype, not args.lenient)
|
guess = guess_extension(gtype, not args.lenient)
|
||||||
if guess:
|
if guess:
|
||||||
print(guess)
|
return str(guess)
|
||||||
else:
|
|
||||||
sys.exit(f"error: unknown type {gtype}")
|
sys.exit(f"error: unknown type {gtype}")
|
||||||
else:
|
else:
|
||||||
for gtype in args.type:
|
for gtype in args.type:
|
||||||
guess, encoding = guess_type(gtype, not args.lenient)
|
guess, encoding = guess_type(gtype, not args.lenient)
|
||||||
if guess:
|
if guess:
|
||||||
print('type:', guess, 'encoding:', encoding)
|
return f"type: {guess} encoding: {encoding}"
|
||||||
else:
|
|
||||||
sys.exit(f"error: media type unknown for {gtype}")
|
sys.exit(f"error: media type unknown for {gtype}")
|
||||||
|
return parser.format_help()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
_main()
|
print(_main())
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import io
|
import io
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
import unittest.mock
|
import unittest.mock
|
||||||
from os import linesep
|
from platform import win32_edition
|
||||||
|
|
||||||
from test import support
|
from test import support
|
||||||
from test.support import os_helper
|
from test.support import os_helper
|
||||||
from test.support.script_helper import run_python_until_end
|
from test.support.script_helper import run_python_until_end
|
||||||
from platform import win32_edition
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import _winapi
|
import _winapi
|
||||||
@@ -390,55 +389,52 @@ class MiscTestCase(unittest.TestCase):
|
|||||||
support.check__all__(self, mimetypes)
|
support.check__all__(self, mimetypes)
|
||||||
|
|
||||||
|
|
||||||
class MimetypesCliTestCase(unittest.TestCase):
|
class CommandLineTest(unittest.TestCase):
|
||||||
|
def test_parse_args(self):
|
||||||
|
args, help_text = mimetypes._parse_args("-h")
|
||||||
|
self.assertTrue(help_text.startswith("usage: "))
|
||||||
|
|
||||||
def mimetypes_cmd(cls, *args, **kwargs):
|
args, help_text = mimetypes._parse_args("--invalid")
|
||||||
result, _ = run_python_until_end('-m', 'mimetypes', *args)
|
self.assertTrue(help_text.startswith("usage: "))
|
||||||
return result.rc, result.out.decode(), result.err.decode()
|
|
||||||
|
|
||||||
def test_help_option(self):
|
args, _ = mimetypes._parse_args(shlex.split("-l -e image/jpg"))
|
||||||
retcode, out, err = self.mimetypes_cmd('-h')
|
self.assertTrue(args.extension)
|
||||||
self.assertEqual(retcode, 0)
|
self.assertTrue(args.lenient)
|
||||||
self.assertStartsWith(out, 'usage: ')
|
self.assertEqual(args.type, ["image/jpg"])
|
||||||
self.assertEqual(err, '')
|
|
||||||
|
|
||||||
def test_invalid_option(self):
|
args, _ = mimetypes._parse_args(shlex.split("-e image/jpg"))
|
||||||
retcode, out, err = self.mimetypes_cmd('--invalid')
|
self.assertTrue(args.extension)
|
||||||
self.assertEqual(retcode, 2)
|
self.assertFalse(args.lenient)
|
||||||
self.assertEqual(out, '')
|
self.assertEqual(args.type, ["image/jpg"])
|
||||||
self.assertStartsWith(err, 'usage: ')
|
|
||||||
|
|
||||||
def test_guess_extension(self):
|
args, _ = mimetypes._parse_args(shlex.split("-l foo.webp"))
|
||||||
retcode, out, err = self.mimetypes_cmd('-l', '-e', 'image/jpg')
|
self.assertFalse(args.extension)
|
||||||
self.assertEqual(retcode, 0)
|
self.assertTrue(args.lenient)
|
||||||
self.assertEqual(out, f'.jpg{linesep}')
|
self.assertEqual(args.type, ["foo.webp"])
|
||||||
self.assertEqual(err, '')
|
|
||||||
|
|
||||||
retcode, out, err = self.mimetypes_cmd('-e', 'image/jpg')
|
args, _ = mimetypes._parse_args(shlex.split("foo.pic"))
|
||||||
self.assertEqual(retcode, 1)
|
self.assertFalse(args.extension)
|
||||||
self.assertEqual(out, '')
|
self.assertFalse(args.lenient)
|
||||||
self.assertEqual(err, f'error: unknown type image/jpg{linesep}')
|
self.assertEqual(args.type, ["foo.pic"])
|
||||||
|
|
||||||
retcode, out, err = self.mimetypes_cmd('-e', 'image/jpeg')
|
|
||||||
self.assertEqual(retcode, 0)
|
|
||||||
self.assertEqual(out, f'.jpg{linesep}')
|
|
||||||
self.assertEqual(err, '')
|
|
||||||
|
|
||||||
def test_guess_type(self):
|
def test_invocation(self):
|
||||||
retcode, out, err = self.mimetypes_cmd('-l', 'foo.webp')
|
for command, expected in [
|
||||||
self.assertEqual(retcode, 0)
|
("-l -e image/jpg", ".jpg"),
|
||||||
self.assertEqual(out, f'type: image/webp encoding: None{linesep}')
|
("-e image/jpeg", ".jpg"),
|
||||||
self.assertEqual(err, '')
|
("-l foo.webp", "type: image/webp encoding: None"),
|
||||||
|
]:
|
||||||
|
self.assertEqual(mimetypes._main(shlex.split(command)), expected)
|
||||||
|
|
||||||
|
|
||||||
|
def test_invocation_error(self):
|
||||||
|
for command, expected in [
|
||||||
|
("-e image/jpg", "error: unknown type image/jpg"),
|
||||||
|
("foo.pic", "error: media type unknown for foo.pic"),
|
||||||
|
]:
|
||||||
|
with self.assertRaisesRegex(SystemExit, expected):
|
||||||
|
mimetypes._main(shlex.split(command))
|
||||||
|
|
||||||
@unittest.skipIf(
|
|
||||||
sys.platform == 'darwin',
|
|
||||||
'macOS lists common_types in mime.types thus making them always known'
|
|
||||||
)
|
|
||||||
def test_guess_type_conflicting_with_mimetypes(self):
|
|
||||||
retcode, out, err = self.mimetypes_cmd('foo.pic')
|
|
||||||
self.assertEqual(retcode, 1)
|
|
||||||
self.assertEqual(out, '')
|
|
||||||
self.assertEqual(err, f'error: media type unknown for foo.pic{linesep}')
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user