|
1 """Tests for distutils.command.build_py.""" |
|
2 |
|
3 import os |
|
4 import sys |
|
5 import StringIO |
|
6 import unittest |
|
7 |
|
8 from distutils.command.build_py import build_py |
|
9 from distutils.core import Distribution |
|
10 from distutils.errors import DistutilsFileError |
|
11 |
|
12 from distutils.tests import support |
|
13 |
|
14 |
|
15 class BuildPyTestCase(support.TempdirManager, |
|
16 support.LoggingSilencer, |
|
17 unittest.TestCase): |
|
18 |
|
19 def test_package_data(self): |
|
20 sources = self.mkdtemp() |
|
21 f = open(os.path.join(sources, "__init__.py"), "w") |
|
22 f.write("# Pretend this is a package.") |
|
23 f.close() |
|
24 f = open(os.path.join(sources, "README.txt"), "w") |
|
25 f.write("Info about this package") |
|
26 f.close() |
|
27 |
|
28 destination = self.mkdtemp() |
|
29 |
|
30 dist = Distribution({"packages": ["pkg"], |
|
31 "package_dir": {"pkg": sources}}) |
|
32 # script_name need not exist, it just need to be initialized |
|
33 dist.script_name = os.path.join(sources, "setup.py") |
|
34 dist.command_obj["build"] = support.DummyCommand( |
|
35 force=0, |
|
36 build_lib=destination) |
|
37 dist.packages = ["pkg"] |
|
38 dist.package_data = {"pkg": ["README.txt"]} |
|
39 dist.package_dir = {"pkg": sources} |
|
40 |
|
41 cmd = build_py(dist) |
|
42 cmd.compile = 1 |
|
43 cmd.ensure_finalized() |
|
44 self.assertEqual(cmd.package_data, dist.package_data) |
|
45 |
|
46 cmd.run() |
|
47 |
|
48 # This makes sure the list of outputs includes byte-compiled |
|
49 # files for Python modules but not for package data files |
|
50 # (there shouldn't *be* byte-code files for those!). |
|
51 # |
|
52 self.assertEqual(len(cmd.get_outputs()), 3) |
|
53 pkgdest = os.path.join(destination, "pkg") |
|
54 files = os.listdir(pkgdest) |
|
55 self.assert_("__init__.py" in files) |
|
56 self.assert_("__init__.pyc" in files) |
|
57 self.assert_("README.txt" in files) |
|
58 |
|
59 def test_empty_package_dir (self): |
|
60 # See SF 1668596/1720897. |
|
61 cwd = os.getcwd() |
|
62 |
|
63 # create the distribution files. |
|
64 sources = self.mkdtemp() |
|
65 open(os.path.join(sources, "__init__.py"), "w").close() |
|
66 |
|
67 testdir = os.path.join(sources, "doc") |
|
68 os.mkdir(testdir) |
|
69 open(os.path.join(testdir, "testfile"), "w").close() |
|
70 |
|
71 os.chdir(sources) |
|
72 sys.stdout = StringIO.StringIO() |
|
73 |
|
74 try: |
|
75 dist = Distribution({"packages": ["pkg"], |
|
76 "package_dir": {"pkg": ""}, |
|
77 "package_data": {"pkg": ["doc/*"]}}) |
|
78 # script_name need not exist, it just need to be initialized |
|
79 dist.script_name = os.path.join(sources, "setup.py") |
|
80 dist.script_args = ["build"] |
|
81 dist.parse_command_line() |
|
82 |
|
83 try: |
|
84 dist.run_commands() |
|
85 except DistutilsFileError: |
|
86 self.fail("failed package_data test when package_dir is ''") |
|
87 finally: |
|
88 # Restore state. |
|
89 os.chdir(cwd) |
|
90 sys.stdout = sys.__stdout__ |
|
91 |
|
92 def test_suite(): |
|
93 return unittest.makeSuite(BuildPyTestCase) |
|
94 |
|
95 if __name__ == "__main__": |
|
96 unittest.main(defaultTest="test_suite") |