Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """Tests for packaging.command.install.""" |
| 2 | |
| 3 | import os |
Éric Araujo | 4b5a5f7 | 2011-10-19 08:18:05 +0200 | [diff] [blame^] | 4 | import imp |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 5 | import sys |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 6 | from sysconfig import (get_scheme_names, get_config_vars, |
| 7 | _SCHEMES, get_config_var, get_path) |
| 8 | |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 9 | from packaging.command.build_ext import build_ext |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 10 | from packaging.command.install_dist import install_dist |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 11 | from packaging.compiler.extension import Extension |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 12 | from packaging.dist import Distribution |
| 13 | from packaging.errors import PackagingOptionError |
| 14 | |
| 15 | from packaging.tests import unittest, support |
| 16 | |
| 17 | |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 18 | _CONFIG_VARS = get_config_vars() |
| 19 | |
| 20 | |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 21 | def _make_ext_name(modname): |
Éric Araujo | 1329185 | 2011-08-26 00:05:11 +0200 | [diff] [blame] | 22 | if os.name == 'nt' and sys.executable.endswith('_d.exe'): |
| 23 | modname += '_d' |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 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 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 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 | |
Éric Araujo | 7724a6c | 2011-09-17 03:31:51 +0200 | [diff] [blame] | 176 | # 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 Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 181 | |
| 182 | def test_old_record(self): |
| 183 | # test pre-PEP 376 --record option (outside dist-info dir) |
| 184 | install_dir = self.mkdtemp() |
Éric Araujo | 4b5a5f7 | 2011-10-19 08:18:05 +0200 | [diff] [blame^] | 185 | project_dir, dist = self.create_dist(py_modules=['hello'], |
| 186 | scripts=['sayhi']) |
Éric Araujo | 746e72d | 2011-08-20 07:34:43 +0200 | [diff] [blame] | 187 | os.chdir(project_dir) |
Éric Araujo | 4b5a5f7 | 2011-10-19 08:18:05 +0200 | [diff] [blame^] | 188 | self.write_file('hello.py', "def main(): print('o hai')") |
| 189 | self.write_file('sayhi', 'from hello import main; main()') |
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()] |
Éric Araujo | 4b5a5f7 | 2011-10-19 08:18:05 +0200 | [diff] [blame^] | 202 | expected = ['hello.py', 'hello.%s.pyc' % imp.get_tag(), 'sayhi', |
| 203 | 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] |
| 204 | self.assertEqual(sorted(found), sorted(expected)) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 205 | |
| 206 | # XXX test that fancy_getopt is okay with options named |
| 207 | # record and no-record but unrelated |
| 208 | |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 209 | 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 Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 216 | |
| 217 | buildextcmd = build_ext(dist) |
| 218 | support.fixup_build_ext(buildextcmd) |
| 219 | buildextcmd.ensure_finalized() |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 220 | |
| 221 | cmd = install_dist(dist) |
| 222 | dist.command_obj['install_dist'] = cmd |
Éric Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 223 | dist.command_obj['build_ext'] = buildextcmd |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 224 | 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 Araujo | e049f47 | 2011-08-24 02:15:25 +0200 | [diff] [blame] | 233 | expected = [_make_ext_name('xx'), |
Éric Araujo | 540edc6 | 2011-08-20 07:42:56 +0200 | [diff] [blame] | 234 | 'METADATA', 'INSTALLER', 'REQUESTED', 'RECORD'] |
| 235 | self.assertEqual(found, expected) |
| 236 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 237 | |
| 238 | def test_suite(): |
| 239 | return unittest.makeSuite(InstallTestCase) |
| 240 | |
| 241 | if __name__ == "__main__": |
| 242 | unittest.main(defaultTest="test_suite") |