blob: a76e3e7a3cb0dc806a9374fe9093e3c213ef95c1 [file] [log] [blame]
Fred Drakeec6229e2004-06-25 23:02:59 +00001"""Tests for distutils.command.install."""
2
3import os
Tarek Ziadé38e3d512009-02-27 12:58:56 +00004import os.path
5import 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 Araujo70ec44a2010-11-06 02:44:43 +00009from test.support import captured_stdout, run_unittest
Tarek Ziadé03d5d082009-09-21 13:01:54 +000010
Fred Drakeec6229e2004-06-25 23:02:59 +000011from distutils.command.install import install
Tarek Ziadé38e3d512009-02-27 12:58:56 +000012from distutils.command import install as install_module
Tarek Ziadé36797272010-07-22 12:50:05 +000013from distutils.command.install import INSTALL_SCHEMES
Fred Drakeec6229e2004-06-25 23:02:59 +000014from distutils.core import Distribution
Tarek Ziadédc144a02009-06-04 07:39:50 +000015from distutils.errors import DistutilsOptionError
Fred Drakeec6229e2004-06-25 23:02:59 +000016
17from distutils.tests import support
18
Tarek Ziadédc144a02009-06-04 07:39:50 +000019class InstallTestCase(support.TempdirManager,
Tarek Ziadé430fb632009-10-18 11:34:51 +000020 support.EnvironGuard,
Tarek Ziadédc144a02009-06-04 07:39:50 +000021 support.LoggingSilencer,
22 unittest.TestCase):
Fred Drakeec6229e2004-06-25 23:02:59 +000023
24 def test_home_installation_scheme(self):
25 # This ensure two things:
26 # - that --home generates the desired set of directory names
27 # - test --home is supported on all platforms
28 builddir = self.mkdtemp()
29 destination = os.path.join(builddir, "installation")
30
31 dist = Distribution({"name": "foopkg"})
32 # script_name need not exist, it just need to be initialized
33 dist.script_name = os.path.join(builddir, "setup.py")
34 dist.command_obj["build"] = support.DummyCommand(
35 build_base=builddir,
36 build_lib=os.path.join(builddir, "lib"),
37 )
38
Tarek Ziadé36797272010-07-22 12:50:05 +000039 cmd = install(dist)
40 cmd.home = destination
41 cmd.ensure_finalized()
Fred Drakeec6229e2004-06-25 23:02:59 +000042
43 self.assertEqual(cmd.install_base, destination)
44 self.assertEqual(cmd.install_platbase, destination)
45
46 def check_path(got, expected):
47 got = os.path.normpath(got)
48 expected = os.path.normpath(expected)
49 self.assertEqual(got, expected)
50
51 libdir = os.path.join(destination, "lib", "python")
52 check_path(cmd.install_lib, libdir)
53 check_path(cmd.install_platlib, libdir)
54 check_path(cmd.install_purelib, libdir)
55 check_path(cmd.install_headers,
56 os.path.join(destination, "include", "python", "foopkg"))
57 check_path(cmd.install_scripts, os.path.join(destination, "bin"))
58 check_path(cmd.install_data, destination)
59
Tarek Ziadé38e3d512009-02-27 12:58:56 +000060 def test_user_site(self):
61 # site.USER_SITE was introduced in 2.6
62 if sys.version < '2.6':
63 return
64
65 # preparing the environement for the test
Tarek Ziadé36797272010-07-22 12:50:05 +000066 self.old_user_base = site.USER_BASE
67 self.old_user_site = site.USER_SITE
Tarek Ziadé38e3d512009-02-27 12:58:56 +000068 self.tmpdir = self.mkdtemp()
69 self.user_base = os.path.join(self.tmpdir, 'B')
70 self.user_site = os.path.join(self.tmpdir, 'S')
Tarek Ziadé36797272010-07-22 12:50:05 +000071 site.USER_BASE = self.user_base
72 site.USER_SITE = self.user_site
73 install_module.USER_BASE = self.user_base
74 install_module.USER_SITE = self.user_site
Tarek Ziadé38e3d512009-02-27 12:58:56 +000075
76 def _expanduser(path):
Tarek Ziadé36797272010-07-22 12:50:05 +000077 return self.tmpdir
Tarek Ziadé38e3d512009-02-27 12:58:56 +000078 self.old_expand = os.path.expanduser
79 os.path.expanduser = _expanduser
80
81 try:
82 # this is the actual test
83 self._test_user_site()
84 finally:
Tarek Ziadé36797272010-07-22 12:50:05 +000085 site.USER_BASE = self.old_user_base
86 site.USER_SITE = self.old_user_site
87 install_module.USER_BASE = self.old_user_base
88 install_module.USER_SITE = self.old_user_site
Tarek Ziadé38e3d512009-02-27 12:58:56 +000089 os.path.expanduser = self.old_expand
90
91 def _test_user_site(self):
Tarek Ziadé36797272010-07-22 12:50:05 +000092 for key in ('nt_user', 'unix_user', 'os2_home'):
93 self.assertTrue(key in INSTALL_SCHEMES)
Tarek Ziadé38e3d512009-02-27 12:58:56 +000094
95 dist = Distribution({'name': 'xx'})
96 cmd = install(dist)
Tarek Ziadé36797272010-07-22 12:50:05 +000097
Tarek Ziadé38e3d512009-02-27 12:58:56 +000098 # making sure the user option is there
99 options = [name for name, short, lable in
100 cmd.user_options]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000101 self.assertTrue('user' in options)
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000102
103 # setting a value
104 cmd.user = 1
105
106 # user base and site shouldn't be created yet
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000107 self.assertTrue(not os.path.exists(self.user_base))
108 self.assertTrue(not os.path.exists(self.user_site))
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000109
110 # let's run finalize
111 cmd.ensure_finalized()
112
113 # now they should
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000114 self.assertTrue(os.path.exists(self.user_base))
115 self.assertTrue(os.path.exists(self.user_site))
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000116
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000117 self.assertTrue('userbase' in cmd.config_vars)
118 self.assertTrue('usersite' in cmd.config_vars)
Fred Drakeec6229e2004-06-25 23:02:59 +0000119
Tarek Ziadédc144a02009-06-04 07:39:50 +0000120 def test_handle_extra_path(self):
121 dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
122 cmd = install(dist)
123
124 # two elements
125 cmd.handle_extra_path()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000126 self.assertEqual(cmd.extra_path, ['path', 'dirs'])
127 self.assertEqual(cmd.extra_dirs, 'dirs')
128 self.assertEqual(cmd.path_file, 'path')
Tarek Ziadédc144a02009-06-04 07:39:50 +0000129
130 # one element
131 cmd.extra_path = ['path']
132 cmd.handle_extra_path()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000133 self.assertEqual(cmd.extra_path, ['path'])
134 self.assertEqual(cmd.extra_dirs, 'path')
135 self.assertEqual(cmd.path_file, 'path')
Tarek Ziadédc144a02009-06-04 07:39:50 +0000136
137 # none
138 dist.extra_path = cmd.extra_path = None
139 cmd.handle_extra_path()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000140 self.assertEqual(cmd.extra_path, None)
141 self.assertEqual(cmd.extra_dirs, '')
142 self.assertEqual(cmd.path_file, None)
Tarek Ziadédc144a02009-06-04 07:39:50 +0000143
144 # three elements (no way !)
145 cmd.extra_path = 'path,dirs,again'
146 self.assertRaises(DistutilsOptionError, cmd.handle_extra_path)
147
148 def test_finalize_options(self):
149 dist = Distribution({'name': 'xx'})
150 cmd = install(dist)
151
152 # must supply either prefix/exec-prefix/home or
153 # install-base/install-platbase -- not both
154 cmd.prefix = 'prefix'
155 cmd.install_base = 'base'
156 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
157
158 # must supply either home or prefix/exec-prefix -- not both
159 cmd.install_base = None
160 cmd.home = 'home'
161 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
162
163 # can't combine user with with prefix/exec_prefix/home or
164 # install_(plat)base
165 cmd.prefix = None
166 cmd.user = 'user'
167 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
168
169 def test_record(self):
170
171 install_dir = self.mkdtemp()
172 pkgdir, dist = self.create_dist()
173
174 dist = Distribution()
175 cmd = install(dist)
176 dist.command_obj['install'] = cmd
177 cmd.root = install_dir
178 cmd.record = os.path.join(pkgdir, 'RECORD')
179 cmd.ensure_finalized()
180
181 cmd.run()
182
183 # let's check the RECORD file was created with one
184 # line (the egg info file)
Éric Araujobee5cef2010-11-05 23:51:56 +0000185 f = open(cmd.record)
186 try:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000187 self.assertEqual(len(f.readlines()), 1)
Éric Araujobee5cef2010-11-05 23:51:56 +0000188 finally:
189 f.close()
Tarek Ziadédc144a02009-06-04 07:39:50 +0000190
Tarek Ziadé36797272010-07-22 12:50:05 +0000191 def test_debug_mode(self):
Tarek Ziadé03d5d082009-09-21 13:01:54 +0000192 # this covers the code called when DEBUG is set
193 old_logs_len = len(self.logs)
194 install_module.DEBUG = True
195 try:
196 with captured_stdout() as stdout:
197 self.test_record()
198 finally:
199 install_module.DEBUG = False
200 self.assertTrue(len(self.logs) > old_logs_len)
201
Fred Drakeec6229e2004-06-25 23:02:59 +0000202def test_suite():
203 return unittest.makeSuite(InstallTestCase)
204
205if __name__ == "__main__":
Éric Araujo70ec44a2010-11-06 02:44:43 +0000206 run_unittest(test_suite())