|
1 | 1 | import io |
2 | 2 | import mimetypes |
3 | 3 | import os |
| 4 | +import pathlib |
4 | 5 | import shlex |
5 | 6 | import sys |
| 7 | +import unittest |
6 | 8 | import unittest.mock |
| 9 | +import warnings |
7 | 10 | from platform import win32_edition |
8 | 11 | from test import support |
9 | 12 | from test.support import cpython_only, force_not_colorized, os_helper, requires_subprocess |
@@ -489,6 +492,94 @@ def test_keywords_args_api(self): |
489 | 492 | type='image/jpeg', strict=True), ['.jpg', '.jpe', '.jpeg']) |
490 | 493 |
|
491 | 494 |
|
| 495 | +class GuessTypeDeprecationTestCase(unittest.TestCase): |
| 496 | + """Tests that guess_type() emits DeprecationWarning for file path inputs.""" |
| 497 | + |
| 498 | + def setUp(self): |
| 499 | + self.db = mimetypes.MimeTypes() |
| 500 | + |
| 501 | + # --- Module-level function tests --- |
| 502 | + |
| 503 | + def test_module_plain_string_path_warns(self): |
| 504 | + """Module-level guess_type() warns for a plain string with no URL scheme.""" |
| 505 | + with self.assertWarns(DeprecationWarning) as cm: |
| 506 | + mimetypes.guess_type("file.txt") |
| 507 | + self.assertIn("guess_file_type", str(cm.warning)) |
| 508 | + self.assertIn("deprecated", str(cm.warning).lower()) |
| 509 | + |
| 510 | + def test_module_pathlike_warns(self): |
| 511 | + """Module-level guess_type() warns for a path-like object.""" |
| 512 | + with self.assertWarns(DeprecationWarning): |
| 513 | + mimetypes.guess_type(pathlib.Path("file.txt")) |
| 514 | + |
| 515 | + def test_module_bytes_path_warns(self): |
| 516 | + """Module-level guess_type() warns for a bytes path.""" |
| 517 | + with self.assertWarns(DeprecationWarning): |
| 518 | + mimetypes.guess_type(b"file.txt") |
| 519 | + |
| 520 | + def test_module_url_with_scheme_no_warning(self): |
| 521 | + """Module-level guess_type() does NOT warn for a proper URL.""" |
| 522 | + with warnings.catch_warnings(): |
| 523 | + warnings.simplefilter("error", DeprecationWarning) |
| 524 | + # Should not raise -- http:// is a valid multi-char scheme. |
| 525 | + mimetypes.guess_type("http://example.com/file.html") |
| 526 | + |
| 527 | + def test_module_data_url_no_warning(self): |
| 528 | + """Module-level guess_type() does NOT warn for data: URLs.""" |
| 529 | + with warnings.catch_warnings(): |
| 530 | + warnings.simplefilter("error", DeprecationWarning) |
| 531 | + mimetypes.guess_type("data:text/plain,hello") |
| 532 | + |
| 533 | + # --- MimeTypes class method tests --- |
| 534 | + |
| 535 | + def test_method_plain_string_path_warns(self): |
| 536 | + """MimeTypes.guess_type() warns for a plain string with no URL scheme.""" |
| 537 | + with self.assertWarns(DeprecationWarning) as cm: |
| 538 | + self.db.guess_type("file.html") |
| 539 | + self.assertIn("guess_file_type", str(cm.warning)) |
| 540 | + |
| 541 | + def test_method_pathlike_warns(self): |
| 542 | + """MimeTypes.guess_type() warns for a path-like object.""" |
| 543 | + with self.assertWarns(DeprecationWarning): |
| 544 | + self.db.guess_type(pathlib.Path("file.html")) |
| 545 | + |
| 546 | + def test_method_bytes_path_warns(self): |
| 547 | + """MimeTypes.guess_type() warns for a bytes path.""" |
| 548 | + with self.assertWarns(DeprecationWarning): |
| 549 | + self.db.guess_type(b"file.html") |
| 550 | + |
| 551 | + def test_method_url_with_scheme_no_warning(self): |
| 552 | + """MimeTypes.guess_type() does NOT warn for a proper URL.""" |
| 553 | + with warnings.catch_warnings(): |
| 554 | + warnings.simplefilter("error", DeprecationWarning) |
| 555 | + self.db.guess_type("http://example.com/file.html") |
| 556 | + |
| 557 | + def test_method_ftp_url_no_warning(self): |
| 558 | + """MimeTypes.guess_type() does NOT warn for an ftp: URL.""" |
| 559 | + with warnings.catch_warnings(): |
| 560 | + warnings.simplefilter("error", DeprecationWarning) |
| 561 | + self.db.guess_type("ftp://example.com/file.tar.gz") |
| 562 | + |
| 563 | + def test_result_unchanged(self): |
| 564 | + """guess_type() with a file path still returns the correct MIME type.""" |
| 565 | + with self.assertWarns(DeprecationWarning): |
| 566 | + result = mimetypes.guess_type("file.html") |
| 567 | + expected = mimetypes.guess_file_type("file.html") |
| 568 | + self.assertEqual(result, expected) |
| 569 | + |
| 570 | + def test_result_unchanged_pathlike(self): |
| 571 | + """guess_type() with a PathLike still returns the correct MIME type.""" |
| 572 | + with self.assertWarns(DeprecationWarning): |
| 573 | + result = self.db.guess_type(pathlib.Path("file.tar.gz")) |
| 574 | + expected = self.db.guess_file_type(pathlib.Path("file.tar.gz")) |
| 575 | + self.assertEqual(result, expected) |
| 576 | + |
| 577 | + def test_os_helper_fakepath_warns(self): |
| 578 | + """guess_type() warns for os_helper.FakePath (a path-like object).""" |
| 579 | + with self.assertWarns(DeprecationWarning): |
| 580 | + self.db.guess_type(os_helper.FakePath("file.tar.gz")) |
| 581 | + |
| 582 | + |
492 | 583 | @unittest.skipUnless(sys.platform.startswith("win"), "Windows only") |
493 | 584 | class Win32MimeTypesTestCase(unittest.TestCase): |
494 | 585 | def setUp(self): |
|
0 commit comments