http://scons.tigris.org/issues/show_bug.cgi?id=2345
[scons.git] / src / engine / SCons / TaskmasterTests.py
index 932458179f0550f8859b16b2468b3401cb4c7f74..3dd22e3e9137f6bda3f93cfbe3f35e817fb8bbac 100644 (file)
@@ -23,6 +23,8 @@
 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
+import SCons.compat
+
 import copy
 import sys
 import unittest
@@ -69,6 +71,9 @@ class Node:
     def disambiguate(self):
         return self
 
+    def push_to_cache(self):
+        pass
+
     def retrieve_from_cache(self):
         global cache_text
         if self.cached:
@@ -87,7 +92,8 @@ class Node:
 
     def built(self):
         global built_text
-        built_text = built_text + " really"
+        if not self.cached:
+            built_text = built_text + " really"
 
     def has_builder(self):
         return not self.builder is None
@@ -166,8 +172,21 @@ class Node:
             class Executor:
                 def prepare(self):
                     pass
+                def get_action_targets(self):
+                    return self.targets
+                def get_all_targets(self):
+                    return self.targets
+                def get_all_children(self):
+                    result = []
+                    for node in self.targets:
+                        result.extend(node.children())
+                    return result
+                def get_all_prerequisites(self):
+                    return []
+                def get_action_side_effects(self):
+                    return []
             self.executor = Executor()
-        self.executor.targets = self.targets
+            self.executor.targets = self.targets
         return self.executor
 
 class OtherError(Exception):
@@ -190,7 +209,7 @@ class TaskmasterTestCase(unittest.TestCase):
         t.prepare()
         t.execute()
         t = tm.next_task()
-        assert t == None
+        assert t is None
 
         n1 = Node("n1")
         n2 = Node("n2")
@@ -219,7 +238,7 @@ class TaskmasterTestCase(unittest.TestCase):
         t.executed()
         t.postprocess()
 
-        assert tm.next_task() == None
+        assert tm.next_task() is None
 
         built_text = "up to date: "
         top_node = n3
@@ -264,7 +283,7 @@ class TaskmasterTestCase(unittest.TestCase):
         t.executed()
         t.postprocess()
 
-        assert tm.next_task() == None
+        assert tm.next_task() is None
 
 
         n1 = Node("n1")
@@ -299,13 +318,13 @@ class TaskmasterTestCase(unittest.TestCase):
         t5.executed()
         t5.postprocess()
 
-        assert tm.next_task() == None
+        assert tm.next_task() is None
 
 
         n4 = Node("n4")
         n4.set_state(SCons.Node.executed)
         tm = SCons.Taskmaster.Taskmaster([n4])
-        assert tm.next_task() == None
+        assert tm.next_task() is None
 
         n1 = Node("n1")
         n2 = Node("n2", [n1])
@@ -314,7 +333,7 @@ class TaskmasterTestCase(unittest.TestCase):
         t.executed()
         t.postprocess()
         t = tm.next_task()
-        assert tm.next_task() == None
+        assert tm.next_task() is None
 
 
         n1 = Node("n1")
@@ -336,7 +355,7 @@ class TaskmasterTestCase(unittest.TestCase):
         assert target == n3, target
         t.executed()
         t.postprocess()
-        assert tm.next_task() == None
+        assert tm.next_task() is None
 
         n1 = Node("n1")
         n2 = Node("n2")
@@ -362,14 +381,14 @@ class TaskmasterTestCase(unittest.TestCase):
         assert t.get_target() == n4
         t.executed()
         t.postprocess()
-        assert tm.next_task() == None
+        assert tm.next_task() is None
         assert scan_called == 4, scan_called
 
         tm = SCons.Taskmaster.Taskmaster([n5])
         t = tm.next_task()
         assert t.get_target() == n5, t.get_target()
         t.executed()
-        assert tm.next_task() == None
+        assert tm.next_task() is None
         assert scan_called == 5, scan_called
 
         n1 = Node("n1")
