blob: 0fd0910ac48f2b4f44cad4a26080e056bce95946 [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,
Tarek Ziadée81b0282010-02-02 22:54:28 +000018 _get_default_scheme, _expand_vars,
19 get_scheme_names)
Tarek Ziadé5633a802010-01-23 09:23:15 +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 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 = test.test_support.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 = wanted.items()
96 wanted.sort()
97 scheme = 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()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000109 self.assertIsInstance(cvars, dict)
Tarek Ziadé5633a802010-01-23 09:23:15 +0000110 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éc64614e2010-01-23 17:52:57 +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
148 maxint = sys.maxint
149 try:
150 sys.maxint = 2147483647
151 self.assertEquals(get_platform(), 'macosx-10.3-ppc')
152 sys.maxint = 9223372036854775807
153 self.assertEquals(get_platform(), 'macosx-10.3-ppc64')
154 finally:
155 sys.maxint = maxint
156
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é5633a802010-01-23 09:23:15 +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')
167
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000168 maxint = sys.maxint
169 try:
170 sys.maxint = 2147483647
171 self.assertEquals(get_platform(), 'macosx-10.3-i386')
172 sys.maxint = 9223372036854775807
173 self.assertEquals(get_platform(), 'macosx-10.3-x86_64')
174 finally:
175 sys.maxint = maxint
Tarek Ziadé5633a802010-01-23 09:23:15 +0000176
177 # macbook with fat binaries (fat, universal or fat64)
178 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
179 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
184 self.assertEquals(get_platform(), 'macosx-10.4-fat')
185
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
191 self.assertEquals(get_platform(), 'macosx-10.4-intel')
192
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')
197 self.assertEquals(get_platform(), 'macosx-10.4-fat3')
198
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')
203 self.assertEquals(get_platform(), 'macosx-10.4-universal')
204
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
210 self.assertEquals(get_platform(), 'macosx-10.4-fat64')
211
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
218 self.assertEquals(get_platform(), 'macosx-10.4-%s'%(arch,))
219
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
228 self.assertEquals(get_platform(), 'linux-i686')
229
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ée81b0282010-02-02 22:54:28 +0000236 def test_get_scheme_names(self):
237 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'posix_home',
238 'posix_prefix', 'posix_user')
239 self.assertEquals(get_scheme_names(), wanted)
240
Tarek Ziadé5633a802010-01-23 09:23:15 +0000241
242def test_main():
243 run_unittest(TestSysConfig)
244
245if __name__ == "__main__":
246 test_main()