From 6a0fcedd8225953c4764cd83122ef2777bd3f6d4 Mon Sep 17 00:00:00 2001 From: Erik Bray Date: Thu, 31 Dec 2015 14:17:41 -0500 Subject: Fixes the original root cause of #231, and re-enables the test when the tempdir is a symlink (this does not explicitly test that /tmp itself is a symlink, but the effect is the same--only one of the path levels needs to be a symlink to reproduce this isssue) --- pkg_resources/__init__.py | 9 +++++++-- pkg_resources/tests/test_resources.py | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 7becc951..7d2fa7e9 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -2182,9 +2182,14 @@ def _handle_ns(packageName, path_item): path = module.__path__ path.append(subpath) loader.load_module(packageName) + + # Ensure that all paths on __path__ have been run through + # normalize_path + normalized_paths = set(_normalize_cached(p) for p in module.__path__) for path_item in path: - if path_item not in module.__path__: - module.__path__.append(path_item) + normalized = _normalize_cached(path_item) + if normalized not in normalized_paths: + module.__path__.append(normalized) return subpath def declare_namespace(packageName): diff --git a/pkg_resources/tests/test_resources.py b/pkg_resources/tests/test_resources.py index 4241765a..ba12d857 100644 --- a/pkg_resources/tests/test_resources.py +++ b/pkg_resources/tests/test_resources.py @@ -610,18 +610,32 @@ class TestNamespaces: def setup_method(self, method): self._ns_pkgs = pkg_resources._namespace_packages.copy() - self._tmpdir = tempfile.mkdtemp(prefix="tests-setuptools-") + + # Further, test case where the temp dir is a symlink, where applicable + # See #231 + if hasattr(os, 'symlink'): + real_tmpdir = tempfile.mkdtemp(prefix="real-tests-setuptools-") + tmpdir_base, tmpdir_name = os.path.split(real_tmpdir) + tmpdir = os.path.join(tmpdir_base, tmpdir_name[5:]) + os.symlink(real_tmpdir, tmpdir) + self._real_tmpdir = real_tmpdir + self._tmpdir = tmpdir + else: + tmpdir = tempfile.mkdtemp(prefix="tests-setuptools-") + self._real_tmpdir = self._tmpdir = tmpdir + os.makedirs(os.path.join(self._tmpdir, "site-pkgs")) self._prev_sys_path = sys.path[:] sys.path.append(os.path.join(self._tmpdir, "site-pkgs")) def teardown_method(self, method): - shutil.rmtree(self._tmpdir) + shutil.rmtree(self._real_tmpdir) + if os.path.islink(self._tmpdir): + os.unlink(self._tmpdir) + pkg_resources._namespace_packages = self._ns_pkgs.copy() sys.path = self._prev_sys_path[:] - @pytest.mark.skipif(os.path.islink(tempfile.gettempdir()), - reason="Test fails when /tmp is a symlink. See #231") def test_two_levels_deep(self): """ Test nested namespace packages @@ -653,7 +667,7 @@ class TestNamespaces: assert pkg_resources._namespace_packages["pkg1"] == ["pkg1.pkg2"] # check the __path__ attribute contains both paths expected = [ - os.path.join(self._tmpdir, "site-pkgs", "pkg1", "pkg2"), - os.path.join(self._tmpdir, "site-pkgs2", "pkg1", "pkg2"), + os.path.join(self._real_tmpdir, "site-pkgs", "pkg1", "pkg2"), + os.path.join(self._real_tmpdir, "site-pkgs2", "pkg1", "pkg2"), ] assert pkg1.pkg2.__path__ == expected -- cgit v1.2.1