test_util.py 11 KB

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