Skip to content

Commit 81268a3

Browse files
Wulian233hugovk
andauthored
gh-136507: Fix mimetypes CLI to handle multiple file parameters (GH-136508)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent edf6e68 commit 81268a3

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

Lib/mimetypes.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -718,24 +718,30 @@ def _parse_args(args):
718718

719719
def _main(args=None):
720720
"""Run the mimetypes command-line interface and return a text to print."""
721-
import sys
722-
723721
args, help_text = _parse_args(args)
724722

723+
results = []
725724
if args.extension:
726725
for gtype in args.type:
727726
guess = guess_extension(gtype, not args.lenient)
728727
if guess:
729-
return str(guess)
730-
sys.exit(f"error: unknown type {gtype}")
728+
results.append(str(guess))
729+
else:
730+
results.append(f"error: unknown type {gtype}")
731+
return results
731732
else:
732733
for gtype in args.type:
733734
guess, encoding = guess_type(gtype, not args.lenient)
734735
if guess:
735-
return f"type: {guess} encoding: {encoding}"
736-
sys.exit(f"error: media type unknown for {gtype}")
737-
return help_text
736+
results.append(f"type: {guess} encoding: {encoding}")
737+
else:
738+
results.append(f"error: media type unknown for {gtype}")
739+
return results
738740

739741

740742
if __name__ == '__main__':
741-
print(_main())
743+
import sys
744+
745+
results = _main()
746+
print("\n".join(results))
747+
sys.exit(any(result.startswith("error: ") for result in results))

Lib/test/test_mimetypes.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,22 +470,40 @@ def test_parse_args(self):
470470
self.assertFalse(args.lenient)
471471
self.assertEqual(args.type, ["foo.pic"])
472472

473+
def test_multiple_inputs(self):
474+
result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.png")))
475+
self.assertEqual(
476+
result,
477+
"type: application/pdf encoding: None\n"
478+
"type: image/png encoding: None"
479+
)
480+
481+
def test_multiple_inputs_error(self):
482+
result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.bar_ext")))
483+
self.assertEqual(
484+
result,
485+
"type: application/pdf encoding: None\n"
486+
"error: media type unknown for foo.bar_ext"
487+
)
488+
489+
473490
def test_invocation(self):
474491
for command, expected in [
475492
("-l -e image/jpg", ".jpg"),
476493
("-e image/jpeg", ".jpg"),
477494
("-l foo.webp", "type: image/webp encoding: None"),
478495
]:
479-
self.assertEqual(mimetypes._main(shlex.split(command)), expected)
496+
result = "\n".join(mimetypes._main(shlex.split(command)))
497+
self.assertEqual(result, expected)
480498

481499
def test_invocation_error(self):
482500
for command, expected in [
483501
("-e image/jpg", "error: unknown type image/jpg"),
484502
("foo.bar_ext", "error: media type unknown for foo.bar_ext"),
485503
]:
486504
with self.subTest(command=command):
487-
with self.assertRaisesRegex(SystemExit, expected):
488-
mimetypes._main(shlex.split(command))
505+
result = "\n".join(mimetypes._main(shlex.split(command)))
506+
self.assertEqual(result, expected)
489507

490508

491509
if __name__ == "__main__":
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix mimetypes CLI to handle multiple file parameters.

0 commit comments

Comments
 (0)