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