|
13 | 13 | import unittest |
14 | 14 | import textwrap |
15 | 15 | import errno |
| 16 | +import shutil |
16 | 17 |
|
| 18 | +import test.support |
17 | 19 | from test.support import ( |
18 | 20 | EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, |
19 | 21 | make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, |
@@ -690,13 +692,64 @@ def test_recompute_pyc_same_second(self): |
690 | 692 | self.assertEqual(m.x, 5) |
691 | 693 |
|
692 | 694 |
|
| 695 | +class TestSymbolicallyLinkedPackage(unittest.TestCase): |
| 696 | + package_name = 'sample' |
| 697 | + |
| 698 | + def setUp(self): |
| 699 | + if os.path.exists(self.tagged): |
| 700 | + shutil.rmtree(self.tagged) |
| 701 | + if os.path.exists(self.package_name): |
| 702 | + os.remove(self.package_name) |
| 703 | + self.orig_sys_path = sys.path[:] |
| 704 | + |
| 705 | + # create a sample package; imagine you have a package with a tag and |
| 706 | + # you want to symbolically link it from its untagged name. |
| 707 | + os.mkdir(self.tagged) |
| 708 | + init_file = os.path.join(self.tagged, '__init__.py') |
| 709 | + open(init_file, 'w').close() |
| 710 | + assert os.path.exists(init_file) |
| 711 | + |
| 712 | + # now create a symlink to the tagged package |
| 713 | + # sample -> sample-tagged |
| 714 | + os.symlink(self.tagged, self.package_name) |
| 715 | + |
| 716 | + # assert os.path.isdir(self.package_name) # currently fails |
| 717 | + assert os.path.isfile(os.path.join(self.package_name, '__init__.py')) |
| 718 | + |
| 719 | + @property |
| 720 | + def tagged(self): |
| 721 | + return self.package_name + '-tagged' |
| 722 | + |
| 723 | + # regression test for issue6727 |
| 724 | + @unittest.skipUnless( |
| 725 | + not hasattr(sys, 'getwindowsversion') |
| 726 | + or sys.getwindowsversion() >= (6, 0), |
| 727 | + "Windows Vista or later required") |
| 728 | + @test.support.skip_unless_symlink |
| 729 | + def test_symlinked_dir_importable(self): |
| 730 | + # make sure sample can only be imported from the current directory. |
| 731 | + sys.path[:] = ['.'] |
| 732 | + |
| 733 | + # and try to import the package |
| 734 | + __import__(self.package_name) |
| 735 | + |
| 736 | + def tearDown(self): |
| 737 | + # now cleanup |
| 738 | + if os.path.exists(self.package_name): |
| 739 | + os.remove(self.package_name) |
| 740 | + if os.path.exists(self.tagged): |
| 741 | + shutil.rmtree(self.tagged) |
| 742 | + sys.path[:] = self.orig_sys_path |
| 743 | + |
| 744 | + |
693 | 745 | def test_main(verbose=None): |
694 | 746 | flag = importlib_util.using___import__ |
695 | 747 | try: |
696 | 748 | importlib_util.using___import__ = True |
697 | 749 | run_unittest(ImportTests, PycacheTests, |
698 | 750 | PycRewritingTests, PathsTests, RelativeImportTests, |
699 | 751 | OverridingImportBuiltinTests, |
| 752 | + TestSymbolicallyLinkedPackage, |
700 | 753 | importlib_import_test_suite()) |
701 | 754 | finally: |
702 | 755 | importlib_util.using___import__ = flag |
|
0 commit comments