Use temproot as a fallback if the current user couldn't be obtained

This commit is contained in:
Bruno Oliveira 2015-09-16 16:42:07 -03:00
parent 558e5406e8
commit 130e6cf8a2
2 changed files with 11 additions and 8 deletions

View File

@ -52,10 +52,14 @@ class TempdirFactory:
basetemp.remove() basetemp.remove()
basetemp.mkdir() basetemp.mkdir()
else: else:
# use a sub-directory in the temproot to speed-up
# make_numbered_dir() call
temproot = py.path.local.get_temproot() temproot = py.path.local.get_temproot()
rootdir = temproot.join('pytest-of-%s' % get_user()) user = get_user()
if user:
# use a sub-directory in the temproot to speed-up
# make_numbered_dir() call
rootdir = temproot.join('pytest-of-%s' % user)
else:
rootdir = temproot
rootdir.ensure(dir=1) rootdir.ensure(dir=1)
basetemp = py.path.local.make_numbered_dir(prefix='pytest-', basetemp = py.path.local.make_numbered_dir(prefix='pytest-',
rootdir=rootdir) rootdir=rootdir)
@ -68,15 +72,14 @@ class TempdirFactory:
def get_user(): def get_user():
"""Return the current user name, falling back to using "tox" as user name """Return the current user name, or None if getuser() does not work
because getpass relies on environment variables which might not be in the current environment (see #1010).
available in a tox environment (see #1010).
""" """
import getpass import getpass
try: try:
return getpass.getuser() return getpass.getuser()
except ImportError: except ImportError:
return 'tox' return None
# backward compatibility # backward compatibility
TempdirHandler = TempdirFactory TempdirHandler = TempdirFactory

View File

@ -145,4 +145,4 @@ def test_get_user(monkeypatch):
from _pytest.tmpdir import get_user from _pytest.tmpdir import get_user
monkeypatch.delenv('USER', raising=False) monkeypatch.delenv('USER', raising=False)
monkeypatch.delenv('USERNAME', raising=False) monkeypatch.delenv('USERNAME', raising=False)
assert get_user() == 'tox' assert get_user() is None