test_build_py.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """Tests for distutils.command.build_py."""
  2. import os
  3. import sys
  4. from distutils.command.build_py import build_py
  5. from distutils.core import Distribution
  6. from distutils.errors import DistutilsFileError
  7. from distutils.tests import support
  8. import jaraco.path
  9. import pytest
  10. @support.combine_markers
  11. class TestBuildPy(support.TempdirManager):
  12. def test_package_data(self):
  13. sources = self.mkdtemp()
  14. jaraco.path.build(
  15. {
  16. '__init__.py': "# Pretend this is a package.",
  17. 'README.txt': 'Info about this package',
  18. },
  19. sources,
  20. )
  21. destination = self.mkdtemp()
  22. dist = Distribution({"packages": ["pkg"], "package_dir": {"pkg": sources}})
  23. # script_name need not exist, it just need to be initialized
  24. dist.script_name = os.path.join(sources, "setup.py")
  25. dist.command_obj["build"] = support.DummyCommand(
  26. force=False, build_lib=destination
  27. )
  28. dist.packages = ["pkg"]
  29. dist.package_data = {"pkg": ["README.txt"]}
  30. dist.package_dir = {"pkg": sources}
  31. cmd = build_py(dist)
  32. cmd.compile = True
  33. cmd.ensure_finalized()
  34. assert cmd.package_data == dist.package_data
  35. cmd.run()
  36. # This makes sure the list of outputs includes byte-compiled
  37. # files for Python modules but not for package data files
  38. # (there shouldn't *be* byte-code files for those!).
  39. assert len(cmd.get_outputs()) == 3
  40. pkgdest = os.path.join(destination, "pkg")
  41. files = os.listdir(pkgdest)
  42. pycache_dir = os.path.join(pkgdest, "__pycache__")
  43. assert "__init__.py" in files
  44. assert "README.txt" in files
  45. if sys.dont_write_bytecode:
  46. assert not os.path.exists(pycache_dir)
  47. else:
  48. pyc_files = os.listdir(pycache_dir)
  49. assert f"__init__.{sys.implementation.cache_tag}.pyc" in pyc_files
  50. def test_empty_package_dir(self):
  51. # See bugs #1668596/#1720897
  52. sources = self.mkdtemp()
  53. jaraco.path.build({'__init__.py': '', 'doc': {'testfile': ''}}, sources)
  54. os.chdir(sources)
  55. dist = Distribution({
  56. "packages": ["pkg"],
  57. "package_dir": {"pkg": ""},
  58. "package_data": {"pkg": ["doc/*"]},
  59. })
  60. # script_name need not exist, it just need to be initialized
  61. dist.script_name = os.path.join(sources, "setup.py")
  62. dist.script_args = ["build"]
  63. dist.parse_command_line()
  64. try:
  65. dist.run_commands()
  66. except DistutilsFileError:
  67. self.fail("failed package_data test when package_dir is ''")
  68. @pytest.mark.skipif('sys.dont_write_bytecode')
  69. def test_byte_compile(self):
  70. project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
  71. os.chdir(project_dir)
  72. self.write_file('boiledeggs.py', 'import antigravity')
  73. cmd = build_py(dist)
  74. cmd.compile = True
  75. cmd.build_lib = 'here'
  76. cmd.finalize_options()
  77. cmd.run()
  78. found = os.listdir(cmd.build_lib)
  79. assert sorted(found) == ['__pycache__', 'boiledeggs.py']
  80. found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
  81. assert found == [f'boiledeggs.{sys.implementation.cache_tag}.pyc']
  82. @pytest.mark.skipif('sys.dont_write_bytecode')
  83. def test_byte_compile_optimized(self):
  84. project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
  85. os.chdir(project_dir)
  86. self.write_file('boiledeggs.py', 'import antigravity')
  87. cmd = build_py(dist)
  88. cmd.compile = False
  89. cmd.optimize = 1
  90. cmd.build_lib = 'here'
  91. cmd.finalize_options()
  92. cmd.run()
  93. found = os.listdir(cmd.build_lib)
  94. assert sorted(found) == ['__pycache__', 'boiledeggs.py']
  95. found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
  96. expect = f'boiledeggs.{sys.implementation.cache_tag}.opt-1.pyc'
  97. assert sorted(found) == [expect]
  98. def test_dir_in_package_data(self):
  99. """
  100. A directory in package_data should not be added to the filelist.
  101. """
  102. # See bug 19286
  103. sources = self.mkdtemp()
  104. jaraco.path.build(
  105. {
  106. 'pkg': {
  107. '__init__.py': '',
  108. 'doc': {
  109. 'testfile': '',
  110. # create a directory that could be incorrectly detected as a file
  111. 'otherdir': {},
  112. },
  113. }
  114. },
  115. sources,
  116. )
  117. os.chdir(sources)
  118. dist = Distribution({"packages": ["pkg"], "package_data": {"pkg": ["doc/*"]}})
  119. # script_name need not exist, it just need to be initialized
  120. dist.script_name = os.path.join(sources, "setup.py")
  121. dist.script_args = ["build"]
  122. dist.parse_command_line()
  123. try:
  124. dist.run_commands()
  125. except DistutilsFileError:
  126. self.fail("failed package_data when data dir includes a dir")
  127. def test_dont_write_bytecode(self, caplog):
  128. # makes sure byte_compile is not used
  129. dist = self.create_dist()[1]
  130. cmd = build_py(dist)
  131. cmd.compile = True
  132. cmd.optimize = 1
  133. old_dont_write_bytecode = sys.dont_write_bytecode
  134. sys.dont_write_bytecode = True
  135. try:
  136. cmd.byte_compile([])
  137. finally:
  138. sys.dont_write_bytecode = old_dont_write_bytecode
  139. assert 'byte-compiling is disabled' in caplog.records[0].message
  140. def test_namespace_package_does_not_warn(self, caplog):
  141. """
  142. Originally distutils implementation did not account for PEP 420
  143. and included warns for package directories that did not contain
  144. ``__init__.py`` files.
  145. After the acceptance of PEP 420, these warnings don't make more sense
  146. so we want to ensure there are not displayed to not confuse the users.
  147. """
  148. # Create a fake project structure with a package namespace:
  149. tmp = self.mkdtemp()
  150. jaraco.path.build({'ns': {'pkg': {'module.py': ''}}}, tmp)
  151. os.chdir(tmp)
  152. # Configure the package:
  153. attrs = {
  154. "name": "ns.pkg",
  155. "packages": ["ns", "ns.pkg"],
  156. "script_name": "setup.py",
  157. }
  158. dist = Distribution(attrs)
  159. # Run code paths that would trigger the trap:
  160. cmd = dist.get_command_obj("build_py")
  161. cmd.finalize_options()
  162. modules = cmd.find_all_modules()
  163. assert len(modules) == 1
  164. module_path = modules[0][-1]
  165. assert module_path.replace(os.sep, "/") == "ns/pkg/module.py"
  166. cmd.run()
  167. assert not any(
  168. "package init file" in msg and "not found" in msg for msg in caplog.messages
  169. )