1from __future__ import absolute_import, unicode_literals 2 3import os 4import sys 5import subprocess 6 7import pytest 8 9from . import namespaces 10from setuptools.command import test 11 12 13class TestNamespaces: 14 15 @pytest.mark.xfail(sys.version_info < (3, 5), 16 reason="Requires importlib.util.module_from_spec") 17 @pytest.mark.skipif(bool(os.environ.get("APPVEYOR")), 18 reason="https://github.com/pypa/setuptools/issues/851") 19 def test_mixed_site_and_non_site(self, tmpdir): 20 """ 21 Installing two packages sharing the same namespace, one installed 22 to a site dir and the other installed just to a path on PYTHONPATH 23 should leave the namespace in tact and both packages reachable by 24 import. 25 """ 26 pkg_A = namespaces.build_namespace_package(tmpdir, 'myns.pkgA') 27 pkg_B = namespaces.build_namespace_package(tmpdir, 'myns.pkgB') 28 site_packages = tmpdir / 'site-packages' 29 path_packages = tmpdir / 'path-packages' 30 targets = site_packages, path_packages 31 # use pip to install to the target directory 32 install_cmd = [ 33 sys.executable, 34 '-m', 35 'pip.__main__', 36 'install', 37 str(pkg_A), 38 '-t', str(site_packages), 39 ] 40 subprocess.check_call(install_cmd) 41 namespaces.make_site_dir(site_packages) 42 install_cmd = [ 43 sys.executable, 44 '-m', 45 'pip.__main__', 46 'install', 47 str(pkg_B), 48 '-t', str(path_packages), 49 ] 50 subprocess.check_call(install_cmd) 51 try_import = [ 52 sys.executable, 53 '-c', 'import myns.pkgA; import myns.pkgB', 54 ] 55 with test.test.paths_on_pythonpath(map(str, targets)): 56 subprocess.check_call(try_import) 57 58 @pytest.mark.skipif(bool(os.environ.get("APPVEYOR")), 59 reason="https://github.com/pypa/setuptools/issues/851") 60 def test_pkg_resources_import(self, tmpdir): 61 """ 62 Ensure that a namespace package doesn't break on import 63 of pkg_resources. 64 """ 65 pkg = namespaces.build_namespace_package(tmpdir, 'myns.pkgA') 66 target = tmpdir / 'packages' 67 target.mkdir() 68 install_cmd = [ 69 sys.executable, 70 '-m', 'easy_install', 71 '-d', str(target), 72 str(pkg), 73 ] 74 with test.test.paths_on_pythonpath([str(target)]): 75 subprocess.check_call(install_cmd) 76 namespaces.make_site_dir(target) 77 try_import = [ 78 sys.executable, 79 '-c', 'import pkg_resources', 80 ] 81 with test.test.paths_on_pythonpath([str(target)]): 82 subprocess.check_call(try_import) 83 84 @pytest.mark.skipif(bool(os.environ.get("APPVEYOR")), 85 reason="https://github.com/pypa/setuptools/issues/851") 86 def test_namespace_package_installed_and_cwd(self, tmpdir): 87 """ 88 Installing a namespace packages but also having it in the current 89 working directory, only one version should take precedence. 90 """ 91 pkg_A = namespaces.build_namespace_package(tmpdir, 'myns.pkgA') 92 target = tmpdir / 'packages' 93 # use pip to install to the target directory 94 install_cmd = [ 95 sys.executable, 96 '-m', 97 'pip.__main__', 98 'install', 99 str(pkg_A), 100 '-t', str(target), 101 ] 102 subprocess.check_call(install_cmd) 103 namespaces.make_site_dir(target) 104 105 # ensure that package imports and pkg_resources imports 106 pkg_resources_imp = [ 107 sys.executable, 108 '-c', 'import pkg_resources; import myns.pkgA', 109 ] 110 with test.test.paths_on_pythonpath([str(target)]): 111 subprocess.check_call(pkg_resources_imp, cwd=str(pkg_A)) 112