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