blob: 496a3d800f5907ac4a213ea4baf0eefb5f2e8e81 [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
96 try:
97 # this is the actual test
98 self._test_user_site()
99 finally:
100 _CONFIG_VARS['userbase'] = self.old_user_base
101 _SCHEMES.set(scheme, 'purelib', self.old_user_site)
102 os.path.expanduser = self.old_expand
103
104 def _test_user_site(self):
105 schemes = get_scheme_names()
106 for key in ('nt_user', 'posix_user', 'os2_home'):
107 self.assertIn(key, schemes)
108
109 dist = Distribution({'name': 'xx'})
110 cmd = install_dist(dist)
111 # making sure the user option is there
112 options = [name for name, short, lable in
113 cmd.user_options]
114 self.assertIn('user', options)
115
116 # setting a value
117 cmd.user = True
118
119 # user base and site shouldn't be created yet
120 self.assertFalse(os.path.exists(self.user_base))
121 self.assertFalse(os.path.exists(self.user_site))
122
123 # let's run finalize
124 cmd.ensure_finalized()
125
126 # now they should
127 self.assertTrue(os.path.exists(self.user_base))
128 self.assertTrue(os.path.exists(self.user_site))
129
130 self.assertIn('userbase', cmd.config_vars)
131 self.assertIn('usersite', cmd.config_vars)
132
133 def test_handle_extra_path(self):
134 dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
135 cmd = install_dist(dist)
136
137 # two elements
138 cmd.handle_extra_path()
139 self.assertEqual(cmd.extra_path, ['path', 'dirs'])
140 self.assertEqual(cmd.extra_dirs, 'dirs')
141 self.assertEqual(cmd.path_file, 'path')
142
143 # one element
144 cmd.extra_path = ['path']
145 cmd.handle_extra_path()
146 self.assertEqual(cmd.extra_path, ['path'])
147 self.assertEqual(cmd.extra_dirs, 'path')
148 self.assertEqual(cmd.path_file, 'path')
149
150 # none
151 dist.extra_path = cmd.extra_path = None
152 cmd.handle_extra_path()
153 self.assertEqual(cmd.extra_path, None)
154 self.assertEqual(cmd.extra_dirs, '')
155 self.assertEqual(cmd.path_file, None)
156
157 # three elements (no way !)
158 cmd.extra_path = 'path,dirs,again'
159 self.assertRaises(PackagingOptionError, cmd.handle_extra_path)
160
161 def test_finalize_options(self):
162 dist = Distribution({'name': 'xx'})
163 cmd = install_dist(dist)
164
165 # must supply either prefix/exec-prefix/home or
166 # install-base/install-platbase -- not both
167 cmd.prefix = 'prefix'
168 cmd.install_base = 'base'
169 self.assertRaises(PackagingOptionError, cmd.finalize_options)
170
171 # must supply either home or prefix/exec-prefix -- not both
172 cmd.install_base = None
173 cmd.home = 'home'
174 self.assertRaises(PackagingOptionError, cmd.finalize_options)
175
Éric Araujo7724a6c2011-09-17 03:31:51 +0200176 # can't combine user with with prefix/exec_prefix/home or
177 # install_(plat)base
178 cmd.prefix = None
179 cmd.user = 'user'
180 self.assertRaises(PackagingOptionError, cmd.finalize_options)
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200181
182 def test_old_record(self):
183 # test pre-PEP 376 --record option (outside dist-info dir)
184 install_dir = self.mkdtemp()
Éric Araujo4b5a5f72011-10-19 08:18:05 +0200185 project_dir, dist = self.create_dist(py_modules=['hello'],
186 scripts=['sayhi'])
Éric Araujo746e72d2011-08-20 07:34:43 +0200187 os.chdir(project_dir)
Éric Araujo4b5a5f72011-10-19 08:18:05 +0200188 self.write_file('hello.py', "def main(): print('o hai')")
189 self.write_file('sayhi', 'from hello import main; main()')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200190
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200191 cmd = install_dist(dist)
192 dist.command_obj['install_dist'] = cmd
193 cmd.root = install_dir
Éric Araujo746e72d2011-08-20 07:34:43 +0200194 cmd.record = os.path.join(project_dir, 'filelist')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200195 cmd.ensure_finalized()
196 cmd.run()
197
Victor Stinner21a9c742011-05-19 15:51:27 +0200198 with open(cmd.record) as f:
Éric Araujo746e72d2011-08-20 07:34:43 +0200199 content = f.read()
200
201 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujo4b5a5f72011-10-19 08:18:05 +0200202 expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi',
203 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
204 self.assertEqual(sorted(found), sorted(expected))
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200205
206 # XXX test that fancy_getopt is okay with options named
207 # record and no-record but unrelated
208
Éric Araujo540edc62011-08-20 07:42:56 +0200209 def test_old_record_extensions(self):
210 # test pre-PEP 376 --record option with ext modules
211 install_dir = self.mkdtemp()
212 project_dir, dist = self.create_dist(ext_modules=[
213 Extension('xx', ['xxmodule.c'])])
214 os.chdir(project_dir)
215 support.copy_xxmodule_c(project_dir)
Éric Araujoe049f472011-08-24 02:15:25 +0200216
217 buildextcmd = build_ext(dist)
218 support.fixup_build_ext(buildextcmd)
219 buildextcmd.ensure_finalized()
Éric Araujo540edc62011-08-20 07:42:56 +0200220
221 cmd = install_dist(dist)
222 dist.command_obj['install_dist'] = cmd
Éric Araujoe049f472011-08-24 02:15:25 +0200223 dist.command_obj['build_ext'] = buildextcmd
Éric Araujo540edc62011-08-20 07:42:56 +0200224 cmd.root = install_dir
225 cmd.record = os.path.join(project_dir, 'filelist')
226 cmd.ensure_finalized()
227 cmd.run()
228
229 with open(cmd.record) as f:
230 content = f.read()
231
232 found = [os.path.basename(line) for line in content.splitlines()]
Éric Araujoe049f472011-08-24 02:15:25 +0200233 expected = [_make_ext_name('xx'),
Éric Araujo540edc62011-08-20 07:42:56 +0200234 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD']
235 self.assertEqual(found, expected)
236
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200237
238def test_suite():
239 return unittest.makeSuite(InstallTestCase)
240
241if __name__ == "__main__":
242 unittest.main(defaultTest="test_suite")