Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71fde31

Browse files
committed
Adding test from issue6727 demonstrating that symlink import issue does not occur here in 3.3
1 parent 9680bdb commit 71fde31

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lib/test/test_import.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import unittest
1414
import textwrap
1515
import errno
16+
import shutil
1617

18+
import test.support
1719
from test.support import (
1820
EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
1921
make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
@@ -690,13 +692,64 @@ def test_recompute_pyc_same_second(self):
690692
self.assertEqual(m.x, 5)
691693

692694

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+
693745
def test_main(verbose=None):
694746
flag = importlib_util.using___import__
695747
try:
696748
importlib_util.using___import__ = True
697749
run_unittest(ImportTests, PycacheTests,
698750
PycRewritingTests, PathsTests, RelativeImportTests,
699751
OverridingImportBuiltinTests,
752+
TestSymbolicallyLinkedPackage,
700753
importlib_import_test_suite())
701754
finally:
702755
importlib_util.using___import__ = flag

0 commit comments

Comments
 (0)