|
4 | 4 | import unittest |
5 | 5 | from copy import copy |
6 | 6 | from test.support import run_unittest |
| 7 | +from unittest import mock |
7 | 8 |
|
8 | 9 | from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError |
9 | 10 | from distutils.util import (get_platform, convert_path, change_root, |
@@ -234,20 +235,35 @@ def _join(*path): |
234 | 235 |
|
235 | 236 | def test_check_environ(self): |
236 | 237 | util._environ_checked = 0 |
237 | | - if 'HOME' in os.environ: |
238 | | - del os.environ['HOME'] |
| 238 | + os.environ.pop('HOME', None) |
239 | 239 |
|
240 | | - # posix without HOME |
241 | | - if os.name == 'posix': # this test won't run on windows |
242 | | - check_environ() |
243 | | - import pwd |
244 | | - self.assertEqual(os.environ['HOME'], pwd.getpwuid(os.getuid())[5]) |
245 | | - else: |
246 | | - check_environ() |
| 240 | + check_environ() |
247 | 241 |
|
248 | 242 | self.assertEqual(os.environ['PLAT'], get_platform()) |
249 | 243 | self.assertEqual(util._environ_checked, 1) |
250 | 244 |
|
| 245 | + @unittest.skipUnless(os.name == 'posix', 'specific to posix') |
| 246 | + def test_check_environ_getpwuid(self): |
| 247 | + util._environ_checked = 0 |
| 248 | + os.environ.pop('HOME', None) |
| 249 | + |
| 250 | + import pwd |
| 251 | + |
| 252 | + # only set pw_dir field, other fields are not used |
| 253 | + result = pwd.struct_passwd((None, None, None, None, None, |
| 254 | + '/home/distutils', None)) |
| 255 | + with mock.patch.object(pwd, 'getpwuid', return_value=result): |
| 256 | + check_environ() |
| 257 | + self.assertEqual(os.environ['HOME'], '/home/distutils') |
| 258 | + |
| 259 | + util._environ_checked = 0 |
| 260 | + os.environ.pop('HOME', None) |
| 261 | + |
| 262 | + # bpo-10496: Catch pwd.getpwuid() error |
| 263 | + with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError): |
| 264 | + check_environ() |
| 265 | + self.assertNotIn('HOME', os.environ) |
| 266 | + |
251 | 267 | def test_split_quoted(self): |
252 | 268 | self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'), |
253 | 269 | ['one', 'two', 'three', 'four']) |
|
0 commit comments