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 |
| 5 | |
| 6 | from sysconfig import (get_scheme_names, get_config_vars, |
| 7 | _SCHEMES, get_config_var, get_path) |
| 8 | |
| 9 | _CONFIG_VARS = get_config_vars() |
| 10 | |
| 11 | from packaging.tests import captured_stdout |
| 12 | |
| 13 | from packaging.command.install_dist import install_dist |
| 14 | from packaging.command import install_dist as install_module |
| 15 | from packaging.dist import Distribution |
| 16 | from packaging.errors import PackagingOptionError |
| 17 | |
| 18 | from packaging.tests import unittest, support |
| 19 | |
| 20 | |
| 21 | class InstallTestCase(support.TempdirManager, |
| 22 | support.LoggingCatcher, |
| 23 | unittest.TestCase): |
| 24 | |
| 25 | def test_home_installation_scheme(self): |
| 26 | # This ensure two things: |
| 27 | # - that --home generates the desired set of directory names |
| 28 | # - test --home is supported on all platforms |
| 29 | builddir = self.mkdtemp() |
| 30 | destination = os.path.join(builddir, "installation") |
| 31 | |
| 32 | dist = Distribution({"name": "foopkg"}) |
| 33 | # script_name need not exist, it just need to be initialized |
| 34 | dist.script_name = os.path.join(builddir, "setup.py") |
| 35 | dist.command_obj["build"] = support.DummyCommand( |
| 36 | build_base=builddir, |
| 37 | build_lib=os.path.join(builddir, "lib"), |
| 38 | ) |
| 39 | |
| 40 | old_posix_prefix = _SCHEMES.get('posix_prefix', 'platinclude') |
| 41 | old_posix_home = _SCHEMES.get('posix_home', 'platinclude') |
| 42 | |
| 43 | new_path = '{platbase}/include/python{py_version_short}' |
| 44 | _SCHEMES.set('posix_prefix', 'platinclude', new_path) |
| 45 | _SCHEMES.set('posix_home', 'platinclude', '{platbase}/include/python') |
| 46 | |
| 47 | try: |
| 48 | cmd = install_dist(dist) |
| 49 | cmd.home = destination |
| 50 | cmd.ensure_finalized() |
| 51 | finally: |
| 52 | _SCHEMES.set('posix_prefix', 'platinclude', old_posix_prefix) |
| 53 | _SCHEMES.set('posix_home', 'platinclude', old_posix_home) |
| 54 | |
| 55 | self.assertEqual(cmd.install_base, destination) |
| 56 | self.assertEqual(cmd.install_platbase, destination) |
| 57 | |
| 58 | def check_path(got, expected): |
| 59 | got = os.path.normpath(got) |
| 60 | expected = os.path.normpath(expected) |
| 61 | self.assertEqual(got, expected) |
| 62 | |
| 63 | libdir = os.path.join(destination, "lib", "python") |
| 64 | check_path(cmd.install_lib, libdir) |
| 65 | check_path(cmd.install_platlib, libdir) |
| 66 | check_path(cmd.install_purelib, libdir) |
| 67 | check_path(cmd.install_headers, |
| 68 | os.path.join(destination, "include", "python", "foopkg")) |
| 69 | check_path(cmd.install_scripts, os.path.join(destination, "bin")) |
| 70 | check_path(cmd.install_data, destination) |
| 71 | |
| 72 | @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') |
| 73 | def test_user_site(self): |
| 74 | # test install with --user |
| 75 | # preparing the environment for the test |
| 76 | self.old_user_base = get_config_var('userbase') |
| 77 | self.old_user_site = get_path('purelib', '%s_user' % os.name) |
| 78 | self.tmpdir = self.mkdtemp() |
| 79 | self.user_base = os.path.join(self.tmpdir, 'B') |
| 80 | self.user_site = os.path.join(self.tmpdir, 'S') |
| 81 | _CONFIG_VARS['userbase'] = self.user_base |
| 82 | scheme = '%s_user' % os.name |
| 83 | _SCHEMES.set(scheme, 'purelib', self.user_site) |
| 84 | |
| 85 | def _expanduser(path): |
| 86 | if path[0] == '~': |
| 87 | path = os.path.normpath(self.tmpdir) + path[1:] |
| 88 | return path |
| 89 | |
| 90 | self.old_expand = os.path.expanduser |
| 91 | os.path.expanduser = _expanduser |
| 92 | |
| 93 | try: |
| 94 | # this is the actual test |
| 95 | self._test_user_site() |
| 96 | finally: |
| 97 | _CONFIG_VARS['userbase'] = self.old_user_base |
| 98 | _SCHEMES.set(scheme, 'purelib', self.old_user_site) |
| 99 | os.path.expanduser = self.old_expand |
| 100 | |
| 101 | def _test_user_site(self): |
| 102 | schemes = get_scheme_names() |
| 103 | for key in ('nt_user', 'posix_user', 'os2_home'): |
| 104 | self.assertIn(key, schemes) |
| 105 | |
| 106 | dist = Distribution({'name': 'xx'}) |
| 107 | cmd = install_dist(dist) |
| 108 | # making sure the user option is there |
| 109 | options = [name for name, short, lable in |
| 110 | cmd.user_options] |
| 111 | self.assertIn('user', options) |
| 112 | |
| 113 | # setting a value |
| 114 | cmd.user = True |
| 115 | |
| 116 | # user base and site shouldn't be created yet |
| 117 | self.assertFalse(os.path.exists(self.user_base)) |
| 118 | self.assertFalse(os.path.exists(self.user_site)) |
| 119 | |
| 120 | # let's run finalize |
| 121 | cmd.ensure_finalized() |
| 122 | |
| 123 | # now they should |
| 124 | self.assertTrue(os.path.exists(self.user_base)) |
| 125 | self.assertTrue(os.path.exists(self.user_site)) |
| 126 | |
| 127 | self.assertIn('userbase', cmd.config_vars) |
| 128 | self.assertIn('usersite', cmd.config_vars) |
| 129 | |
| 130 | def test_handle_extra_path(self): |
| 131 | dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'}) |
| 132 | cmd = install_dist(dist) |
| 133 | |
| 134 | # two elements |
| 135 | cmd.handle_extra_path() |
| 136 | self.assertEqual(cmd.extra_path, ['path', 'dirs']) |
| 137 | self.assertEqual(cmd.extra_dirs, 'dirs') |
| 138 | self.assertEqual(cmd.path_file, 'path') |
| 139 | |
| 140 | # one element |
| 141 | cmd.extra_path = ['path'] |
| 142 | cmd.handle_extra_path() |
| 143 | self.assertEqual(cmd.extra_path, ['path']) |
| 144 | self.assertEqual(cmd.extra_dirs, 'path') |
| 145 | self.assertEqual(cmd.path_file, 'path') |
| 146 | |
| 147 | # none |
| 148 | dist.extra_path = cmd.extra_path = None |
| 149 | cmd.handle_extra_path() |
| 150 | self.assertEqual(cmd.extra_path, None) |
| 151 | self.assertEqual(cmd.extra_dirs, '') |
| 152 | self.assertEqual(cmd.path_file, None) |
| 153 | |
| 154 | # three elements (no way !) |
| 155 | cmd.extra_path = 'path,dirs,again' |
| 156 | self.assertRaises(PackagingOptionError, cmd.handle_extra_path) |
| 157 | |
| 158 | def test_finalize_options(self): |
| 159 | dist = Distribution({'name': 'xx'}) |
| 160 | cmd = install_dist(dist) |
| 161 | |
| 162 | # must supply either prefix/exec-prefix/home or |
| 163 | # install-base/install-platbase -- not both |
| 164 | cmd.prefix = 'prefix' |
| 165 | cmd.install_base = 'base' |
| 166 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 167 | |
| 168 | # must supply either home or prefix/exec-prefix -- not both |
| 169 | cmd.install_base = None |
| 170 | cmd.home = 'home' |
| 171 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 172 | |
| 173 | if sys.version >= '2.6': |
| 174 | # can't combine user with with prefix/exec_prefix/home or |
| 175 | # install_(plat)base |
| 176 | cmd.prefix = None |
| 177 | cmd.user = 'user' |
| 178 | self.assertRaises(PackagingOptionError, cmd.finalize_options) |
| 179 | |
| 180 | def test_old_record(self): |
| 181 | # test pre-PEP 376 --record option (outside dist-info dir) |
| 182 | install_dir = self.mkdtemp() |
| 183 | pkgdir, dist = self.create_dist() |
| 184 | |
| 185 | dist = Distribution() |
| 186 | cmd = install_dist(dist) |
| 187 | dist.command_obj['install_dist'] = cmd |
| 188 | cmd.root = install_dir |
| 189 | cmd.record = os.path.join(pkgdir, 'filelist') |
| 190 | cmd.ensure_finalized() |
| 191 | cmd.run() |
| 192 | |
| 193 | # let's check the record file was created with four |
| 194 | # lines, one for each .dist-info entry: METADATA, |
| 195 | # INSTALLER, REQUSTED, RECORD |
| 196 | f = open(cmd.record) |
| 197 | try: |
| 198 | self.assertEqual(len(f.readlines()), 4) |
| 199 | finally: |
| 200 | f.close() |
| 201 | |
| 202 | # XXX test that fancy_getopt is okay with options named |
| 203 | # record and no-record but unrelated |
| 204 | |
| 205 | |
| 206 | def test_suite(): |
| 207 | return unittest.makeSuite(InstallTestCase) |
| 208 | |
| 209 | if __name__ == "__main__": |
| 210 | unittest.main(defaultTest="test_suite") |