• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests for distutils.command.install."""
2
3import os
4import sys
5import unittest
6import site
7
8from test.test_support import captured_stdout, run_unittest
9
10from distutils import sysconfig
11from distutils.command.install import install
12from distutils.command import install as install_module
13from distutils.command.build_ext import build_ext
14from distutils.command.install import INSTALL_SCHEMES
15from distutils.core import Distribution
16from distutils.errors import DistutilsOptionError
17from distutils.extension import Extension
18
19from distutils.tests import support
20
21
22def _make_ext_name(modname):
23    if os.name == 'nt' and sys.executable.endswith('_d.exe'):
24        modname += '_d'
25    return modname + sysconfig.get_config_var('SO')
26
27
28class InstallTestCase(support.TempdirManager,
29                      support.LoggingSilencer,
30                      unittest.TestCase):
31
32    def test_home_installation_scheme(self):
33        # This ensure two things:
34        # - that --home generates the desired set of directory names
35        # - test --home is supported on all platforms
36        builddir = self.mkdtemp()
37        destination = os.path.join(builddir, "installation")
38
39        dist = Distribution({"name": "foopkg"})
40        # script_name need not exist, it just need to be initialized
41        dist.script_name = os.path.join(builddir, "setup.py")
42        dist.command_obj["build"] = support.DummyCommand(
43            build_base=builddir,
44            build_lib=os.path.join(builddir, "lib"),
45            )
46
47        cmd = install(dist)
48        cmd.home = destination
49        cmd.ensure_finalized()
50
51        self.assertEqual(cmd.install_base, destination)
52        self.assertEqual(cmd.install_platbase, destination)
53
54        def check_path(got, expected):
55            got = os.path.normpath(got)
56            expected = os.path.normpath(expected)
57            self.assertEqual(got, expected)
58
59        libdir = os.path.join(destination, "lib", "python")
60        check_path(cmd.install_lib, libdir)
61        check_path(cmd.install_platlib, libdir)
62        check_path(cmd.install_purelib, libdir)
63        check_path(cmd.install_headers,
64                   os.path.join(destination, "include", "python", "foopkg"))
65        check_path(cmd.install_scripts, os.path.join(destination, "bin"))
66        check_path(cmd.install_data, destination)
67
68    @unittest.skipIf(sys.version < '2.6',
69                     'site.USER_SITE was introduced in 2.6')
70    def test_user_site(self):
71        # preparing the environment for the test
72        self.old_user_base = site.USER_BASE
73        self.old_user_site = site.USER_SITE
74        self.tmpdir = self.mkdtemp()
75        self.user_base = os.path.join(self.tmpdir, 'B')
76        self.user_site = os.path.join(self.tmpdir, 'S')
77        site.USER_BASE = self.user_base
78        site.USER_SITE = self.user_site
79        install_module.USER_BASE = self.user_base
80        install_module.USER_SITE = self.user_site
81
82        def _expanduser(path):
83            return self.tmpdir
84        self.old_expand = os.path.expanduser
85        os.path.expanduser = _expanduser
86
87        def cleanup():
88            site.USER_BASE = self.old_user_base
89            site.USER_SITE = self.old_user_site
90            install_module.USER_BASE = self.old_user_base
91            install_module.USER_SITE = self.old_user_site
92            os.path.expanduser = self.old_expand
93
94        self.addCleanup(cleanup)
95
96        for key in ('nt_user', 'unix_user', 'os2_home'):
97            self.assertIn(key, INSTALL_SCHEMES)
98
99        dist = Distribution({'name': 'xx'})
100        cmd = install(dist)
101
102        # making sure the user option is there
103        options = [name for name, short, lable in
104                   cmd.user_options]
105        self.assertIn('user', options)
106
107        # setting a value
108        cmd.user = 1
109
110        # user base and site shouldn't be created yet
111        self.assertFalse(os.path.exists(self.user_base))
112        self.assertFalse(os.path.exists(self.user_site))
113
114        # let's run finalize
115        cmd.ensure_finalized()
116
117        # now they should
118        self.assertTrue(os.path.exists(self.user_base))
119        self.assertTrue(os.path.exists(self.user_site))
120
121        self.assertIn('userbase', cmd.config_vars)
122        self.assertIn('usersite', cmd.config_vars)
123
124    def test_handle_extra_path(self):
125        dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
126        cmd = install(dist)
127
128        # two elements
129        cmd.handle_extra_path()
130        self.assertEqual(cmd.extra_path, ['path', 'dirs'])
131        self.assertEqual(cmd.extra_dirs, 'dirs')
132        self.assertEqual(cmd.path_file, 'path')
133
134        # one element
135        cmd.extra_path = ['path']
136        cmd.handle_extra_path()
137        self.assertEqual(cmd.extra_path, ['path'])
138        self.assertEqual(cmd.extra_dirs, 'path')
139        self.assertEqual(cmd.path_file, 'path')
140
141        # none
142        dist.extra_path = cmd.extra_path = None
143        cmd.handle_extra_path()
144        self.assertEqual(cmd.extra_path, None)
145        self.assertEqual(cmd.extra_dirs, '')
146        self.assertEqual(cmd.path_file, None)
147
148        # three elements (no way !)
149        cmd.extra_path = 'path,dirs,again'
150        self.assertRaises(DistutilsOptionError, cmd.handle_extra_path)
151
152    def test_finalize_options(self):
153        dist = Distribution({'name': 'xx'})
154        cmd = install(dist)
155
156        # must supply either prefix/exec-prefix/home or
157        # install-base/install-platbase -- not both
158        cmd.prefix = 'prefix'
159        cmd.install_base = 'base'
160        self.assertRaises(DistutilsOptionError, cmd.finalize_options)
161
162        # must supply either home or prefix/exec-prefix -- not both
163        cmd.install_base = None
164        cmd.home = 'home'
165        self.assertRaises(DistutilsOptionError, cmd.finalize_options)
166
167        # can't combine user with prefix/exec_prefix/home or
168        # install_(plat)base
169        cmd.prefix = None
170        cmd.user = 'user'
171        self.assertRaises(DistutilsOptionError, cmd.finalize_options)
172
173    def test_record(self):
174        install_dir = self.mkdtemp()
175        project_dir, dist = self.create_dist(py_modules=['hello'],
176                                             scripts=['sayhi'])
177        os.chdir(project_dir)
178        self.write_file('hello.py', "def main(): print 'o hai'")
179        self.write_file('sayhi', 'from hello import main; main()')
180
181        cmd = install(dist)
182        dist.command_obj['install'] = cmd
183        cmd.root = install_dir
184        cmd.record = os.path.join(project_dir, 'filelist')
185        cmd.ensure_finalized()
186        cmd.run()
187
188        f = open(cmd.record)
189        try:
190            content = f.read()
191        finally:
192            f.close()
193
194        found = [os.path.basename(line) for line in content.splitlines()]
195        expected = ['hello.py', 'hello.pyc', 'sayhi',
196                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
197        self.assertEqual(found, expected)
198
199    def test_record_extensions(self):
200        install_dir = self.mkdtemp()
201        project_dir, dist = self.create_dist(ext_modules=[
202            Extension('xx', ['xxmodule.c'])])
203        os.chdir(project_dir)
204        support.copy_xxmodule_c(project_dir)
205
206        buildextcmd = build_ext(dist)
207        support.fixup_build_ext(buildextcmd)
208        buildextcmd.ensure_finalized()
209
210        cmd = install(dist)
211        dist.command_obj['install'] = cmd
212        dist.command_obj['build_ext'] = buildextcmd
213        cmd.root = install_dir
214        cmd.record = os.path.join(project_dir, 'filelist')
215        cmd.ensure_finalized()
216        cmd.run()
217
218        f = open(cmd.record)
219        try:
220            content = f.read()
221        finally:
222            f.close()
223
224        found = [os.path.basename(line) for line in content.splitlines()]
225        expected = [_make_ext_name('xx'),
226                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
227        self.assertEqual(found, expected)
228
229    def test_debug_mode(self):
230        # this covers the code called when DEBUG is set
231        old_logs_len = len(self.logs)
232        install_module.DEBUG = True
233        try:
234            with captured_stdout():
235                self.test_record()
236        finally:
237            install_module.DEBUG = False
238        self.assertGreater(len(self.logs), old_logs_len)
239
240
241def test_suite():
242    return unittest.makeSuite(InstallTestCase)
243
244if __name__ == "__main__":
245    run_unittest(test_suite())
246