blob: 3345d2e39afba68b23f03ff69a5cda42cee5b887 [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Tests for packaging.command.install."""
2
3import os
Éric Araujo4b5a5f72011-10-19 08:18:05 +02004import imp
Tarek Ziade1231a4e2011-05-19 13:07:25 +02005import sys
Tarek Ziade1231a4e2011-05-19 13:07:25 +02006from sysconfig import (get_scheme_names, get_config_vars,
7 _SCHEMES, get_config_var, get_path)
8
Éric Araujoe049f472011-08-24 02:15:25 +02009from packaging.command.build_ext import build_ext
Tarek Ziade1231a4e2011-05-19 13:07:25 +020010from packaging.command.install_dist import install_dist
Éric Araujo540edc62011-08-20 07:42:56 +020011from packaging.compiler.extension import Extension
Tarek Ziade1231a4e2011-05-19 13:07:25 +020012from packaging.dist import Distribution
13from packaging.errors import PackagingOptionError
14
15from packaging.tests import unittest, support
16
17
Éric Araujo746e72d2011-08-20 07:34:43 +020018_CONFIG_VARS = get_config_vars()
19
20
Éric Araujoe049f472011-08-24 02:15:25 +020021def _make_ext_name(modname):
Éric Araujo13291852011-08-26 00:05:11 +020022 if os.name == 'nt' and sys.executable.endswith('_d.exe'):
23 modname += '_d'
Éric Araujoe049f472011-08-24 02:15:25 +020024 return modname + get_config_var('SO')
25
26
Tarek Ziade1231a4e2011-05-19 13:07:25 +020027class InstallTestCase(support.TempdirManager,
28 support.LoggingCatcher,
29 unittest.TestCase):
30
31 def test_home_installation_scheme(self):
32 # This ensure two things:
33 # - that --home generates the desired set of directory names
34 # - test --home is supported on all platforms
35 builddir = self.mkdtemp()
36 destination = os.path.join(builddir, "installation")
37
38 dist = Distribution({"name": "foopkg"})
Tarek Ziade1231a4e2011-05-19 13:07:25 +020039 dist.command_obj["build"] = support.DummyCommand(
40 build_base=builddir,
41 build_lib=os.path.join(builddir, "lib"),
42 )
43
44 old_posix_prefix = _SCHEMES.get('posix_prefix', 'platinclude')
45 old_posix_home = _SCHEMES.get('posix_home', 'platinclude')
46
47 new_path = '{platbase}/include/python{py_version_short}'
48 _SCHEMES.set('posix_prefix', 'platinclude', new_path)
49 _SCHEMES.set('posix_home', 'platinclude', '{platbase}/include/python')
50
51 try:
52 cmd = install_dist(dist)
53 cmd.home = destination
54 cmd.ensure_finalized()
55 finally:
56 _SCHEMES.set('posix_prefix', 'platinclude', old_posix_prefix)
57 _SCHEMES.set('posix_home', 'platinclude', old_posix_home)
58
59 self.assertEqual(cmd.install_base, destination)
60 self.assertEqual(cmd.install_platbase, destination)
61
62 def check_path(got, expected):
63 got = os.path.normpath(got)
64 expected = os.path.normpath(expected)
65 self.assertEqual(got, expected)
66
67 libdir = os.path.join(destination, "lib", "python")
68 check_path(cmd.install_lib, libdir)
69 check_path(cmd.install_platlib, libdir)
70 check_path(cmd.install_purelib, libdir)
71 check_path(cmd.install_headers,
72 os.path.join(destination, "include", "python", "foopkg"))
73 check_path(cmd.install_scripts, os.path.join(destination, "bin"))
74 check_path(cmd.install_data, destination)
75
Tarek Ziade1231a4e2011-05-19 13:07:25 +020076 def test_user_site(self):
77 # test install with --user
78 # preparing the environment for the test
79 self.old_user_base = get_config_var('userbase')
80 self.old_user_site = get_path('purelib', '%s_user' % os.name)
81 self.tmpdir = self.mkdtemp()
82 self.user_base = os.path.join(self.tmpdir, 'B')
83 self.user_site = os.path.join(self.tmpdir, 'S')
84 _CONFIG_VARS['userbase'] = self.user_base
85 scheme = '%s_user' % os.name
86 _SCHEMES.set(scheme, 'purelib', self.user_site)
87
88 def _expanduser(path):
89 if path[0] == '~':
90 path = os.path.normpath(self.tmpdir) + path[1:]
91 return path
92
93 self.old_expand = os.path.expanduser
94 os.path.expanduser = _expanduser
95
Éric Araujof3f283a2011-10-19 08:49:20 +020096 def cleanup():
Tarek Ziade1231a4e2011-05-19 13:07:25 +020097 _CONFIG_VARS['userbase'] = self.old_user_base
98 _SCHEMES.set(scheme, 'purelib', self.old_user_site)
99 os.path.expanduser = self.old_expand
100
Éric Araujof3f283a2011-10-19 08:49:20 +0200101 self.addCleanup(cleanup)
102
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200103 schemes = get_scheme_names()
104 for key in ('nt_user', 'posix_user', 'os2_home'):
105 self.assertIn(key, schemes)
106
107 dist = Distribution({'name': 'xx'})
108 cmd = install_dist(dist)
Éric Araujof3f283a2011-10-19 08:49:20 +0200109
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200110 # 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 Araujo4b5a5f72011-10-19 08:18:05 +0200184 project_dir, dist = self.create_dist(py_modules=['hello'],
185 scripts=['sayhi'])
Éric Araujo746e72d2011-08-20 07:34:43 +0200186 os.chdir(project_dir)
Éric Araujo4b5a5f72011-10-19 08:18:05 +0200187 self.write_file('hello.py', "def main(): print('o hai')")
188 self.write_file('sayhi', 'from hello import main; main()')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200189
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200190 cmd = install_dist(dist)
191 dist.command_obj['install_dist'] = cmd
192 cmd.root = install_dir
Éric Araujo746e72d2011-08-20 07:34:43 +0200193 cmd.record = os.path.join(project_dir, 'filelist')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200194 cmd.ensure_finalized()
195 cmd.run()
196
Victor Stinner21a9c742011-05-19 15:51:27 +0200197 with open(cmd.record) as f:
Éric Araujo746e72d2011-08-20 07:34:43 +0200198 content = f.read()
199
200 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujo4b5a5f72011-10-19 08:18:05 +0200201 expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi',
202 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
203 self.assertEqual(sorted(found), sorted(expected))
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200204
205 # XXX test that fancy_getopt is okay with options named
206 # record and no-record but unrelated
207
Éric Araujo540edc62011-08-20 07:42:56 +0200208 def test_old_record_extensions(self):
209 # test pre-PEP 376 --record option with ext modules
210 install_dir = self.mkdtemp()
211 project_dir, dist = self.create_dist(ext_modules=[
212 Extension('xx', ['xxmodule.c'])])
213 os.chdir(project_dir)
214 support.copy_xxmodule_c(project_dir)
Éric Araujoe049f472011-08-24 02:15:25 +0200215
216 buildextcmd = build_ext(dist)
217 support.fixup_build_ext(buildextcmd)
218 buildextcmd.ensure_finalized()
Éric Araujo540edc62011-08-20 07:42:56 +0200219
220 cmd = install_dist(dist)
221 dist.command_obj['install_dist'] = cmd
Éric Araujoe049f472011-08-24 02:15:25 +0200222 dist.command_obj['build_ext'] = buildextcmd
Éric Araujo540edc62011-08-20 07:42:56 +0200223 cmd.root = install_dir
224 cmd.record = os.path.join(project_dir, 'filelist')
225 cmd.ensure_finalized()
226 cmd.run()
227
228 with open(cmd.record) as f:
229 content = f.read()
230
231 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujoe049f472011-08-24 02:15:25 +0200232 expected = [_make_ext_name('xx'),
Éric Araujo540edc62011-08-20 07:42:56 +0200233 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
234 self.assertEqual(found, expected)
235
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200236
237def test_suite():
238 return unittest.makeSuite(InstallTestCase)
239
240if __name__ == "__main__":
241 unittest.main(defaultTest="test_suite")