test_namespaces.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import subprocess
  2. import sys
  3. from setuptools._path import paths_on_pythonpath
  4. from . import namespaces
  5. class TestNamespaces:
  6. def test_mixed_site_and_non_site(self, tmpdir):
  7. """
  8. Installing two packages sharing the same namespace, one installed
  9. to a site dir and the other installed just to a path on PYTHONPATH
  10. should leave the namespace in tact and both packages reachable by
  11. import.
  12. """
  13. pkg_A = namespaces.build_namespace_package(tmpdir, 'myns.pkgA')
  14. pkg_B = namespaces.build_namespace_package(tmpdir, 'myns.pkgB')
  15. site_packages = tmpdir / 'site-packages'
  16. path_packages = tmpdir / 'path-packages'
  17. targets = site_packages, path_packages
  18. # use pip to install to the target directory
  19. install_cmd = [
  20. sys.executable,
  21. '-m',
  22. 'pip.__main__',
  23. 'install',
  24. str(pkg_A),
  25. '-t',
  26. str(site_packages),
  27. ]
  28. subprocess.check_call(install_cmd)
  29. namespaces.make_site_dir(site_packages)
  30. install_cmd = [
  31. sys.executable,
  32. '-m',
  33. 'pip.__main__',
  34. 'install',
  35. str(pkg_B),
  36. '-t',
  37. str(path_packages),
  38. ]
  39. subprocess.check_call(install_cmd)
  40. try_import = [
  41. sys.executable,
  42. '-c',
  43. 'import myns.pkgA; import myns.pkgB',
  44. ]
  45. with paths_on_pythonpath(map(str, targets)):
  46. subprocess.check_call(try_import)
  47. def test_namespace_package_installed_and_cwd(self, tmpdir):
  48. """
  49. Installing a namespace packages but also having it in the current
  50. working directory, only one version should take precedence.
  51. """
  52. pkg_A = namespaces.build_namespace_package(tmpdir, 'myns.pkgA')
  53. target = tmpdir / 'packages'
  54. # use pip to install to the target directory
  55. install_cmd = [
  56. sys.executable,
  57. '-m',
  58. 'pip.__main__',
  59. 'install',
  60. str(pkg_A),
  61. '-t',
  62. str(target),
  63. ]
  64. subprocess.check_call(install_cmd)
  65. namespaces.make_site_dir(target)
  66. # ensure that package imports
  67. pkg_resources_imp = [
  68. sys.executable,
  69. '-c',
  70. 'import myns.pkgA',
  71. ]
  72. with paths_on_pythonpath([str(target)]):
  73. subprocess.check_call(pkg_resources_imp, cwd=str(pkg_A))