blob: 5b50901631ebe592773f97d0a864fdc51ee98016 [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
9import test
10import os
11from copy import copy, deepcopy
12
13from test.support import run_unittest, TESTFN
14
15import sysconfig
16from sysconfig import (get_paths, get_platform, get_config_vars,
17 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadébd797682010-02-02 23:16:13 +000018 _get_default_scheme, _expand_vars,
19 get_scheme_names)
Tarek Ziadéedacea32010-01-29 11:41:03 +000020
21class TestSysConfig(unittest.TestCase):
22
23 def setUp(self):
24 """Make a copy of sys.path"""
25 super(TestSysConfig, self).setUp()
26 self.sys_path = sys.path[:]
27 self.makefile = None
28 # patching os.uname
29 if hasattr(os, 'uname'):
30 self.uname = os.uname
31 self._uname = os.uname()
32 else:
33 self.uname = None
34 self._uname = None
35 os.uname = self._get_uname
36 # saving the environment
37 self.name = os.name
38 self.platform = sys.platform
39 self.version = sys.version
40 self.sep = os.sep
41 self.join = os.path.join
42 self.isabs = os.path.isabs
43 self.splitdrive = os.path.splitdrive
44 self._config_vars = copy(sysconfig._CONFIG_VARS)
45 self.old_environ = deepcopy(os.environ)
46
47 def tearDown(self):
48 """Restore sys.path"""
49 sys.path[:] = self.sys_path
50 if self.makefile is not None:
51 os.unlink(self.makefile)
52 self._cleanup_testfn()
53 if self.uname is not None:
54 os.uname = self.uname
55 else:
56 del os.uname
57 os.name = self.name
58 sys.platform = self.platform
59 sys.version = self.version
60 os.sep = self.sep
61 os.path.join = self.join
62 os.path.isabs = self.isabs
63 os.path.splitdrive = self.splitdrive
64 sysconfig._CONFIG_VARS = copy(self._config_vars)
65 for key, value in self.old_environ.items():
66 if os.environ.get(key) != value:
67 os.environ[key] = value
68
69 for key in list(os.environ.keys()):
70 if key not in self.old_environ:
71 del os.environ[key]
72
73 super(TestSysConfig, self).tearDown()
74
75 def _set_uname(self, uname):
76 self._uname = uname
77
78 def _get_uname(self):
79 return self._uname
80
81 def _cleanup_testfn(self):
82 path = TESTFN
83 if os.path.isfile(path):
84 os.remove(path)
85 elif os.path.isdir(path):
86 shutil.rmtree(path)
87
88 def test_get_path_names(self):
89 self.assertEquals(get_path_names(), sysconfig._SCHEME_KEYS)
90
91 def test_get_paths(self):
92 scheme = get_paths()
93 default_scheme = _get_default_scheme()
94 wanted = _expand_vars(default_scheme, None)
95 wanted = list(wanted.items())
96 wanted.sort()
97 scheme = list(scheme.items())
98 scheme.sort()
99 self.assertEquals(scheme, wanted)
100
101 def test_get_path(self):
102 # xxx make real tests here
103 for scheme in _INSTALL_SCHEMES:
104 for name in _INSTALL_SCHEMES[scheme]:
105 res = get_path(name, scheme)
106
107 def test_get_config_vars(self):
108 cvars = get_config_vars()
109 self.assertTrue(isinstance(cvars, dict))
110 self.assertTrue(cvars)
111
112 def test_get_platform(self):
113 # windows XP, 32bits
114 os.name = 'nt'
115 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
116 '[MSC v.1310 32 bit (Intel)]')
117 sys.platform = 'win32'
118 self.assertEquals(get_platform(), 'win32')
119
120 # windows XP, amd64
121 os.name = 'nt'
122 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
123 '[MSC v.1310 32 bit (Amd64)]')
124 sys.platform = 'win32'
125 self.assertEquals(get_platform(), 'win-amd64')
126
127 # windows XP, itanium
128 os.name = 'nt'
129 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
130 '[MSC v.1310 32 bit (Itanium)]')
131 sys.platform = 'win32'
132 self.assertEquals(get_platform(), 'win-ia64')
133
134 # macbook
135 os.name = 'posix'
136 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
137 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
138 sys.platform = 'darwin'
139 self._set_uname(('Darwin', 'macziade', '8.11.1',
140 ('Darwin Kernel Version 8.11.1: '
141 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000142 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
143 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
144
145 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
146 '-fwrapv -O3 -Wall -Wstrict-prototypes')
147
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000148 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000149 try:
150 sys.maxsize = 2147483647
151 self.assertEquals(get_platform(), 'macosx-10.3-ppc')
152 sys.maxsize = 9223372036854775807
153 self.assertEquals(get_platform(), 'macosx-10.3-ppc64')
154 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000155 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000156
157
158 self._set_uname(('Darwin', 'macziade', '8.11.1',
159 ('Darwin Kernel Version 8.11.1: '
160 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000161 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
162 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
163 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
164
165 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
166 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000167 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000168 try:
169 sys.maxsize = 2147483647
170 self.assertEquals(get_platform(), 'macosx-10.3-i386')
171 sys.maxsize = 9223372036854775807
172 self.assertEquals(get_platform(), 'macosx-10.3-x86_64')
173 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000174 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000175
176 # macbook with fat binaries (fat, universal or fat64)
177 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
178 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
179 '/Developer/SDKs/MacOSX10.4u.sdk '
180 '-fno-strict-aliasing -fno-common '
181 '-dynamic -DNDEBUG -g -O3')
182
183 self.assertEquals(get_platform(), 'macosx-10.4-fat')
184
185 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
186 '/Developer/SDKs/MacOSX10.4u.sdk '
187 '-fno-strict-aliasing -fno-common '
188 '-dynamic -DNDEBUG -g -O3')
189
190 self.assertEquals(get_platform(), 'macosx-10.4-intel')
191
192 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
193 '/Developer/SDKs/MacOSX10.4u.sdk '
194 '-fno-strict-aliasing -fno-common '
195 '-dynamic -DNDEBUG -g -O3')
196 self.assertEquals(get_platform(), 'macosx-10.4-fat3')
197
198 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
199 '/Developer/SDKs/MacOSX10.4u.sdk '
200 '-fno-strict-aliasing -fno-common '
201 '-dynamic -DNDEBUG -g -O3')
202 self.assertEquals(get_platform(), 'macosx-10.4-universal')
203
204 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
205 '/Developer/SDKs/MacOSX10.4u.sdk '
206 '-fno-strict-aliasing -fno-common '
207 '-dynamic -DNDEBUG -g -O3')
208
209 self.assertEquals(get_platform(), 'macosx-10.4-fat64')
210
211 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
212 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
213 '/Developer/SDKs/MacOSX10.4u.sdk '
214 '-fno-strict-aliasing -fno-common '
215 '-dynamic -DNDEBUG -g -O3'%(arch,))
216
217 self.assertEquals(get_platform(), 'macosx-10.4-%s'%(arch,))
218
219 # linux debian sarge
220 os.name = 'posix'
221 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
222 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
223 sys.platform = 'linux2'
224 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
225 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
226
227 self.assertEquals(get_platform(), 'linux-i686')
228
229 # XXX more platforms to tests here
230
231 def test_get_config_h_filename(self):
232 config_h = sysconfig.get_config_h_filename()
233 self.assertTrue(os.path.isfile(config_h), config_h)
234
Tarek Ziadébd797682010-02-02 23:16:13 +0000235 def test_get_scheme_names(self):
236 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'posix_home',
237 'posix_prefix', 'posix_user')
238 self.assertEquals(get_scheme_names(), wanted)
239
Tarek Ziadéedacea32010-01-29 11:41:03 +0000240
241def test_main():
242 run_unittest(TestSysConfig)
243
244if __name__ == "__main__":
245 test_main()