Extract get_user logic into a separate function

This commit is contained in:
Bruno Oliveira 2015-09-16 12:47:50 -03:00
parent 6676aeda5a
commit 1150e87e31
2 changed files with 23 additions and 6 deletions

View File

@ -54,13 +54,8 @@ class TempdirFactory:
else:
# use a sub-directory in the temproot to speed-up
# make_numbered_dir() call
import getpass
temproot = py.path.local.get_temproot()
try:
rootdir = temproot.join('pytest-of-%s' % getpass.getuser())
except ImportError:
# see issue #1010
rootdir = temproot.join('pytest-tox')
rootdir = temproot.join('pytest-of-%s' % get_user())
rootdir.ensure(dir=1)
basetemp = py.path.local.make_numbered_dir(prefix='pytest-',
rootdir=rootdir)
@ -71,6 +66,18 @@ class TempdirFactory:
def finish(self):
self.trace("finish")
def get_user():
"""Return the current user name, falling back to using "tox" as user name
because getpass relies on environment variables which might not be
available in a tox environment (see #1010).
"""
import getpass
try:
return getpass.getuser()
except ImportError:
return 'tox'
# backward compatibility
TempdirHandler = TempdirFactory

View File

@ -133,3 +133,13 @@ def test_tmpdir_fallback_tox_env(testdir, monkeypatch):
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_get_user(monkeypatch):
"""Test that get_user() function works even if environment variables
required by getpass module are missing from the environment (#1010).
"""
from _pytest.tmpdir import get_user
monkeypatch.delenv('USER', raising=False)
monkeypatch.delenv('USERNAME', raising=False)
assert get_user() == 'tox'