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): |
| 21 | if os.name == 'nt': |
| 22 | if sys.executable.endswith('_d.exe'): |
| 23 | modname += '_d' |
| 24 | return modname + get_config_var('SO') |
| 25 | |
| 26 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 27 | class 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 Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 39 | 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 | |
| 76 | @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') |
| 77 | def test_user_site(self): |
| 78 | # test install with --user |
| 79 | # preparing the environment for the test |
| 80 | self.old_user_base = get_config_var('userbase') |
| 81 | self.old_user_site = get_path('purelib', '%s_user' % os.name) |
| 82 | self.tmpdir = self.mkdtemp() |
| 83 | self.user_base = os.path.join(self.tmpdir, 'B') |
| 84 | self.user_site = os.path.join(self.tmpdir, 'S') |
| 85 | _CONFIG_VARS['userbase'] = self.user_base |
| 86 | scheme = '%s_user' % os.name |
| 87 | _SCHEMES.set(scheme, 'purelib', self.user_site) |
| 88 | |
| 89 | def _expanduser(path): |
| 90 | if path[0] == '~': |
| 91 | path = os.path.normpath(self.tmpdir) + path[1:] |
| 92 | return path |
| 93 | |
| 94 | self.old_expand = os.path.expanduser |
| 95 | os.path.expanduser = _expanduser |
| 96 | |
| 97 | try: |
| 98 | # this is the actual test |
| 99 | self._test_user_site() |
| 100 | finally: |
| 101 | _CONFIG_VARS['userbase'] = self.old_user_base |
| 102 | _SCHEMES.set(scheme, 'purelib', self.old_user_site) |
| 103 | os.path.expanduser = self.old_expand |
| 104 | |
| 105 | def _test_user_site(self): |
| 106 | schemes = get_scheme_names() |
| 107 | for key in ('nt_user', 'posix_user', 'os2_home'): |
| 108 | self.assertIn(key, schemes) |
| 109 | |
| 110 | dist = Distribution({'name': 'xx'}) |
| 111 | cmd = install_dist(dist) |
| 112 | # making sure the user option is there |
| 113 | options = [name for name, short, lable in |
| 114 | cmd.user_options] |
| 115 | self.assertIn('user', options) |
| 116 | |
| 117 | # setting a value |
| 118 | cmd.user = True |
| 119 | |
| 120 | # user base and site shouldn't be created yet |
| 121 | self.assertFalse(os.path.exists(self.user_base)) |
| 122 | self.assertFalse(os.path.exists(self.user_site)) |
| 123 | |
| 124 | # let's run finalize |
| 125 | cmd.ensure_finalized() |
| 126 | |
| 127 | # now they should |
| 128 | self.assertTrue(os.path.exists(self.user_base)) |
| 129 | self.assertTrue(os.path.exists(self.user_site)) |
| 130 | |
| 131 | self.assertIn('userbase', cmd.config_vars) |
| 132 | self.assertIn('usersite', cmd.config_vars) |
| 133 | |
| 134 | def test_handle_extra_path(self): |
| 135 | dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'}) |
| 136 | cmd = install_dist(dist) |
| 137 | |
| 138 | # two elements |
| 139 | cmd.handle_extra_path() |
| 140 | self.assertEqual(cmd.extra_path, ['path', 'dirs']) |
| 141 | self.assertEqual(cmd.extra_dirs, 'dirs') |
| 142 | self.assertEqual(cmd.path_file, 'path') |
| 143 | |
| 144 | # one element |
| 145 | cmd.extra_path = ['path'] |
| 146 | cmd.handle_extra_path() |
| 147 | self.assertEqual(cmd.extra_path, ['path']) |
| 148 | self.assertEqual(cmd.extra_dirs, 'path') |
| 149 | self.assertEqual(cmd.path_file, 'path') |
| 150 | |
| 151 | # none |
| 152 | dist.extra_path = cmd.extra_path = None |
| 153 | cmd.handle_extra_path() |
| 154 | self.assertEqual(cmd.extra_path, None) |
| 155 | self.assertEqual(cmd.extra_dirs, '') |
| 156 | self.assertEqual(cmd.path_file, None) |
| 157 | |
| 158 | # three elements (no way !) |
| 159 | cmd.extra_path = 'path,dirs,again' |
| 160 | self.assertRaises(PackagingOptionError, cmd.handle_extra_path) |
| 161 | |
| 162 | def test_finalize_options(self): |
| 163 | dist = Distribution({'name': 'xx'}) |
| 164 | cmd = install_dist(dist) |
| 165 | |
| 166 | # must supply either prefix/exec-prefix/home or |
| 167 | # install-base/install-platbase -- not both |
| 168 | cmd.prefix = 'prefix' |
| 169 | cmd.install_base = 'base' |
| 170 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 171 | |
| 172 | # must supply either home or prefix/exec-prefix -- not both |
| 173 | cmd.install_base = None |
| 174 | cmd.home = 'home' |
| 175 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 176 | |
| 177 | if sys.version >= '2.6': |
| 178 | # can't combine user with with prefix/exec_prefix/home or |
| 179 | # install_(plat)base |
| 180 | cmd.prefix = None |
| 181 | cmd.user = 'user' |
| 182 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 183 | |
| 184 | def test_old_record(self): |
| 185 | # test pre-PEP 376 --record option (outside dist-info dir) |
| 186 | install_dir = self.mkdtemp() |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 187 | project_dir, dist = self.create_dist(scripts=['hello']) |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 188 | os.chdir(project_dir) |
| 189 | self.write_file('hello', "print('o hai')") |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 190 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 191 | cmd = install_dist(dist) |
| 192 | dist.command_obj['install_dist'] = cmd |
| 193 | cmd.root = install_dir |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 194 | cmd.record = os.path.join(project_dir, 'filelist') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 195 | cmd.ensure_finalized() |
| 196 | cmd.run() |
| 197 | |
Victor Stinner | 21a9c74 | 2011-05-19 15:51:27 +0200 | [diff] [blame] | 198 | with open(cmd.record) as f: |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 199 | content = f.read() |
| 200 | |
| 201 | found = [os.path.basename(line) for line in content.splitlines()] |
| 202 | expected = ['hello', 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] |
| 203 | self.assertEqual(found, expected) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 204 | |
| 205 | # XXX test that fancy_getopt is okay with options named |
| 206 | # record and no-record but unrelated |
| 207 | |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 208 | 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 Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame^] | 215 | |
| 216 | buildextcmd = build_ext(dist) |
| 217 | support.fixup_build_ext(buildextcmd) |
| 218 | buildextcmd.ensure_finalized() |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 219 | |
| 220 | cmd = install_dist(dist) |
| 221 | dist.command_obj['install_dist'] = cmd |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame^] | 222 | dist.command_obj['build_ext'] = buildextcmd |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 223 | 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 Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame^] | 232 | expected = [_make_ext_name('xx'), |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 233 | 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] |
| 234 | self.assertEqual(found, expected) |
| 235 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 236 | |
| 237 | def test_suite(): |
| 238 | return unittest.makeSuite(InstallTestCase) |
| 239 | |
| 240 | if __name__ == "__main__": |
| 241 | unittest.main(defaultTest="test_suite") |