Make tmpdir more resilient in case environment variables required by getpass are missing

Fix #1010
This commit is contained in:
Bruno Oliveira 2015-09-16 12:20:07 -03:00
parent 2093889ac2
commit 6676aeda5a
2 changed files with 21 additions and 2 deletions

View File

@ -56,7 +56,11 @@ class TempdirFactory:
# make_numbered_dir() call # make_numbered_dir() call
import getpass import getpass
temproot = py.path.local.get_temproot() temproot = py.path.local.get_temproot()
rootdir = temproot.join('pytest-of-%s' % getpass.getuser()) try:
rootdir = temproot.join('pytest-of-%s' % getpass.getuser())
except ImportError:
# see issue #1010
rootdir = temproot.join('pytest-tox')
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)

View File

@ -117,4 +117,19 @@ def test_tmpdir_factory(testdir):
session_dir.isdir() session_dir.isdir()
""") """)
reprec = testdir.inline_run() reprec = testdir.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_tmpdir_fallback_tox_env(testdir, monkeypatch):
"""Test that tmpdir works even if environment variables required by getpass
module are missing (#1010).
"""
monkeypatch.delenv('USER', raising=False)
monkeypatch.delenv('USERNAME', raising=False)
testdir.makepyfile("""
import pytest
def test_some(tmpdir):
assert tmpdir.isdir()
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)