Skip to content
Merged
Next Next commit
fix test_mimetypes.test_guess_type_conflicting_with_mimetypes
Using `run_python_until_end()` ignores `setUpModule()`. In particular,
mocking `mimetypes.knownfiles` has no effect for the CLI tests and
leads to issues on platforms defining non-standard MIME types such
as macOS or openSUSE.
  • Loading branch information
picnixz committed Mar 18, 2025
commit 62863a0b655451d8ae1657c118d6c3f62e641ce4
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}")
print(f"error: unknown type {gtype}", file=sys.stderr)
sys.exit(1)
Comment thread
picnixz marked this conversation as resolved.
Outdated
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}")
print(f"error: media type unknown for {gtype}", file=sys.stderr)
sys.exit(1)


if __name__ == '__main__':
Expand Down
25 changes: 17 additions & 8 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import contextlib
Comment thread
picnixz marked this conversation as resolved.
Outdated
import io
import mimetypes
import os
import sys
import tempfile
Comment thread
picnixz marked this conversation as resolved.
Outdated
import unittest.mock
from os import linesep

Expand Down Expand Up @@ -392,9 +394,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 Down Expand Up @@ -430,15 +442,12 @@ def test_guess_type(self):
self.assertEqual(out, f'type: image/webp encoding: None{linesep}')
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):
def test_z_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__":
unittest.main()
Loading