blob: f00e041d9a7e8522114d50fd4f8a23e9e58f3ea7 [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
Georg Brandld1fa76e2010-02-06 23:12:12 +000011import shutil
Tarek Ziadé5633a802010-01-23 09:23:15 +000012from copy import copy, deepcopy
13
14from test.test_support import run_unittest, TESTFN
15
16import sysconfig
17from sysconfig import (get_paths, get_platform, get_config_vars,
18 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadée81b0282010-02-02 22:54:28 +000019 _get_default_scheme, _expand_vars,
20 get_scheme_names)
Tarek Ziadé5633a802010-01-23 09:23:15 +000021
22class TestSysConfig(unittest.TestCase):
23
24 def setUp(self):
25 """Make a copy of sys.path"""
26 super(TestSysConfig, self).setUp()
27 self.sys_path = sys.path[:]
28 self.makefile = None
29 # 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
51 if self.makefile is not None:
52 os.unlink(self.makefile)
53 self._cleanup_testfn()
54 if self.uname is not None:
55 os.uname = self.uname
56 else:
57 del os.uname
58 os.name = self.name
59 sys.platform = self.platform
60 sys.version = self.version
61 os.sep = self.sep
62 os.path.join = self.join
63 os.path.isabs = self.isabs
64 os.path.splitdrive = self.splitdrive
65 sysconfig._CONFIG_VARS = copy(self._config_vars)
66 for key, value in self.old_environ.items():
67 if os.environ.get(key) != value:
68 os.environ[key] = value
69
70 for key in os.environ.keys():
71 if key not in self.old_environ:
72 del os.environ[key]
73
74 super(TestSysConfig, self).tearDown()
75
76 def _set_uname(self, uname):
77 self._uname = uname
78
79 def _get_uname(self):
80 return self._uname
81
82 def _cleanup_testfn(self):
83 path = test.test_support.TESTFN
84 if os.path.isfile(path):
85 os.remove(path)
86 elif os.path.isdir(path):
87 shutil.rmtree(path)
88
89 def test_get_path_names(self):
90 self.assertEquals(get_path_names(), sysconfig._SCHEME_KEYS)
91
92 def test_get_paths(self):
93 scheme = get_paths()
94 default_scheme = _get_default_scheme()
95 wanted = _expand_vars(default_scheme, None)
96 wanted = wanted.items()
97 wanted.sort()
98 scheme = scheme.items()
99 scheme.sort()
100 self.assertEquals(scheme, wanted)
101
102 def test_get_path(self):
103 # xxx make real tests here
104 for scheme in _INSTALL_SCHEMES:
105 for name in _INSTALL_SCHEMES[scheme]:
106 res = get_path(name, scheme)
107
108 def test_get_config_vars(self):
109 cvars = get_config_vars()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000110 self.assertIsInstance(cvars, dict)
Tarek Ziadé5633a802010-01-23 09:23:15 +0000111 self.assertTrue(cvars)
112
113 def test_get_platform(self):
114 # windows XP, 32bits
115 os.name = 'nt'
116 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
117 '[MSC v.1310 32 bit (Intel)]')
118 sys.platform = 'win32'
119 self.assertEquals(get_platform(), 'win32')
120
121 # windows XP, amd64
122 os.name = 'nt'
123 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
124 '[MSC v.1310 32 bit (Amd64)]')
125 sys.platform = 'win32'
126 self.assertEquals(get_platform(), 'win-amd64')
127
128 # windows XP, itanium
129 os.name = 'nt'
130 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
131 '[MSC v.1310 32 bit (Itanium)]')
132 sys.platform = 'win32'
133 self.assertEquals(get_platform(), 'win-ia64')
134
135 # macbook
136 os.name = 'posix'
137 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
138 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
139 sys.platform = 'darwin'
140 self._set_uname(('Darwin', 'macziade', '8.11.1',
141 ('Darwin Kernel Version 8.11.1: '
142 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000143 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
144 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
145
146 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
147 '-fwrapv -O3 -Wall -Wstrict-prototypes')
148
149 maxint = sys.maxint
150 try:
151 sys.maxint = 2147483647
152 self.assertEquals(get_platform(), 'macosx-10.3-ppc')
153 sys.maxint = 9223372036854775807
154 self.assertEquals(get_platform(), 'macosx-10.3-ppc64')
155 finally:
156 sys.maxint = maxint
157
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é5633a802010-01-23 09:23:15 +0000162 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
163 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
164 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
165
166 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
167 '-fwrapv -O3 -Wall -Wstrict-prototypes')
168
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000169 maxint = sys.maxint
170 try:
171 sys.maxint = 2147483647
172 self.assertEquals(get_platform(), 'macosx-10.3-i386')
173 sys.maxint = 9223372036854775807
174 self.assertEquals(get_platform(), 'macosx-10.3-x86_64')
175 finally:
176 sys.maxint = maxint
Tarek Ziadé5633a802010-01-23 09:23:15 +0000177
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
Tarek Ziadée81b0282010-02-02 22:54:28 +0000237 def test_get_scheme_names(self):
238 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'posix_home',
239 'posix_prefix', 'posix_user')
240 self.assertEquals(get_scheme_names(), wanted)
241
Tarek Ziadé5633a802010-01-23 09:23:15 +0000242
243def test_main():
244 run_unittest(TestSysConfig)
245
246if __name__ == "__main__":
247 test_main()