|
3 | 3 | import os
|
4 | 4 | import sys
|
5 | 5 | import unittest.mock
|
| 6 | +from os import linesep |
6 | 7 |
|
7 | 8 | from test import support
|
8 | 9 | from test.support import os_helper
|
| 10 | +from test.support.script_helper import run_python_until_end |
9 | 11 | from platform import win32_edition
|
10 | 12 |
|
11 | 13 | try:
|
@@ -390,50 +392,53 @@ def test__all__(self):
|
390 | 392 |
|
391 | 393 | class MimetypesCliTestCase(unittest.TestCase):
|
392 | 394 |
|
393 |
| - def mimetypes_cmd(self, *args, **kwargs): |
394 |
| - support.patch(self, sys, "argv", [sys.executable, *args]) |
395 |
| - with support.captured_stdout() as output: |
396 |
| - mimetypes._main() |
397 |
| - return output.getvalue().strip() |
| 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() |
398 | 398 |
|
399 | 399 | def test_help_option(self):
|
400 |
| - support.patch(self, sys, "argv", [sys.executable, "-h"]) |
401 |
| - with support.captured_stdout() as output: |
402 |
| - with self.assertRaises(SystemExit) as cm: |
403 |
| - mimetypes._main() |
404 |
| - |
405 |
| - self.assertIn("Usage: mimetypes.py", output.getvalue()) |
406 |
| - self.assertEqual(cm.exception.code, 0) |
| 400 | + retcode, out, err = self.mimetypes_cmd('-h') |
| 401 | + self.assertEqual(retcode, 0) |
| 402 | + self.assertStartsWith(out, 'usage: ') |
| 403 | + self.assertEqual(err, '') |
407 | 404 |
|
408 | 405 | def test_invalid_option(self):
|
409 |
| - support.patch(self, sys, "argv", [sys.executable, "--invalid"]) |
410 |
| - with support.captured_stdout() as output: |
411 |
| - with self.assertRaises(SystemExit) as cm: |
412 |
| - mimetypes._main() |
413 |
| - |
414 |
| - self.assertIn("Usage: mimetypes.py", output.getvalue()) |
415 |
| - self.assertEqual(cm.exception.code, 1) |
| 406 | + retcode, out, err = self.mimetypes_cmd('--invalid') |
| 407 | + self.assertEqual(retcode, 2) |
| 408 | + self.assertEqual(out, '') |
| 409 | + self.assertStartsWith(err, 'usage: ') |
416 | 410 |
|
417 | 411 | def test_guess_extension(self):
|
418 |
| - eq = self.assertEqual |
419 |
| - |
420 |
| - extension = self.mimetypes_cmd("-l", "-e", "image/jpg") |
421 |
| - eq(extension, ".jpg") |
| 412 | + retcode, out, err = self.mimetypes_cmd('-l', '-e', 'image/jpg') |
| 413 | + self.assertEqual(retcode, 0) |
| 414 | + self.assertEqual(out, f'.jpg{linesep}') |
| 415 | + self.assertEqual(err, '') |
422 | 416 |
|
423 |
| - extension = self.mimetypes_cmd("-e", "image/jpg") |
424 |
| - eq(extension, "I don't know anything about type image/jpg") |
| 417 | + retcode, out, err = self.mimetypes_cmd('-e', 'image/jpg') |
| 418 | + self.assertEqual(retcode, 1) |
| 419 | + self.assertEqual(out, '') |
| 420 | + self.assertEqual(err, f'error: unknown type image/jpg{linesep}') |
425 | 421 |
|
426 |
| - extension = self.mimetypes_cmd("-e", "image/jpeg") |
427 |
| - eq(extension, ".jpg") |
| 422 | + retcode, out, err = self.mimetypes_cmd('-e', 'image/jpeg') |
| 423 | + self.assertEqual(retcode, 0) |
| 424 | + self.assertEqual(out, f'.jpg{linesep}') |
| 425 | + self.assertEqual(err, '') |
428 | 426 |
|
429 | 427 | def test_guess_type(self):
|
430 |
| - eq = self.assertEqual |
431 |
| - |
432 |
| - type_info = self.mimetypes_cmd("-l", "foo.pic") |
433 |
| - eq(type_info, "type: image/pict encoding: None") |
434 |
| - |
435 |
| - type_info = self.mimetypes_cmd("foo.pic") |
436 |
| - eq(type_info, "I don't know anything about type foo.pic") |
| 428 | + retcode, out, err = self.mimetypes_cmd('-l', 'foo.webp') |
| 429 | + self.assertEqual(retcode, 0) |
| 430 | + self.assertEqual(out, f'type: image/webp encoding: None{linesep}') |
| 431 | + self.assertEqual(err, '') |
| 432 | + |
| 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): |
| 438 | + retcode, out, err = self.mimetypes_cmd('foo.pic') |
| 439 | + self.assertEqual(retcode, 1) |
| 440 | + self.assertEqual(out, '') |
| 441 | + self.assertEqual(err, f'error: media type unknown for foo.pic{linesep}') |
437 | 442 |
|
438 | 443 | if __name__ == "__main__":
|
439 | 444 | unittest.main()
|
0 commit comments