test_namespaces.py 4.5 KB

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