| 1 | n/a | """Tests for distutils.command.build_py.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import sys |
|---|
| 5 | n/a | import imp |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from packaging.command.build_py import build_py |
|---|
| 8 | n/a | from packaging.dist import Distribution |
|---|
| 9 | n/a | from packaging.errors import PackagingFileError |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | from packaging.tests import unittest, support |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | class BuildPyTestCase(support.TempdirManager, |
|---|
| 15 | n/a | support.LoggingCatcher, |
|---|
| 16 | n/a | unittest.TestCase): |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | def test_package_data(self): |
|---|
| 19 | n/a | sources = self.mkdtemp() |
|---|
| 20 | n/a | pkg_dir = os.path.join(sources, 'pkg') |
|---|
| 21 | n/a | os.mkdir(pkg_dir) |
|---|
| 22 | n/a | f = open(os.path.join(pkg_dir, "__init__.py"), "w") |
|---|
| 23 | n/a | try: |
|---|
| 24 | n/a | f.write("# Pretend this is a package.") |
|---|
| 25 | n/a | finally: |
|---|
| 26 | n/a | f.close() |
|---|
| 27 | n/a | # let's have two files to make sure globbing works |
|---|
| 28 | n/a | f = open(os.path.join(pkg_dir, "README.txt"), "w") |
|---|
| 29 | n/a | try: |
|---|
| 30 | n/a | f.write("Info about this package") |
|---|
| 31 | n/a | finally: |
|---|
| 32 | n/a | f.close() |
|---|
| 33 | n/a | f = open(os.path.join(pkg_dir, "HACKING.txt"), "w") |
|---|
| 34 | n/a | try: |
|---|
| 35 | n/a | f.write("How to contribute") |
|---|
| 36 | n/a | finally: |
|---|
| 37 | n/a | f.close() |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | destination = self.mkdtemp() |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | dist = Distribution({"packages": ["pkg"], |
|---|
| 42 | n/a | "package_dir": sources}) |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | dist.command_obj["build"] = support.DummyCommand( |
|---|
| 45 | n/a | force=False, |
|---|
| 46 | n/a | build_lib=destination, |
|---|
| 47 | n/a | use_2to3_fixers=None, |
|---|
| 48 | n/a | convert_2to3_doctests=None, |
|---|
| 49 | n/a | use_2to3=False) |
|---|
| 50 | n/a | dist.packages = ["pkg"] |
|---|
| 51 | n/a | dist.package_data = {"pkg": ["*.txt"]} |
|---|
| 52 | n/a | dist.package_dir = sources |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | cmd = build_py(dist) |
|---|
| 55 | n/a | cmd.compile = True |
|---|
| 56 | n/a | cmd.ensure_finalized() |
|---|
| 57 | n/a | self.assertEqual(cmd.package_data, dist.package_data) |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | cmd.run() |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | # This makes sure the list of outputs includes byte-compiled |
|---|
| 62 | n/a | # files for Python modules but not for package data files |
|---|
| 63 | n/a | # (there shouldn't *be* byte-code files for those!). |
|---|
| 64 | n/a | # FIXME the test below is not doing what the comment above says, and |
|---|
| 65 | n/a | # if it did it would show a code bug: if we add a demo.py file to |
|---|
| 66 | n/a | # package_data, it gets byte-compiled! |
|---|
| 67 | n/a | outputs = cmd.get_outputs() |
|---|
| 68 | n/a | self.assertEqual(len(outputs), 4, outputs) |
|---|
| 69 | n/a | pkgdest = os.path.join(destination, "pkg") |
|---|
| 70 | n/a | files = os.listdir(pkgdest) |
|---|
| 71 | n/a | pycache_dir = os.path.join(pkgdest, "__pycache__") |
|---|
| 72 | n/a | self.assertIn("__init__.py", files) |
|---|
| 73 | n/a | self.assertIn("README.txt", files) |
|---|
| 74 | n/a | self.assertIn("HACKING.txt", files) |
|---|
| 75 | n/a | pyc_files = os.listdir(pycache_dir) |
|---|
| 76 | n/a | self.assertEqual(["__init__.%s.pyc" % imp.get_tag()], pyc_files) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def test_empty_package_dir(self): |
|---|
| 79 | n/a | # See SF 1668596/1720897. |
|---|
| 80 | n/a | # create the distribution files. |
|---|
| 81 | n/a | sources = self.mkdtemp() |
|---|
| 82 | n/a | pkg = os.path.join(sources, 'pkg') |
|---|
| 83 | n/a | os.mkdir(pkg) |
|---|
| 84 | n/a | open(os.path.join(pkg, "__init__.py"), "wb").close() |
|---|
| 85 | n/a | testdir = os.path.join(pkg, "doc") |
|---|
| 86 | n/a | os.mkdir(testdir) |
|---|
| 87 | n/a | open(os.path.join(testdir, "testfile"), "wb").close() |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | os.chdir(sources) |
|---|
| 90 | n/a | dist = Distribution({"packages": ["pkg"], |
|---|
| 91 | n/a | "package_dir": sources, |
|---|
| 92 | n/a | "package_data": {"pkg": ["doc/*"]}}) |
|---|
| 93 | n/a | dist.script_args = ["build"] |
|---|
| 94 | n/a | dist.parse_command_line() |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | try: |
|---|
| 97 | n/a | dist.run_commands() |
|---|
| 98 | n/a | except PackagingFileError: |
|---|
| 99 | n/a | self.fail("failed package_data test when package_dir is ''") |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | def test_byte_compile(self): |
|---|
| 102 | n/a | project_dir, dist = self.create_dist(py_modules=['boiledeggs']) |
|---|
| 103 | n/a | os.chdir(project_dir) |
|---|
| 104 | n/a | self.write_file('boiledeggs.py', 'import antigravity') |
|---|
| 105 | n/a | cmd = build_py(dist) |
|---|
| 106 | n/a | cmd.compile = True |
|---|
| 107 | n/a | cmd.build_lib = 'here' |
|---|
| 108 | n/a | cmd.finalize_options() |
|---|
| 109 | n/a | cmd.run() |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | found = os.listdir(cmd.build_lib) |
|---|
| 112 | n/a | self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) |
|---|
| 113 | n/a | found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) |
|---|
| 114 | n/a | self.assertEqual(found, ['boiledeggs.%s.pyc' % imp.get_tag()]) |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | def test_byte_compile_optimized(self): |
|---|
| 117 | n/a | project_dir, dist = self.create_dist(py_modules=['boiledeggs']) |
|---|
| 118 | n/a | os.chdir(project_dir) |
|---|
| 119 | n/a | self.write_file('boiledeggs.py', 'import antigravity') |
|---|
| 120 | n/a | cmd = build_py(dist) |
|---|
| 121 | n/a | cmd.compile = True |
|---|
| 122 | n/a | cmd.optimize = 1 |
|---|
| 123 | n/a | cmd.build_lib = 'here' |
|---|
| 124 | n/a | cmd.finalize_options() |
|---|
| 125 | n/a | cmd.run() |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | found = os.listdir(cmd.build_lib) |
|---|
| 128 | n/a | self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) |
|---|
| 129 | n/a | found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) |
|---|
| 130 | n/a | self.assertEqual(sorted(found), ['boiledeggs.%s.pyc' % imp.get_tag(), |
|---|
| 131 | n/a | 'boiledeggs.%s.pyo' % imp.get_tag()]) |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def test_byte_compile_under_B(self): |
|---|
| 134 | n/a | # make sure byte compilation works under -B (dont_write_bytecode) |
|---|
| 135 | n/a | self.addCleanup(setattr, sys, 'dont_write_bytecode', |
|---|
| 136 | n/a | sys.dont_write_bytecode) |
|---|
| 137 | n/a | sys.dont_write_bytecode = True |
|---|
| 138 | n/a | self.test_byte_compile() |
|---|
| 139 | n/a | self.test_byte_compile_optimized() |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | def test_suite(): |
|---|
| 143 | n/a | return unittest.makeSuite(BuildPyTestCase) |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | if __name__ == "__main__": |
|---|
| 146 | n/a | unittest.main(defaultTest="test_suite") |
|---|