|
| 1 | +import contextlib |
1 | 2 | import io
|
2 | 3 | import mimetypes
|
3 | 4 | import os
|
4 | 5 | import sys
|
| 6 | +import tempfile |
5 | 7 | import unittest.mock
|
6 | 8 | from os import linesep
|
7 | 9 |
|
@@ -392,9 +394,19 @@ def test__all__(self):
|
392 | 394 |
|
393 | 395 | class MimetypesCliTestCase(unittest.TestCase):
|
394 | 396 |
|
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() |
| 397 | + def mimetypes_cmd(self, *args): |
| 398 | + # We cannot use run_python_until_end() as the latter would not |
| 399 | + # call setUpModule() which unsets mimetypes.knowfiles. Instead, |
| 400 | + # we need to directly call the main() function in order to avoid |
| 401 | + # re-initializing the database. |
| 402 | + rc, out, err = 0, io.StringIO(), io.StringIO() |
| 403 | + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): |
| 404 | + try: |
| 405 | + mimetypes._main(args) |
| 406 | + except SystemExit as exc: |
| 407 | + self.assertIsInstance(exc.code, int) |
| 408 | + rc = exc.code |
| 409 | + return rc, out.getvalue(), err.getvalue() |
398 | 410 |
|
399 | 411 | def test_help_option(self):
|
400 | 412 | retcode, out, err = self.mimetypes_cmd('-h')
|
@@ -430,15 +442,12 @@ def test_guess_type(self):
|
430 | 442 | self.assertEqual(out, f'type: image/webp encoding: None{linesep}')
|
431 | 443 | self.assertEqual(err, '')
|
432 | 444 |
|
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): |
| 445 | + def test_z_guess_type_conflicting_with_mimetypes(self): |
438 | 446 | retcode, out, err = self.mimetypes_cmd('foo.pic')
|
439 | 447 | self.assertEqual(retcode, 1)
|
440 | 448 | self.assertEqual(out, '')
|
441 | 449 | self.assertEqual(err, f'error: media type unknown for foo.pic{linesep}')
|
442 | 450 |
|
| 451 | + |
443 | 452 | if __name__ == "__main__":
|
444 | 453 | unittest.main()
|
0 commit comments