diff options
author | James Westby <james.westby@linaro.org> | 2011-12-06 21:04:11 -0500 |
---|---|---|
committer | James Westby <james.westby@linaro.org> | 2011-12-06 21:04:11 -0500 |
commit | 236d3ff9cf33fc2420e85b45091335a5b578910f (patch) | |
tree | 1eeae65bc7ebafaa886adee931cc54aef7825f3e | |
parent | dcf082089a533f1e844a1f861a5994d449a61a59 (diff) | |
download | fixtures-236d3ff9cf33fc2420e85b45091335a5b578910f.tar.gz |
Add a TempHomeDir fixture.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | README | 14 | ||||
-rw-r--r-- | lib/fixtures/__init__.py | 1 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/__init__.py | 3 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/temphomedir.py | 45 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_temphomedir.py | 48 |
6 files changed, 112 insertions, 0 deletions
@@ -4,6 +4,7 @@ PYTHON ?= python all: check check: + echo $(PYTHONPATH) PYTHONPATH=$(PYTHONPATH) $(PYTHON) -m testtools.run \ fixtures.test_suite @@ -346,6 +346,20 @@ Create a temporary directory and clean it up later. The created directory is stored in the ``path`` attribute of the fixture after setUp. +TempHomeDir ++++++++++++ + +Create a temporary directory and set is as $HOME in the environment. + + >>> fixture = fixtures.TempHomeDir() + +The created directory is stored in the ``path`` attribute of the fixture after +setUp. The TempDir fixture that created the path is available at ``tempdir`` +if you need it. + +The environment will now have $HOME set to the same path, and the value +will be returned to its previous value after tearDown. + Timeout +++++++ diff --git a/lib/fixtures/__init__.py b/lib/fixtures/__init__.py index d8f0e1f..fff9057 100644 --- a/lib/fixtures/__init__.py +++ b/lib/fixtures/__init__.py @@ -78,6 +78,7 @@ from fixtures._fixtures import ( PythonPackage, PythonPathEntry, TempDir, + TempHomeDir, Timeout, TimeoutException, ) diff --git a/lib/fixtures/_fixtures/__init__.py b/lib/fixtures/_fixtures/__init__.py index f342a2f..05db7cd 100644 --- a/lib/fixtures/_fixtures/__init__.py +++ b/lib/fixtures/_fixtures/__init__.py @@ -54,6 +54,9 @@ from fixtures._fixtures.tempdir import ( NestedTempfile, TempDir, ) +from fixtures._fixtures.temphomedir import ( + TempHomeDir, + ) from fixtures._fixtures.timeout import ( Timeout, TimeoutException, diff --git a/lib/fixtures/_fixtures/temphomedir.py b/lib/fixtures/_fixtures/temphomedir.py new file mode 100644 index 0000000..73d23f4 --- /dev/null +++ b/lib/fixtures/_fixtures/temphomedir.py @@ -0,0 +1,45 @@ +# fixtures: Fixtures with cleanups for testing and convenience. +# +# Copyright (c) 2010, Canonical Ltd. +# +# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause +# license at the users choice. A copy of both licenses are available in the +# project source as Apache-2.0 and BSD. You may not use this file except in +# compliance with one of these two licences. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# license you chose for the specific language governing permissions and +# limitations under that license. + +__all__ = [ + 'TempHomeDir', + ] + +import fixtures + + +class TempHomeDir(fixtures.Fixture): + """Create a temporary directory and set it as $HOME + + :ivar path: the path of the temporary directory. + :ivar tempdir: The TempDir fixture providing the directory. + """ + + def __init__(self, rootdir=None): + """Create a TempDir. + + :param rootdir: If supplied force the temporary directory to be a + child of rootdir. + """ + self.rootdir = rootdir + + def setUp(self): + super(TempHomeDir, self).setUp() + self.tempdir = self.useFixture(fixtures.TempDir(rootdir=self.rootdir)) + self.useFixture(fixtures.EnvironmentVariable("HOME", self.tempdir.path)) + + @property + def path(self): + return self.tempdir.path diff --git a/lib/fixtures/tests/_fixtures/test_temphomedir.py b/lib/fixtures/tests/_fixtures/test_temphomedir.py new file mode 100644 index 0000000..45b239b --- /dev/null +++ b/lib/fixtures/tests/_fixtures/test_temphomedir.py @@ -0,0 +1,48 @@ +# fixtures: Fixtures with cleanups for testing and convenience. +# +# Copyright (c) 2011 Canonical Ltd. +# +# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause +# license at the users choice. A copy of both licenses are available in the +# project source as Apache-2.0 and BSD. You may not use this file except in +# compliance with one of these two licences. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# license you chose for the specific language governing permissions and +# limitations under that license. + +import os + +import testtools +from testtools.matchers import StartsWith + +from fixtures import ( + TempDir, + TempHomeDir, + ) + +class TestTempDir(testtools.TestCase): + + def test_basic(self): + fixture = TempHomeDir() + sentinel = object() + self.assertEqual(sentinel, getattr(fixture, 'path', sentinel)) + self.assertEqual(sentinel, getattr(fixture, 'tempdir', sentinel)) + fixture.setUp() + try: + path = fixture.path + self.assertTrue(os.path.isdir(path)) + self.assertEqual(path, fixture.tempdir.path) + self.assertEqual(path, os.environ.get("HOME")) + finally: + fixture.cleanUp() + self.assertFalse(os.path.isdir(path)) + + def test_under_dir(self): + root = self.useFixture(TempDir()).path + fixture = TempHomeDir(root) + fixture.setUp() + with fixture: + self.assertThat(fixture.path, StartsWith(root)) |