Skip to content

Commit 1ef2b9c

Browse files
committed
gh-131178: Add tests for mimetypes command-line interface
1 parent 56171da commit 1ef2b9c

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

Lib/test/test_mimetypes.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import unittest.mock
77
from platform import win32_edition
88
from test import support
9-
from test.support import cpython_only, force_not_colorized, os_helper
9+
from test.support import cpython_only, force_not_colorized, os_helper, requires_subprocess
1010
from test.support.import_helper import ensure_lazy_imports
11+
from test.support.script_helper import assert_python_ok, assert_python_failure
1112

1213
try:
1314
import _winapi
@@ -508,5 +509,51 @@ def test_invocation_error(self):
508509
self.assertEqual(result, expected)
509510

510511

512+
@requires_subprocess()
513+
class CommandLineSubprocessTest(unittest.TestCase):
514+
def test_help(self):
515+
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '--help')
516+
self.assertIn(b'mimetypes', stdout)
517+
self.assertIn(b'--help', stdout)
518+
self.assertIn(b'--extension', stdout)
519+
self.assertIn(b'--lenient', stdout)
520+
521+
def test_type_lookup(self):
522+
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf')
523+
self.assertIn(b'application/pdf', stdout)
524+
525+
def test_type_lookup_unknown(self):
526+
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', 'foo.unknownext12345')
527+
self.assertIn(b'error:', stdout)
528+
529+
def test_extension_flag(self):
530+
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', 'image/jpeg')
531+
self.assertIn(b'.jpg', stdout)
532+
533+
def test_extension_flag_unknown(self):
534+
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '-e', 'image/unknowntype12345')
535+
self.assertIn(b'error:', stdout)
536+
537+
def test_lenient_flag(self):
538+
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '--lenient', 'foo.webp')
539+
self.assertIn(b'image/webp', stdout)
540+
541+
def test_multiple_inputs(self):
542+
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf', 'foo.png')
543+
self.assertIn(b'application/pdf', stdout)
544+
self.assertIn(b'image/png', stdout)
545+
546+
def test_multiple_inputs_with_error(self):
547+
rc, stdout, stderr = assert_python_failure(
548+
'-m', 'mimetypes', 'foo.pdf', 'foo.unknownext12345'
549+
)
550+
self.assertIn(b'application/pdf', stdout)
551+
self.assertIn(b'error:', stdout)
552+
553+
def test_unknown_flag(self):
554+
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '--unknown-flag')
555+
self.assertNotEqual(rc, 0)
556+
557+
511558
if __name__ == "__main__":
512559
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for the :mod:`mimetypes` command-line interface.

0 commit comments

Comments
 (0)