blob: d323222475118ea7929d988b3983a36bcac33aa7 [file] [log] [blame]
Tarek Ziadé5633a802010-01-23 09:23:15 +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.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,
18 _get_default_scheme, _expand_vars)
19
20class TestSysConfig(unittest.TestCase):
21
22 def setUp(self):
23 """Make a copy of sys.path"""
24 super(TestSysConfig, self).setUp()
25 self.sys_path = sys.path[:]
26 self.makefile = None
27 # patching os.uname
28 if hasattr(os, 'uname'):
29 self.uname = os.uname
30 self._uname = os.uname()
31 else:
32 self.uname = None
33 self._uname = None
34 os.uname = self._get_uname
35 # saving the environment
36 self.name = os.name
37 self.platform = sys.platform
38 self.version = sys.version
39 self.sep = os.sep
40 self.join = os.path.join
41 self.isabs = os.path.isabs
42 self.splitdrive = os.path.splitdrive
43 self._config_vars = copy(sysconfig._CONFIG_VARS)
44 self.old_environ = deepcopy(os.environ)
45
46 def tearDown(self):
47 """Restore sys.path"""
48 sys.path[:] = self.sys_path
49 if self.makefile is not None:
50 os.unlink(self.makefile)
51 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 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 = test.test_support.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):
88 self.assertEquals(get_path_names(), sysconfig._SCHEME_KEYS)
89
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 = wanted.items()
95 wanted.sort()
96 scheme = scheme.items()
97 scheme.sort()
98 self.assertEquals(scheme, wanted)
99
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'
117 self.assertEquals(get_platform(), 'win32')
118
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'
124 self.assertEquals(get_platform(), 'win-amd64')
125
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'
131 self.assertEquals(get_platform(), 'win-ia64')
132
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éc64614e2010-01-23 17:52:57 +0000141 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
142 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
143
144 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
145 '-fwrapv -O3 -Wall -Wstrict-prototypes')
146
147 maxint = sys.maxint
148 try:
149 sys.maxint = 2147483647
150 self.assertEquals(get_platform(), 'macosx-10.3-ppc')
151 sys.maxint = 9223372036854775807
152 self.assertEquals(get_platform(), 'macosx-10.3-ppc64')
153 finally:
154 sys.maxint = maxint
155
156
157 self._set_uname(('Darwin', 'macziade', '8.11.1',
158 ('Darwin Kernel Version 8.11.1: '
159 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé5633a802010-01-23 09:23:15 +0000160 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
161 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
162 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
163
164 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
165 '-fwrapv -O3 -Wall -Wstrict-prototypes')
166
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000167 maxint = sys.maxint
168 try:
169 sys.maxint = 2147483647
170 self.assertEquals(get_platform(), 'macosx-10.3-i386')
171 sys.maxint = 9223372036854775807
172 self.assertEquals(get_platform(), 'macosx-10.3-x86_64')
173 finally:
174 sys.maxint = maxint
Tarek Ziadé5633a802010-01-23 09:23:15 +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
235
236def test_main():
237 run_unittest(TestSysConfig)
238
239if __name__ == "__main__":
240 test_main()