mirror of
https://github.com/python/cpython.git
synced 2025-11-24 12:20:42 +00:00
gh-136507: Fix mimetypes CLI to handle multiple file parameters (GH-136508)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
parent
edf6e6819b
commit
81268a3e2a
3 changed files with 36 additions and 11 deletions
|
|
@ -718,24 +718,30 @@ def _parse_args(args):
|
|||
|
||||
def _main(args=None):
|
||||
"""Run the mimetypes command-line interface and return a text to print."""
|
||||
import sys
|
||||
|
||||
args, help_text = _parse_args(args)
|
||||
|
||||
results = []
|
||||
if args.extension:
|
||||
for gtype in args.type:
|
||||
guess = guess_extension(gtype, not args.lenient)
|
||||
if guess:
|
||||
return str(guess)
|
||||
sys.exit(f"error: unknown type {gtype}")
|
||||
results.append(str(guess))
|
||||
else:
|
||||
results.append(f"error: unknown type {gtype}")
|
||||
return results
|
||||
else:
|
||||
for gtype in args.type:
|
||||
guess, encoding = guess_type(gtype, not args.lenient)
|
||||
if guess:
|
||||
return f"type: {guess} encoding: {encoding}"
|
||||
sys.exit(f"error: media type unknown for {gtype}")
|
||||
return help_text
|
||||
results.append(f"type: {guess} encoding: {encoding}")
|
||||
else:
|
||||
results.append(f"error: media type unknown for {gtype}")
|
||||
return results
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(_main())
|
||||
import sys
|
||||
|
||||
results = _main()
|
||||
print("\n".join(results))
|
||||
sys.exit(any(result.startswith("error: ") for result in results))
|
||||
|
|
|
|||
|
|
@ -470,13 +470,31 @@ class CommandLineTest(unittest.TestCase):
|
|||
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 [
|
||||
|
|
@ -484,8 +502,8 @@ class CommandLineTest(unittest.TestCase):
|
|||
("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__":
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fix mimetypes CLI to handle multiple file parameters.
|
||||
Loading…
Add table
Add a link
Reference in a new issue