Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback TEMPS_DIR to tempfile.gettempdir #1

Merged
merged 1 commit into from Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion temps.py
Expand Up @@ -62,11 +62,12 @@
import os
import shutil
import uuid
from tempfile import gettempdir


# SET THE DEFAULTS

TEMPS_DIR = os.environ.get('TEMPS_DIR') or os.getcwd()
TEMPS_DIR = os.environ.get('TEMPS_DIR') or gettempdir()
TEMPS_PREFIX = os.environ.get('TEMPS_PREFIX', '')
TEMPS_SUFFIX = os.environ.get('TEMPS_SUFFIX', '')
TEMPS_MODE = int(os.environ.get('TEMPS_MODE', '0777'), 8)
Expand Down
54 changes: 51 additions & 3 deletions tests/test_temps.py
@@ -1,8 +1,10 @@



import temps
import os
from subprocess import Popen
from subprocess import PIPE
from nose.tools import eq_
from tempfile import gettempdir
from shutil import rmtree

def test_tmpfile():
with temps.tmpfile() as path:
Expand Down Expand Up @@ -34,4 +36,50 @@ def test_tmppath():
if os.path.isdir(path):
os.rmdir(path)

class TestTempsDir:
def _popen(self, overrides=None):
"""
:type override: (str, str)
:param override: (var, val)
"""

if not overrides:
overrides = []

p = Popen(
["env", "-i"]
+ ["=".join(xs) for xs in overrides]
+ ["python", "-c", "import temps; print temps.TEMPS_DIR;"]
, stdout=PIPE, stderr=PIPE
)
out, _ = p.communicate()
return out.strip()

def setUp(self):
self.tmpdir = '/tmp/foo'
os.makedirs(self.tmpdir)
# L{tempfile.gettempdir} checks wheter the dir is
# readable/writable, if not it continues fallbacking

def tearDown(self):
rmtree(self.tmpdir)

def test_pure_fallback(self):
"""
Test we get the `gettempdir`s platform specific fallback when
give no env overrides
"""
eq_(self._popen(), gettempdir())

def test_gettempdir_env_var(self):
"""
Test we get an override handled by `gettempdir`
"""
eq_(self._popen([("TMP", self.tmpdir)]), self.tmpdir)

def test_temps_env_var(self):
"""
Test we get an override for temps itself
"""
v = "/foo2"
eq_(self._popen(dict(TMP = self.tmpdir, TEMPS_DIR = v).items()), v)