summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2011-08-22 11:26:43 +1200
committerRobert Collins <robertc@robertcollins.net>2011-08-22 11:26:43 +1200
commit884b72e3bb703024a01bf369bacf349bf4299418 (patch)
tree08310a87db4e1e7de43a6dd70cce12722f3a01c9
parenta1e8a04e70a6717f8e6d6eb42428422c8a92d934 (diff)
downloadfixtures-884b72e3bb703024a01bf369bacf349bf4299418.tar.gz
Extend TempDir to allow controlling the root directory.
-rw-r--r--NEWS5
-rw-r--r--lib/fixtures/_fixtures/tempdir.py10
-rw-r--r--lib/fixtures/tests/_fixtures/test_tempdir.py13
3 files changed, 25 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 5d9a27b..61abf87 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,11 @@ CHANGES:
* Python 3 now supported. (Jonathan Lange, #816665)
+* ``TempDir`` supports creating the temporary directory under a specific path.
+ An example of where this is useful would be if you have some specific long
+ lived temporary items and need to avoid running afoul of tmpreaper.
+ (Robert Collins, #830757)
+
* Updated to match the changed ``gather_details`` API in testtools. See #801027.
This is an incompatible change in testtools and requires testtools 0.9.12.
(Jonathan Lange)
diff --git a/lib/fixtures/_fixtures/tempdir.py b/lib/fixtures/_fixtures/tempdir.py
index 8437dcf..923ff43 100644
--- a/lib/fixtures/_fixtures/tempdir.py
+++ b/lib/fixtures/_fixtures/tempdir.py
@@ -29,7 +29,15 @@ class TempDir(Fixture):
:ivar path: The path of the temporary directory.
"""
+ def __init__(self, rootdir=None):
+ """Create a TempDir.
+
+ :param rootdir: If supplied force the tempoary directory to be a child
+ of rootdir.
+ """
+ self.rootdir = rootdir
+
def setUp(self):
Fixture.setUp(self)
- self.path = tempfile.mkdtemp()
+ self.path = tempfile.mkdtemp(dir=self.rootdir)
self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
diff --git a/lib/fixtures/tests/_fixtures/test_tempdir.py b/lib/fixtures/tests/_fixtures/test_tempdir.py
index ef4884e..4d13ba1 100644
--- a/lib/fixtures/tests/_fixtures/test_tempdir.py
+++ b/lib/fixtures/tests/_fixtures/test_tempdir.py
@@ -16,12 +16,13 @@
import os
import testtools
+from testtools.matchers import StartsWith
import fixtures
-from fixtures import TempDir, TestWithFixtures
+from fixtures import TempDir
-class TestTempDir(testtools.TestCase, TestWithFixtures):
+class TestTempDir(testtools.TestCase):
def test_basic(self):
fixture = TempDir()
@@ -34,3 +35,11 @@ class TestTempDir(testtools.TestCase, TestWithFixtures):
finally:
fixture.cleanUp()
self.assertFalse(os.path.isdir(path))
+
+ def test_under_dir(self):
+ root = self.useFixture(TempDir()).path
+ fixture = TempDir(root)
+ fixture.setUp()
+ with fixture:
+ self.assertThat(fixture.path, StartsWith(root))
+