PageRenderTime 234ms CodeModel.GetById 101ms app.highlight 10ms RepoModel.GetById 120ms app.codeStats 1ms

/Lib/distutils/tests/test_build_py.py

http://unladen-swallow.googlecode.com/
Python | 97 lines | 73 code | 16 blank | 8 comment | 4 complexity | 47f1b37813b0f3e38c47677adf4b28b2 MD5 | raw file
 1"""Tests for distutils.command.build_py."""
 2
 3import os
 4import sys
 5import StringIO
 6import unittest
 7
 8from distutils.command.build_py import build_py
 9from distutils.core import Distribution
10from distutils.errors import DistutilsFileError
11
12from distutils.tests import support
13
14
15class 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_("README.txt" in files)
57        if sys.flags.optimize <= 1:
58            self.assert_("__init__.pyc" in files)
59
60    def test_empty_package_dir (self):
61        # See SF 1668596/1720897.
62        cwd = os.getcwd()
63
64        # create the distribution files.
65        sources = self.mkdtemp()
66        open(os.path.join(sources, "__init__.py"), "w").close()
67
68        testdir = os.path.join(sources, "doc")
69        os.mkdir(testdir)
70        open(os.path.join(testdir, "testfile"), "w").close()
71
72        os.chdir(sources)
73        sys.stdout = StringIO.StringIO()
74
75        try:
76            dist = Distribution({"packages": ["pkg"],
77                                 "package_dir": {"pkg": ""},
78                                 "package_data": {"pkg": ["doc/*"]}})
79            # script_name need not exist, it just need to be initialized
80            dist.script_name = os.path.join(sources, "setup.py")
81            dist.script_args = ["build"]
82            dist.parse_command_line()
83
84            try:
85                dist.run_commands()
86            except DistutilsFileError:
87                self.fail("failed package_data test when package_dir is ''")
88        finally:
89            # Restore state.
90            os.chdir(cwd)
91            sys.stdout = sys.__stdout__
92
93def test_suite():
94    return unittest.makeSuite(BuildPyTestCase)
95
96if __name__ == "__main__":
97    unittest.main(defaultTest="test_suite")