@@ -520,7 +539,7 @@ class TaskmasterTestCase(unittest.TestCase):
         """
         class MyTask(SCons.Taskmaster.Task):
             def make_ready(self):
-                raise MyException, "from make_ready()"
+                raise MyException("from make_ready()")
 
         n1 = Node("n1")
         tm = SCons.Taskmaster.Taskmaster(targets = [n1], tasker = MyTask)
@@ -596,7 +615,7 @@ class TaskmasterTestCase(unittest.TestCase):
         """
         class StopNode(Node):
             def children(self):
-                raise SCons.Errors.StopError, "stop!"
+                raise SCons.Errors.StopError("stop!")
         class ExitNode(Node):
             def children(self):
                 sys.exit(77)
@@ -643,7 +662,7 @@ class TaskmasterTestCase(unittest.TestCase):
         t = tm.next_task()
         assert t.targets == [n1], t.targets
         t.fail_stop()
-        assert t.targets == [n3], map(str, t.targets)
+        assert t.targets == [n3], list(map(str, t.targets))
         assert t.top == 1, t.top
 
     def test_stop(self):
@@ -752,7 +771,7 @@ class TaskmasterTestCase(unittest.TestCase):
         # set it up by having something that approximates a real Builder
         # return this list--but that's more work than is probably
         # warranted right now.
-        t.targets = [n1, n2]
+        n1.get_executor().targets = [n1, n2]
         t.prepare()
         assert n1.prepared
         assert n2.prepared
@@ -763,7 +782,7 @@ class TaskmasterTestCase(unittest.TestCase):
         t = tm.next_task()
         # More bogus reaching in and setting the targets.
         n3.set_state(SCons.Node.up_to_date)
-        t.targets = [n3, n4]
+        n3.get_executor().targets = [n3, n4]
         t.prepare()
         assert n3.prepared
         assert n4.prepared
@@ -803,7 +822,7 @@ class TaskmasterTestCase(unittest.TestCase):
         tm = SCons.Taskmaster.Taskmaster([n6, n7])
         t = tm.next_task()
         # More bogus reaching in and setting the targets.
-        t.targets = [n6, n7]
+        n6.get_executor().targets = [n6, n7]
         t.prepare()
         assert n6.prepared
         assert n7.prepared
@@ -814,10 +833,22 @@ class TaskmasterTestCase(unittest.TestCase):
         # Make sure we call an Executor's prepare() method.
         class ExceptionExecutor:
             def prepare(self):
-                raise Exception, "Executor.prepare() exception"
+                raise Exception("Executor.prepare() exception")
+            def get_all_targets(self):
+                return self.nodes
+            def get_all_children(self):
+                result = []
+                for node in self.nodes:
+                    result.extend(node.children())
+                return result
+            def get_all_prerequisites(self):
+                return []
+            def get_action_side_effects(self):
+                return []
 
         n11 = Node("n11")
         n11.executor = ExceptionExecutor()
+        n11.executor.nodes = [n11]
         tm = SCons.Taskmaster.Taskmaster([n11])
         t = tm.next_task()
         try:
@@ -825,7 +856,7 @@ class TaskmasterTestCase(unittest.TestCase):
         except Exception, e:
             assert str(e) == "Executor.prepare() exception", e
         else:
