2009-02-13 18:22:03 -04:00
|
|
|
"""Tests for distutils.util."""
|
2018-12-18 12:34:51 -04:00
|
|
|
import os
|
2009-02-13 18:22:03 -04:00
|
|
|
import sys
|
|
|
|
import unittest
|
2018-12-18 12:34:51 -04:00
|
|
|
from test.test_support import run_unittest, swap_attr
|
2009-02-13 18:22:03 -04:00
|
|
|
|
2014-03-12 04:14:48 -03:00
|
|
|
from distutils.errors import DistutilsByteCompileError
|
2017-05-03 12:28:10 -03:00
|
|
|
from distutils.tests import support
|
2018-12-18 12:34:51 -04:00
|
|
|
from distutils import util # used to patch _environ_checked
|
|
|
|
from distutils.util import (byte_compile, grok_environment_error,
|
|
|
|
check_environ, get_platform)
|
2014-03-12 04:14:48 -03:00
|
|
|
|
2009-07-16 12:35:45 -03:00
|
|
|
|
2017-05-03 12:28:10 -03:00
|
|
|
class UtilTestCase(support.EnvironGuard, unittest.TestCase):
|
2009-07-16 12:35:45 -03:00
|
|
|
|
2009-10-24 12:10:37 -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:
|
2009-10-24 12:19:03 -03:00
|
|
|
sys.dont_write_bytecode = old_dont_write_bytecode
|
2009-10-24 12:10:37 -03:00
|
|
|
|
2014-03-12 04:14:48 -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")
|
|
|
|
|
2018-12-18 12:34:51 -04:00
|
|
|
def test_check_environ(self):
|
|
|
|
util._environ_checked = 0
|
|
|
|
os.environ.pop('HOME', None)
|
|
|
|
|
|
|
|
check_environ()
|
|
|
|
|
|
|
|
self.assertEqual(os.environ['PLAT'], get_platform())
|
|
|
|
self.assertEqual(util._environ_checked, 1)
|
|
|
|
|
|
|
|
@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
|
|
|
|
def mock_getpwuid(uid):
|
|
|
|
return pwd.struct_passwd((None, None, None, None, None,
|
|
|
|
'/home/distutils', None))
|
|
|
|
|
|
|
|
with swap_attr(pwd, 'getpwuid', mock_getpwuid):
|
|
|
|
check_environ()
|
|
|
|
self.assertEqual(os.environ['HOME'], '/home/distutils')
|
|
|
|
|
|
|
|
util._environ_checked = 0
|
|
|
|
os.environ.pop('HOME', None)
|
|
|
|
|
|
|
|
# bpo-10496: Catch pwd.getpwuid() error
|
|
|
|
def getpwuid_err(uid):
|
|
|
|
raise KeyError
|
|
|
|
with swap_attr(pwd, 'getpwuid', getpwuid_err):
|
|
|
|
check_environ()
|
|
|
|
self.assertNotIn('HOME', os.environ)
|
|
|
|
|
2014-03-12 04:14:48 -03:00
|
|
|
|
2009-02-13 18:22:03 -04:00
|
|
|
def test_suite():
|
2009-05-10 09:17:30 -03:00
|
|
|
return unittest.makeSuite(UtilTestCase)
|
2009-02-13 18:22:03 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
Merged revisions 86236,86240,86332,86340,87271,87273,87447 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
To comply with the 2.x doc style, the methods in trace.rst use brackets around
optional arguments. The rest is a mostly straight merge, modulo support changed
to test_support and use of the old super call style in test_tuple.
........
r86236 | eric.araujo | 2010-11-06 03:44:43 +0100 (sam., 06 nov. 2010) | 2 lines
Make sure each test can be run standalone (./python Lib/distutils/tests/x.py)
........
r86240 | eric.araujo | 2010-11-06 05:11:59 +0100 (sam., 06 nov. 2010) | 2 lines
Prevent ResourceWarnings in test_gettext
........
r86332 | eric.araujo | 2010-11-08 19:15:17 +0100 (lun., 08 nov. 2010) | 4 lines
Add missing NEWS entry for a fix committed by Senthil.
All recent modifications to distutils should now be covered in NEWS.
........
r86340 | eric.araujo | 2010-11-08 22:48:23 +0100 (lun., 08 nov. 2010) | 2 lines
This was actually fixed for the previous alpha.
........
r87271 | eric.araujo | 2010-12-15 20:09:58 +0100 (mer., 15 déc. 2010) | 2 lines
Improve trace documentation (#9264). Patch by Eli Bendersky.
........
r87273 | eric.araujo | 2010-12-15 20:30:15 +0100 (mer., 15 déc. 2010) | 2 lines
Use nested method directives, rewrap long lines, fix whitespace.
........
r87447 | eric.araujo | 2010-12-23 20:13:05 +0100 (jeu., 23 déc. 2010) | 2 lines
Fix typo in superclass method name
........
2011-02-02 20:12:18 -04:00
|
|
|
run_unittest(test_suite())
|