1 | n/a | """Tests for distutils.util.""" |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | import unittest |
---|
5 | n/a | from copy import copy |
---|
6 | n/a | from test.support import run_unittest |
---|
7 | n/a | |
---|
8 | n/a | from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError |
---|
9 | n/a | from distutils.util import (get_platform, convert_path, change_root, |
---|
10 | n/a | check_environ, split_quoted, strtobool, |
---|
11 | n/a | rfc822_escape, byte_compile, |
---|
12 | n/a | grok_environment_error) |
---|
13 | n/a | from distutils import util # used to patch _environ_checked |
---|
14 | n/a | from distutils.sysconfig import get_config_vars |
---|
15 | n/a | from distutils import sysconfig |
---|
16 | n/a | from distutils.tests import support |
---|
17 | n/a | import _osx_support |
---|
18 | n/a | |
---|
19 | n/a | class UtilTestCase(support.EnvironGuard, unittest.TestCase): |
---|
20 | n/a | |
---|
21 | n/a | def setUp(self): |
---|
22 | n/a | super(UtilTestCase, self).setUp() |
---|
23 | n/a | # saving the environment |
---|
24 | n/a | self.name = os.name |
---|
25 | n/a | self.platform = sys.platform |
---|
26 | n/a | self.version = sys.version |
---|
27 | n/a | self.sep = os.sep |
---|
28 | n/a | self.join = os.path.join |
---|
29 | n/a | self.isabs = os.path.isabs |
---|
30 | n/a | self.splitdrive = os.path.splitdrive |
---|
31 | n/a | self._config_vars = copy(sysconfig._config_vars) |
---|
32 | n/a | |
---|
33 | n/a | # patching os.uname |
---|
34 | n/a | if hasattr(os, 'uname'): |
---|
35 | n/a | self.uname = os.uname |
---|
36 | n/a | self._uname = os.uname() |
---|
37 | n/a | else: |
---|
38 | n/a | self.uname = None |
---|
39 | n/a | self._uname = None |
---|
40 | n/a | |
---|
41 | n/a | os.uname = self._get_uname |
---|
42 | n/a | |
---|
43 | n/a | def tearDown(self): |
---|
44 | n/a | # getting back the environment |
---|
45 | n/a | os.name = self.name |
---|
46 | n/a | sys.platform = self.platform |
---|
47 | n/a | sys.version = self.version |
---|
48 | n/a | os.sep = self.sep |
---|
49 | n/a | os.path.join = self.join |
---|
50 | n/a | os.path.isabs = self.isabs |
---|
51 | n/a | os.path.splitdrive = self.splitdrive |
---|
52 | n/a | if self.uname is not None: |
---|
53 | n/a | os.uname = self.uname |
---|
54 | n/a | else: |
---|
55 | n/a | del os.uname |
---|
56 | n/a | sysconfig._config_vars = copy(self._config_vars) |
---|
57 | n/a | super(UtilTestCase, self).tearDown() |
---|
58 | n/a | |
---|
59 | n/a | def _set_uname(self, uname): |
---|
60 | n/a | self._uname = uname |
---|
61 | n/a | |
---|
62 | n/a | def _get_uname(self): |
---|
63 | n/a | return self._uname |
---|
64 | n/a | |
---|
65 | n/a | def test_get_platform(self): |
---|
66 | n/a | |
---|
67 | n/a | # windows XP, 32bits |
---|
68 | n/a | os.name = 'nt' |
---|
69 | n/a | sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' |
---|
70 | n/a | '[MSC v.1310 32 bit (Intel)]') |
---|
71 | n/a | sys.platform = 'win32' |
---|
72 | n/a | self.assertEqual(get_platform(), 'win32') |
---|
73 | n/a | |
---|
74 | n/a | # windows XP, amd64 |
---|
75 | n/a | os.name = 'nt' |
---|
76 | n/a | sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' |
---|
77 | n/a | '[MSC v.1310 32 bit (Amd64)]') |
---|
78 | n/a | sys.platform = 'win32' |
---|
79 | n/a | self.assertEqual(get_platform(), 'win-amd64') |
---|
80 | n/a | |
---|
81 | n/a | # windows XP, itanium |
---|
82 | n/a | os.name = 'nt' |
---|
83 | n/a | sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' |
---|
84 | n/a | '[MSC v.1310 32 bit (Itanium)]') |
---|
85 | n/a | sys.platform = 'win32' |
---|
86 | n/a | self.assertEqual(get_platform(), 'win-ia64') |
---|
87 | n/a | |
---|
88 | n/a | # macbook |
---|
89 | n/a | os.name = 'posix' |
---|
90 | n/a | sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' |
---|
91 | n/a | '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') |
---|
92 | n/a | sys.platform = 'darwin' |
---|
93 | n/a | self._set_uname(('Darwin', 'macziade', '8.11.1', |
---|
94 | n/a | ('Darwin Kernel Version 8.11.1: ' |
---|
95 | n/a | 'Wed Oct 10 18:23:28 PDT 2007; ' |
---|
96 | n/a | 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) |
---|
97 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
98 | n/a | get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' |
---|
99 | n/a | |
---|
100 | n/a | get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' |
---|
101 | n/a | '-fwrapv -O3 -Wall -Wstrict-prototypes') |
---|
102 | n/a | |
---|
103 | n/a | cursize = sys.maxsize |
---|
104 | n/a | sys.maxsize = (2 ** 31)-1 |
---|
105 | n/a | try: |
---|
106 | n/a | self.assertEqual(get_platform(), 'macosx-10.3-i386') |
---|
107 | n/a | finally: |
---|
108 | n/a | sys.maxsize = cursize |
---|
109 | n/a | |
---|
110 | n/a | # macbook with fat binaries (fat, universal or fat64) |
---|
111 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
112 | n/a | get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4' |
---|
113 | n/a | get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' |
---|
114 | n/a | '/Developer/SDKs/MacOSX10.4u.sdk ' |
---|
115 | n/a | '-fno-strict-aliasing -fno-common ' |
---|
116 | n/a | '-dynamic -DNDEBUG -g -O3') |
---|
117 | n/a | |
---|
118 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-fat') |
---|
119 | n/a | |
---|
120 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
121 | n/a | os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.1' |
---|
122 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-fat') |
---|
123 | n/a | |
---|
124 | n/a | |
---|
125 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
126 | n/a | get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' |
---|
127 | n/a | '/Developer/SDKs/MacOSX10.4u.sdk ' |
---|
128 | n/a | '-fno-strict-aliasing -fno-common ' |
---|
129 | n/a | '-dynamic -DNDEBUG -g -O3') |
---|
130 | n/a | |
---|
131 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-intel') |
---|
132 | n/a | |
---|
133 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
134 | n/a | get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' |
---|
135 | n/a | '/Developer/SDKs/MacOSX10.4u.sdk ' |
---|
136 | n/a | '-fno-strict-aliasing -fno-common ' |
---|
137 | n/a | '-dynamic -DNDEBUG -g -O3') |
---|
138 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-fat3') |
---|
139 | n/a | |
---|
140 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
141 | n/a | get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' |
---|
142 | n/a | '/Developer/SDKs/MacOSX10.4u.sdk ' |
---|
143 | n/a | '-fno-strict-aliasing -fno-common ' |
---|
144 | n/a | '-dynamic -DNDEBUG -g -O3') |
---|
145 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-universal') |
---|
146 | n/a | |
---|
147 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
148 | n/a | get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' |
---|
149 | n/a | '/Developer/SDKs/MacOSX10.4u.sdk ' |
---|
150 | n/a | '-fno-strict-aliasing -fno-common ' |
---|
151 | n/a | '-dynamic -DNDEBUG -g -O3') |
---|
152 | n/a | |
---|
153 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-fat64') |
---|
154 | n/a | |
---|
155 | n/a | for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): |
---|
156 | n/a | _osx_support._remove_original_values(get_config_vars()) |
---|
157 | n/a | get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' |
---|
158 | n/a | '/Developer/SDKs/MacOSX10.4u.sdk ' |
---|
159 | n/a | '-fno-strict-aliasing -fno-common ' |
---|
160 | n/a | '-dynamic -DNDEBUG -g -O3'%(arch,)) |
---|
161 | n/a | |
---|
162 | n/a | self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,)) |
---|
163 | n/a | |
---|
164 | n/a | |
---|
165 | n/a | # linux debian sarge |
---|
166 | n/a | os.name = 'posix' |
---|
167 | n/a | sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' |
---|
168 | n/a | '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') |
---|
169 | n/a | sys.platform = 'linux2' |
---|
170 | n/a | self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', |
---|
171 | n/a | '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) |
---|
172 | n/a | |
---|
173 | n/a | self.assertEqual(get_platform(), 'linux-i686') |
---|
174 | n/a | |
---|
175 | n/a | # XXX more platforms to tests here |
---|
176 | n/a | |
---|
177 | n/a | def test_convert_path(self): |
---|
178 | n/a | # linux/mac |
---|
179 | n/a | os.sep = '/' |
---|
180 | n/a | def _join(path): |
---|
181 | n/a | return '/'.join(path) |
---|
182 | n/a | os.path.join = _join |
---|
183 | n/a | |
---|
184 | n/a | self.assertEqual(convert_path('/home/to/my/stuff'), |
---|
185 | n/a | '/home/to/my/stuff') |
---|
186 | n/a | |
---|
187 | n/a | # win |
---|
188 | n/a | os.sep = '\\' |
---|
189 | n/a | def _join(*path): |
---|
190 | n/a | return '\\'.join(path) |
---|
191 | n/a | os.path.join = _join |
---|
192 | n/a | |
---|
193 | n/a | self.assertRaises(ValueError, convert_path, '/home/to/my/stuff') |
---|
194 | n/a | self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/') |
---|
195 | n/a | |
---|
196 | n/a | self.assertEqual(convert_path('home/to/my/stuff'), |
---|
197 | n/a | 'home\\to\\my\\stuff') |
---|
198 | n/a | self.assertEqual(convert_path('.'), |
---|
199 | n/a | os.curdir) |
---|
200 | n/a | |
---|
201 | n/a | def test_change_root(self): |
---|
202 | n/a | # linux/mac |
---|
203 | n/a | os.name = 'posix' |
---|
204 | n/a | def _isabs(path): |
---|
205 | n/a | return path[0] == '/' |
---|
206 | n/a | os.path.isabs = _isabs |
---|
207 | n/a | def _join(*path): |
---|
208 | n/a | return '/'.join(path) |
---|
209 | n/a | os.path.join = _join |
---|
210 | n/a | |
---|
211 | n/a | self.assertEqual(change_root('/root', '/old/its/here'), |
---|
212 | n/a | '/root/old/its/here') |
---|
213 | n/a | self.assertEqual(change_root('/root', 'its/here'), |
---|
214 | n/a | '/root/its/here') |
---|
215 | n/a | |
---|
216 | n/a | # windows |
---|
217 | n/a | os.name = 'nt' |
---|
218 | n/a | def _isabs(path): |
---|
219 | n/a | return path.startswith('c:\\') |
---|
220 | n/a | os.path.isabs = _isabs |
---|
221 | n/a | def _splitdrive(path): |
---|
222 | n/a | if path.startswith('c:'): |
---|
223 | n/a | return ('', path.replace('c:', '')) |
---|
224 | n/a | return ('', path) |
---|
225 | n/a | os.path.splitdrive = _splitdrive |
---|
226 | n/a | def _join(*path): |
---|
227 | n/a | return '\\'.join(path) |
---|
228 | n/a | os.path.join = _join |
---|
229 | n/a | |
---|
230 | n/a | self.assertEqual(change_root('c:\\root', 'c:\\old\\its\\here'), |
---|
231 | n/a | 'c:\\root\\old\\its\\here') |
---|
232 | n/a | self.assertEqual(change_root('c:\\root', 'its\\here'), |
---|
233 | n/a | 'c:\\root\\its\\here') |
---|
234 | n/a | |
---|
235 | n/a | # BugsBunny os (it's a great os) |
---|
236 | n/a | os.name = 'BugsBunny' |
---|
237 | n/a | self.assertRaises(DistutilsPlatformError, |
---|
238 | n/a | change_root, 'c:\\root', 'its\\here') |
---|
239 | n/a | |
---|
240 | n/a | # XXX platforms to be covered: mac |
---|
241 | n/a | |
---|
242 | n/a | def test_check_environ(self): |
---|
243 | n/a | util._environ_checked = 0 |
---|
244 | n/a | if 'HOME' in os.environ: |
---|
245 | n/a | del os.environ['HOME'] |
---|
246 | n/a | |
---|
247 | n/a | # posix without HOME |
---|
248 | n/a | if os.name == 'posix': # this test won't run on windows |
---|
249 | n/a | check_environ() |
---|
250 | n/a | import pwd |
---|
251 | n/a | self.assertEqual(os.environ['HOME'], pwd.getpwuid(os.getuid())[5]) |
---|
252 | n/a | else: |
---|
253 | n/a | check_environ() |
---|
254 | n/a | |
---|
255 | n/a | self.assertEqual(os.environ['PLAT'], get_platform()) |
---|
256 | n/a | self.assertEqual(util._environ_checked, 1) |
---|
257 | n/a | |
---|
258 | n/a | def test_split_quoted(self): |
---|
259 | n/a | self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'), |
---|
260 | n/a | ['one', 'two', 'three', 'four']) |
---|
261 | n/a | |
---|
262 | n/a | def test_strtobool(self): |
---|
263 | n/a | yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1') |
---|
264 | n/a | no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N') |
---|
265 | n/a | |
---|
266 | n/a | for y in yes: |
---|
267 | n/a | self.assertTrue(strtobool(y)) |
---|
268 | n/a | |
---|
269 | n/a | for n in no: |
---|
270 | n/a | self.assertFalse(strtobool(n)) |
---|
271 | n/a | |
---|
272 | n/a | def test_rfc822_escape(self): |
---|
273 | n/a | header = 'I am a\npoor\nlonesome\nheader\n' |
---|
274 | n/a | res = rfc822_escape(header) |
---|
275 | n/a | wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s' |
---|
276 | n/a | 'header%(8s)s') % {'8s': '\n'+8*' '} |
---|
277 | n/a | self.assertEqual(res, wanted) |
---|
278 | n/a | |
---|
279 | n/a | def test_dont_write_bytecode(self): |
---|
280 | n/a | # makes sure byte_compile raise a DistutilsError |
---|
281 | n/a | # if sys.dont_write_bytecode is True |
---|
282 | n/a | old_dont_write_bytecode = sys.dont_write_bytecode |
---|
283 | n/a | sys.dont_write_bytecode = True |
---|
284 | n/a | try: |
---|
285 | n/a | self.assertRaises(DistutilsByteCompileError, byte_compile, []) |
---|
286 | n/a | finally: |
---|
287 | n/a | sys.dont_write_bytecode = old_dont_write_bytecode |
---|
288 | n/a | |
---|
289 | n/a | def test_grok_environment_error(self): |
---|
290 | n/a | # test obsolete function to ensure backward compat (#4931) |
---|
291 | n/a | exc = IOError("Unable to find batch file") |
---|
292 | n/a | msg = grok_environment_error(exc) |
---|
293 | n/a | self.assertEqual(msg, "error: Unable to find batch file") |
---|
294 | n/a | |
---|
295 | n/a | |
---|
296 | n/a | def test_suite(): |
---|
297 | n/a | return unittest.makeSuite(UtilTestCase) |
---|
298 | n/a | |
---|
299 | n/a | if __name__ == "__main__": |
---|
300 | n/a | run_unittest(test_suite()) |
---|