blob: 2ea8819dc1f2302c2ad9498d172bbc1a2c8a53b4 [file] [log] [blame]
Tarek Ziadéedacea32010-01-29 11:41:03 +00001"""Tests for 'site'.
2
3Tests assume the initial paths in sys.path once the interpreter has begun
4executing have not been removed.
5
6"""
7import unittest
8import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +00009import os
Florent Xiclunaa4707382010-03-11 00:05:17 +000010import subprocess
Georg Brandl89fad142010-03-14 10:23:39 +000011import shutil
Tarek Ziadéedacea32010-01-29 11:41:03 +000012from copy import copy, deepcopy
13
Tarek Ziadéa7514992010-05-25 09:44:36 +000014from test.support import (run_unittest, TESTFN, unlink, get_attribute,
Brian Curtin3b4499c2010-12-28 14:31:47 +000015 captured_stdout, skip_unless_symlink)
Tarek Ziadéedacea32010-01-29 11:41:03 +000016
17import sysconfig
18from sysconfig import (get_paths, get_platform, get_config_vars,
19 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadébd797682010-02-02 23:16:13 +000020 _get_default_scheme, _expand_vars,
Tarek Ziadéa7514992010-05-25 09:44:36 +000021 get_scheme_names, get_config_var, _main)
Tarek Ziadéedacea32010-01-29 11:41:03 +000022
23class 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[:]
Tarek Ziadéedacea32010-01-29 11:41:03 +000029 # patching os.uname
30 if hasattr(os, 'uname'):
31 self.uname = os.uname
32 self._uname = os.uname()
33 else:
34 self.uname = None
35 self._uname = None
36 os.uname = self._get_uname
37 # saving the environment
38 self.name = os.name
39 self.platform = sys.platform
40 self.version = sys.version
41 self.sep = os.sep
42 self.join = os.path.join
43 self.isabs = os.path.isabs
44 self.splitdrive = os.path.splitdrive
45 self._config_vars = copy(sysconfig._CONFIG_VARS)
46 self.old_environ = deepcopy(os.environ)
47
48 def tearDown(self):
49 """Restore sys.path"""
50 sys.path[:] = self.sys_path
Tarek Ziadéedacea32010-01-29 11:41:03 +000051 self._cleanup_testfn()
52 if self.uname is not None:
53 os.uname = self.uname
54 else:
55 del os.uname
56 os.name = self.name
57 sys.platform = self.platform
58 sys.version = self.version
59 os.sep = self.sep
60 os.path.join = self.join
61 os.path.isabs = self.isabs
62 os.path.splitdrive = self.splitdrive
63 sysconfig._CONFIG_VARS = copy(self._config_vars)
64 for key, value in self.old_environ.items():
65 if os.environ.get(key) != value:
66 os.environ[key] = value
67
68 for key in list(os.environ.keys()):
69 if key not in self.old_environ:
70 del os.environ[key]
71
72 super(TestSysConfig, self).tearDown()
73
74 def _set_uname(self, uname):
75 self._uname = uname
76
77 def _get_uname(self):
78 return self._uname
79
80 def _cleanup_testfn(self):
81 path = TESTFN
82 if os.path.isfile(path):
83 os.remove(path)
84 elif os.path.isdir(path):
85 shutil.rmtree(path)
86
87 def test_get_path_names(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000088 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadéedacea32010-01-29 11:41:03 +000089
90 def test_get_paths(self):
91 scheme = get_paths()
92 default_scheme = _get_default_scheme()
93 wanted = _expand_vars(default_scheme, None)
94 wanted = list(wanted.items())
95 wanted.sort()
96 scheme = list(scheme.items())
97 scheme.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +000098 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +000099
100 def test_get_path(self):
101 # xxx make real tests here
102 for scheme in _INSTALL_SCHEMES:
103 for name in _INSTALL_SCHEMES[scheme]:
104 res = get_path(name, scheme)
105
106 def test_get_config_vars(self):
107 cvars = get_config_vars()
108 self.assertTrue(isinstance(cvars, dict))
109 self.assertTrue(cvars)
110
111 def test_get_platform(self):
112 # windows XP, 32bits
113 os.name = 'nt'
114 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
115 '[MSC v.1310 32 bit (Intel)]')
116 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000117 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000118
119 # windows XP, amd64
120 os.name = 'nt'
121 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
122 '[MSC v.1310 32 bit (Amd64)]')
123 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000124 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000125
126 # windows XP, itanium
127 os.name = 'nt'
128 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
129 '[MSC v.1310 32 bit (Itanium)]')
130 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000131 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000132
133 # macbook
134 os.name = 'posix'
135 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
136 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
137 sys.platform = 'darwin'
138 self._set_uname(('Darwin', 'macziade', '8.11.1',
139 ('Darwin Kernel Version 8.11.1: '
140 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000141 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200142
143
144 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000145
146 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
147 '-fwrapv -O3 -Wall -Wstrict-prototypes')
148
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000149 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000150 try:
151 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000152 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000153 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000154 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000155 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000156 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000157
158
159 self._set_uname(('Darwin', 'macziade', '8.11.1',
160 ('Darwin Kernel Version 8.11.1: '
161 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000162 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
163 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200164 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000165
166 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
167 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000168 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000169 try:
170 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000171 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000172 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000173 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000174 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000175 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000176
177 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200178 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000179 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
180 '/Developer/SDKs/MacOSX10.4u.sdk '
181 '-fno-strict-aliasing -fno-common '
182 '-dynamic -DNDEBUG -g -O3')
183
Ezio Melottib3aedd42010-11-20 19:04:17 +0000184 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000185
186 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
187 '/Developer/SDKs/MacOSX10.4u.sdk '
188 '-fno-strict-aliasing -fno-common '
189 '-dynamic -DNDEBUG -g -O3')
190
Ezio Melottib3aedd42010-11-20 19:04:17 +0000191 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000192
193 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
194 '/Developer/SDKs/MacOSX10.4u.sdk '
195 '-fno-strict-aliasing -fno-common '
196 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000197 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000198
199 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
200 '/Developer/SDKs/MacOSX10.4u.sdk '
201 '-fno-strict-aliasing -fno-common '
202 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000203 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000204
205 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
206 '/Developer/SDKs/MacOSX10.4u.sdk '
207 '-fno-strict-aliasing -fno-common '
208 '-dynamic -DNDEBUG -g -O3')
209
Ezio Melottib3aedd42010-11-20 19:04:17 +0000210 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000211
212 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
213 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
214 '/Developer/SDKs/MacOSX10.4u.sdk '
215 '-fno-strict-aliasing -fno-common '
216 '-dynamic -DNDEBUG -g -O3'%(arch,))
217
Ezio Melottib3aedd42010-11-20 19:04:17 +0000218 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000219
220 # linux debian sarge
221 os.name = 'posix'
222 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
223 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
224 sys.platform = 'linux2'
225 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
226 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
227
Ezio Melottib3aedd42010-11-20 19:04:17 +0000228 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000229
230 # XXX more platforms to tests here
231
232 def test_get_config_h_filename(self):
233 config_h = sysconfig.get_config_h_filename()
234 self.assertTrue(os.path.isfile(config_h), config_h)
235
Tarek Ziadébd797682010-02-02 23:16:13 +0000236 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000237 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
238 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000239 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000240
Brian Curtin3b4499c2010-12-28 14:31:47 +0000241 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000242 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000243 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
244 # to add the directory to the path.
245 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +0000246 os.environ["Path"] = "{};{}".format(
247 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000248
Florent Xiclunaa4707382010-03-11 00:05:17 +0000249 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000250 def get(python):
251 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000252 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000253 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000254 return p.communicate()
255 real = os.path.realpath(sys.executable)
256 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000257 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000258 try:
259 self.assertEqual(get(real), get(link))
260 finally:
261 unlink(link)
262
Tarek Ziadé06710a82010-05-19 22:25:00 +0000263 def test_user_similar(self):
264 # Issue 8759 : make sure the posix scheme for the users
265 # is similar to the global posix_prefix one
266 base = get_config_var('base')
267 user = get_config_var('userbase')
268 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
269 global_path = get_path(name, 'posix_prefix')
270 user_path = get_path(name, 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000271 self.assertEqual(user_path, global_path.replace(base, user))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000272
Tarek Ziadéa7514992010-05-25 09:44:36 +0000273 def test_main(self):
274 # just making sure _main() runs and returns things in the stdout
275 with captured_stdout() as output:
276 _main()
277 self.assertTrue(len(output.getvalue().split('\n')) > 0)
278
Brian Curtindb902ac2010-07-22 15:38:28 +0000279 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000280 def test_ldshared_value(self):
281 ldflags = sysconfig.get_config_var('LDFLAGS')
282 ldshared = sysconfig.get_config_var('LDSHARED')
283
284 self.assertIn(ldflags, ldshared)
285
Tarek Ziadéa7514992010-05-25 09:44:36 +0000286
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200287 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
288 def test_platform_in_subprocess(self):
289 my_platform = sysconfig.get_platform()
290
291 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
292
293 env = os.environ.copy()
294 if 'MACOSX_DEPLOYMENT_TARGET' in env:
295 del env['MACOSX_DEPLOYMENT_TARGET']
296
297 with open('/dev/null', 'w') as devnull_fp:
298 p = subprocess.Popen([
299 sys.executable, '-c',
300 'import sysconfig; print(sysconfig.get_platform())',
301 ],
302 stdout=subprocess.PIPE,
303 stderr=devnull_fp,
304 env=env)
305 test_platform = p.communicate()[0].strip()
306 test_platform = test_platform.decode('utf-8')
307 status = p.wait()
308
309 self.assertEqual(status, 0)
310 self.assertEqual(my_platform, test_platform)
311
312
313 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
314 # using a value that is unlikely to be the default one.
315 env = os.environ.copy()
316 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
317
318 p = subprocess.Popen([
319 sys.executable, '-c',
320 'import sysconfig; print(sysconfig.get_platform())',
321 ],
322 stdout=subprocess.PIPE,
323 stderr=open('/dev/null'),
324 env=env)
325 test_platform = p.communicate()[0].strip()
326 test_platform = test_platform.decode('utf-8')
327 status = p.wait()
328
329 self.assertEqual(status, 0)
330 self.assertEqual(my_platform, test_platform)
331
332
Victor Stinner1273b7c2011-05-24 23:37:07 +0200333class MakefileTests(unittest.TestCase):
334 @unittest.skipIf(sys.platform.startswith('win'),
335 'Test is not Windows compatible')
336 def test_get_makefile_filename(self):
337 makefile = sysconfig.get_makefile_filename()
338 self.assertTrue(os.path.isfile(makefile), makefile)
339
340 def test_parse_makefile(self):
341 self.addCleanup(unlink, TESTFN)
342 with open(TESTFN, "w") as makefile:
343 print("var1=a$(VAR2)", file=makefile)
344 print("VAR2=b$(var3)", file=makefile)
345 print("var3=42", file=makefile)
346 print("var4=$/invalid", file=makefile)
347 print("var5=dollar$$5", file=makefile)
348 vars = sysconfig._parse_makefile(TESTFN)
349 self.assertEqual(vars, {
350 'var1': 'ab42',
351 'VAR2': 'b42',
352 'var3': 42,
353 'var4': '$/invalid',
354 'var5': 'dollar$5',
355 })
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200356
357
Tarek Ziadéedacea32010-01-29 11:41:03 +0000358def test_main():
Victor Stinner1273b7c2011-05-24 23:37:07 +0200359 run_unittest(TestSysConfig, MakefileTests)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000360
361if __name__ == "__main__":
362 test_main()