blob: 808b568cdb908efa4753aaa61611524c7e04fbab [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Tests for packaging.command.install."""
2
3import os
4import sys
Tarek Ziade1231a4e2011-05-19 13:07:25 +02005from sysconfig import (get_scheme_names, get_config_vars,
6 _SCHEMES, get_config_var, get_path)
7
Éric Araujoe049f472011-08-24 02:15:25 +02008from packaging.command.build_ext import build_ext
Tarek Ziade1231a4e2011-05-19 13:07:25 +02009from packaging.command.install_dist import install_dist
Éric Araujo540edc62011-08-20 07:42:56 +020010from packaging.compiler.extension import Extension
Tarek Ziade1231a4e2011-05-19 13:07:25 +020011from packaging.dist import Distribution
12from packaging.errors import PackagingOptionError
13
14from packaging.tests import unittest, support
15
16
Éric Araujo746e72d2011-08-20 07:34:43 +020017_CONFIG_VARS = get_config_vars()
18
19
Éric Araujoe049f472011-08-24 02:15:25 +020020def _make_ext_name(modname):
Éric Araujo13291852011-08-26 00:05:11 +020021 if os.name == 'nt' and sys.executable.endswith('_d.exe'):
22 modname += '_d'
Éric Araujoe049f472011-08-24 02:15:25 +020023 return modname + get_config_var('SO')
24
25
Tarek Ziade1231a4e2011-05-19 13:07:25 +020026class InstallTestCase(support.TempdirManager,
27 support.LoggingCatcher,
28 unittest.TestCase):
29
30 def test_home_installation_scheme(self):
31 # This ensure two things:
32 # - that --home generates the desired set of directory names
33 # - test --home is supported on all platforms
34 builddir = self.mkdtemp()
35 destination = os.path.join(builddir, "installation")
36
37 dist = Distribution({"name": "foopkg"})
Tarek Ziade1231a4e2011-05-19 13:07:25 +020038 dist.command_obj["build"] = support.DummyCommand(
39 build_base=builddir,
40 build_lib=os.path.join(builddir, "lib"),
41 )
42
43 old_posix_prefix = _SCHEMES.get('posix_prefix', 'platinclude')
44 old_posix_home = _SCHEMES.get('posix_home', 'platinclude')
45
46 new_path = '{platbase}/include/python{py_version_short}'
47 _SCHEMES.set('posix_prefix', 'platinclude', new_path)
48 _SCHEMES.set('posix_home', 'platinclude', '{platbase}/include/python')
49
50 try:
51 cmd = install_dist(dist)
52 cmd.home = destination
53 cmd.ensure_finalized()
54 finally:
55 _SCHEMES.set('posix_prefix', 'platinclude', old_posix_prefix)
56 _SCHEMES.set('posix_home', 'platinclude', old_posix_home)
57
58 self.assertEqual(cmd.install_base, destination)
59 self.assertEqual(cmd.install_platbase, destination)
60
61 def check_path(got, expected):
62 got = os.path.normpath(got)
63 expected = os.path.normpath(expected)
64 self.assertEqual(got, expected)
65
66 libdir = os.path.join(destination, "lib", "python")
67 check_path(cmd.install_lib, libdir)
68 check_path(cmd.install_platlib, libdir)
69 check_path(cmd.install_purelib, libdir)
70 check_path(cmd.install_headers,
71 os.path.join(destination, "include", "python", "foopkg"))
72 check_path(cmd.install_scripts, os.path.join(destination, "bin"))
73 check_path(cmd.install_data, destination)
74
Tarek Ziade1231a4e2011-05-19 13:07:25 +020075 def test_user_site(self):
76 # test install with --user
77 # preparing the environment for the test
78 self.old_user_base = get_config_var('userbase')
79 self.old_user_site = get_path('purelib', '%s_user' % os.name)
80 self.tmpdir = self.mkdtemp()
81 self.user_base = os.path.join(self.tmpdir, 'B')
82 self.user_site = os.path.join(self.tmpdir, 'S')
83 _CONFIG_VARS['userbase'] = self.user_base
84 scheme = '%s_user' % os.name
85 _SCHEMES.set(scheme, 'purelib', self.user_site)
86
87 def _expanduser(path):
88 if path[0] == '~':
89 path = os.path.normpath(self.tmpdir) + path[1:]
90 return path
91
92 self.old_expand = os.path.expanduser
93 os.path.expanduser = _expanduser
94
95 try:
96 # this is the actual test
97 self._test_user_site()
98 finally:
99 _CONFIG_VARS['userbase'] = self.old_user_base
100 _SCHEMES.set(scheme, 'purelib', self.old_user_site)
101 os.path.expanduser = self.old_expand
102
103 def _test_user_site(self):
104 schemes = get_scheme_names()
105 for key in ('nt_user', 'posix_user', 'os2_home'):
106 self.assertIn(key, schemes)
107
108 dist = Distribution({'name': 'xx'})
109 cmd = install_dist(dist)
110 # making sure the user option is there
111 options = [name for name, short, lable in
112 cmd.user_options]
113 self.assertIn('user', options)
114
115 # setting a value
116 cmd.user = True
117
118 # user base and site shouldn't be created yet
119 self.assertFalse(os.path.exists(self.user_base))
120 self.assertFalse(os.path.exists(self.user_site))
121
122 # let's run finalize
123 cmd.ensure_finalized()
124
125 # now they should
126 self.assertTrue(os.path.exists(self.user_base))
127 self.assertTrue(os.path.exists(self.user_site))
128
129 self.assertIn('userbase', cmd.config_vars)
130 self.assertIn('usersite', cmd.config_vars)
131
132 def test_handle_extra_path(self):
133 dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
134 cmd = install_dist(dist)
135
136 # two elements
137 cmd.handle_extra_path()
138 self.assertEqual(cmd.extra_path, ['path', 'dirs'])
139 self.assertEqual(cmd.extra_dirs, 'dirs')
140 self.assertEqual(cmd.path_file, 'path')
141
142 # one element
143 cmd.extra_path = ['path']
144 cmd.handle_extra_path()
145 self.assertEqual(cmd.extra_path, ['path'])
146 self.assertEqual(cmd.extra_dirs, 'path')
147 self.assertEqual(cmd.path_file, 'path')
148
149 # none
150 dist.extra_path = cmd.extra_path = None
151 cmd.handle_extra_path()
152 self.assertEqual(cmd.extra_path, None)
153 self.assertEqual(cmd.extra_dirs, '')
154 self.assertEqual(cmd.path_file, None)
155
156 # three elements (no way !)
157 cmd.extra_path = 'path,dirs,again'
158 self.assertRaises(PackagingOptionError, cmd.handle_extra_path)
159
160 def test_finalize_options(self):
161 dist = Distribution({'name': 'xx'})
162 cmd = install_dist(dist)
163
164 # must supply either prefix/exec-prefix/home or
165 # install-base/install-platbase -- not both
166 cmd.prefix = 'prefix'
167 cmd.install_base = 'base'
168 self.assertRaises(PackagingOptionError, cmd.finalize_options)
169
170 # must supply either home or prefix/exec-prefix -- not both
171 cmd.install_base = None
172 cmd.home = 'home'
173 self.assertRaises(PackagingOptionError, cmd.finalize_options)
174
Éric Araujo7724a6c2011-09-17 03:31:51 +0200175 # can't combine user with with prefix/exec_prefix/home or
176 # install_(plat)base
177 cmd.prefix = None
178 cmd.user = 'user'
179 self.assertRaises(PackagingOptionError, cmd.finalize_options)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200180
181 def test_old_record(self):
182 # test pre-PEP 376 --record option (outside dist-info dir)
183 install_dir = self.mkdtemp()
Éric Araujo746e72d2011-08-20 07:34:43 +0200184 project_dir, dist = self.create_dist(scripts=['hello'])
Éric Araujo746e72d2011-08-20 07:34:43 +0200185 os.chdir(project_dir)
186 self.write_file('hello', "print('o hai')")
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200187
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200188 cmd = install_dist(dist)
189 dist.command_obj['install_dist'] = cmd
190 cmd.root = install_dir
Éric Araujo746e72d2011-08-20 07:34:43 +0200191 cmd.record = os.path.join(project_dir, 'filelist')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200192 cmd.ensure_finalized()
193 cmd.run()
194
Victor Stinner21a9c742011-05-19 15:51:27 +0200195 with open(cmd.record) as f:
Éric Araujo746e72d2011-08-20 07:34:43 +0200196 content = f.read()
197
198 found = [os.path.basename(line) for line in content.splitlines()]
199 expected = ['hello', 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
200 self.assertEqual(found, expected)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200201
202 # XXX test that fancy_getopt is okay with options named
203 # record and no-record but unrelated
204
Éric Araujo540edc62011-08-20 07:42:56 +0200205 def test_old_record_extensions(self):
206 # test pre-PEP 376 --record option with ext modules
207 install_dir = self.mkdtemp()
208 project_dir, dist = self.create_dist(ext_modules=[
209 Extension('xx', ['xxmodule.c'])])
210 os.chdir(project_dir)
211 support.copy_xxmodule_c(project_dir)
Éric Araujoe049f472011-08-24 02:15:25 +0200212
213 buildextcmd = build_ext(dist)
214 support.fixup_build_ext(buildextcmd)
215 buildextcmd.ensure_finalized()
Éric Araujo540edc62011-08-20 07:42:56 +0200216
217 cmd = install_dist(dist)
218 dist.command_obj['install_dist'] = cmd
Éric Araujoe049f472011-08-24 02:15:25 +0200219 dist.command_obj['build_ext'] = buildextcmd
Éric Araujo540edc62011-08-20 07:42:56 +0200220 cmd.root = install_dir
221 cmd.record = os.path.join(project_dir, 'filelist')
222 cmd.ensure_finalized()
223 cmd.run()
224
225 with open(cmd.record) as f:
226 content = f.read()
227
228 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujoe049f472011-08-24 02:15:25 +0200229 expected = [_make_ext_name('xx'),
Éric Araujo540edc62011-08-20 07:42:56 +0200230 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
231 self.assertEqual(found, expected)
232
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200233
234def test_suite():
235 return unittest.makeSuite(InstallTestCase)
236
237if __name__ == "__main__":
238 unittest.main(defaultTest="test_suite")