From 6676aeda5a6d720e1c667c12e3db25d3929b4ffb Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 16 Sep 2015 12:20:07 -0300 Subject: [PATCH 1/4] Make tmpdir more resilient in case environment variables required by getpass are missing Fix #1010 --- _pytest/tmpdir.py | 6 +++++- testing/test_tmpdir.py | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/_pytest/tmpdir.py b/_pytest/tmpdir.py index 44e980e2e..cff39065f 100644 --- a/_pytest/tmpdir.py +++ b/_pytest/tmpdir.py @@ -56,7 +56,11 @@ class TempdirFactory: # make_numbered_dir() call import getpass 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) basetemp = py.path.local.make_numbered_dir(prefix='pytest-', rootdir=rootdir) diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 05b24fc59..0ea8e5263 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -117,4 +117,19 @@ def test_tmpdir_factory(testdir): session_dir.isdir() """) reprec = testdir.inline_run() - reprec.assertoutcome(passed=1) \ No newline at end of file + 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) From 1150e87e3127e16ce0215f119572ff1956826a17 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 16 Sep 2015 12:47:50 -0300 Subject: [PATCH 2/4] Extract get_user logic into a separate function --- _pytest/tmpdir.py | 19 +++++++++++++------ testing/test_tmpdir.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/_pytest/tmpdir.py b/_pytest/tmpdir.py index cff39065f..a2a063200 100644 --- a/_pytest/tmpdir.py +++ b/_pytest/tmpdir.py @@ -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 diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 0ea8e5263..2dc070bd6 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -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' From 558e5406e84026528b4ee3055d8dc9411819e1d7 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 16 Sep 2015 13:06:39 -0300 Subject: [PATCH 3/4] test_get_user should execute on windows only --- testing/test_tmpdir.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 2dc070bd6..b04c73583 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -1,3 +1,4 @@ +import sys import py import pytest @@ -135,9 +136,11 @@ def test_tmpdir_fallback_tox_env(testdir, monkeypatch): reprec.assertoutcome(passed=1) +@pytest.mark.skipif(not sys.platform.startswith('win'), reason='win only') 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). + required by getpass module are missing from the environment on Windows + (#1010). """ from _pytest.tmpdir import get_user monkeypatch.delenv('USER', raising=False) From 130e6cf8a29465b36b61221b6cae27dd20b4e94d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 16 Sep 2015 16:42:07 -0300 Subject: [PATCH 4/4] Use temproot as a fallback if the current user couldn't be obtained --- _pytest/tmpdir.py | 17 ++++++++++------- testing/test_tmpdir.py | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/_pytest/tmpdir.py b/_pytest/tmpdir.py index a2a063200..b90e35948 100644 --- a/_pytest/tmpdir.py +++ b/_pytest/tmpdir.py @@ -52,10 +52,14 @@ class TempdirFactory: basetemp.remove() basetemp.mkdir() else: - # use a sub-directory in the temproot to speed-up - # make_numbered_dir() call 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) basetemp = py.path.local.make_numbered_dir(prefix='pytest-', rootdir=rootdir) @@ -68,15 +72,14 @@ class TempdirFactory: 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). + """Return the current user name, or None if getuser() does not work + in the current environment (see #1010). """ import getpass try: return getpass.getuser() except ImportError: - return 'tox' + return None # backward compatibility TempdirHandler = TempdirFactory diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index b04c73583..9bc0cf40a 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -145,4 +145,4 @@ def test_get_user(monkeypatch): from _pytest.tmpdir import get_user monkeypatch.delenv('USER', raising=False) monkeypatch.delenv('USERNAME', raising=False) - assert get_user() == 'tox' + assert get_user() is None