summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2011-12-05 16:00:17 +1300
committerRobert Collins <robertc@robertcollins.net>2011-12-05 16:00:17 +1300
commite8e03da9fc52b15d32764212f195cb08ccdf3927 (patch)
tree1a1a3709abd714af8eae3c498ef358cadd8fd667
parentb20e3cb4eb6b1bd36ecc337d21ae8f2610c66231 (diff)
parent9f9c91f79e9455674730ccafeeb8979ed902526a (diff)
downloadfixtures-e8e03da9fc52b15d32764212f195cb08ccdf3927.tar.gz
MNerge NestedTempfile - make tempfile default to a new default location.
-rw-r--r--NEWS3
-rw-r--r--README10
-rw-r--r--lib/fixtures/__init__.py8
-rw-r--r--lib/fixtures/_fixtures/__init__.py6
-rw-r--r--lib/fixtures/_fixtures/tempdir.py30
-rw-r--r--lib/fixtures/tests/_fixtures/test_tempdir.py35
6 files changed, 80 insertions, 12 deletions
diff --git a/NEWS b/NEWS
index 3fd556e..e87e63f 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,9 @@ CHANGES:
* gather_details fixed to handle the case where two child fixtures have the
same detail name. (Jonathan Lange, #895652)
+* ``NestedTempfile`` which will change the default path for tempfile temporary
+ file / directory creation. (Gavin Panella)
+
* New Timeout fixture. (Martin Pool)
0.3.7
diff --git a/README b/README
index a0dd865..d6aa6d9 100644
--- a/README
+++ b/README
@@ -300,6 +300,16 @@ Control the value of a named python attribute.
... pass
>>> fixture = fixtures.MonkeyPatch('__builtin__.open', fake_open)
+NestedTempfile
+++++++++++++++
+
+Change the default directory that the tempfile module places temporary files
+and directories in. This can be useful for containing the noise created by
+code which doesn't clean up its temporary files. This does not affect
+temporary file creation where an explicit containing directory was provided.
+
+ >>> fixture = fixtures.NestedTempfile()
+
PackagePathEntry
++++++++++++++++
diff --git a/lib/fixtures/__init__.py b/lib/fixtures/__init__.py
index fca045d..5e105d8 100644
--- a/lib/fixtures/__init__.py
+++ b/lib/fixtures/__init__.py
@@ -48,6 +48,7 @@ __all__ = [
'LoggerFixture',
'MethodFixture',
'MonkeyPatch',
+ 'NestedTempfile',
'PackagePathEntry',
'PopenFixture',
'PythonPackage',
@@ -59,7 +60,11 @@ __all__ = [
]
-from fixtures.fixture import Fixture, FunctionFixture, MethodFixture
+from fixtures.fixture import (
+ Fixture,
+ FunctionFixture,
+ MethodFixture,
+ )
from fixtures._fixtures import (
EnvironmentVariable,
EnvironmentVariableFixture,
@@ -67,6 +72,7 @@ from fixtures._fixtures import (
FakePopen,
LoggerFixture,
MonkeyPatch,
+ NestedTempfile,
PackagePathEntry,
PopenFixture,
PythonPackage,
diff --git a/lib/fixtures/_fixtures/__init__.py b/lib/fixtures/_fixtures/__init__.py
index b60f4e1..f342a2f 100644
--- a/lib/fixtures/_fixtures/__init__.py
+++ b/lib/fixtures/_fixtures/__init__.py
@@ -23,6 +23,7 @@ __all__ = [
'FakePopen',
'LoggerFixture',
'MonkeyPatch',
+ 'NestedTempfile',
'PackagePathEntry',
'PopenFixture',
'PythonPackage',
@@ -49,7 +50,10 @@ from fixtures._fixtures.popen import (
from fixtures._fixtures.packagepath import PackagePathEntry
from fixtures._fixtures.pythonpackage import PythonPackage
from fixtures._fixtures.pythonpath import PythonPathEntry
-from fixtures._fixtures.tempdir import TempDir
+from fixtures._fixtures.tempdir import (
+ NestedTempfile,
+ TempDir,
+ )
from fixtures._fixtures.timeout import (
Timeout,
TimeoutException,
diff --git a/lib/fixtures/_fixtures/tempdir.py b/lib/fixtures/_fixtures/tempdir.py
index 1851f8b..fd5502c 100644
--- a/lib/fixtures/_fixtures/tempdir.py
+++ b/lib/fixtures/_fixtures/tempdir.py
@@ -14,16 +14,17 @@
# limitations under that license.
__all__ = [
- 'TempDir'
+ 'NestedTempfile',
+ 'TempDir',
]
import shutil
import tempfile
-from fixtures import Fixture
+import fixtures
-class TempDir(Fixture):
+class TempDir(fixtures.Fixture):
"""Create a temporary directory.
:ivar path: The path of the temporary directory.
@@ -32,13 +33,28 @@ class TempDir(Fixture):
def __init__(self, rootdir=None):
"""Create a TempDir.
- :param rootdir: If supplied force the tempoary directory to be a child
- of rootdir.
+ :param rootdir: If supplied force the temporary directory to be a
+ child of rootdir.
"""
- Fixture.setUp(self)
self.rootdir = rootdir
def setUp(self):
- Fixture.setUp(self)
+ super(TempDir, self).setUp()
self.path = tempfile.mkdtemp(dir=self.rootdir)
self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
+
+
+class NestedTempfile(fixtures.Fixture):
+ """Nest all temporary files and directories inside another directory.
+
+ This temporarily monkey-patches the default location that the `tempfile`
+ package creates temporary files and directories in to be a new temporary
+ directory. This new temporary directory is removed when the fixture is torn
+ down.
+ """
+
+ def setUp(self):
+ super(NestedTempfile, self).setUp()
+ tempdir = self.useFixture(TempDir()).path
+ patch = fixtures.MonkeyPatch("tempfile.tempdir", tempdir)
+ self.useFixture(patch)
diff --git a/lib/fixtures/tests/_fixtures/test_tempdir.py b/lib/fixtures/tests/_fixtures/test_tempdir.py
index 4d13ba1..1e42257 100644
--- a/lib/fixtures/tests/_fixtures/test_tempdir.py
+++ b/lib/fixtures/tests/_fixtures/test_tempdir.py
@@ -14,14 +14,16 @@
# limitations under that license.
import os
+import tempfile
import testtools
from testtools.matchers import StartsWith
-import fixtures
-from fixtures import TempDir
+from fixtures import (
+ NestedTempfile,
+ TempDir,
+ )
-
class TestTempDir(testtools.TestCase):
def test_basic(self):
@@ -43,3 +45,30 @@ class TestTempDir(testtools.TestCase):
with fixture:
self.assertThat(fixture.path, StartsWith(root))
+
+class NestedTempfileTest(testtools.TestCase):
+ """Tests for `NestedTempfile`."""
+
+ def test_normal(self):
+ # The temp directory is removed when the context is exited.
+ starting_tempdir = tempfile.gettempdir()
+ with NestedTempfile():
+ self.assertEqual(tempfile.tempdir, tempfile.gettempdir())
+ self.assertNotEqual(starting_tempdir, tempfile.tempdir)
+ self.assertTrue(os.path.isdir(tempfile.tempdir))
+ nested_tempdir = tempfile.tempdir
+ self.assertEqual(tempfile.tempdir, tempfile.gettempdir())
+ self.assertEqual(starting_tempdir, tempfile.tempdir)
+ self.assertFalse(os.path.isdir(nested_tempdir))
+
+ def test_exception(self):
+ # The temp directory is removed when the context is exited, even if
+ # the code running in context raises an exception.
+ class ContrivedException(Exception):
+ pass
+ try:
+ with NestedTempfile():
+ nested_tempdir = tempfile.tempdir
+ raise ContrivedException
+ except ContrivedException:
+ self.assertFalse(os.path.isdir(nested_tempdir))