test_util.py 11 KB

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