Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 1 | """Tests for 'site'. |
| 2 | |
| 3 | Tests assume the initial paths in sys.path once the interpreter has begun |
| 4 | executing have not been removed. |
| 5 | |
| 6 | """ |
| 7 | import unittest |
| 8 | import sys |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 9 | import os |
Florent Xicluna | a470738 | 2010-03-11 00:05:17 +0000 | [diff] [blame] | 10 | import subprocess |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 11 | import shutil |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 12 | from copy import copy, deepcopy |
| 13 | |
Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 14 | from test.support import (run_unittest, TESTFN, unlink, get_attribute, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 15 | captured_stdout, skip_unless_symlink) |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 16 | |
| 17 | import sysconfig |
| 18 | from sysconfig import (get_paths, get_platform, get_config_vars, |
| 19 | get_path, get_path_names, _INSTALL_SCHEMES, |
Tarek Ziadé | bd79768 | 2010-02-02 23:16:13 +0000 | [diff] [blame] | 20 | _get_default_scheme, _expand_vars, |
Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 21 | get_scheme_names, get_config_var, _main) |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 22 | |
| 23 | class TestSysConfig(unittest.TestCase): |
| 24 | |
| 25 | def setUp(self): |
| 26 | """Make a copy of sys.path""" |
| 27 | super(TestSysConfig, self).setUp() |
| 28 | self.sys_path = sys.path[:] |
| 29 | self.makefile = None |
| 30 | # patching os.uname |
| 31 | if hasattr(os, 'uname'): |
| 32 | self.uname = os.uname |
| 33 | self._uname = os.uname() |
| 34 | else: |
| 35 | self.uname = None |
| 36 | self._uname = None |
| 37 | os.uname = self._get_uname |
| 38 | # saving the environment |
| 39 | self.name = os.name |
| 40 | self.platform = sys.platform |
| 41 | self.version = sys.version |
| 42 | self.sep = os.sep |
| 43 | self.join = os.path.join |
| 44 | self.isabs = os.path.isabs |
| 45 | self.splitdrive = os.path.splitdrive |
| 46 | self._config_vars = copy(sysconfig._CONFIG_VARS) |
| 47 | self.old_environ = deepcopy(os.environ) |
| 48 | |
| 49 | def tearDown(self): |
| 50 | """Restore sys.path""" |
| 51 | sys.path[:] = self.sys_path |
| 52 | if self.makefile is not None: |
| 53 | os.unlink(self.makefile) |
| 54 | self._cleanup_testfn() |
| 55 | if self.uname is not None: |
| 56 | os.uname = self.uname |
| 57 | else: |
| 58 | del os.uname |
| 59 | os.name = self.name |
| 60 | sys.platform = self.platform |
| 61 | sys.version = self.version |
| 62 | os.sep = self.sep |
| 63 | os.path.join = self.join |
| 64 | os.path.isabs = self.isabs |
| 65 | os.path.splitdrive = self.splitdrive |
| 66 | sysconfig._CONFIG_VARS = copy(self._config_vars) |
| 67 | for key, value in self.old_environ.items(): |
| 68 | if os.environ.get(key) != value: |
| 69 | os.environ[key] = value |
| 70 | |
| 71 | for key in list(os.environ.keys()): |
| 72 | if key not in self.old_environ: |
| 73 | del os.environ[key] |
| 74 | |
| 75 | super(TestSysConfig, self).tearDown() |
| 76 | |
| 77 | def _set_uname(self, uname): |
| 78 | self._uname = uname |
| 79 | |
| 80 | def _get_uname(self): |
| 81 | return self._uname |
| 82 | |
| 83 | def _cleanup_testfn(self): |
| 84 | path = TESTFN |
| 85 | if os.path.isfile(path): |
| 86 | os.remove(path) |
| 87 | elif os.path.isdir(path): |
| 88 | shutil.rmtree(path) |
| 89 | |
| 90 | def test_get_path_names(self): |
| 91 | self.assertEquals(get_path_names(), sysconfig._SCHEME_KEYS) |
| 92 | |
| 93 | def test_get_paths(self): |
| 94 | scheme = get_paths() |
| 95 | default_scheme = _get_default_scheme() |
| 96 | wanted = _expand_vars(default_scheme, None) |
| 97 | wanted = list(wanted.items()) |
| 98 | wanted.sort() |
| 99 | scheme = list(scheme.items()) |
| 100 | scheme.sort() |
| 101 | self.assertEquals(scheme, wanted) |
| 102 | |
| 103 | def test_get_path(self): |
| 104 | # xxx make real tests here |
| 105 | for scheme in _INSTALL_SCHEMES: |
| 106 | for name in _INSTALL_SCHEMES[scheme]: |
| 107 | res = get_path(name, scheme) |
| 108 | |
| 109 | def test_get_config_vars(self): |
| 110 | cvars = get_config_vars() |
| 111 | self.assertTrue(isinstance(cvars, dict)) |
| 112 | self.assertTrue(cvars) |
| 113 | |
| 114 | def test_get_platform(self): |
| 115 | # windows XP, 32bits |
| 116 | os.name = 'nt' |
| 117 | sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' |
| 118 | '[MSC v.1310 32 bit (Intel)]') |
| 119 | sys.platform = 'win32' |
| 120 | self.assertEquals(get_platform(), 'win32') |
| 121 | |
| 122 | # windows XP, amd64 |
| 123 | os.name = 'nt' |
| 124 | sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' |
| 125 | '[MSC v.1310 32 bit (Amd64)]') |
| 126 | sys.platform = 'win32' |
| 127 | self.assertEquals(get_platform(), 'win-amd64') |
| 128 | |
| 129 | # windows XP, itanium |
| 130 | os.name = 'nt' |
| 131 | sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' |
| 132 | '[MSC v.1310 32 bit (Itanium)]') |
| 133 | sys.platform = 'win32' |
| 134 | self.assertEquals(get_platform(), 'win-ia64') |
| 135 | |
| 136 | # macbook |
| 137 | os.name = 'posix' |
| 138 | sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' |
| 139 | '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') |
| 140 | sys.platform = 'darwin' |
| 141 | self._set_uname(('Darwin', 'macziade', '8.11.1', |
| 142 | ('Darwin Kernel Version 8.11.1: ' |
| 143 | 'Wed Oct 10 18:23:28 PDT 2007; ' |
Tarek Ziadé | 8b441d0 | 2010-01-29 11:46:31 +0000 | [diff] [blame] | 144 | 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC')) |
| 145 | os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3' |
| 146 | |
| 147 | get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' |
| 148 | '-fwrapv -O3 -Wall -Wstrict-prototypes') |
| 149 | |
Benjamin Peterson | d69fe2a | 2010-02-03 02:59:43 +0000 | [diff] [blame] | 150 | maxint = sys.maxsize |
Tarek Ziadé | 8b441d0 | 2010-01-29 11:46:31 +0000 | [diff] [blame] | 151 | try: |
| 152 | sys.maxsize = 2147483647 |
| 153 | self.assertEquals(get_platform(), 'macosx-10.3-ppc') |
| 154 | sys.maxsize = 9223372036854775807 |
| 155 | self.assertEquals(get_platform(), 'macosx-10.3-ppc64') |
| 156 | finally: |
Benjamin Peterson | d69fe2a | 2010-02-03 02:59:43 +0000 | [diff] [blame] | 157 | sys.maxsize = maxint |
Tarek Ziadé | 8b441d0 | 2010-01-29 11:46:31 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | self._set_uname(('Darwin', 'macziade', '8.11.1', |
| 161 | ('Darwin Kernel Version 8.11.1: ' |
| 162 | 'Wed Oct 10 18:23:28 PDT 2007; ' |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 163 | 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) |
| 164 | get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' |
| 165 | os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3' |
| 166 | |
| 167 | get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' |
| 168 | '-fwrapv -O3 -Wall -Wstrict-prototypes') |
Benjamin Peterson | d69fe2a | 2010-02-03 02:59:43 +0000 | [diff] [blame] | 169 | maxint = sys.maxsize |
Tarek Ziadé | 8b441d0 | 2010-01-29 11:46:31 +0000 | [diff] [blame] | 170 | try: |
| 171 | sys.maxsize = 2147483647 |
| 172 | self.assertEquals(get_platform(), 'macosx-10.3-i386') |
| 173 | sys.maxsize = 9223372036854775807 |
| 174 | self.assertEquals(get_platform(), 'macosx-10.3-x86_64') |
| 175 | finally: |
Benjamin Peterson | d69fe2a | 2010-02-03 02:59:43 +0000 | [diff] [blame] | 176 | sys.maxsize = maxint |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 177 | |
| 178 | # macbook with fat binaries (fat, universal or fat64) |
| 179 | os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4' |
| 180 | get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' |
| 181 | '/Developer/SDKs/MacOSX10.4u.sdk ' |
| 182 | '-fno-strict-aliasing -fno-common ' |
| 183 | '-dynamic -DNDEBUG -g -O3') |
| 184 | |
| 185 | self.assertEquals(get_platform(), 'macosx-10.4-fat') |
| 186 | |
| 187 | get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' |
| 188 | '/Developer/SDKs/MacOSX10.4u.sdk ' |
| 189 | '-fno-strict-aliasing -fno-common ' |
| 190 | '-dynamic -DNDEBUG -g -O3') |
| 191 | |
| 192 | self.assertEquals(get_platform(), 'macosx-10.4-intel') |
| 193 | |
| 194 | get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' |
| 195 | '/Developer/SDKs/MacOSX10.4u.sdk ' |
| 196 | '-fno-strict-aliasing -fno-common ' |
| 197 | '-dynamic -DNDEBUG -g -O3') |
| 198 | self.assertEquals(get_platform(), 'macosx-10.4-fat3') |
| 199 | |
| 200 | get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' |
| 201 | '/Developer/SDKs/MacOSX10.4u.sdk ' |
| 202 | '-fno-strict-aliasing -fno-common ' |
| 203 | '-dynamic -DNDEBUG -g -O3') |
| 204 | self.assertEquals(get_platform(), 'macosx-10.4-universal') |
| 205 | |
| 206 | get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' |
| 207 | '/Developer/SDKs/MacOSX10.4u.sdk ' |
| 208 | '-fno-strict-aliasing -fno-common ' |
| 209 | '-dynamic -DNDEBUG -g -O3') |
| 210 | |
| 211 | self.assertEquals(get_platform(), 'macosx-10.4-fat64') |
| 212 | |
| 213 | for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): |
| 214 | get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' |
| 215 | '/Developer/SDKs/MacOSX10.4u.sdk ' |
| 216 | '-fno-strict-aliasing -fno-common ' |
| 217 | '-dynamic -DNDEBUG -g -O3'%(arch,)) |
| 218 | |
| 219 | self.assertEquals(get_platform(), 'macosx-10.4-%s'%(arch,)) |
| 220 | |
| 221 | # linux debian sarge |
| 222 | os.name = 'posix' |
| 223 | sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' |
| 224 | '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') |
| 225 | sys.platform = 'linux2' |
| 226 | self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', |
| 227 | '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) |
| 228 | |
| 229 | self.assertEquals(get_platform(), 'linux-i686') |
| 230 | |
| 231 | # XXX more platforms to tests here |
| 232 | |
| 233 | def test_get_config_h_filename(self): |
| 234 | config_h = sysconfig.get_config_h_filename() |
| 235 | self.assertTrue(os.path.isfile(config_h), config_h) |
| 236 | |
Barry Warsaw | 0646b4b | 2010-09-20 19:12:07 +0000 | [diff] [blame] | 237 | @unittest.skipIf(sys.platform.startswith('win'), |
| 238 | 'Test is not Windows compatible') |
Barry Warsaw | ebbef6f | 2010-09-20 15:29:53 +0000 | [diff] [blame] | 239 | def test_get_makefile_filename(self): |
| 240 | makefile = sysconfig.get_makefile_filename() |
| 241 | self.assertTrue(os.path.isfile(makefile), makefile) |
| 242 | |
Tarek Ziadé | bd79768 | 2010-02-02 23:16:13 +0000 | [diff] [blame] | 243 | def test_get_scheme_names(self): |
Benjamin Peterson | 3c451e6 | 2010-05-08 15:51:23 +0000 | [diff] [blame] | 244 | wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user', |
| 245 | 'posix_home', 'posix_prefix', 'posix_user') |
Tarek Ziadé | bd79768 | 2010-02-02 23:16:13 +0000 | [diff] [blame] | 246 | self.assertEquals(get_scheme_names(), wanted) |
| 247 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 248 | @skip_unless_symlink |
Florent Xicluna | a470738 | 2010-03-11 00:05:17 +0000 | [diff] [blame] | 249 | def test_symlink(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 250 | # On Windows, the EXE needs to know where pythonXY.dll is at so we have |
| 251 | # to add the directory to the path. |
| 252 | if sys.platform == "win32": |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 253 | os.environ["Path"] = "{};{}".format( |
| 254 | os.path.dirname(sys.executable), os.environ["Path"]) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 255 | |
Florent Xicluna | a470738 | 2010-03-11 00:05:17 +0000 | [diff] [blame] | 256 | # Issue 7880 |
Florent Xicluna | a470738 | 2010-03-11 00:05:17 +0000 | [diff] [blame] | 257 | def get(python): |
| 258 | cmd = [python, '-c', |
Florent Xicluna | 01fe610 | 2010-03-13 15:35:12 +0000 | [diff] [blame] | 259 | 'import sysconfig; print(sysconfig.get_platform())'] |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 260 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ) |
Florent Xicluna | a470738 | 2010-03-11 00:05:17 +0000 | [diff] [blame] | 261 | return p.communicate() |
| 262 | real = os.path.realpath(sys.executable) |
| 263 | link = os.path.abspath(TESTFN) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 264 | os.symlink(real, link) |
Florent Xicluna | a470738 | 2010-03-11 00:05:17 +0000 | [diff] [blame] | 265 | try: |
| 266 | self.assertEqual(get(real), get(link)) |
| 267 | finally: |
| 268 | unlink(link) |
| 269 | |
Tarek Ziadé | 06710a8 | 2010-05-19 22:25:00 +0000 | [diff] [blame] | 270 | def test_user_similar(self): |
| 271 | # Issue 8759 : make sure the posix scheme for the users |
| 272 | # is similar to the global posix_prefix one |
| 273 | base = get_config_var('base') |
| 274 | user = get_config_var('userbase') |
| 275 | for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): |
| 276 | global_path = get_path(name, 'posix_prefix') |
| 277 | user_path = get_path(name, 'posix_user') |
| 278 | self.assertEquals(user_path, global_path.replace(base, user)) |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 279 | |
Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 280 | def test_main(self): |
| 281 | # just making sure _main() runs and returns things in the stdout |
| 282 | with captured_stdout() as output: |
| 283 | _main() |
| 284 | self.assertTrue(len(output.getvalue().split('\n')) > 0) |
| 285 | |
Brian Curtin | db902ac | 2010-07-22 15:38:28 +0000 | [diff] [blame] | 286 | @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows") |
Ronald Oussoren | f4ebe2e | 2010-07-19 13:00:36 +0000 | [diff] [blame] | 287 | def test_ldshared_value(self): |
| 288 | ldflags = sysconfig.get_config_var('LDFLAGS') |
| 289 | ldshared = sysconfig.get_config_var('LDSHARED') |
| 290 | |
| 291 | self.assertIn(ldflags, ldshared) |
| 292 | |
Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 293 | |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 294 | def test_main(): |
| 295 | run_unittest(TestSysConfig) |
| 296 | |
| 297 | if __name__ == "__main__": |
| 298 | test_main() |