summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Lange <jml@canonical.com>2012-08-05 22:02:23 +0100
committerJonathan Lange <jml@canonical.com>2012-08-05 22:02:23 +0100
commit6cec2cf6609b1ecef6f285154f69a2b879a1074a (patch)
treee15eab3bb0e208eed54f2b67dff2b3ac39e22269
parentdc58d0be2121ca963ab3dfe2da0a7cad2ba8208f (diff)
parent44497898c79cc5e9cf755158eb7c74e6ae1a508c (diff)
downloadfixtures-6cec2cf6609b1ecef6f285154f69a2b879a1074a.tar.gz
Add 'join' method to TempDir.
-rw-r--r--NEWS5
-rw-r--r--lib/fixtures/_fixtures/tempdir.py10
-rw-r--r--lib/fixtures/tests/_fixtures/test_tempdir.py21
3 files changed, 36 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 7338b62..a994487 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,11 @@ fixtures release notes
NEXT
~~~~
+CHANGES:
+
+* Add ``join`` method to ``TempDir`` to more readily get paths relative
+ to a temporary directory. (Jonathan Lange)
+
0.3.9
~~~~~
diff --git a/lib/fixtures/_fixtures/tempdir.py b/lib/fixtures/_fixtures/tempdir.py
index fd5502c..663d3eb 100644
--- a/lib/fixtures/_fixtures/tempdir.py
+++ b/lib/fixtures/_fixtures/tempdir.py
@@ -18,6 +18,7 @@ __all__ = [
'TempDir',
]
+import os
import shutil
import tempfile
@@ -43,6 +44,15 @@ class TempDir(fixtures.Fixture):
self.path = tempfile.mkdtemp(dir=self.rootdir)
self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
+ def join(self, *children):
+ """Return an absolute path, given one relative to this ``TempDir``.
+
+ WARNING: This does not do any checking of ``children`` to make sure
+ they aren't walking up the tree using path segments like '..' or
+ '/usr'. Use at your own risk.
+ """
+ return os.path.abspath(os.path.join(self.path, *children))
+
class NestedTempfile(fixtures.Fixture):
"""Nest all temporary files and directories inside another directory.
diff --git a/lib/fixtures/tests/_fixtures/test_tempdir.py b/lib/fixtures/tests/_fixtures/test_tempdir.py
index 7fc5d45..d0def55 100644
--- a/lib/fixtures/tests/_fixtures/test_tempdir.py
+++ b/lib/fixtures/tests/_fixtures/test_tempdir.py
@@ -46,6 +46,27 @@ class TestTempDir(testtools.TestCase):
with fixture:
self.assertThat(fixture.path, StartsWith(root))
+ def test_join(self):
+ temp_dir = self.useFixture(TempDir())
+ root = temp_dir.path
+ relpath = 'foo/bar/baz'
+ self.assertEqual(
+ os.path.join(root, relpath), temp_dir.join(relpath))
+
+ def test_join_multiple_children(self):
+ temp_dir = self.useFixture(TempDir())
+ root = temp_dir.path
+ self.assertEqual(
+ os.path.join(root, 'foo', 'bar', 'baz'),
+ temp_dir.join('foo', 'bar', 'baz'))
+
+ def test_join_naughty_children(self):
+ temp_dir = self.useFixture(TempDir())
+ root = temp_dir.path
+ self.assertEqual(
+ os.path.abspath(os.path.join(root, '..', 'bar', 'baz')),
+ temp_dir.join('..', 'bar', 'baz'))
+
class NestedTempfileTest(testtools.TestCase):
"""Tests for `NestedTempfile`."""