diff options
author | Robert Collins <robertc@robertcollins.net> | 2011-03-19 21:13:50 +1300 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2011-03-19 21:13:50 +1300 |
commit | e5d2c7544711a83d9956d7f73140105027814de0 (patch) | |
tree | 647f35d43ec5ad3c7b40bbeb07b92998dd1fe2ea | |
parent | fc34dbd958aa175e86de1db93ac0af81ec279f54 (diff) | |
download | fixtures-e5d2c7544711a83d9956d7f73140105027814de0.tar.gz |
* New fixture ``PythonPathEntry`` which patches sys.path.
(Robert Collins, #737503)
-rw-r--r-- | NEWS | 5 | ||||
-rw-r--r-- | README | 11 | ||||
-rw-r--r-- | lib/fixtures/__init__.py | 2 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/__init__.py | 2 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/pythonpath.py | 43 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/__init__.py | 1 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_pythonpath.py | 45 |
7 files changed, 109 insertions, 0 deletions
@@ -6,6 +6,11 @@ fixtures release notes IN DEVELOPMENT ~~~~~~~~~~~~~~ +CHANGES: + +* New fixture ``PythonPathEntry`` which patches sys.path. + (Robert Collins, #737503) + 0.3.5 ~~~~~ @@ -297,9 +297,20 @@ points to Python programs. >>> fixture = fixtures.PythonPackage('foo.bar', [('quux.py', '')]) +PythonPathEntry ++++++++++++++++ + +Adds a single directory to sys.path. If the directory is already in the path, +nothing happens, if it isn't then it is added on setUp and removed on cleanUp. + + >>> fixture = fixtures.PythonPathEntry('/foo/bar') + TempDir +++++++ Create a temporary directory and clean it up later. >>> fixture = fixtures.TempDir() + +The created directory is stored in the ``path`` attribute of the fixture after +setUp. diff --git a/lib/fixtures/__init__.py b/lib/fixtures/__init__.py index 4a561f2..1729a9e 100644 --- a/lib/fixtures/__init__.py +++ b/lib/fixtures/__init__.py @@ -46,6 +46,7 @@ __all__ = [ 'MonkeyPatch', 'PopenFixture', 'PythonPackage', + 'PythonPathEntry', 'TempDir', 'TestWithFixtures', ] @@ -57,6 +58,7 @@ from fixtures._fixtures import ( MonkeyPatch, PopenFixture, PythonPackage, + PythonPathEntry, TempDir, ) from fixtures.testcase import TestWithFixtures diff --git a/lib/fixtures/_fixtures/__init__.py b/lib/fixtures/_fixtures/__init__.py index fb23907..b18775c 100644 --- a/lib/fixtures/_fixtures/__init__.py +++ b/lib/fixtures/_fixtures/__init__.py @@ -21,6 +21,7 @@ __all__ = [ 'MonkeyPatch', 'PopenFixture', 'PythonPackage', + 'PythonPathEntry', 'TempDir', ] @@ -29,4 +30,5 @@ from fixtures._fixtures.environ import EnvironmentVariableFixture from fixtures._fixtures.monkeypatch import MonkeyPatch from fixtures._fixtures.popen import PopenFixture from fixtures._fixtures.pythonpackage import PythonPackage +from fixtures._fixtures.pythonpath import PythonPathEntry from fixtures._fixtures.tempdir import TempDir diff --git a/lib/fixtures/_fixtures/pythonpath.py b/lib/fixtures/_fixtures/pythonpath.py new file mode 100644 index 0000000..89c1968 --- /dev/null +++ b/lib/fixtures/_fixtures/pythonpath.py @@ -0,0 +1,43 @@ +# fixtures: Fixtures with cleanups for testing and convenience. +# +# Copyright (c) 2011, Robert Collins <robertc@robertcollins.net> +# +# 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__ = [ + 'PythonPathEntry' + ] + +import sys + +from fixtures import Fixture + + +class PythonPathEntry(Fixture): + """Add a path to sys.path. + + If the path is already in sys.path, sys.path will not be altered. + """ + + def __init__(self, directory): + """Create a PythonPathEntry. + + :param directory: The directory to add to sys.path. + """ + self.directory = directory + + def setUp(self): + Fixture.setUp(self) + if self.directory in sys.path: + return + self.addCleanup(sys.path.remove, self.directory) + sys.path.append(self.directory) diff --git a/lib/fixtures/tests/_fixtures/__init__.py b/lib/fixtures/tests/_fixtures/__init__.py index 00b345b..f5dfeef 100644 --- a/lib/fixtures/tests/_fixtures/__init__.py +++ b/lib/fixtures/tests/_fixtures/__init__.py @@ -19,6 +19,7 @@ def load_tests(loader, standard_tests, pattern): 'monkeypatch', 'popen', 'pythonpackage', + 'pythonpath', 'tempdir', ] prefix = "fixtures.tests._fixtures.test_" diff --git a/lib/fixtures/tests/_fixtures/test_pythonpath.py b/lib/fixtures/tests/_fixtures/test_pythonpath.py new file mode 100644 index 0000000..b0e0281 --- /dev/null +++ b/lib/fixtures/tests/_fixtures/test_pythonpath.py @@ -0,0 +1,45 @@ +# fixtures: Fixtures with cleanups for testing and convenience. +# +# Copyright (c) 2011, Robert Collins <robertc@robertcollins.net> +# +# 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 sys + +import testtools + +import fixtures +from fixtures import ( + PythonPathEntry, + TempDir, + ) + + +class TestPythonPathEntry(testtools.TestCase): + + def test_adds_missing_to_end_sys_path(self): + uniquedir = self.useFixture(TempDir()).path + fixture = PythonPathEntry(uniquedir) + self.assertFalse(uniquedir in sys.path) + with fixture: + self.assertTrue(uniquedir in sys.path) + self.assertFalse(uniquedir in sys.path) + + def test_doesnt_alter_existing_entry(self): + existingdir = sys.path[0] + expectedlen = len(sys.path) + fixture = PythonPathEntry(existingdir) + with fixture: + self.assertTrue(existingdir in sys.path) + self.assertEqual(expectedlen, len(sys.path)) + self.assertTrue(existingdir in sys.path) + self.assertEqual(expectedlen, len(sys.path)) |