2009-02-13 18:26:15 -04:00
|
|
|
"""Tests for distutils.util."""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2009-04-13 09:37:57 -03:00
|
|
|
from copy import copy
|
2010-11-05 23:44:43 -03:00
|
|
|
from test.support import run_unittest
|
2018-12-18 11:17:56 -04:00
|
|
|
from unittest import mock
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2009-10-25 20:08:47 -03:00
|
|
|
from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
|
2010-07-22 09:50:05 -03:00
|
|
|
from distutils.util import (get_platform, convert_path, change_root,
|
2009-05-10 09:20:44 -03:00
|
|
|
check_environ, split_quoted, strtobool,
|
2014-03-12 04:34:02 -03:00
|
|
|
rfc822_escape, byte_compile,
|
|
|
|
grok_environment_error)
|
2010-07-22 09:50:05 -03:00
|
|
|
from distutils import util # used to patch _environ_checked
|
|
|
|
from distutils.sysconfig import get_config_vars
|
|
|
|
from distutils import sysconfig
|
2009-05-10 09:20:44 -03:00
|
|
|
from distutils.tests import support
|
2012-07-21 09:36:30 -03:00
|
|
|
import _osx_support
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2009-05-10 09:20:44 -03:00
|
|
|
class UtilTestCase(support.EnvironGuard, unittest.TestCase):
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2009-05-10 09:20:44 -03:00
|
|
|
super(UtilTestCase, self).setUp()
|
2009-02-13 18:26:15 -04:00
|
|
|
# saving the environment
|
|
|
|
self.name = os.name
|
|
|
|
self.platform = sys.platform
|
|
|
|
self.version = sys.version
|
|
|
|
self.sep = os.sep
|
|
|
|
self.join = os.path.join
|
|
|
|
self.isabs = os.path.isabs
|
|
|
|
self.splitdrive = os.path.splitdrive
|
2010-07-22 09:50:05 -03:00
|
|
|
self._config_vars = copy(sysconfig._config_vars)
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
# patching os.uname
|
|
|
|
if hasattr(os, 'uname'):
|
|
|
|
self.uname = os.uname
|
|
|
|
self._uname = os.uname()
|
|
|
|
else:
|
|
|
|
self.uname = None
|
|
|
|
self._uname = None
|
2009-02-13 19:48:11 -04:00
|
|
|
|
2010-07-22 09:50:05 -03:00
|
|
|
os.uname = self._get_uname
|
2009-07-16 13:18:19 -03:00
|
|
|
|
2009-02-13 18:26:15 -04:00
|
|
|
def tearDown(self):
|
2009-04-13 09:37:57 -03:00
|
|
|
# getting back the environment
|
2009-02-13 18:26:15 -04:00
|
|
|
os.name = self.name
|
|
|
|
sys.platform = self.platform
|
|
|
|
sys.version = self.version
|
|
|
|
os.sep = self.sep
|
|
|
|
os.path.join = self.join
|
|
|
|
os.path.isabs = self.isabs
|
|
|
|
os.path.splitdrive = self.splitdrive
|
|
|
|
if self.uname is not None:
|
|
|
|
os.uname = self.uname
|
2009-03-11 09:52:00 -03:00
|
|
|
else:
|
|
|
|
del os.uname
|
2010-07-22 09:50:05 -03:00
|
|
|
sysconfig._config_vars = copy(self._config_vars)
|
2009-05-10 09:20:44 -03:00
|
|
|
super(UtilTestCase, self).tearDown()
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def _set_uname(self, uname):
|
|
|
|
self._uname = uname
|
|
|
|
|
|
|
|
def _get_uname(self):
|
|
|
|
return self._uname
|
|
|
|
|
2010-01-29 07:46:31 -04:00
|
|
|
def test_get_platform(self):
|
2010-07-22 09:50:05 -03:00
|
|
|
|
|
|
|
# windows XP, 32bits
|
|
|
|
os.name = 'nt'
|
|
|
|
sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
|
|
|
|
'[MSC v.1310 32 bit (Intel)]')
|
|
|
|
sys.platform = 'win32'
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'win32')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
|
|
|
# windows XP, amd64
|
|
|
|
os.name = 'nt'
|
|
|
|
sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
|
|
|
|
'[MSC v.1310 32 bit (Amd64)]')
|
|
|
|
sys.platform = 'win32'
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'win-amd64')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
|
|
|
# macbook
|
|
|
|
os.name = 'posix'
|
|
|
|
sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
|
|
|
|
'\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
|
|
|
|
sys.platform = 'darwin'
|
|
|
|
self._set_uname(('Darwin', 'macziade', '8.11.1',
|
|
|
|
('Darwin Kernel Version 8.11.1: '
|
|
|
|
'Wed Oct 10 18:23:28 PDT 2007; '
|
|
|
|
'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2011-05-15 11:46:11 -03:00
|
|
|
get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
|
2010-07-22 09:50:05 -03:00
|
|
|
|
|
|
|
get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
|
|
|
|
'-fwrapv -O3 -Wall -Wstrict-prototypes')
|
|
|
|
|
|
|
|
cursize = sys.maxsize
|
|
|
|
sys.maxsize = (2 ** 31)-1
|
|
|
|
try:
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.3-i386')
|
2010-07-22 09:50:05 -03:00
|
|
|
finally:
|
|
|
|
sys.maxsize = cursize
|
|
|
|
|
|
|
|
# macbook with fat binaries (fat, universal or fat64)
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2011-05-15 11:46:11 -03:00
|
|
|
get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
|
2010-07-22 09:50:05 -03:00
|
|
|
get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
|
|
|
|
'/Developer/SDKs/MacOSX10.4u.sdk '
|
|
|
|
'-fno-strict-aliasing -fno-common '
|
|
|
|
'-dynamic -DNDEBUG -g -O3')
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-fat')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2011-05-15 11:46:11 -03:00
|
|
|
os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
|
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-fat')
|
|
|
|
|
|
|
|
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2010-07-22 09:50:05 -03:00
|
|
|
get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
|
|
|
|
'/Developer/SDKs/MacOSX10.4u.sdk '
|
|
|
|
'-fno-strict-aliasing -fno-common '
|
|
|
|
'-dynamic -DNDEBUG -g -O3')
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-intel')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2010-07-22 09:50:05 -03:00
|
|
|
get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
|
|
|
|
'/Developer/SDKs/MacOSX10.4u.sdk '
|
|
|
|
'-fno-strict-aliasing -fno-common '
|
|
|
|
'-dynamic -DNDEBUG -g -O3')
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-fat3')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2010-07-22 09:50:05 -03:00
|
|
|
get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
|
|
|
|
'/Developer/SDKs/MacOSX10.4u.sdk '
|
|
|
|
'-fno-strict-aliasing -fno-common '
|
|
|
|
'-dynamic -DNDEBUG -g -O3')
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-universal')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2010-07-22 09:50:05 -03:00
|
|
|
get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
|
|
|
|
'/Developer/SDKs/MacOSX10.4u.sdk '
|
|
|
|
'-fno-strict-aliasing -fno-common '
|
|
|
|
'-dynamic -DNDEBUG -g -O3')
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-fat64')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
|
|
|
for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
|
2012-07-21 09:36:30 -03:00
|
|
|
_osx_support._remove_original_values(get_config_vars())
|
2010-07-22 09:50:05 -03:00
|
|
|
get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
|
|
|
|
'/Developer/SDKs/MacOSX10.4u.sdk '
|
|
|
|
'-fno-strict-aliasing -fno-common '
|
|
|
|
'-dynamic -DNDEBUG -g -O3'%(arch,))
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
|
2010-07-22 09:50:05 -03:00
|
|
|
|
2011-05-15 11:46:11 -03:00
|
|
|
|
2010-07-22 09:50:05 -03:00
|
|
|
# linux debian sarge
|
|
|
|
os.name = 'posix'
|
|
|
|
sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
|
|
|
|
'\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
|
|
|
|
sys.platform = 'linux2'
|
|
|
|
self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
|
|
|
|
'#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(get_platform(), 'linux-i686')
|
2010-07-22 09:50:05 -03:00
|
|
|
|
|
|
|
# XXX more platforms to tests here
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def test_convert_path(self):
|
|
|
|
# linux/mac
|
|
|
|
os.sep = '/'
|
|
|
|
def _join(path):
|
|
|
|
return '/'.join(path)
|
|
|
|
os.path.join = _join
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(convert_path('/home/to/my/stuff'),
|
|
|
|
'/home/to/my/stuff')
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
# win
|
|
|
|
os.sep = '\\'
|
|
|
|
def _join(*path):
|
|
|
|
return '\\'.join(path)
|
|
|
|
os.path.join = _join
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, convert_path, '/home/to/my/stuff')
|
|
|
|
self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/')
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(convert_path('home/to/my/stuff'),
|
|
|
|
'home\\to\\my\\stuff')
|
|
|
|
self.assertEqual(convert_path('.'),
|
|
|
|
os.curdir)
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def test_change_root(self):
|
|
|
|
# linux/mac
|
|
|
|
os.name = 'posix'
|
|
|
|
def _isabs(path):
|
|
|
|
return path[0] == '/'
|
|
|
|
os.path.isabs = _isabs
|
2009-02-13 19:48:11 -04:00
|
|
|
def _join(*path):
|
|
|
|
return '/'.join(path)
|
|
|
|
os.path.join = _join
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(change_root('/root', '/old/its/here'),
|
|
|
|
'/root/old/its/here')
|
|
|
|
self.assertEqual(change_root('/root', 'its/here'),
|
|
|
|
'/root/its/here')
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
# windows
|
|
|
|
os.name = 'nt'
|
|
|
|
def _isabs(path):
|
|
|
|
return path.startswith('c:\\')
|
|
|
|
os.path.isabs = _isabs
|
|
|
|
def _splitdrive(path):
|
|
|
|
if path.startswith('c:'):
|
|
|
|
return ('', path.replace('c:', ''))
|
|
|
|
return ('', path)
|
|
|
|
os.path.splitdrive = _splitdrive
|
|
|
|
def _join(*path):
|
|
|
|
return '\\'.join(path)
|
|
|
|
os.path.join = _join
|
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(change_root('c:\\root', 'c:\\old\\its\\here'),
|
|
|
|
'c:\\root\\old\\its\\here')
|
|
|
|
self.assertEqual(change_root('c:\\root', 'its\\here'),
|
|
|
|
'c:\\root\\its\\here')
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
# BugsBunny os (it's a great os)
|
|
|
|
os.name = 'BugsBunny'
|
|
|
|
self.assertRaises(DistutilsPlatformError,
|
|
|
|
change_root, 'c:\\root', 'its\\here')
|
|
|
|
|
2012-10-10 20:20:12 -03:00
|
|
|
# XXX platforms to be covered: mac
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def test_check_environ(self):
|
|
|
|
util._environ_checked = 0
|
2018-12-18 11:17:56 -04:00
|
|
|
os.environ.pop('HOME', None)
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2018-12-18 11:17:56 -04:00
|
|
|
check_environ()
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(os.environ['PLAT'], get_platform())
|
|
|
|
self.assertEqual(util._environ_checked, 1)
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2018-12-18 11:17:56 -04:00
|
|
|
@unittest.skipUnless(os.name == 'posix', 'specific to posix')
|
|
|
|
def test_check_environ_getpwuid(self):
|
|
|
|
util._environ_checked = 0
|
|
|
|
os.environ.pop('HOME', None)
|
|
|
|
|
|
|
|
import pwd
|
|
|
|
|
|
|
|
# only set pw_dir field, other fields are not used
|
|
|
|
result = pwd.struct_passwd((None, None, None, None, None,
|
|
|
|
'/home/distutils', None))
|
|
|
|
with mock.patch.object(pwd, 'getpwuid', return_value=result):
|
|
|
|
check_environ()
|
|
|
|
self.assertEqual(os.environ['HOME'], '/home/distutils')
|
|
|
|
|
|
|
|
util._environ_checked = 0
|
|
|
|
os.environ.pop('HOME', None)
|
|
|
|
|
|
|
|
# bpo-10496: Catch pwd.getpwuid() error
|
|
|
|
with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
|
|
|
|
check_environ()
|
|
|
|
self.assertNotIn('HOME', os.environ)
|
|
|
|
|
2009-02-13 18:26:15 -04:00
|
|
|
def test_split_quoted(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'),
|
|
|
|
['one', 'two', 'three', 'four'])
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def test_strtobool(self):
|
|
|
|
yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1')
|
|
|
|
no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N')
|
|
|
|
|
|
|
|
for y in yes:
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(strtobool(y))
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
for n in no:
|
2013-11-16 18:17:46 -04:00
|
|
|
self.assertFalse(strtobool(n))
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
def test_rfc822_escape(self):
|
|
|
|
header = 'I am a\npoor\nlonesome\nheader\n'
|
|
|
|
res = rfc822_escape(header)
|
|
|
|
wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s'
|
|
|
|
'header%(8s)s') % {'8s': '\n'+8*' '}
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(res, wanted)
|
2009-02-13 18:26:15 -04:00
|
|
|
|
2009-10-25 20:08:47 -03:00
|
|
|
def test_dont_write_bytecode(self):
|
|
|
|
# makes sure byte_compile raise a DistutilsError
|
|
|
|
# if sys.dont_write_bytecode is True
|
|
|
|
old_dont_write_bytecode = sys.dont_write_bytecode
|
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
try:
|
|
|
|
self.assertRaises(DistutilsByteCompileError, byte_compile, [])
|
|
|
|
finally:
|
|
|
|
sys.dont_write_bytecode = old_dont_write_bytecode
|
|
|
|
|
2014-03-12 04:34:02 -03:00
|
|
|
def test_grok_environment_error(self):
|
|
|
|
# test obsolete function to ensure backward compat (#4931)
|
|
|
|
exc = IOError("Unable to find batch file")
|
|
|
|
msg = grok_environment_error(exc)
|
|
|
|
self.assertEqual(msg, "error: Unable to find batch file")
|
|
|
|
|
|
|
|
|
2009-02-13 18:26:15 -04:00
|
|
|
def test_suite():
|
2009-05-10 09:20:44 -03:00
|
|
|
return unittest.makeSuite(UtilTestCase)
|
2009-02-13 18:26:15 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-11-05 23:44:43 -03:00
|
|
|
run_unittest(test_suite())
|