-            raise AssertionError, "did not catch expected exception"
+            raise AssertionError("did not catch expected exception")
 
     def test_execute(self):
         """Test executing a task
@@ -850,7 +881,7 @@ class TaskmasterTestCase(unittest.TestCase):
         except SCons.Errors.UserError:
             pass
         else:
-            raise TestFailed, "did not catch expected UserError"
+            raise TestFailed("did not catch expected UserError")
 
         def raise_BuildError():
             raise SCons.Errors.BuildError
@@ -863,7 +894,7 @@ class TaskmasterTestCase(unittest.TestCase):
         except SCons.Errors.BuildError:
             pass
         else:
-            raise TestFailed, "did not catch expected BuildError"
+            raise TestFailed("did not catch expected BuildError")
 
         # On a generic (non-BuildError) exception from a Builder,
         # the target should throw a BuildError exception with the
@@ -876,14 +907,14 @@ class TaskmasterTestCase(unittest.TestCase):
         t = tm.next_task()
         try:
             t.execute()
-        except SCons.Errors.TaskmasterException, e:
+        except SCons.Errors.BuildError, e:
             assert e.node == n4, e.node
-            assert e.errstr == "Exception", e.errstr
+            assert e.errstr == "OtherError : ", e.errstr
             assert len(e.exc_info) == 3, e.exc_info
             exc_traceback = sys.exc_info()[2]
-            assert type(e.exc_info[2]) == type(exc_traceback), e.exc_info[2]
+            assert isinstance(e.exc_info[2], type(exc_traceback)), e.exc_info[2]
         else:
-            raise TestFailed, "did not catch expected BuildError"
+            raise TestFailed("did not catch expected BuildError")
 
         built_text = None
         cache_text = []
@@ -1025,9 +1056,9 @@ class TaskmasterTestCase(unittest.TestCase):
     def test_trace(self):
         """Test Taskmaster tracing
         """
-        import StringIO
+        import io
 
-        trace = StringIO.StringIO()
+        trace = io.StringIO()
         n1 = Node("n1")
         n2 = Node("n2")
         n3 = Node("n3", [n1, n2])
@@ -1056,22 +1087,39 @@ Taskmaster: Looking for a node to evaluate
 Taskmaster:     Considering node <no_state   0   'n1'> and its children:
 Taskmaster: Evaluating <pending    0   'n1'>
 
+Task.make_ready_current(): node <pending    0   'n1'>
+Task.prepare():      node <executing  0   'n1'>
+Task.execute():      node <executing  0   'n1'>
+Task.postprocess():  node <executing  0   'n1'>
+
 Taskmaster: Looking for a node to evaluate
 Taskmaster:     Considering node <executed   0   'n1'> and its children:
 Taskmaster:        already handled (executed)
 Taskmaster:     Considering node <no_state   0   'n3'> and its children:
 Taskmaster:        <executed   0   'n1'>
 Taskmaster:        <no_state   0   'n2'>
-Taskmaster:      adjusting ref count: <pending    1   'n3'>
+Taskmaster:      adjusted ref count: <pending    1   'n3'>, child 'n2'
 Taskmaster:     Considering node <no_state   0   'n2'> and its children:
 Taskmaster: Evaluating <pending    0   'n2'>
 
+Task.make_ready_current(): node <pending    0   'n2'>
+Task.prepare():      node <executing  0   'n2'>
+Task.execute():      node <executing  0   'n2'>
+Task.postprocess():  node <executing  0   'n2'>
+Task.postprocess():  removing <executing  0   'n2'>
+Task.postprocess():  adjusted parent ref count <pending    0   'n3'>
+
 Taskmaster: Looking for a node to evaluate
 Taskmaster:     Considering node <pending    0   'n3'> and its children:
 Taskmaster:        <executed   0   'n1'>
 Taskmaster:        <executed   0   'n2'>
 Taskmaster: Evaluating <pending    0   'n3'>
 
+Task.make_ready_current(): node <pending    0   'n3'>
+Task.prepare():      node <executing  0   'n3'>
+Task.execute():      node <executing  0   'n3'>
+Task.postprocess():  node <executing  0   'n3'>
+
 Taskmaster: Looking for a node to evaluate
 Taskmaster: No candidate anymore.
 
@@ -1084,3 +1132,9 @@ if __name__ == "__main__":
     suite = unittest.makeSuite(TaskmasterTestCase, 'test_')
     if not unittest.TextTestRunner().run(suite).wasSuccessful():
         sys.exit(1)
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4: