codekingpro/portable-devtools
114k
1"""Tests for distutils.command.build_py."""2 3import os4import sys5from distutils.command.build_py import build_py6from distutils.core import Distribution7from distutils.errors import DistutilsFileError8from distutils.tests import support9 10import jaraco.path11import pytest12 13 14@support.combine_markers15class TestBuildPy(support.TempdirManager):16 def test_package_data(self):17 sources = self.mkdtemp()18 jaraco.path.build(19 {20 '__init__.py': "# Pretend this is a package.",21 'README.txt': 'Info about this package',22 },23 sources,24 )25 26 destination = self.mkdtemp()27 28 dist = Distribution({"packages": ["pkg"], "package_dir": {"pkg": sources}})29 # script_name need not exist, it just need to be initialized30 dist.script_name = os.path.join(sources, "setup.py")31 dist.command_obj["build"] = support.DummyCommand(32 force=False, build_lib=destination33 )34 dist.packages = ["pkg"]35 dist.package_data = {"pkg": ["README.txt"]}36 dist.package_dir = {"pkg": sources}37 38 cmd = build_py(dist)39 cmd.compile = True40 cmd.ensure_finalized()41 assert cmd.package_data == dist.package_data42 43 cmd.run()44 45 # This makes sure the list of outputs includes byte-compiled46 # files for Python modules but not for package data files47 # (there shouldn't *be* byte-code files for those!).48 assert len(cmd.get_outputs()) == 349 pkgdest = os.path.join(destination, "pkg")50 files = os.listdir(pkgdest)51 pycache_dir = os.path.join(pkgdest, "__pycache__")52 assert "__init__.py" in files53 assert "README.txt" in files54 if sys.dont_write_bytecode:55 assert not os.path.exists(pycache_dir)56 else:57 pyc_files = os.listdir(pycache_dir)58 assert f"__init__.{sys.implementation.cache_tag}.pyc" in pyc_files59 60 def test_empty_package_dir(self):61 # See bugs #1668596/#172089762 sources = self.mkdtemp()63 jaraco.path.build({'__init__.py': '', 'doc': {'testfile': ''}}, sources)64 65 os.chdir(sources)66 dist = Distribution({67 "packages": ["pkg"],68 "package_dir": {"pkg": ""},69 "package_data": {"pkg": ["doc/*"]},70 })71 # script_name need not exist, it just need to be initialized72 dist.script_name = os.path.join(sources, "setup.py")73 dist.script_args = ["build"]74 dist.parse_command_line()75 76 try:77 dist.run_commands()78 except DistutilsFileError:79 self.fail("failed package_data test when package_dir is ''")80 81 @pytest.mark.skipif('sys.dont_write_bytecode')82 def test_byte_compile(self):83 project_dir, dist = self.create_dist(py_modules=['boiledeggs'])84 os.chdir(project_dir)85 self.write_file('boiledeggs.py', 'import antigravity')86 cmd = build_py(dist)87 cmd.compile = True88 cmd.build_lib = 'here'89 cmd.finalize_options()90 cmd.run()91 92 found = os.listdir(cmd.build_lib)93 assert sorted(found) == ['__pycache__', 'boiledeggs.py']94 found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))95 assert found == [f'boiledeggs.{sys.implementation.cache_tag}.pyc']96 97 @pytest.mark.skipif('sys.dont_write_bytecode')98 def test_byte_compile_optimized(self):99 project_dir, dist = self.create_dist(py_modules=['boiledeggs'])100 os.chdir(project_dir)101 self.write_file('boiledeggs.py', 'import antigravity')102 cmd = build_py(dist)103 cmd.compile = False104 cmd.optimize = 1105 cmd.build_lib = 'here'106 cmd.finalize_options()107 cmd.run()108 109 found = os.listdir(cmd.build_lib)110 assert sorted(found) == ['__pycache__', 'boiledeggs.py']111 found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))112 expect = f'boiledeggs.{sys.implementation.cache_tag}.opt-1.pyc'113 assert sorted(found) == [expect]114 115 def test_dir_in_package_data(self):116 """117 A directory in package_data should not be added to the filelist.118 """119 # See bug 19286120 sources = self.mkdtemp()121 jaraco.path.build(122 {123 'pkg': {124 '__init__.py': '',125 'doc': {126 'testfile': '',127 # create a directory that could be incorrectly detected as a file128 'otherdir': {},129 },130 }131 },132 sources,133 )134 135 os.chdir(sources)136 dist = Distribution({"packages": ["pkg"], "package_data": {"pkg": ["doc/*"]}})137 # script_name need not exist, it just need to be initialized138 dist.script_name = os.path.join(sources, "setup.py")139 dist.script_args = ["build"]140 dist.parse_command_line()141 142 try:143 dist.run_commands()144 except DistutilsFileError:145 self.fail("failed package_data when data dir includes a dir")146 147 def test_dont_write_bytecode(self, caplog):148 # makes sure byte_compile is not used149 dist = self.create_dist()[1]150 cmd = build_py(dist)151 cmd.compile = True152 cmd.optimize = 1153 154 old_dont_write_bytecode = sys.dont_write_bytecode155 sys.dont_write_bytecode = True156 try:157 cmd.byte_compile([])158 finally:159 sys.dont_write_bytecode = old_dont_write_bytecode160 161 assert 'byte-compiling is disabled' in caplog.records[0].message162 163 def test_namespace_package_does_not_warn(self, caplog):164 """165 Originally distutils implementation did not account for PEP 420166 and included warns for package directories that did not contain167 ``__init__.py`` files.168 After the acceptance of PEP 420, these warnings don't make more sense169 so we want to ensure there are not displayed to not confuse the users.170 """171 # Create a fake project structure with a package namespace:172 tmp = self.mkdtemp()173 jaraco.path.build({'ns': {'pkg': {'module.py': ''}}}, tmp)174 os.chdir(tmp)175 176 # Configure the package:177 attrs = {178 "name": "ns.pkg",179 "packages": ["ns", "ns.pkg"],180 "script_name": "setup.py",181 }182 dist = Distribution(attrs)183 184 # Run code paths that would trigger the trap:185 cmd = dist.get_command_obj("build_py")186 cmd.finalize_options()187 modules = cmd.find_all_modules()188 assert len(modules) == 1189 module_path = modules[0][-1]190 assert module_path.replace(os.sep, "/") == "ns/pkg/module.py"191 192 cmd.run()193 194 assert not any(195 "package init file" in msg and "not found" in msg for msg in caplog.messages196 )197 