diff options
author | Robert Collins <robertc@robertcollins.net> | 2011-06-23 17:54:36 +1200 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2011-06-23 17:54:36 +1200 |
commit | 6b5695a1e5b6969ba16ad382256005c211e281c7 (patch) | |
tree | beda0a73609a73e6b73b215452b4d4976cb8fd86 | |
parent | 5ef29b108b18a2814944936b3e2862bb53f1b3c2 (diff) | |
download | fixtures-6b5695a1e5b6969ba16ad382256005c211e281c7.tar.gz |
New fixture ``PackagePathEntry`` which patches the path of an existing
package, allowing importing part of it from aonther directory.
(Robert Collins)
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | README | 9 | ||||
-rw-r--r-- | lib/fixtures/__init__.py | 2 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/__init__.py | 2 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/packagepath.py | 48 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/__init__.py | 1 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_packagepath.py | 45 |
7 files changed, 111 insertions, 0 deletions
@@ -13,6 +13,10 @@ CHANGES: gather_details helper). (Gavin Panella, #796253) +* New fixture ``PackagePathEntry`` which patches the path of an existing + package, allowing importing part of it from aonther directory. + (Robert Collins) + * New fixture ``PythonPathEntry`` which patches sys.path. (Robert Collins, #737503) @@ -283,6 +283,15 @@ Control the value of a named python attribute. ... pass >>> fixture = fixtures.MonkeyPatch('__builtin__.open', fake_open) +PackagePathEntry +++++++++++++++++ + +Adds a single directory to the path for an existing python package. This adds +to the package.__path__ list. 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.PackagePathEntry('package/name', '/foo/bar') + PopenFixture ++++++++++++ diff --git a/lib/fixtures/__init__.py b/lib/fixtures/__init__.py index 1729a9e..a697680 100644 --- a/lib/fixtures/__init__.py +++ b/lib/fixtures/__init__.py @@ -44,6 +44,7 @@ __all__ = [ 'FunctionFixture', 'MethodFixture', 'MonkeyPatch', + 'PackagePathEntry', 'PopenFixture', 'PythonPackage', 'PythonPathEntry', @@ -56,6 +57,7 @@ from fixtures.fixture import Fixture, FunctionFixture, MethodFixture from fixtures._fixtures import ( EnvironmentVariableFixture, MonkeyPatch, + PackagePathEntry, PopenFixture, PythonPackage, PythonPathEntry, diff --git a/lib/fixtures/_fixtures/__init__.py b/lib/fixtures/_fixtures/__init__.py index b18775c..6287242 100644 --- a/lib/fixtures/_fixtures/__init__.py +++ b/lib/fixtures/_fixtures/__init__.py @@ -19,6 +19,7 @@ __all__ = [ 'EnvironmentVariableFixture', 'MonkeyPatch', + 'PackagePathEntry', 'PopenFixture', 'PythonPackage', 'PythonPathEntry', @@ -29,6 +30,7 @@ __all__ = [ from fixtures._fixtures.environ import EnvironmentVariableFixture from fixtures._fixtures.monkeypatch import MonkeyPatch from fixtures._fixtures.popen import PopenFixture +from fixtures._fixtures.packagepath import PackagePathEntry from fixtures._fixtures.pythonpackage import PythonPackage from fixtures._fixtures.pythonpath import PythonPathEntry from fixtures._fixtures.tempdir import TempDir diff --git a/lib/fixtures/_fixtures/packagepath.py b/lib/fixtures/_fixtures/packagepath.py new file mode 100644 index 0000000..43a9bf3 --- /dev/null +++ b/lib/fixtures/_fixtures/packagepath.py @@ -0,0 +1,48 @@ +# 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__ = [ + 'PackagePathEntry' + ] + +import sys + +from fixtures import Fixture + + +class PackagePathEntry(Fixture): + """Add a path to the path of a python package. + + The python package needs to be already imported. + + If this new path is already in the packages __path__ list then the __path__ + list will not be altered. + """ + + def __init__(self, packagename, directory): + """Create a PackagePathEntry. + + :param directory: The directory to add to the package.__path__. + """ + self.packagename = packagename + self.directory = directory + + def setUp(self): + Fixture.setUp(self) + path = sys.modules[self.packagename].__path__ + if self.directory in path: + return + self.addCleanup(path.remove, self.directory) + path.append(self.directory) diff --git a/lib/fixtures/tests/_fixtures/__init__.py b/lib/fixtures/tests/_fixtures/__init__.py index f5dfeef..e41bcc7 100644 --- a/lib/fixtures/tests/_fixtures/__init__.py +++ b/lib/fixtures/tests/_fixtures/__init__.py @@ -17,6 +17,7 @@ def load_tests(loader, standard_tests, pattern): test_modules = [ 'environ', 'monkeypatch', + 'packagepath', 'popen', 'pythonpackage', 'pythonpath', diff --git a/lib/fixtures/tests/_fixtures/test_packagepath.py b/lib/fixtures/tests/_fixtures/test_packagepath.py new file mode 100644 index 0000000..fd52194 --- /dev/null +++ b/lib/fixtures/tests/_fixtures/test_packagepath.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 ( + PackagePathEntry, + TempDir, + ) + + +class TestPackagePathEntry(testtools.TestCase): + + def test_adds_missing_to_end_package_path(self): + uniquedir = self.useFixture(TempDir()).path + fixture = PackagePathEntry('fixtures', uniquedir) + self.assertFalse(uniquedir in fixtures.__path__) + with fixture: + self.assertTrue(uniquedir in fixtures.__path__) + self.assertFalse(uniquedir in fixtures.__path__) + + def test_doesnt_alter_existing_entry(self): + existingdir = fixtures.__path__[0] + expectedlen = len(fixtures.__path__) + fixture = PackagePathEntry('fixtures', existingdir) + with fixture: + self.assertTrue(existingdir in fixtures.__path__) + self.assertEqual(expectedlen, len(fixtures.__path__)) + self.assertTrue(existingdir in fixtures.__path__) + self.assertEqual(expectedlen, len(fixtures.__path__)) |