| 1 | n/a | """Tests for distutils.command.install.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import sys |
|---|
| 5 | n/a | import unittest |
|---|
| 6 | n/a | import site |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | from test.support import captured_stdout, run_unittest |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | from distutils import sysconfig |
|---|
| 11 | n/a | from distutils.command.install import install |
|---|
| 12 | n/a | from distutils.command import install as install_module |
|---|
| 13 | n/a | from distutils.command.build_ext import build_ext |
|---|
| 14 | n/a | from distutils.command.install import INSTALL_SCHEMES |
|---|
| 15 | n/a | from distutils.core import Distribution |
|---|
| 16 | n/a | from distutils.errors import DistutilsOptionError |
|---|
| 17 | n/a | from distutils.extension import Extension |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | from distutils.tests import support |
|---|
| 20 | n/a | from test import support as test_support |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | def _make_ext_name(modname): |
|---|
| 24 | n/a | return modname + sysconfig.get_config_var('EXT_SUFFIX') |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | class InstallTestCase(support.TempdirManager, |
|---|
| 28 | n/a | support.EnvironGuard, |
|---|
| 29 | n/a | support.LoggingSilencer, |
|---|
| 30 | n/a | unittest.TestCase): |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | def test_home_installation_scheme(self): |
|---|
| 33 | n/a | # This ensure two things: |
|---|
| 34 | n/a | # - that --home generates the desired set of directory names |
|---|
| 35 | n/a | # - test --home is supported on all platforms |
|---|
| 36 | n/a | builddir = self.mkdtemp() |
|---|
| 37 | n/a | destination = os.path.join(builddir, "installation") |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | dist = Distribution({"name": "foopkg"}) |
|---|
| 40 | n/a | # script_name need not exist, it just need to be initialized |
|---|
| 41 | n/a | dist.script_name = os.path.join(builddir, "setup.py") |
|---|
| 42 | n/a | dist.command_obj["build"] = support.DummyCommand( |
|---|
| 43 | n/a | build_base=builddir, |
|---|
| 44 | n/a | build_lib=os.path.join(builddir, "lib"), |
|---|
| 45 | n/a | ) |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | cmd = install(dist) |
|---|
| 48 | n/a | cmd.home = destination |
|---|
| 49 | n/a | cmd.ensure_finalized() |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | self.assertEqual(cmd.install_base, destination) |
|---|
| 52 | n/a | self.assertEqual(cmd.install_platbase, destination) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def check_path(got, expected): |
|---|
| 55 | n/a | got = os.path.normpath(got) |
|---|
| 56 | n/a | expected = os.path.normpath(expected) |
|---|
| 57 | n/a | self.assertEqual(got, expected) |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | libdir = os.path.join(destination, "lib", "python") |
|---|
| 60 | n/a | check_path(cmd.install_lib, libdir) |
|---|
| 61 | n/a | check_path(cmd.install_platlib, libdir) |
|---|
| 62 | n/a | check_path(cmd.install_purelib, libdir) |
|---|
| 63 | n/a | check_path(cmd.install_headers, |
|---|
| 64 | n/a | os.path.join(destination, "include", "python", "foopkg")) |
|---|
| 65 | n/a | check_path(cmd.install_scripts, os.path.join(destination, "bin")) |
|---|
| 66 | n/a | check_path(cmd.install_data, destination) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def test_user_site(self): |
|---|
| 69 | n/a | # test install with --user |
|---|
| 70 | n/a | # preparing the environment for the test |
|---|
| 71 | n/a | self.old_user_base = site.USER_BASE |
|---|
| 72 | n/a | self.old_user_site = site.USER_SITE |
|---|
| 73 | n/a | self.tmpdir = self.mkdtemp() |
|---|
| 74 | n/a | self.user_base = os.path.join(self.tmpdir, 'B') |
|---|
| 75 | n/a | self.user_site = os.path.join(self.tmpdir, 'S') |
|---|
| 76 | n/a | site.USER_BASE = self.user_base |
|---|
| 77 | n/a | site.USER_SITE = self.user_site |
|---|
| 78 | n/a | install_module.USER_BASE = self.user_base |
|---|
| 79 | n/a | install_module.USER_SITE = self.user_site |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def _expanduser(path): |
|---|
| 82 | n/a | return self.tmpdir |
|---|
| 83 | n/a | self.old_expand = os.path.expanduser |
|---|
| 84 | n/a | os.path.expanduser = _expanduser |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def cleanup(): |
|---|
| 87 | n/a | site.USER_BASE = self.old_user_base |
|---|
| 88 | n/a | site.USER_SITE = self.old_user_site |
|---|
| 89 | n/a | install_module.USER_BASE = self.old_user_base |
|---|
| 90 | n/a | install_module.USER_SITE = self.old_user_site |
|---|
| 91 | n/a | os.path.expanduser = self.old_expand |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | self.addCleanup(cleanup) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | for key in ('nt_user', 'unix_user'): |
|---|
| 96 | n/a | self.assertIn(key, INSTALL_SCHEMES) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | dist = Distribution({'name': 'xx'}) |
|---|
| 99 | n/a | cmd = install(dist) |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | # making sure the user option is there |
|---|
| 102 | n/a | options = [name for name, short, lable in |
|---|
| 103 | n/a | cmd.user_options] |
|---|
| 104 | n/a | self.assertIn('user', options) |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | # setting a value |
|---|
| 107 | n/a | cmd.user = 1 |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | # user base and site shouldn't be created yet |
|---|
| 110 | n/a | self.assertFalse(os.path.exists(self.user_base)) |
|---|
| 111 | n/a | self.assertFalse(os.path.exists(self.user_site)) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | # let's run finalize |
|---|
| 114 | n/a | cmd.ensure_finalized() |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | # now they should |
|---|
| 117 | n/a | self.assertTrue(os.path.exists(self.user_base)) |
|---|
| 118 | n/a | self.assertTrue(os.path.exists(self.user_site)) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | self.assertIn('userbase', cmd.config_vars) |
|---|
| 121 | n/a | self.assertIn('usersite', cmd.config_vars) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | def test_handle_extra_path(self): |
|---|
| 124 | n/a | dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'}) |
|---|
| 125 | n/a | cmd = install(dist) |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | # two elements |
|---|
| 128 | n/a | cmd.handle_extra_path() |
|---|
| 129 | n/a | self.assertEqual(cmd.extra_path, ['path', 'dirs']) |
|---|
| 130 | n/a | self.assertEqual(cmd.extra_dirs, 'dirs') |
|---|
| 131 | n/a | self.assertEqual(cmd.path_file, 'path') |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | # one element |
|---|
| 134 | n/a | cmd.extra_path = ['path'] |
|---|
| 135 | n/a | cmd.handle_extra_path() |
|---|
| 136 | n/a | self.assertEqual(cmd.extra_path, ['path']) |
|---|
| 137 | n/a | self.assertEqual(cmd.extra_dirs, 'path') |
|---|
| 138 | n/a | self.assertEqual(cmd.path_file, 'path') |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | # none |
|---|
| 141 | n/a | dist.extra_path = cmd.extra_path = None |
|---|
| 142 | n/a | cmd.handle_extra_path() |
|---|
| 143 | n/a | self.assertEqual(cmd.extra_path, None) |
|---|
| 144 | n/a | self.assertEqual(cmd.extra_dirs, '') |
|---|
| 145 | n/a | self.assertEqual(cmd.path_file, None) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | # three elements (no way !) |
|---|
| 148 | n/a | cmd.extra_path = 'path,dirs,again' |
|---|
| 149 | n/a | self.assertRaises(DistutilsOptionError, cmd.handle_extra_path) |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | def test_finalize_options(self): |
|---|
| 152 | n/a | dist = Distribution({'name': 'xx'}) |
|---|
| 153 | n/a | cmd = install(dist) |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | # must supply either prefix/exec-prefix/home or |
|---|
| 156 | n/a | # install-base/install-platbase -- not both |
|---|
| 157 | n/a | cmd.prefix = 'prefix' |
|---|
| 158 | n/a | cmd.install_base = 'base' |
|---|
| 159 | n/a | self.assertRaises(DistutilsOptionError, cmd.finalize_options) |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | # must supply either home or prefix/exec-prefix -- not both |
|---|
| 162 | n/a | cmd.install_base = None |
|---|
| 163 | n/a | cmd.home = 'home' |
|---|
| 164 | n/a | self.assertRaises(DistutilsOptionError, cmd.finalize_options) |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | # can't combine user with prefix/exec_prefix/home or |
|---|
| 167 | n/a | # install_(plat)base |
|---|
| 168 | n/a | cmd.prefix = None |
|---|
| 169 | n/a | cmd.user = 'user' |
|---|
| 170 | n/a | self.assertRaises(DistutilsOptionError, cmd.finalize_options) |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def test_record(self): |
|---|
| 173 | n/a | install_dir = self.mkdtemp() |
|---|
| 174 | n/a | project_dir, dist = self.create_dist(py_modules=['hello'], |
|---|
| 175 | n/a | scripts=['sayhi']) |
|---|
| 176 | n/a | os.chdir(project_dir) |
|---|
| 177 | n/a | self.write_file('hello.py', "def main(): print('o hai')") |
|---|
| 178 | n/a | self.write_file('sayhi', 'from hello import main; main()') |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | cmd = install(dist) |
|---|
| 181 | n/a | dist.command_obj['install'] = cmd |
|---|
| 182 | n/a | cmd.root = install_dir |
|---|
| 183 | n/a | cmd.record = os.path.join(project_dir, 'filelist') |
|---|
| 184 | n/a | cmd.ensure_finalized() |
|---|
| 185 | n/a | cmd.run() |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | f = open(cmd.record) |
|---|
| 188 | n/a | try: |
|---|
| 189 | n/a | content = f.read() |
|---|
| 190 | n/a | finally: |
|---|
| 191 | n/a | f.close() |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | found = [os.path.basename(line) for line in content.splitlines()] |
|---|
| 194 | n/a | expected = ['hello.py', 'hello.%s.pyc' % sys.implementation.cache_tag, |
|---|
| 195 | n/a | 'sayhi', |
|---|
| 196 | n/a | 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] |
|---|
| 197 | n/a | self.assertEqual(found, expected) |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | def test_record_extensions(self): |
|---|
| 200 | n/a | cmd = test_support.missing_compiler_executable() |
|---|
| 201 | n/a | if cmd is not None: |
|---|
| 202 | n/a | self.skipTest('The %r command is not found' % cmd) |
|---|
| 203 | n/a | install_dir = self.mkdtemp() |
|---|
| 204 | n/a | project_dir, dist = self.create_dist(ext_modules=[ |
|---|
| 205 | n/a | Extension('xx', ['xxmodule.c'])]) |
|---|
| 206 | n/a | os.chdir(project_dir) |
|---|
| 207 | n/a | support.copy_xxmodule_c(project_dir) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | buildextcmd = build_ext(dist) |
|---|
| 210 | n/a | support.fixup_build_ext(buildextcmd) |
|---|
| 211 | n/a | buildextcmd.ensure_finalized() |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | cmd = install(dist) |
|---|
| 214 | n/a | dist.command_obj['install'] = cmd |
|---|
| 215 | n/a | dist.command_obj['build_ext'] = buildextcmd |
|---|
| 216 | n/a | cmd.root = install_dir |
|---|
| 217 | n/a | cmd.record = os.path.join(project_dir, 'filelist') |
|---|
| 218 | n/a | cmd.ensure_finalized() |
|---|
| 219 | n/a | cmd.run() |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | f = open(cmd.record) |
|---|
| 222 | n/a | try: |
|---|
| 223 | n/a | content = f.read() |
|---|
| 224 | n/a | finally: |
|---|
| 225 | n/a | f.close() |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | found = [os.path.basename(line) for line in content.splitlines()] |
|---|
| 228 | n/a | expected = [_make_ext_name('xx'), |
|---|
| 229 | n/a | 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]] |
|---|
| 230 | n/a | self.assertEqual(found, expected) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def test_debug_mode(self): |
|---|
| 233 | n/a | # this covers the code called when DEBUG is set |
|---|
| 234 | n/a | old_logs_len = len(self.logs) |
|---|
| 235 | n/a | install_module.DEBUG = True |
|---|
| 236 | n/a | try: |
|---|
| 237 | n/a | with captured_stdout(): |
|---|
| 238 | n/a | self.test_record() |
|---|
| 239 | n/a | finally: |
|---|
| 240 | n/a | install_module.DEBUG = False |
|---|
| 241 | n/a | self.assertGreater(len(self.logs), old_logs_len) |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | def test_suite(): |
|---|
| 245 | n/a | return unittest.makeSuite(InstallTestCase) |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | if __name__ == "__main__": |
|---|
| 248 | n/a | run_unittest(test_suite()) |
|---|