From da2c52a50211af97f215148a5cf73dab936fba21 Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Sat, 4 Aug 2012 18:32:01 +0100 Subject: Add an 'abspath' helper. --- lib/fixtures/_fixtures/tempdir.py | 10 ++++++++++ lib/fixtures/tests/_fixtures/test_tempdir.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/fixtures/_fixtures/tempdir.py b/lib/fixtures/_fixtures/tempdir.py index fd5502c..b37a32b 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 abspath(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..cb0584e 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_abspath(self): + temp_dir = self.useFixture(TempDir()) + root = temp_dir.path + relpath = 'foo/bar/baz' + self.assertEqual( + os.path.join(root, relpath), temp_dir.abspath(relpath)) + + def test_abspath_multiple_children(self): + temp_dir = self.useFixture(TempDir()) + root = temp_dir.path + self.assertEqual( + os.path.join(root, 'foo', 'bar', 'baz'), + temp_dir.abspath('foo', 'bar', 'baz')) + + def test_abspath_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.abspath('..', 'bar', 'baz')) + class NestedTempfileTest(testtools.TestCase): """Tests for `NestedTempfile`.""" -- cgit v1.2.1 From 114770f7d858ca91c474f8f8d6f0e3735298d25e Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Sat, 4 Aug 2012 18:32:46 +0100 Subject: Update NEWS. --- NEWS | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS b/NEWS index 7338b62..544011e 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,11 @@ fixtures release notes NEXT ~~~~ +CHANGES: + +* Add ``abspath`` method to ``TempDir`` to more readily get paths relative + to a temporary directory. (Jonathan Lange) + 0.3.9 ~~~~~ -- cgit v1.2.1 From 44497898c79cc5e9cf755158eb7c74e6ae1a508c Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Sun, 5 Aug 2012 21:57:24 +0100 Subject: Rename to 'join'. --- NEWS | 2 +- lib/fixtures/_fixtures/tempdir.py | 2 +- lib/fixtures/tests/_fixtures/test_tempdir.py | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 544011e..a994487 100644 --- a/NEWS +++ b/NEWS @@ -8,7 +8,7 @@ NEXT CHANGES: -* Add ``abspath`` method to ``TempDir`` to more readily get paths relative +* Add ``join`` method to ``TempDir`` to more readily get paths relative to a temporary directory. (Jonathan Lange) diff --git a/lib/fixtures/_fixtures/tempdir.py b/lib/fixtures/_fixtures/tempdir.py index b37a32b..663d3eb 100644 --- a/lib/fixtures/_fixtures/tempdir.py +++ b/lib/fixtures/_fixtures/tempdir.py @@ -44,7 +44,7 @@ class TempDir(fixtures.Fixture): self.path = tempfile.mkdtemp(dir=self.rootdir) self.addCleanup(shutil.rmtree, self.path, ignore_errors=True) - def abspath(self, *children): + 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 diff --git a/lib/fixtures/tests/_fixtures/test_tempdir.py b/lib/fixtures/tests/_fixtures/test_tempdir.py index cb0584e..d0def55 100644 --- a/lib/fixtures/tests/_fixtures/test_tempdir.py +++ b/lib/fixtures/tests/_fixtures/test_tempdir.py @@ -46,26 +46,26 @@ class TestTempDir(testtools.TestCase): with fixture: self.assertThat(fixture.path, StartsWith(root)) - def test_abspath(self): + 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.abspath(relpath)) + os.path.join(root, relpath), temp_dir.join(relpath)) - def test_abspath_multiple_children(self): + 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.abspath('foo', 'bar', 'baz')) + temp_dir.join('foo', 'bar', 'baz')) - def test_abspath_naughty_children(self): + 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.abspath('..', 'bar', 'baz')) + temp_dir.join('..', 'bar', 'baz')) class NestedTempfileTest(testtools.TestCase): -- cgit v1.2.1