diff options
author | Jonathan Lange <jml@mumak.net> | 2012-03-21 18:48:59 +0000 |
---|---|---|
committer | Jonathan Lange <jml@mumak.net> | 2012-03-21 18:48:59 +0000 |
commit | 661cad431efbe11c72f69c587343120f8860ce72 (patch) | |
tree | 607719b587873472b816f581c4ee6d3794d3c503 | |
parent | dcf082089a533f1e844a1f861a5994d449a61a59 (diff) | |
parent | 8b745215bee560e9faeb0e3159a4bac0a7765d31 (diff) | |
download | fixtures-661cad431efbe11c72f69c587343120f8860ce72.tar.gz |
New TempHomeDir fixture to create and activate a temporary home dir (james_w)
Merge ~james-w/python-fixtures/temp-home
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | README | 13 | ||||
-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 | 32 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/__init__.py | 1 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_temphomedir.py | 46 |
7 files changed, 102 insertions, 0 deletions
@@ -6,6 +6,12 @@ fixtures release notes IN DEVELOPMENT ~~~~~~~~~~~~~~ +CHANGES: + +* New TempHomeDir fixture to create and activate a temporary home directory. + (James Westby) + + 0.3.8 ~~~~~ @@ -346,6 +346,19 @@ 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 it as $HOME in the environment. + + >>> fixture = fixtures.TempHomeDir() + +The created directory is stored in the ``path`` attribute of the fixture after +setUp. + +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..2601a8d --- /dev/null +++ b/lib/fixtures/_fixtures/temphomedir.py @@ -0,0 +1,32 @@ +# 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 +from fixtures._fixtures.tempdir import TempDir + + +class TempHomeDir(TempDir): + """Create a temporary directory and set it as $HOME + + :ivar path: the path of the temporary directory. + """ + + def setUp(self): + super(TempHomeDir, self).setUp() + self.useFixture(fixtures.EnvironmentVariable("HOME", self.path)) diff --git a/lib/fixtures/tests/_fixtures/__init__.py b/lib/fixtures/tests/_fixtures/__init__.py index 8e817a0..d8e0979 100644 --- a/lib/fixtures/tests/_fixtures/__init__.py +++ b/lib/fixtures/tests/_fixtures/__init__.py @@ -23,6 +23,7 @@ def load_tests(loader, standard_tests, pattern): 'pythonpackage', 'pythonpath', 'tempdir', + 'temphomedir', 'timeout', ] prefix = "fixtures.tests._fixtures.test_" diff --git a/lib/fixtures/tests/_fixtures/test_temphomedir.py b/lib/fixtures/tests/_fixtures/test_temphomedir.py new file mode 100644 index 0000000..339ce2c --- /dev/null +++ b/lib/fixtures/tests/_fixtures/test_temphomedir.py @@ -0,0 +1,46 @@ +# 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)) + fixture.setUp() + try: + path = fixture.path + self.assertTrue(os.path.isdir(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)) |