diff options
author | Robert Collins <robertc@robertcollins.net> | 2009-06-17 20:23:42 +1000 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2009-06-17 20:23:42 +1000 |
commit | 67d75808760811b276f72539aa3a635f64570208 (patch) | |
tree | 11fa3c4c3619e467f9d864f425b6b14cc14c2996 | |
parent | 3d7ddb2e1db611df34b28ba9c34953ac4d084c32 (diff) | |
download | testresources-67d75808760811b276f72539aa3a635f64570208.tar.gz |
A dirty or changed dependency of a resource makes the resource dirty too, fixing bug 324202.
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | lib/testresources/__init__.py | 20 | ||||
-rw-r--r-- | lib/testresources/tests/test_test_resource.py | 49 |
3 files changed, 70 insertions, 2 deletions
@@ -29,6 +29,9 @@ IN DEVELOPMENT * All resources are dropped when a test with no declared resources is run. (James Henstridge) + * A dirty or changed dependency of a resource makes the resource dirty too. + (Robert Collins, #324202) + API CHANGES: * adsorbSuite is now deprecated in favour of addTest. addTest now flattens 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() |