blob: c2dd5dbf221caf610adc53fe9ff1dbbbea8a0227 [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; '
141 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
142 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
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 self.assertEquals(get_platform(), 'macosx-10.3-i386')
149
150 # macbook with fat binaries (fat, universal or fat64)
151 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
152 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
153 '/Developer/SDKs/MacOSX10.4u.sdk '
154 '-fno-strict-aliasing -fno-common '
155 '-dynamic -DNDEBUG -g -O3')
156
157 self.assertEquals(get_platform(), 'macosx-10.4-fat')
158
159 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
160 '/Developer/SDKs/MacOSX10.4u.sdk '
161 '-fno-strict-aliasing -fno-common '
162 '-dynamic -DNDEBUG -g -O3')
163
164 self.assertEquals(get_platform(), 'macosx-10.4-intel')
165
166 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
167 '/Developer/SDKs/MacOSX10.4u.sdk '
168 '-fno-strict-aliasing -fno-common '
169 '-dynamic -DNDEBUG -g -O3')
170 self.assertEquals(get_platform(), 'macosx-10.4-fat3')
171
172 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
173 '/Developer/SDKs/MacOSX10.4u.sdk '
174 '-fno-strict-aliasing -fno-common '
175 '-dynamic -DNDEBUG -g -O3')
176 self.assertEquals(get_platform(), 'macosx-10.4-universal')
177
178 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -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-fat64')
184
185 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
186 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
187 '/Developer/SDKs/MacOSX10.4u.sdk '
188 '-fno-strict-aliasing -fno-common '
189 '-dynamic -DNDEBUG -g -O3'%(arch,))
190
191 self.assertEquals(get_platform(), 'macosx-10.4-%s'%(arch,))
192
193 # linux debian sarge
194 os.name = 'posix'
195 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
196 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
197 sys.platform = 'linux2'
198 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
199 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
200
201 self.assertEquals(get_platform(), 'linux-i686')
202
203 # XXX more platforms to tests here
204
205 def test_get_config_h_filename(self):
206 config_h = sysconfig.get_config_h_filename()
207 self.assertTrue(os.path.isfile(config_h), config_h)
208
209
210def test_main():
211 run_unittest(TestSysConfig)
212
213if __name__ == "__main__":
214 test_main()