gh-136507: Fix mimetypes CLI to handle multiple file parameters by Wulian233 · Pull Request #136508 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Lib/mimetypes.py
24 changes: 21 additions & 3 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,40 @@ def test_parse_args(self):
self.assertFalse(args.lenient)
self.assertEqual(args.type, ["foo.pic"])

def test_multiple_inputs(self):
result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.png")))
self.assertEqual(
result,
"type: application/pdf encoding: None\n"
"type: image/png encoding: None"
)

def test_multiple_inputs_error(self):
result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.bar_ext")))
self.assertEqual(
result,
"type: application/pdf encoding: None\n"
"error: media type unknown for foo.bar_ext"
)


def test_invocation(self):
for command, expected in [
("-l -e image/jpg", ".jpg"),
("-e image/jpeg", ".jpg"),
("-l foo.webp", "type: image/webp encoding: None"),
]:
self.assertEqual(mimetypes._main(shlex.split(command)), expected)
result = "\n".join(mimetypes._main(shlex.split(command)))
self.assertEqual(result, expected)

def test_invocation_error(self):
for command, expected in [
("-e image/jpg", "error: unknown type image/jpg"),
("foo.bar_ext", "error: media type unknown for foo.bar_ext"),
]:
with self.subTest(command=command):
with self.assertRaisesRegex(SystemExit, expected):
mimetypes._main(shlex.split(command))
result = "\n".join(mimetypes._main(shlex.split(command)))
self.assertEqual(result, expected)


if __name__ == "__main__":
Expand Down
Loading