Skip to content

gh-93096: fix test_mimetypes.test_guess_type_conflicting_with_mimetypes #131408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 8, 2025
Merged
10 changes: 6 additions & 4 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ def _default_mime_types():
_default_mime_types()


def _main():
def _main(args=None):
"""Run the mimetypes command-line interface."""
import sys
from argparse import ArgumentParser
Expand All @@ -686,22 +686,24 @@ def _main():
help='additionally search for common but non-standard types'
)
parser.add_argument('type', nargs='+', help='a type to search')
args = parser.parse_args()
args = parser.parse_args(args)

if args.extension:
for gtype in args.type:
guess = guess_extension(gtype, not args.lenient)
if guess:
print(guess)
else:
sys.exit(f"error: unknown type {gtype}")
# do not use parser.error() as it prints a help message
parser.exit(1, f"error: unknown type {gtype}\n")
else:
for gtype in args.type:
guess, encoding = guess_type(gtype, not args.lenient)
if guess:
print('type:', guess, 'encoding:', encoding)
else:
sys.exit(f"error: media type unknown for {gtype}")
# do not use parser.error() as it prints a help message
parser.exit(1, f"error: media type unknown for {gtype}\n")


if __name__ == '__main__':
Expand Down
36 changes: 21 additions & 15 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import contextlib
import io
import mimetypes
import os
import sys
import unittest.mock
from os import linesep

from platform import win32_edition
from test import support
from test.support import os_helper
from test.support.script_helper import run_python_until_end
from platform import win32_edition

try:
import _winapi
Expand Down Expand Up @@ -392,9 +391,19 @@ def test__all__(self):

class MimetypesCliTestCase(unittest.TestCase):

def mimetypes_cmd(cls, *args, **kwargs):
result, _ = run_python_until_end('-m', 'mimetypes', *args)
return result.rc, result.out.decode(), result.err.decode()
def mimetypes_cmd(self, *args):
# We cannot use run_python_until_end() as the latter would not
# call setUpModule() which unsets mimetypes.knowfiles. Instead,
# we need to directly call the main() function in order to avoid
# re-initializing the database.
rc, out, err = 0, io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
try:
mimetypes._main(args)
except SystemExit as exc:
self.assertIsInstance(exc.code, int)
rc = exc.code
return rc, out.getvalue(), err.getvalue()

def test_help_option(self):
retcode, out, err = self.mimetypes_cmd('-h')
Expand All @@ -411,34 +420,31 @@ def test_invalid_option(self):
def test_guess_extension(self):
retcode, out, err = self.mimetypes_cmd('-l', '-e', 'image/jpg')
self.assertEqual(retcode, 0)
self.assertEqual(out, f'.jpg{linesep}')
self.assertEqual(out, '.jpg\n')
self.assertEqual(err, '')

retcode, out, err = self.mimetypes_cmd('-e', 'image/jpg')
self.assertEqual(retcode, 1)
self.assertEqual(out, '')
self.assertEqual(err, f'error: unknown type image/jpg{linesep}')
self.assertEqual(err, 'error: unknown type image/jpg\n')

retcode, out, err = self.mimetypes_cmd('-e', 'image/jpeg')
self.assertEqual(retcode, 0)
self.assertEqual(out, f'.jpg{linesep}')
self.assertEqual(out, '.jpg\n')
self.assertEqual(err, '')

def test_guess_type(self):
retcode, out, err = self.mimetypes_cmd('-l', 'foo.webp')
self.assertEqual(retcode, 0)
self.assertEqual(out, f'type: image/webp encoding: None{linesep}')
self.assertEqual(out, 'type: image/webp encoding: None\n')
self.assertEqual(err, '')

@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}')
self.assertEqual(err, 'error: media type unknown for foo.pic\n')


if __name__ == "__main__":
unittest.main()
Loading