ยปCore Development>Code coverage>Lib/distutils/tests/test_util.py

Python code coverage for Lib/distutils/tests/test_util.py

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