diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/testresources/__init__.py | 20 | ||||
-rw-r--r-- | lib/testresources/tests/test_test_resource.py | 49 |
2 files changed, 67 insertions, 2 deletions
diff --git a/lib/testresources/__init__.py b/lib/testresources/__init__.py index 820d62b..e8c0685 100644 --- a/lib/testresources/__init__.py +++ b/lib/testresources/__init__.py @@ -246,11 +246,29 @@ class TestResource(object): """ if self._uses == 0: self._setResource(self.make_all()) - elif self._dirty: + elif self.isDirty(): self._resetResource(self._currentResource) self._uses += 1 return self._currentResource + def isDirty(self): + """Return True if this managers cached resource is dirty. + + Calling when the resource is not currently held has undefined + behaviour. + """ + if self._dirty: + return True + for name, mgr in self.resources: + if mgr.isDirty(): + return True + res = mgr.getResource() + try: + if res is not getattr(self._currentResource, name): + return True + finally: + mgr.finishedWith(res) + def make_all(self): """Make the dependencies of this resource and this resource.""" dependency_resources = {} diff --git a/lib/testresources/tests/test_test_resource.py b/lib/testresources/tests/test_test_resource.py index 9c33d86..a11c987 100644 --- a/lib/testresources/tests/test_test_resource.py +++ b/lib/testresources/tests/test_test_resource.py @@ -29,6 +29,18 @@ def test_suite(): return result +class MockResourceInstance(object): + + def __init__(self, name): + self._name = name + + def __cmp__(self, other): + return cmp(self.__dict__, other.__dict__) + + def __repr__(self): + return self._name + + class MockResource(testresources.TestResource): """Mock resource that logs the number of make and clean calls.""" @@ -42,7 +54,7 @@ class MockResource(testresources.TestResource): def make(self, dependency_resources): self.makes += 1 - return "Boo!" + return MockResourceInstance("Boo!") class TestTestResource(testtools.TestCase): @@ -130,6 +142,41 @@ class TestTestResource(testtools.TestCase): resource_manager.getResource() self.assertEqual(1, resource_manager.makes) + def testIsDirty(self): + resource_manager = MockResource() + r = resource_manager.getResource() + resource_manager.dirtied(r) + self.assertTrue(resource_manager.isDirty()) + resource_manager.finishedWith(r) + + def testIsDirtyIsTrueIfDependenciesChanged(self): + resource_manager = MockResource() + dep1 = MockResource() + dep2 = MockResource() + dep3 = MockResource() + resource_manager.resources.append(("dep1", dep1)) + resource_manager.resources.append(("dep2", dep2)) + resource_manager.resources.append(("dep3", dep3)) + r = resource_manager.getResource() + dep2.dirtied(r.dep2) + r2 =dep2.getResource() + self.assertTrue(resource_manager.isDirty()) + resource_manager.finishedWith(r) + dep2.finishedWith(r2) + + def testIsDirtyIsTrueIfDependenciesAreDirty(self): + resource_manager = MockResource() + dep1 = MockResource() + dep2 = MockResource() + dep3 = MockResource() + resource_manager.resources.append(("dep1", dep1)) + resource_manager.resources.append(("dep2", dep2)) + resource_manager.resources.append(("dep3", dep3)) + r = resource_manager.getResource() + dep2.dirtied(r.dep2) + self.assertTrue(resource_manager.isDirty()) + resource_manager.finishedWith(r) + def testRepeatedGetResourceCallsMakeResourceOnceOnly(self): resource_manager = MockResource() resource_manager.getResource() |