Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 62863a0

Browse filesBrowse files
committed
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.
1 parent ab6333f commit 62863a0
Copy full SHA for 62863a0

File tree

2 files changed

+23
-12
lines changed
Filter options

2 files changed

+23
-12
lines changed

‎Lib/mimetypes.py

Copy file name to clipboardExpand all lines: Lib/mimetypes.py
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def _default_mime_types():
669669
_default_mime_types()
670670

671671

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

691691
if args.extension:
692692
for gtype in args.type:
693693
guess = guess_extension(gtype, not args.lenient)
694694
if guess:
695695
print(guess)
696696
else:
697-
sys.exit(f"error: unknown type {gtype}")
697+
print(f"error: unknown type {gtype}", file=sys.stderr)
698+
sys.exit(1)
698699
else:
699700
for gtype in args.type:
700701
guess, encoding = guess_type(gtype, not args.lenient)
701702
if guess:
702703
print('type:', guess, 'encoding:', encoding)
703704
else:
704-
sys.exit(f"error: media type unknown for {gtype}")
705+
print(f"error: media type unknown for {gtype}", file=sys.stderr)
706+
sys.exit(1)
705707

706708

707709
if __name__ == '__main__':

‎Lib/test/test_mimetypes.py

Copy file name to clipboardExpand all lines: Lib/test/test_mimetypes.py
+17-8Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import contextlib
12
import io
23
import mimetypes
34
import os
45
import sys
6+
import tempfile
57
import unittest.mock
68
from os import linesep
79

@@ -392,9 +394,19 @@ def test__all__(self):
392394

393395
class MimetypesCliTestCase(unittest.TestCase):
394396

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()
397+
def mimetypes_cmd(self, *args):
398+
# We cannot use run_python_until_end() as the latter would not
399+
# call setUpModule() which unsets mimetypes.knowfiles. Instead,
400+
# we need to directly call the main() function in order to avoid
401+
# re-initializing the database.
402+
rc, out, err = 0, io.StringIO(), io.StringIO()
403+
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
404+
try:
405+
mimetypes._main(args)
406+
except SystemExit as exc:
407+
self.assertIsInstance(exc.code, int)
408+
rc = exc.code
409+
return rc, out.getvalue(), err.getvalue()
398410

399411
def test_help_option(self):
400412
retcode, out, err = self.mimetypes_cmd('-h')
@@ -430,15 +442,12 @@ def test_guess_type(self):
430442
self.assertEqual(out, f'type: image/webp encoding: None{linesep}')
431443
self.assertEqual(err, '')
432444

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):
445+
def test_z_guess_type_conflicting_with_mimetypes(self):
438446
retcode, out, err = self.mimetypes_cmd('foo.pic')
439447
self.assertEqual(retcode, 1)
440448
self.assertEqual(out, '')
441449
self.assertEqual(err, f'error: media type unknown for foo.pic{linesep}')
442450

451+
443452
if __name__ == "__main__":
444453
unittest.main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.