Debian Codegraph @

cpython27/Lib/distutils/tests/test_util.py

raw symbols SCIP
1 """Tests for distutils.util."""
2 import os
3 import sys
4 import unittest
5 from test.test_support import run_unittest, swap_attr
6
7 from distutils.errors import DistutilsByteCompileError
8 from distutils.tests import support
9 from distutils import util # used to patch _environ_checked
10 from distutils.util import (byte_compile, grok_environment_error,
11 check_environ, get_platform)
12
13
14 class UtilTestCase(support.EnvironGuard, unittest.TestCase):
15
16 def test_dont_write_bytecode(self):
17 # makes sure byte_compile raise a DistutilsError
18 # if sys.dont_write_bytecode is True
19 old_dont_write_bytecode = sys.dont_write_bytecode
20 sys.dont_write_bytecode = True
21 try:
22 self.assertRaises(DistutilsByteCompileError, byte_compile, [])
23 finally:
24 sys.dont_write_bytecode = old_dont_write_bytecode
25
26 def test_grok_environment_error(self):
27 # test obsolete function to ensure backward compat (#4931)
28 exc = IOError("Unable to find batch file")
29 msg = grok_environment_error(exc)
30 self.assertEqual(msg, "error: Unable to find batch file")
31
32 def test_check_environ(self):
33 util._environ_checked = 0
34 os.environ.pop('HOME', None)
35
36 check_environ()
37
38 self.assertEqual(os.environ['PLAT'], get_platform())
39 self.assertEqual(util._environ_checked, 1)
40
41 @unittest.skipUnless(os.name == 'posix', 'specific to posix')
42 def test_check_environ_getpwuid(self):
43 util._environ_checked = 0
44 os.environ.pop('HOME', None)
45
46 import pwd
47
48 # only set pw_dir field, other fields are not used
49 def mock_getpwuid(uid):
50 return pwd.struct_passwd((None, None, None, None, None,
51 '/home/distutils', None))
52
53 with swap_attr(pwd, 'getpwuid', mock_getpwuid):
54 check_environ()
55 self.assertEqual(os.environ['HOME'], '/home/distutils')
56
57 util._environ_checked = 0
58 os.environ.pop('HOME', None)
59
60 # bpo-10496: Catch pwd.getpwuid() error
61 def getpwuid_err(uid):
62 raise KeyError
63 with swap_attr(pwd, 'getpwuid', getpwuid_err):
64 check_environ()
65 self.assertNotIn('HOME', os.environ)
66
67
68 def test_suite():
69 return unittest.makeSuite(UtilTestCase)
70
71 if __name__ == "__main__":
72 run_unittest(test_suite())