From 884b72e3bb703024a01bf369bacf349bf4299418 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Mon, 22 Aug 2011 11:26:43 +1200 Subject: Extend TempDir to allow controlling the root directory. --- NEWS | 5 +++++ lib/fixtures/_fixtures/tempdir.py | 10 +++++++++- lib/fixtures/tests/_fixtures/test_tempdir.py | 13 +++++++++++-- 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)) + -- cgit v1.2.1