blob: e9b2642732e9a6fd3b7ba89e50470accb3652c3f [file] [log] [blame]
Fred Drakeec6229e2004-06-25 23:02:59 +00001"""Tests for distutils.command.install."""
2
3import os
Éric Araujoc465b2f2011-11-03 03:45:33 +01004import imp
Tarek Ziadé38e3d512009-02-27 12:58:56 +00005import sys
Fred Drakeec6229e2004-06-25 23:02:59 +00006import unittest
Tarek Ziadé38e3d512009-02-27 12:58:56 +00007import site
Fred Drakeec6229e2004-06-25 23:02:59 +00008
Éric Araujob344dd02011-02-02 21:38:37 +00009from test.support import captured_stdout, run_unittest
Tarek Ziadé83496692009-09-21 13:10:05 +000010
Éric Araujo17725412011-08-20 07:08:51 +020011from distutils import sysconfig
Fred Drakeec6229e2004-06-25 23:02:59 +000012from distutils.command.install import install
Tarek Ziadé38e3d512009-02-27 12:58:56 +000013from distutils.command import install as install_module
Éric Araujo17725412011-08-20 07:08:51 +020014from distutils.command.build_ext import build_ext
Tarek Ziadé38e3d512009-02-27 12:58:56 +000015from distutils.command.install import INSTALL_SCHEMES
Fred Drakeec6229e2004-06-25 23:02:59 +000016from distutils.core import Distribution
Tarek Ziadédc144a02009-06-04 07:39:50 +000017from distutils.errors import DistutilsOptionError
Éric Araujo17725412011-08-20 07:08:51 +020018from distutils.extension import Extension
Fred Drakeec6229e2004-06-25 23:02:59 +000019
20from distutils.tests import support
21
Éric Araujo175eb992011-08-24 01:29:10 +020022
23def _make_ext_name(modname):
Éric Araujoc6861672011-08-26 00:03:22 +020024 if os.name == 'nt' and sys.executable.endswith('_d.exe'):
25 modname += '_d'
doko@ubuntu.comd5537d02013-03-21 13:21:49 -070026 return modname + sysconfig.get_config_var('EXT_SUFFIX')
Éric Araujo175eb992011-08-24 01:29:10 +020027
28
Tarek Ziadédc144a02009-06-04 07:39:50 +000029class InstallTestCase(support.TempdirManager,
Tarek Ziadéf456a152009-10-18 12:41:30 +000030 support.EnvironGuard,
Tarek Ziadédc144a02009-06-04 07:39:50 +000031 support.LoggingSilencer,
32 unittest.TestCase):
Fred Drakeec6229e2004-06-25 23:02:59 +000033
34 def test_home_installation_scheme(self):
35 # This ensure two things:
36 # - that --home generates the desired set of directory names
37 # - test --home is supported on all platforms
38 builddir = self.mkdtemp()
39 destination = os.path.join(builddir, "installation")
40
41 dist = Distribution({"name": "foopkg"})
42 # script_name need not exist, it just need to be initialized
43 dist.script_name = os.path.join(builddir, "setup.py")
44 dist.command_obj["build"] = support.DummyCommand(
45 build_base=builddir,
46 build_lib=os.path.join(builddir, "lib"),
47 )
48
49 cmd = install(dist)
50 cmd.home = destination
51 cmd.ensure_finalized()
52
53 self.assertEqual(cmd.install_base, destination)
54 self.assertEqual(cmd.install_platbase, destination)
55
56 def check_path(got, expected):
57 got = os.path.normpath(got)
58 expected = os.path.normpath(expected)
59 self.assertEqual(got, expected)
60
61 libdir = os.path.join(destination, "lib", "python")
62 check_path(cmd.install_lib, libdir)
63 check_path(cmd.install_platlib, libdir)
64 check_path(cmd.install_purelib, libdir)
65 check_path(cmd.install_headers,
66 os.path.join(destination, "include", "python", "foopkg"))
67 check_path(cmd.install_scripts, os.path.join(destination, "bin"))
68 check_path(cmd.install_data, destination)
69
Tarek Ziadé38e3d512009-02-27 12:58:56 +000070 def test_user_site(self):
Éric Araujoc465b2f2011-11-03 03:45:33 +010071 # test install with --user
Ezio Melotti13925002011-03-16 11:05:33 +020072 # preparing the environment for the test
Tarek Ziadé38e3d512009-02-27 12:58:56 +000073 self.old_user_base = site.USER_BASE
74 self.old_user_site = site.USER_SITE
75 self.tmpdir = self.mkdtemp()
76 self.user_base = os.path.join(self.tmpdir, 'B')
77 self.user_site = os.path.join(self.tmpdir, 'S')
78 site.USER_BASE = self.user_base
79 site.USER_SITE = self.user_site
80 install_module.USER_BASE = self.user_base
81 install_module.USER_SITE = self.user_site
82
83 def _expanduser(path):
84 return self.tmpdir
85 self.old_expand = os.path.expanduser
86 os.path.expanduser = _expanduser
87
Éric Araujoaf2ffd72011-11-02 18:05:41 +010088 def cleanup():
Tarek Ziadé38e3d512009-02-27 12:58:56 +000089 site.USER_BASE = self.old_user_base
90 site.USER_SITE = self.old_user_site
91 install_module.USER_BASE = self.old_user_base
92 install_module.USER_SITE = self.old_user_site
93 os.path.expanduser = self.old_expand
94
Éric Araujoaf2ffd72011-11-02 18:05:41 +010095 self.addCleanup(cleanup)
96
Jesus Cead17833d2012-10-11 01:20:12 +020097 for key in ('nt_user', 'unix_user'):
Éric Araujoaf2ffd72011-11-02 18:05:41 +010098 self.assertIn(key, INSTALL_SCHEMES)
Tarek Ziadé38e3d512009-02-27 12:58:56 +000099
100 dist = Distribution({'name': 'xx'})
101 cmd = install(dist)
102
103 # making sure the user option is there
104 options = [name for name, short, lable in
105 cmd.user_options]
Éric Araujoaf2ffd72011-11-02 18:05:41 +0100106 self.assertIn('user', options)
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000107
108 # setting a value
109 cmd.user = 1
110
111 # user base and site shouldn't be created yet
Éric Araujoaf2ffd72011-11-02 18:05:41 +0100112 self.assertFalse(os.path.exists(self.user_base))
113 self.assertFalse(os.path.exists(self.user_site))
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000114
115 # let's run finalize
116 cmd.ensure_finalized()
117
118 # now they should
Georg Brandlab91fde2009-08-13 08:51:18 +0000119 self.assertTrue(os.path.exists(self.user_base))
120 self.assertTrue(os.path.exists(self.user_site))
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000121
Éric Araujoaf2ffd72011-11-02 18:05:41 +0100122 self.assertIn('userbase', cmd.config_vars)
123 self.assertIn('usersite', cmd.config_vars)
Fred Drakeec6229e2004-06-25 23:02:59 +0000124
Tarek Ziadédc144a02009-06-04 07:39:50 +0000125 def test_handle_extra_path(self):
126 dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
127 cmd = install(dist)
128
129 # two elements
130 cmd.handle_extra_path()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000131 self.assertEqual(cmd.extra_path, ['path', 'dirs'])
132 self.assertEqual(cmd.extra_dirs, 'dirs')
133 self.assertEqual(cmd.path_file, 'path')
Tarek Ziadédc144a02009-06-04 07:39:50 +0000134
135 # one element
136 cmd.extra_path = ['path']
137 cmd.handle_extra_path()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000138 self.assertEqual(cmd.extra_path, ['path'])
139 self.assertEqual(cmd.extra_dirs, 'path')
140 self.assertEqual(cmd.path_file, 'path')
Tarek Ziadédc144a02009-06-04 07:39:50 +0000141
142 # none
143 dist.extra_path = cmd.extra_path = None
144 cmd.handle_extra_path()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000145 self.assertEqual(cmd.extra_path, None)
146 self.assertEqual(cmd.extra_dirs, '')
147 self.assertEqual(cmd.path_file, None)
Tarek Ziadédc144a02009-06-04 07:39:50 +0000148
149 # three elements (no way !)
150 cmd.extra_path = 'path,dirs,again'
151 self.assertRaises(DistutilsOptionError, cmd.handle_extra_path)
152
153 def test_finalize_options(self):
154 dist = Distribution({'name': 'xx'})
155 cmd = install(dist)
156
157 # must supply either prefix/exec-prefix/home or
158 # install-base/install-platbase -- not both
159 cmd.prefix = 'prefix'
160 cmd.install_base = 'base'
161 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
162
163 # must supply either home or prefix/exec-prefix -- not both
164 cmd.install_base = None
165 cmd.home = 'home'
166 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
167
Terry Jan Reedyc30b7b12013-03-11 17:57:08 -0400168 # can't combine user with prefix/exec_prefix/home or
Tarek Ziadédc144a02009-06-04 07:39:50 +0000169 # install_(plat)base
170 cmd.prefix = None
171 cmd.user = 'user'
172 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
173
174 def test_record(self):
Tarek Ziadédc144a02009-06-04 07:39:50 +0000175 install_dir = self.mkdtemp()
Éric Araujoc465b2f2011-11-03 03:45:33 +0100176 project_dir, dist = self.create_dist(py_modules=['hello'],
177 scripts=['sayhi'])
Éric Araujo9358bfd2011-08-20 07:00:41 +0200178 os.chdir(project_dir)
Éric Araujoc465b2f2011-11-03 03:45:33 +0100179 self.write_file('hello.py', "def main(): print('o hai')")
180 self.write_file('sayhi', 'from hello import main; main()')
Tarek Ziadédc144a02009-06-04 07:39:50 +0000181
Tarek Ziadédc144a02009-06-04 07:39:50 +0000182 cmd = install(dist)
183 dist.command_obj['install'] = cmd
184 cmd.root = install_dir
Éric Araujoaf2ffd72011-11-02 18:05:41 +0100185 cmd.record = os.path.join(project_dir, 'filelist')
Tarek Ziadédc144a02009-06-04 07:39:50 +0000186 cmd.ensure_finalized()
Tarek Ziadédc144a02009-06-04 07:39:50 +0000187 cmd.run()
188
Éric Araujoc6d7ead2010-11-06 02:58:56 +0000189 f = open(cmd.record)
190 try:
Éric Araujo9358bfd2011-08-20 07:00:41 +0200191 content = f.read()
Éric Araujoc6d7ead2010-11-06 02:58:56 +0000192 finally:
193 f.close()
Tarek Ziadédc144a02009-06-04 07:39:50 +0000194
Éric Araujo9358bfd2011-08-20 07:00:41 +0200195 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujoc465b2f2011-11-03 03:45:33 +0100196 expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi',
Éric Araujo9358bfd2011-08-20 07:00:41 +0200197 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
198 self.assertEqual(found, expected)
199
Éric Araujo17725412011-08-20 07:08:51 +0200200 def test_record_extensions(self):
201 install_dir = self.mkdtemp()
202 project_dir, dist = self.create_dist(ext_modules=[
203 Extension('xx', ['xxmodule.c'])])
Éric Araujo17725412011-08-20 07:08:51 +0200204 os.chdir(project_dir)
205 support.copy_xxmodule_c(project_dir)
206
Éric Araujo175eb992011-08-24 01:29:10 +0200207 buildextcmd = build_ext(dist)
208 support.fixup_build_ext(buildextcmd)
209 buildextcmd.ensure_finalized()
Éric Araujo17725412011-08-20 07:08:51 +0200210
211 cmd = install(dist)
212 dist.command_obj['install'] = cmd
Éric Araujo175eb992011-08-24 01:29:10 +0200213 dist.command_obj['build_ext'] = buildextcmd
Éric Araujo17725412011-08-20 07:08:51 +0200214 cmd.root = install_dir
Éric Araujoaf2ffd72011-11-02 18:05:41 +0100215 cmd.record = os.path.join(project_dir, 'filelist')
Éric Araujo17725412011-08-20 07:08:51 +0200216 cmd.ensure_finalized()
217 cmd.run()
218
219 f = open(cmd.record)
220 try:
221 content = f.read()
222 finally:
223 f.close()
224
225 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujo175eb992011-08-24 01:29:10 +0200226 expected = [_make_ext_name('xx'),
Éric Araujo17725412011-08-20 07:08:51 +0200227 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
228 self.assertEqual(found, expected)
229
Tarek Ziadé83496692009-09-21 13:10:05 +0000230 def test_debug_mode(self):
231 # this covers the code called when DEBUG is set
232 old_logs_len = len(self.logs)
233 install_module.DEBUG = True
234 try:
Éric Araujo9358bfd2011-08-20 07:00:41 +0200235 with captured_stdout():
Tarek Ziadé83496692009-09-21 13:10:05 +0000236 self.test_record()
237 finally:
238 install_module.DEBUG = False
239 self.assertTrue(len(self.logs) > old_logs_len)
240
Éric Araujoc465b2f2011-11-03 03:45:33 +0100241
Fred Drakeec6229e2004-06-25 23:02:59 +0000242def test_suite():
243 return unittest.makeSuite(InstallTestCase)
244
245if __name__ == "__main__":
Éric Araujob344dd02011-02-02 21:38:37 +0000246 run_unittest(test_suite())