Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """Tests for packaging.command.install.""" |
| 2 | |
| 3 | import os |
| 4 | import sys |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 5 | from sysconfig import (get_scheme_names, get_config_vars, |
| 6 | _SCHEMES, get_config_var, get_path) |
| 7 | |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 8 | from packaging.command.build_ext import build_ext |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 9 | from packaging.command.install_dist import install_dist |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 10 | from packaging.compiler.extension import Extension |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 11 | from packaging.dist import Distribution |
| 12 | from packaging.errors import PackagingOptionError |
| 13 | |
| 14 | from packaging.tests import unittest, support |
| 15 | |
| 16 | |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 17 | _CONFIG_VARS = get_config_vars() |
| 18 | |
| 19 | |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 20 | def _make_ext_name(modname): |
Éric Araujo | 1329185 | 2011-08-26 00:05:11 +0200 | [diff] [blame^] | 21 | if os.name == 'nt' and sys.executable.endswith('_d.exe'): |
| 22 | modname += '_d' |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 23 | return modname + get_config_var('SO') |
| 24 | |
| 25 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 26 | class 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 Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 38 | 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 | |
| 75 | @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') |
| 76 | 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 | |
| 176 | if sys.version >= '2.6': |
| 177 | # can't combine user with with prefix/exec_prefix/home or |
| 178 | # install_(plat)base |
| 179 | cmd.prefix = None |
| 180 | cmd.user = 'user' |
| 181 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 182 | |
| 183 | def test_old_record(self): |
| 184 | # test pre-PEP 376 --record option (outside dist-info dir) |
| 185 | install_dir = self.mkdtemp() |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 186 | project_dir, dist = self.create_dist(scripts=['hello']) |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 187 | os.chdir(project_dir) |
| 188 | self.write_file('hello', "print('o hai')") |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 189 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 190 | cmd = install_dist(dist) |
| 191 | dist.command_obj['install_dist'] = cmd |
| 192 | cmd.root = install_dir |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 193 | cmd.record = os.path.join(project_dir, 'filelist') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 194 | cmd.ensure_finalized() |
| 195 | cmd.run() |
| 196 | |
Victor Stinner | 21a9c74 | 2011-05-19 15:51:27 +0200 | [diff] [blame] | 197 | with open(cmd.record) as f: |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 198 | content = f.read() |
| 199 | |
| 200 | found = [os.path.basename(line) for line in content.splitlines()] |
| 201 | expected = ['hello', 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] |
| 202 | self.assertEqual(found, expected) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 203 | |
| 204 | # XXX test that fancy_getopt is okay with options named |
| 205 | # record and no-record but unrelated |
| 206 | |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 207 | def test_old_record_extensions(self): |
| 208 | # test pre-PEP 376 --record option with ext modules |
| 209 | install_dir = self.mkdtemp() |
| 210 | project_dir, dist = self.create_dist(ext_modules=[ |
| 211 | Extension('xx', ['xxmodule.c'])]) |
| 212 | os.chdir(project_dir) |
| 213 | support.copy_xxmodule_c(project_dir) |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 214 | |
| 215 | buildextcmd = build_ext(dist) |
| 216 | support.fixup_build_ext(buildextcmd) |
| 217 | buildextcmd.ensure_finalized() |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 218 | |
| 219 | cmd = install_dist(dist) |
| 220 | dist.command_obj['install_dist'] = cmd |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 221 | dist.command_obj['build_ext'] = buildextcmd |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 222 | cmd.root = install_dir |
| 223 | cmd.record = os.path.join(project_dir, 'filelist') |
| 224 | cmd.ensure_finalized() |
| 225 | cmd.run() |
| 226 | |
| 227 | with open(cmd.record) as f: |
| 228 | content = f.read() |
| 229 | |
| 230 | found = [os.path.basename(line) for line in content.splitlines()] |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 231 | expected = [_make_ext_name('xx'), |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 232 | 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] |
| 233 | self.assertEqual(found, expected) |
| 234 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 235 | |
| 236 | def test_suite(): |
| 237 | return unittest.makeSuite(InstallTestCase) |
| 238 | |
| 239 | if __name__ == "__main__": |
| 240 | unittest.main(defaultTest="test_suite") |