Debian Codegraph @

lib-python/3/distutils/tests/test_util.py

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