blob: c4498bca56469647a056d6e946d64cae9d45f295 [file] [log] [blame]
Fred Drakeb849edd2004-06-17 20:16:19 +00001"""Tests for distutils.command.build_py."""
2
3import os
Neal Norwitz4a700bb2007-06-01 07:29:12 +00004import sys
5import StringIO
Fred Drakeb849edd2004-06-17 20:16:19 +00006import unittest
7
8from distutils.command.build_py import build_py
9from distutils.core import Distribution
Neal Norwitz4a700bb2007-06-01 07:29:12 +000010from distutils.errors import DistutilsFileError
Fred Drakeb849edd2004-06-17 20:16:19 +000011
12from distutils.tests import support
Éric Araujo54274ad2011-02-03 00:12:18 +000013from test.test_support import run_unittest
Fred Drakeb849edd2004-06-17 20:16:19 +000014
15
Fred Drakeedcac8f2004-08-03 18:53:07 +000016class BuildPyTestCase(support.TempdirManager,
17 support.LoggingSilencer,
18 unittest.TestCase):
Fred Drakeb849edd2004-06-17 20:16:19 +000019
Éric Araujobe778312011-05-28 23:21:19 +020020 def test_package_data(self):
Fred Drakeb849edd2004-06-17 20:16:19 +000021 sources = self.mkdtemp()
22 f = open(os.path.join(sources, "__init__.py"), "w")
Éric Araujod1feff72010-11-06 04:06:18 +000023 try:
24 f.write("# Pretend this is a package.")
25 finally:
26 f.close()
Fred Drakeb849edd2004-06-17 20:16:19 +000027 f = open(os.path.join(sources, "README.txt"), "w")
Éric Araujod1feff72010-11-06 04:06:18 +000028 try:
29 f.write("Info about this package")
30 finally:
31 f.close()
Fred Drakeb849edd2004-06-17 20:16:19 +000032
33 destination = self.mkdtemp()
34
35 dist = Distribution({"packages": ["pkg"],
36 "package_dir": {"pkg": sources}})
37 # script_name need not exist, it just need to be initialized
38 dist.script_name = os.path.join(sources, "setup.py")
39 dist.command_obj["build"] = support.DummyCommand(
40 force=0,
41 build_lib=destination)
42 dist.packages = ["pkg"]
43 dist.package_data = {"pkg": ["README.txt"]}
44 dist.package_dir = {"pkg": sources}
45
46 cmd = build_py(dist)
Fred Drakeb3d55d32004-07-21 18:53:06 +000047 cmd.compile = 1
Fred Drakeb849edd2004-06-17 20:16:19 +000048 cmd.ensure_finalized()
49 self.assertEqual(cmd.package_data, dist.package_data)
50
51 cmd.run()
52
Fred Drakeb3d55d32004-07-21 18:53:06 +000053 # This makes sure the list of outputs includes byte-compiled
54 # files for Python modules but not for package data files
55 # (there shouldn't *be* byte-code files for those!).
56 #
57 self.assertEqual(len(cmd.get_outputs()), 3)
Fred Drakeb849edd2004-06-17 20:16:19 +000058 pkgdest = os.path.join(destination, "pkg")
59 files = os.listdir(pkgdest)
Éric Araujobe778312011-05-28 23:21:19 +020060 self.assertIn("__init__.py", files)
61 self.assertIn("README.txt", files)
62 # XXX even with -O, distutils writes pyc, not pyo; bug?
63 if sys.dont_write_bytecode:
64 self.assertNotIn("__init__.pyc", files)
65 else:
66 self.assertIn("__init__.pyc", files)
R. David Murrayf28fd242010-02-23 00:24:49 +000067
Éric Araujobe778312011-05-28 23:21:19 +020068 def test_empty_package_dir(self):
Neal Norwitz4a700bb2007-06-01 07:29:12 +000069 # See SF 1668596/1720897.
70 cwd = os.getcwd()
71
72 # create the distribution files.
73 sources = self.mkdtemp()
74 open(os.path.join(sources, "__init__.py"), "w").close()
75
76 testdir = os.path.join(sources, "doc")
77 os.mkdir(testdir)
78 open(os.path.join(testdir, "testfile"), "w").close()
79
80 os.chdir(sources)
Tarek Ziadé9c48da72009-11-01 22:33:45 +000081 old_stdout = sys.stdout
Neal Norwitz4a700bb2007-06-01 07:29:12 +000082 sys.stdout = StringIO.StringIO()
83
84 try:
85 dist = Distribution({"packages": ["pkg"],
86 "package_dir": {"pkg": ""},
87 "package_data": {"pkg": ["doc/*"]}})
88 # script_name need not exist, it just need to be initialized
89 dist.script_name = os.path.join(sources, "setup.py")
90 dist.script_args = ["build"]
91 dist.parse_command_line()
92
93 try:
94 dist.run_commands()
95 except DistutilsFileError:
96 self.fail("failed package_data test when package_dir is ''")
97 finally:
98 # Restore state.
99 os.chdir(cwd)
Tarek Ziadé9c48da72009-11-01 22:33:45 +0000100 sys.stdout = old_stdout
Fred Drakeb849edd2004-06-17 20:16:19 +0000101
Jason R. Coombs83aafda2013-11-02 11:29:33 -0400102 def test_dir_in_package_data(self):
103 """
104 A directory in package_data should not be added to the filelist.
105 """
106 # See bug 19286
107 sources = self.mkdtemp()
108 pkg_dir = os.path.join(sources, "pkg")
109
110 os.mkdir(pkg_dir)
111 open(os.path.join(pkg_dir, "__init__.py"), "w").close()
112
113 docdir = os.path.join(pkg_dir, "doc")
114 os.mkdir(docdir)
115 open(os.path.join(docdir, "testfile"), "w").close()
116
117 # create the directory that could be incorrectly detected as a file
118 os.mkdir(os.path.join(docdir, 'otherdir'))
119
120 os.chdir(sources)
121 dist = Distribution({"packages": ["pkg"],
122 "package_data": {"pkg": ["doc/*"]}})
123 # script_name need not exist, it just need to be initialized
124 dist.script_name = os.path.join(sources, "setup.py")
125 dist.script_args = ["build"]
126 dist.parse_command_line()
127
128 try:
129 dist.run_commands()
130 except DistutilsFileError:
131 self.fail("failed package_data when data dir includes a dir")
132
Tarek Ziadéb9c1cfc2009-10-24 15:10:37 +0000133 def test_dont_write_bytecode(self):
134 # makes sure byte_compile is not used
135 pkg_dir, dist = self.create_dist()
136 cmd = build_py(dist)
137 cmd.compile = 1
138 cmd.optimize = 1
139
140 old_dont_write_bytecode = sys.dont_write_bytecode
141 sys.dont_write_bytecode = True
142 try:
143 cmd.byte_compile([])
144 finally:
145 sys.dont_write_bytecode = old_dont_write_bytecode
146
Éric Araujobe778312011-05-28 23:21:19 +0200147 self.assertIn('byte-compiling is disabled', self.logs[0][1])
Tarek Ziadéb9c1cfc2009-10-24 15:10:37 +0000148
Fred Drakeb849edd2004-06-17 20:16:19 +0000149def test_suite():
150 return unittest.makeSuite(BuildPyTestCase)
Fred Drake22021572004-06-25 19:04:21 +0000151
152if __name__ == "__main__":
Éric Araujo54274ad2011-02-03 00:12:18 +0000153 run_unittest(test_suite())