|
6 | 6 | import unittest.mock |
7 | 7 | from platform import win32_edition |
8 | 8 | 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 |
10 | 10 | from test.support.import_helper import ensure_lazy_imports |
| 11 | +from test.support.script_helper import assert_python_ok, assert_python_failure |
11 | 12 |
|
12 | 13 | try: |
13 | 14 | import _winapi |
@@ -508,5 +509,57 @@ def test_invocation_error(self): |
508 | 509 | self.assertEqual(result, expected) |
509 | 510 |
|
510 | 511 |
|
| 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'--extension', stdout) |
| 518 | + self.assertIn(b'--lenient', stdout) |
| 519 | + |
| 520 | + def test_type_lookup(self): |
| 521 | + rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf') |
| 522 | + self.assertIn(b'application/pdf', stdout) |
| 523 | + self.assertEqual(stderr, b'') |
| 524 | + |
| 525 | + def test_type_lookup_unknown(self): |
| 526 | + rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', 'foo.unknownext12345') |
| 527 | + self.assertEqual(stdout, b'') |
| 528 | + self.assertIn(b'error:', stderr) |
| 529 | + |
| 530 | + def test_extension_flag(self): |
| 531 | + rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', 'image/jpeg') |
| 532 | + self.assertIn(b'.jpg', stdout) |
| 533 | + self.assertEqual(stderr, b'') |
| 534 | + |
| 535 | + def test_extension_flag_unknown(self): |
| 536 | + rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '-e', 'image/unknowntype12345') |
| 537 | + self.assertEqual(stdout, b'') |
| 538 | + self.assertIn(b'error:', stderr) |
| 539 | + |
| 540 | + def test_lenient_flag(self): |
| 541 | + rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', '--lenient', 'text/xul') |
| 542 | + self.assertIn(b'.xul', stdout) |
| 543 | + self.assertEqual(stderr, b'') |
| 544 | + |
| 545 | + def test_multiple_inputs(self): |
| 546 | + rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf', 'foo.png') |
| 547 | + self.assertIn(b'application/pdf', stdout) |
| 548 | + self.assertIn(b'image/png', stdout) |
| 549 | + self.assertEqual(stderr, b'') |
| 550 | + |
| 551 | + def test_multiple_inputs_with_error(self): |
| 552 | + rc, stdout, stderr = assert_python_failure( |
| 553 | + '-m', 'mimetypes', 'foo.pdf', 'foo.unknownext12345' |
| 554 | + ) |
| 555 | + self.assertIn(b'application/pdf', stdout) |
| 556 | + self.assertNotIn(b'error:', stdout) |
| 557 | + self.assertIn(b'error:', stderr) |
| 558 | + |
| 559 | + def test_unknown_flag(self): |
| 560 | + rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '--unknown-flag') |
| 561 | + self.assertNotEqual(rc, 0) |
| 562 | + |
| 563 | + |
511 | 564 | if __name__ == "__main__": |
512 | 565 | unittest.main() |
0 commit comments