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 unittest |
---|
6 | n/a | |
---|
7 | n/a | from distutils.command.build_py import build_py |
---|
8 | n/a | from distutils.core import Distribution |
---|
9 | n/a | from distutils.errors import DistutilsFileError |
---|
10 | n/a | |
---|
11 | n/a | from distutils.tests import support |
---|
12 | n/a | from test.support import run_unittest |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | class BuildPyTestCase(support.TempdirManager, |
---|
16 | n/a | support.LoggingSilencer, |
---|
17 | n/a | unittest.TestCase): |
---|
18 | n/a | |
---|
19 | n/a | def test_package_data(self): |
---|
20 | n/a | sources = self.mkdtemp() |
---|
21 | n/a | f = open(os.path.join(sources, "__init__.py"), "w") |
---|
22 | n/a | try: |
---|
23 | n/a | f.write("# Pretend this is a package.") |
---|
24 | n/a | finally: |
---|
25 | n/a | f.close() |
---|
26 | n/a | f = open(os.path.join(sources, "README.txt"), "w") |
---|
27 | n/a | try: |
---|
28 | n/a | f.write("Info about this package") |
---|
29 | n/a | finally: |
---|
30 | n/a | f.close() |
---|
31 | n/a | |
---|
32 | n/a | destination = self.mkdtemp() |
---|
33 | n/a | |
---|
34 | n/a | dist = Distribution({"packages": ["pkg"], |
---|
35 | n/a | "package_dir": {"pkg": sources}}) |
---|
36 | n/a | # script_name need not exist, it just need to be initialized |
---|
37 | n/a | dist.script_name = os.path.join(sources, "setup.py") |
---|
38 | n/a | dist.command_obj["build"] = support.DummyCommand( |
---|
39 | n/a | force=0, |
---|
40 | n/a | build_lib=destination) |
---|
41 | n/a | dist.packages = ["pkg"] |
---|
42 | n/a | dist.package_data = {"pkg": ["README.txt"]} |
---|
43 | n/a | dist.package_dir = {"pkg": sources} |
---|
44 | n/a | |
---|
45 | n/a | cmd = build_py(dist) |
---|
46 | n/a | cmd.compile = 1 |
---|
47 | n/a | cmd.ensure_finalized() |
---|
48 | n/a | self.assertEqual(cmd.package_data, dist.package_data) |
---|
49 | n/a | |
---|
50 | n/a | cmd.run() |
---|
51 | n/a | |
---|
52 | n/a | # This makes sure the list of outputs includes byte-compiled |
---|
53 | n/a | # files for Python modules but not for package data files |
---|
54 | n/a | # (there shouldn't *be* byte-code files for those!). |
---|
55 | n/a | self.assertEqual(len(cmd.get_outputs()), 3) |
---|
56 | n/a | pkgdest = os.path.join(destination, "pkg") |
---|
57 | n/a | files = os.listdir(pkgdest) |
---|
58 | n/a | pycache_dir = os.path.join(pkgdest, "__pycache__") |
---|
59 | n/a | self.assertIn("__init__.py", files) |
---|
60 | n/a | self.assertIn("README.txt", files) |
---|
61 | n/a | if sys.dont_write_bytecode: |
---|
62 | n/a | self.assertFalse(os.path.exists(pycache_dir)) |
---|
63 | n/a | else: |
---|
64 | n/a | pyc_files = os.listdir(pycache_dir) |
---|
65 | n/a | self.assertIn("__init__.%s.pyc" % sys.implementation.cache_tag, |
---|
66 | n/a | pyc_files) |
---|
67 | n/a | |
---|
68 | n/a | def test_empty_package_dir(self): |
---|
69 | n/a | # See bugs #1668596/#1720897 |
---|
70 | n/a | sources = self.mkdtemp() |
---|
71 | n/a | open(os.path.join(sources, "__init__.py"), "w").close() |
---|
72 | n/a | |
---|
73 | n/a | testdir = os.path.join(sources, "doc") |
---|
74 | n/a | os.mkdir(testdir) |
---|
75 | n/a | open(os.path.join(testdir, "testfile"), "w").close() |
---|
76 | n/a | |
---|
77 | n/a | os.chdir(sources) |
---|
78 | n/a | dist = Distribution({"packages": ["pkg"], |
---|
79 | n/a | "package_dir": {"pkg": ""}, |
---|
80 | n/a | "package_data": {"pkg": ["doc/*"]}}) |
---|
81 | n/a | # script_name need not exist, it just need to be initialized |
---|
82 | n/a | dist.script_name = os.path.join(sources, "setup.py") |
---|
83 | n/a | dist.script_args = ["build"] |
---|
84 | n/a | dist.parse_command_line() |
---|
85 | n/a | |
---|
86 | n/a | try: |
---|
87 | n/a | dist.run_commands() |
---|
88 | n/a | except DistutilsFileError: |
---|
89 | n/a | self.fail("failed package_data test when package_dir is ''") |
---|
90 | n/a | |
---|
91 | n/a | @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') |
---|
92 | n/a | def test_byte_compile(self): |
---|
93 | n/a | project_dir, dist = self.create_dist(py_modules=['boiledeggs']) |
---|
94 | n/a | os.chdir(project_dir) |
---|
95 | n/a | self.write_file('boiledeggs.py', 'import antigravity') |
---|
96 | n/a | cmd = build_py(dist) |
---|
97 | n/a | cmd.compile = 1 |
---|
98 | n/a | cmd.build_lib = 'here' |
---|
99 | n/a | cmd.finalize_options() |
---|
100 | n/a | cmd.run() |
---|
101 | n/a | |
---|
102 | n/a | found = os.listdir(cmd.build_lib) |
---|
103 | n/a | self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) |
---|
104 | n/a | found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) |
---|
105 | n/a | self.assertEqual(found, |
---|
106 | n/a | ['boiledeggs.%s.pyc' % sys.implementation.cache_tag]) |
---|
107 | n/a | |
---|
108 | n/a | @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') |
---|
109 | n/a | def test_byte_compile_optimized(self): |
---|
110 | n/a | project_dir, dist = self.create_dist(py_modules=['boiledeggs']) |
---|
111 | n/a | os.chdir(project_dir) |
---|
112 | n/a | self.write_file('boiledeggs.py', 'import antigravity') |
---|
113 | n/a | cmd = build_py(dist) |
---|
114 | n/a | cmd.compile = 0 |
---|
115 | n/a | cmd.optimize = 1 |
---|
116 | n/a | cmd.build_lib = 'here' |
---|
117 | n/a | cmd.finalize_options() |
---|
118 | n/a | cmd.run() |
---|
119 | n/a | |
---|
120 | n/a | found = os.listdir(cmd.build_lib) |
---|
121 | n/a | self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) |
---|
122 | n/a | found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) |
---|
123 | n/a | expect = 'boiledeggs.{}.opt-1.pyc'.format(sys.implementation.cache_tag) |
---|
124 | n/a | self.assertEqual(sorted(found), [expect]) |
---|
125 | n/a | |
---|
126 | n/a | def test_dir_in_package_data(self): |
---|
127 | n/a | """ |
---|
128 | n/a | A directory in package_data should not be added to the filelist. |
---|
129 | n/a | """ |
---|
130 | n/a | # See bug 19286 |
---|
131 | n/a | sources = self.mkdtemp() |
---|
132 | n/a | pkg_dir = os.path.join(sources, "pkg") |
---|
133 | n/a | |
---|
134 | n/a | os.mkdir(pkg_dir) |
---|
135 | n/a | open(os.path.join(pkg_dir, "__init__.py"), "w").close() |
---|
136 | n/a | |
---|
137 | n/a | docdir = os.path.join(pkg_dir, "doc") |
---|
138 | n/a | os.mkdir(docdir) |
---|
139 | n/a | open(os.path.join(docdir, "testfile"), "w").close() |
---|
140 | n/a | |
---|
141 | n/a | # create the directory that could be incorrectly detected as a file |
---|
142 | n/a | os.mkdir(os.path.join(docdir, 'otherdir')) |
---|
143 | n/a | |
---|
144 | n/a | os.chdir(sources) |
---|
145 | n/a | dist = Distribution({"packages": ["pkg"], |
---|
146 | n/a | "package_data": {"pkg": ["doc/*"]}}) |
---|
147 | n/a | # script_name need not exist, it just need to be initialized |
---|
148 | n/a | dist.script_name = os.path.join(sources, "setup.py") |
---|
149 | n/a | dist.script_args = ["build"] |
---|
150 | n/a | dist.parse_command_line() |
---|
151 | n/a | |
---|
152 | n/a | try: |
---|
153 | n/a | dist.run_commands() |
---|
154 | n/a | except DistutilsFileError: |
---|
155 | n/a | self.fail("failed package_data when data dir includes a dir") |
---|
156 | n/a | |
---|
157 | n/a | def test_dont_write_bytecode(self): |
---|
158 | n/a | # makes sure byte_compile is not used |
---|
159 | n/a | dist = self.create_dist()[1] |
---|
160 | n/a | cmd = build_py(dist) |
---|
161 | n/a | cmd.compile = 1 |
---|
162 | n/a | cmd.optimize = 1 |
---|
163 | n/a | |
---|
164 | n/a | old_dont_write_bytecode = sys.dont_write_bytecode |
---|
165 | n/a | sys.dont_write_bytecode = True |
---|
166 | n/a | try: |
---|
167 | n/a | cmd.byte_compile([]) |
---|
168 | n/a | finally: |
---|
169 | n/a | sys.dont_write_bytecode = old_dont_write_bytecode |
---|
170 | n/a | |
---|
171 | n/a | self.assertIn('byte-compiling is disabled', |
---|
172 | n/a | self.logs[0][1] % self.logs[0][2]) |
---|
173 | n/a | |
---|
174 | n/a | |
---|
175 | n/a | def test_suite(): |
---|
176 | n/a | return unittest.makeSuite(BuildPyTestCase) |
---|
177 | n/a | |
---|
178 | n/a | if __name__ == "__main__": |
---|
179 | n/a | run_unittest(test_suite()) |
---|