test_namespaces.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_pkg_resources_import(self, tmpdir):
  48. """
  49. Ensure that a namespace package doesn't break on import
  50. of pkg_resources.
  51. """
  52. pkg = namespaces.build_namespace_package(tmpdir, 'myns.pkgA')
  53. target = tmpdir / 'packages'
  54. target.mkdir()
  55. install_cmd = [
  56. sys.executable,
  57. '-m',
  58. 'pip',
  59. 'install',
  60. '-t',
  61. str(target),
  62. str(pkg),
  63. ]
  64. with paths_on_pythonpath([str(target)]):
  65. subprocess.check_call(install_cmd)
  66. namespaces.make_site_dir(target)
  67. try_import = [
  68. sys.executable,
  69. '-c',
  70. 'import pkg_resources',
  71. ]
  72. with paths_on_pythonpath([str(target)]):
  73. subprocess.check_call(try_import)
  74. def test_namespace_package_installed_and_cwd(self, tmpdir):
  75. """
  76. Installing a namespace packages but also having it in the current
  77. working directory, only one version should take precedence.
  78. """
  79. pkg_A = namespaces.build_namespace_package(tmpdir, 'myns.pkgA')
  80. target = tmpdir / 'packages'
  81. # use pip to install to the target directory
  82. install_cmd = [
  83. sys.executable,
  84. '-m',
  85. 'pip.__main__',
  86. 'install',
  87. str(pkg_A),
  88. '-t',
  89. str(target),
  90. ]
  91. subprocess.check_call(install_cmd)
  92. namespaces.make_site_dir(target)
  93. # ensure that package imports and pkg_resources imports
  94. pkg_resources_imp = [
  95. sys.executable,
  96. '-c',
  97. 'import pkg_resources; import myns.pkgA',
  98. ]
  99. with paths_on_pythonpath([str(target)]):
  100. subprocess.check_call(pkg_resources_imp, cwd=str(pkg_A))
  101. def test_packages_in_the_same_namespace_installed_and_cwd(self, tmpdir):
  102. """
  103. Installing one namespace package and also have another in the same
  104. namespace in the current working directory, both of them must be
  105. importable.
  106. """
  107. pkg_A = namespaces.build_namespace_package(tmpdir, 'myns.pkgA')
  108. pkg_B = namespaces.build_namespace_package(tmpdir, 'myns.pkgB')
  109. target = tmpdir / 'packages'
  110. # use pip to install to the target directory
  111. install_cmd = [
  112. sys.executable,
  113. '-m',
  114. 'pip.__main__',
  115. 'install',
  116. str(pkg_A),
  117. '-t',
  118. str(target),
  119. ]
  120. subprocess.check_call(install_cmd)
  121. namespaces.make_site_dir(target)
  122. # ensure that all packages import and pkg_resources imports
  123. pkg_resources_imp = [
  124. sys.executable,
  125. '-c',
  126. 'import pkg_resources; import myns.pkgA; import myns.pkgB',
  127. ]
  128. with paths_on_pythonpath([str(target)]):
  129. subprocess.check_call(pkg_resources_imp, cwd=str(pkg_B))