blob: e416edd4a1e7125f846560fd6bdcd4302a1e8ad1 [file] [log] [blame]
Fred Drakeb849edd2004-06-17 20:16:19 +00001"""Tests for distutils.command.build_py."""
2
3import os
Guido van Rossumcd16bf62007-06-13 18:07:49 +00004import sys
Éric Araujo47a45212011-10-08 00:34:13 +02005import imp
Fred Drakeb849edd2004-06-17 20:16:19 +00006import unittest
7
8from distutils.command.build_py import build_py
9from distutils.core import Distribution
Guido van Rossumcd16bf62007-06-13 18:07:49 +000010from distutils.errors import DistutilsFileError
Fred Drakeb849edd2004-06-17 20:16:19 +000011
12from distutils.tests import support
Éric Araujob344dd02011-02-02 21:38:37 +000013from 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
20 def test_package_data(self):
21 sources = self.mkdtemp()
22 f = open(os.path.join(sources, "__init__.py"), "w")
Éric Araujoc6d7ead2010-11-06 02:58:56 +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 Araujoc6d7ead2010-11-06 02:58:56 +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!).
Fred Drakeb3d55d32004-07-21 18:53:06 +000056 self.assertEqual(len(cmd.get_outputs()), 3)
Fred Drakeb849edd2004-06-17 20:16:19 +000057 pkgdest = os.path.join(destination, "pkg")
58 files = os.listdir(pkgdest)
Éric Araujo47a45212011-10-08 00:34:13 +020059 pycache_dir = os.path.join(pkgdest, "__pycache__")
Éric Araujo489c8ff2011-05-28 23:32:50 +020060 self.assertIn("__init__.py", files)
61 self.assertIn("README.txt", files)
Éric Araujo489c8ff2011-05-28 23:32:50 +020062 if sys.dont_write_bytecode:
Éric Araujo47a45212011-10-08 00:34:13 +020063 self.assertFalse(os.path.exists(pycache_dir))
Éric Araujo489c8ff2011-05-28 23:32:50 +020064 else:
Éric Araujo47a45212011-10-08 00:34:13 +020065 pyc_files = os.listdir(pycache_dir)
66 self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
Fred Drakeb849edd2004-06-17 20:16:19 +000067
Éric Araujo489c8ff2011-05-28 23:32:50 +020068 def test_empty_package_dir(self):
Éric Araujoc465b2f2011-11-03 03:45:33 +010069 # See bugs #1668596/#1720897
Guido van Rossumcd16bf62007-06-13 18:07:49 +000070 sources = self.mkdtemp()
71 open(os.path.join(sources, "__init__.py"), "w").close()
72
73 testdir = os.path.join(sources, "doc")
74 os.mkdir(testdir)
75 open(os.path.join(testdir, "testfile"), "w").close()
76
77 os.chdir(sources)
Éric Araujoc465b2f2011-11-03 03:45:33 +010078 dist = Distribution({"packages": ["pkg"],
79 "package_dir": {"pkg": ""},
80 "package_data": {"pkg": ["doc/*"]}})
81 # script_name need not exist, it just need to be initialized
82 dist.script_name = os.path.join(sources, "setup.py")
83 dist.script_args = ["build"]
84 dist.parse_command_line()
Guido van Rossumcd16bf62007-06-13 18:07:49 +000085
86 try:
Éric Araujoc465b2f2011-11-03 03:45:33 +010087 dist.run_commands()
88 except DistutilsFileError:
89 self.fail("failed package_data test when package_dir is ''")
Guido van Rossumcd16bf62007-06-13 18:07:49 +000090
Éric Araujoc465b2f2011-11-03 03:45:33 +010091 @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
92 def test_byte_compile(self):
93 project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
94 os.chdir(project_dir)
95 self.write_file('boiledeggs.py', 'import antigravity')
96 cmd = build_py(dist)
97 cmd.compile = 1
98 cmd.build_lib = 'here'
99 cmd.finalize_options()
100 cmd.run()
101
102 found = os.listdir(cmd.build_lib)
103 self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
104 found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
105 self.assertEqual(found, ['boiledeggs.%s.pyc' % imp.get_tag()])
106
107 @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
108 def test_byte_compile_optimized(self):
109 project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
110 os.chdir(project_dir)
111 self.write_file('boiledeggs.py', 'import antigravity')
112 cmd = build_py(dist)
113 cmd.compile = 0
114 cmd.optimize = 1
115 cmd.build_lib = 'here'
116 cmd.finalize_options()
117 cmd.run()
118
119 found = os.listdir(cmd.build_lib)
120 self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
121 found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
122 self.assertEqual(sorted(found), ['boiledeggs.%s.pyo' % imp.get_tag()])
Fred Drakeb849edd2004-06-17 20:16:19 +0000123
Tarek Ziadéc76bcea2009-10-25 23:16:51 +0000124 def test_dont_write_bytecode(self):
125 # makes sure byte_compile is not used
Éric Araujoc465b2f2011-11-03 03:45:33 +0100126 dist = self.create_dist()[1]
Tarek Ziadéc76bcea2009-10-25 23:16:51 +0000127 cmd = build_py(dist)
128 cmd.compile = 1
129 cmd.optimize = 1
130
131 old_dont_write_bytecode = sys.dont_write_bytecode
132 sys.dont_write_bytecode = True
133 try:
134 cmd.byte_compile([])
135 finally:
136 sys.dont_write_bytecode = old_dont_write_bytecode
137
Éric Araujo489c8ff2011-05-28 23:32:50 +0200138 self.assertIn('byte-compiling is disabled', self.logs[0][1])
Tarek Ziadéc76bcea2009-10-25 23:16:51 +0000139
Éric Araujoc465b2f2011-11-03 03:45:33 +0100140
Fred Drakeb849edd2004-06-17 20:16:19 +0000141def test_suite():
142 return unittest.makeSuite(BuildPyTestCase)
Fred Drake22021572004-06-25 19:04:21 +0000143
144if __name__ == "__main__":
Éric Araujob344dd02011-02-02 21:38:37 +0000145 run_unittest(test_suite())