Add missing issue number in Misc/NEWS entry.
[python.git] / Lib / distutils / tests / test_util.py
blob6722997f5815da132ee0b63664c784e02fd6cc0e
1 """Tests for distutils.util."""
2 import os
3 import sys
4 import unittest
5 from copy import copy
6 from StringIO import StringIO
7 import subprocess
9 from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
10 from distutils.util import (get_platform, convert_path, change_root,
11 check_environ, split_quoted, strtobool,
12 rfc822_escape, get_compiler_versions,
13 _find_exe_version, _MAC_OS_X_LD_VERSION,
14 byte_compile)
15 from distutils import util
16 from distutils.sysconfig import get_config_vars
17 from distutils import sysconfig
18 from distutils.tests import support
19 from distutils.version import LooseVersion
21 class FakePopen(object):
22 test_class = None
23 def __init__(self, cmd, shell, stdout, stderr):
24 self.cmd = cmd.split()[0]
25 exes = self.test_class._exes
26 if self.cmd not in exes:
27 # we don't want to call the system, returning an empty
28 # output so it doesn't match
29 self.stdout = StringIO()
30 self.stderr = StringIO()
31 else:
32 self.stdout = StringIO(exes[self.cmd])
33 self.stderr = StringIO()
35 class UtilTestCase(support.EnvironGuard, unittest.TestCase):
37 def setUp(self):
38 super(UtilTestCase, self).setUp()
39 # saving the environment
40 self.name = os.name
41 self.platform = sys.platform
42 self.version = sys.version
43 self.sep = os.sep
44 self.join = os.path.join
45 self.isabs = os.path.isabs
46 self.splitdrive = os.path.splitdrive
47 self._config_vars = copy(sysconfig._config_vars)
49 # patching os.uname
50 if hasattr(os, 'uname'):
51 self.uname = os.uname
52 self._uname = os.uname()
53 else:
54 self.uname = None
55 self._uname = None
56 os.uname = self._get_uname
58 # patching POpen
59 self.old_find_executable = util.find_executable
60 util.find_executable = self._find_executable
61 self._exes = {}
62 self.old_popen = subprocess.Popen
63 self.old_stdout = sys.stdout
64 self.old_stderr = sys.stderr
65 FakePopen.test_class = self
66 subprocess.Popen = FakePopen
68 def tearDown(self):
69 # getting back the environment
70 os.name = self.name
71 sys.platform = self.platform
72 sys.version = self.version
73 os.sep = self.sep
74 os.path.join = self.join
75 os.path.isabs = self.isabs
76 os.path.splitdrive = self.splitdrive
77 if self.uname is not None:
78 os.uname = self.uname
79 else:
80 del os.uname
81 sysconfig._config_vars = copy(self._config_vars)
82 util.find_executable = self.old_find_executable
83 subprocess.Popen = self.old_popen
84 sys.old_stdout = self.old_stdout
85 sys.old_stderr = self.old_stderr
86 super(UtilTestCase, self).tearDown()
88 def _set_uname(self, uname):
89 self._uname = uname
91 def _get_uname(self):
92 return self._uname
94 def test_get_platform(self):
96 # windows XP, 32bits
97 os.name = 'nt'
98 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
99 '[MSC v.1310 32 bit (Intel)]')
100 sys.platform = 'win32'
101 self.assertEquals(get_platform(), 'win32')
103 # windows XP, amd64
104 os.name = 'nt'
105 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
106 '[MSC v.1310 32 bit (Amd64)]')
107 sys.platform = 'win32'
108 self.assertEquals(get_platform(), 'win-amd64')
110 # windows XP, itanium
111 os.name = 'nt'
112 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
113 '[MSC v.1310 32 bit (Itanium)]')
114 sys.platform = 'win32'
115 self.assertEquals(get_platform(), 'win-ia64')
117 # macbook
118 os.name = 'posix'
119 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
120 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
121 sys.platform = 'darwin'
122 self._set_uname(('Darwin', 'macziade', '8.11.1',
123 ('Darwin Kernel Version 8.11.1: '
124 'Wed Oct 10 18:23:28 PDT 2007; '
125 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
126 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
128 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
129 '-fwrapv -O3 -Wall -Wstrict-prototypes')
131 self.assertEquals(get_platform(), 'macosx-10.3-i386')
133 # macbook with fat binaries (fat, universal or fat64)
134 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
135 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
136 '/Developer/SDKs/MacOSX10.4u.sdk '
137 '-fno-strict-aliasing -fno-common '
138 '-dynamic -DNDEBUG -g -O3')
140 self.assertEquals(get_platform(), 'macosx-10.4-fat')
142 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
143 '/Developer/SDKs/MacOSX10.4u.sdk '
144 '-fno-strict-aliasing -fno-common '
145 '-dynamic -DNDEBUG -g -O3')
147 self.assertEquals(get_platform(), 'macosx-10.4-intel')
149 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
150 '/Developer/SDKs/MacOSX10.4u.sdk '
151 '-fno-strict-aliasing -fno-common '
152 '-dynamic -DNDEBUG -g -O3')
153 self.assertEquals(get_platform(), 'macosx-10.4-fat3')
155 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
156 '/Developer/SDKs/MacOSX10.4u.sdk '
157 '-fno-strict-aliasing -fno-common '
158 '-dynamic -DNDEBUG -g -O3')
159 self.assertEquals(get_platform(), 'macosx-10.4-universal')
161 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
162 '/Developer/SDKs/MacOSX10.4u.sdk '
163 '-fno-strict-aliasing -fno-common '
164 '-dynamic -DNDEBUG -g -O3')
166 self.assertEquals(get_platform(), 'macosx-10.4-fat64')
168 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
169 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
170 '/Developer/SDKs/MacOSX10.4u.sdk '
171 '-fno-strict-aliasing -fno-common '
172 '-dynamic -DNDEBUG -g -O3'%(arch,))
174 self.assertEquals(get_platform(), 'macosx-10.4-%s'%(arch,))
176 # linux debian sarge
177 os.name = 'posix'
178 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
179 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
180 sys.platform = 'linux2'
181 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
182 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
184 self.assertEquals(get_platform(), 'linux-i686')
186 # XXX more platforms to tests here
188 def test_convert_path(self):
189 # linux/mac
190 os.sep = '/'
191 def _join(path):
192 return '/'.join(path)
193 os.path.join = _join
195 self.assertEquals(convert_path('/home/to/my/stuff'),
196 '/home/to/my/stuff')
198 # win
199 os.sep = '\\'
200 def _join(*path):
201 return '\\'.join(path)
202 os.path.join = _join
204 self.assertRaises(ValueError, convert_path, '/home/to/my/stuff')
205 self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/')
207 self.assertEquals(convert_path('home/to/my/stuff'),
208 'home\\to\\my\\stuff')
209 self.assertEquals(convert_path('.'),
210 os.curdir)
212 def test_change_root(self):
213 # linux/mac
214 os.name = 'posix'
215 def _isabs(path):
216 return path[0] == '/'
217 os.path.isabs = _isabs
218 def _join(*path):
219 return '/'.join(path)
220 os.path.join = _join
222 self.assertEquals(change_root('/root', '/old/its/here'),
223 '/root/old/its/here')
224 self.assertEquals(change_root('/root', 'its/here'),
225 '/root/its/here')
227 # windows
228 os.name = 'nt'
229 def _isabs(path):
230 return path.startswith('c:\\')
231 os.path.isabs = _isabs
232 def _splitdrive(path):
233 if path.startswith('c:'):
234 return ('', path.replace('c:', ''))
235 return ('', path)
236 os.path.splitdrive = _splitdrive
237 def _join(*path):
238 return '\\'.join(path)
239 os.path.join = _join
241 self.assertEquals(change_root('c:\\root', 'c:\\old\\its\\here'),
242 'c:\\root\\old\\its\\here')
243 self.assertEquals(change_root('c:\\root', 'its\\here'),
244 'c:\\root\\its\\here')
246 # BugsBunny os (it's a great os)
247 os.name = 'BugsBunny'
248 self.assertRaises(DistutilsPlatformError,
249 change_root, 'c:\\root', 'its\\here')
251 # XXX platforms to be covered: os2, mac
253 def test_check_environ(self):
254 util._environ_checked = 0
255 if 'HOME' in os.environ:
256 del os.environ['HOME']
258 # posix without HOME
259 if os.name == 'posix': # this test won't run on windows
260 check_environ()
261 import pwd
262 self.assertEquals(os.environ['HOME'], pwd.getpwuid(os.getuid())[5])
263 else:
264 check_environ()
266 self.assertEquals(os.environ['PLAT'], get_platform())
267 self.assertEquals(util._environ_checked, 1)
269 def test_split_quoted(self):
270 self.assertEquals(split_quoted('""one"" "two" \'three\' \\four'),
271 ['one', 'two', 'three', 'four'])
273 def test_strtobool(self):
274 yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1')
275 no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N')
277 for y in yes:
278 self.assertTrue(strtobool(y))
280 for n in no:
281 self.assertTrue(not strtobool(n))
283 def test_rfc822_escape(self):
284 header = 'I am a\npoor\nlonesome\nheader\n'
285 res = rfc822_escape(header)
286 wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s'
287 'header%(8s)s') % {'8s': '\n'+8*' '}
288 self.assertEquals(res, wanted)
290 def test_find_exe_version(self):
291 # the ld version scheme under MAC OS is:
292 # ^@(#)PROGRAM:ld PROJECT:ld64-VERSION
294 # where VERSION is a 2-digit number for major
295 # revisions. For instance under Leopard, it's
296 # currently 77
298 # Dots are used when branching is done.
300 # The SnowLeopard ld64 is currently 95.2.12
302 for output, version in (('@(#)PROGRAM:ld PROJECT:ld64-77', '77'),
303 ('@(#)PROGRAM:ld PROJECT:ld64-95.2.12',
304 '95.2.12')):
305 result = _MAC_OS_X_LD_VERSION.search(output)
306 self.assertEquals(result.group(1), version)
308 def _find_executable(self, name):
309 if name in self._exes:
310 return name
311 return None
313 def test_get_compiler_versions(self):
314 # get_versions calls distutils.spawn.find_executable on
315 # 'gcc', 'ld' and 'dllwrap'
316 self.assertEquals(get_compiler_versions(), (None, None, None))
318 # Let's fake we have 'gcc' and it returns '3.4.5'
319 self._exes['gcc'] = 'gcc (GCC) 3.4.5 (mingw special)\nFSF'
320 res = get_compiler_versions()
321 self.assertEquals(str(res[0]), '3.4.5')
323 # and let's see what happens when the version
324 # doesn't match the regular expression
325 # (\d+\.\d+(\.\d+)*)
326 self._exes['gcc'] = 'very strange output'
327 res = get_compiler_versions()
328 self.assertEquals(res[0], None)
330 # same thing for ld
331 if sys.platform != 'darwin':
332 self._exes['ld'] = 'GNU ld version 2.17.50 20060824'
333 res = get_compiler_versions()
334 self.assertEquals(str(res[1]), '2.17.50')
335 self._exes['ld'] = '@(#)PROGRAM:ld PROJECT:ld64-77'
336 res = get_compiler_versions()
337 self.assertEquals(res[1], None)
338 else:
339 self._exes['ld'] = 'GNU ld version 2.17.50 20060824'
340 res = get_compiler_versions()
341 self.assertEquals(res[1], None)
342 self._exes['ld'] = '@(#)PROGRAM:ld PROJECT:ld64-77'
343 res = get_compiler_versions()
344 self.assertEquals(str(res[1]), '77')
346 # and dllwrap
347 self._exes['dllwrap'] = 'GNU dllwrap 2.17.50 20060824\nFSF'
348 res = get_compiler_versions()
349 self.assertEquals(str(res[2]), '2.17.50')
350 self._exes['dllwrap'] = 'Cheese Wrap'
351 res = get_compiler_versions()
352 self.assertEquals(res[2], None)
354 def test_dont_write_bytecode(self):
355 # makes sure byte_compile raise a DistutilsError
356 # if sys.dont_write_bytecode is True
357 old_dont_write_bytecode = sys.dont_write_bytecode
358 sys.dont_write_bytecode = True
359 try:
360 self.assertRaises(DistutilsByteCompileError, byte_compile, [])
361 finally:
362 sys.dont_write_bytecode = old_dont_write_bytecode
364 def test_suite():
365 return unittest.makeSuite(UtilTestCase)
367 if __name__ == "__main__":
368 unittest.main(defaultTest="test_suite")