summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/TaskmasterTests.py
blob: a15a673880e7bb1c5aa525e09e6cf7c14f657c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#
# Copyright (c) 2001 Steven Knight
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import sys
import unittest

import SCons.Taskmaster



built = None
executed = None

class Node:
    def __init__(self, name, kids = []):
        self.name = name
        self.kids = kids
        self.builder = Node.build
        self.signature = None
        self.state = None
        self.parents = []

        for kid in kids:
            kid.parents.append(self)

    def build(self):
        global built
        built = self.name + " built"

    def children(self):
	return self.kids
  
    def get_parents(self):
        return self.parents

    def get_state(self):
        return self.state

    def set_state(self, state):
        self.state = state

    def set_signature(self, sig):
        self.signature = sig
  
    def children_are_executed(self):
        return reduce(lambda x,y: ((y.get_state() == SCons.Node.executed
                                   or y.get_state() == SCons.Node.up_to_date)
                                   and x),
                      self.children(),
                      1)



#class Task(unittest.TestCase):
#    def test_execute(self):
#        pass
#
#    def test_get_target(self):
#        pass
#
#    def test_set_sig(self):
#        pass
#
#    def test_set_state(self):
#        pass
#
#    def test_up_to_date(self):
#        pass
#
#    def test_executed(self):
#        pass
#
#    def test_failed(self):
#        pass
#
#    def test_fail_stop(self):
#        pass
#
#    def test_fail_continue(self):
#        pass

    

class Task:
    def __init__(self, target):
        self.target = target

    def get_target(self):
        return self.target

    def up_to_date(self):
        pass

    def executed(self):
        pass

    def failed(self):
        pass


class TaskmasterTestCase(unittest.TestCase):

    def test_next_task(self):
	"""Test fetching the next task
	"""
	global built
        
	n1 = Node("n1")
        tm = SCons.Taskmaster.Taskmaster([n1, n1])
        t = tm.next_task()
        t.executed()
        t = tm.next_task()
        assert t == None

        n1 = Node("n1")
        n2 = Node("n2")
        n3 = Node("n3", [n1, n2])
        
	tm = SCons.Taskmaster.Taskmaster([n3])

        t = tm.next_task()
        t.execute()
        assert built == "n1 built"
        t.executed()

        t = tm.next_task()
        t.execute()
        assert built == "n2 built"
        t.executed()

        t = tm.next_task()
        t.execute()
        assert built == "n3 built"
        t.executed()

        assert tm.next_task() == None

	built = "up to date: "

        global top_node
        top_node = n3
        class MyTask(SCons.Taskmaster.Task):
            def up_to_date(self):
                if self.target == top_node:
                    assert self.top
                global built
                built = built + " " + self.target.name
                SCons.Taskmaster.Task.up_to_date(self)

        class MyCalc(SCons.Taskmaster.Calc):
            def current(self, node, sig):
                return 1

        n1.set_state(None)
        n2.set_state(None)
        n3.set_state(None)
        tm = SCons.Taskmaster.Taskmaster(targets = [n3],
                                         tasker = MyTask, calc = MyCalc())
	assert tm.next_task() == None
	assert built == "up to date:  n1 n2 n3"


        n1 = Node("n1")
	n2 = Node("n2")
        n3 = Node("n3", [n1, n2])
        n4 = Node("n4")
        n5 = Node("n5", [n3, n4])
        tm = SCons.Taskmaster.Taskmaster([n5])

        assert not tm.is_blocked()
        
        t1 = tm.next_task()
        assert t1.get_target() == n1
        assert not tm.is_blocked()
        
        t2 = tm.next_task()
        assert t2.get_target() == n2
        assert not tm.is_blocked()

        t4 = tm.next_task()
        assert t4.get_target() == n4
        assert tm.is_blocked()
        t4.executed()
        assert tm.is_blocked()
        
        t1.executed()
        assert tm.is_blocked()
        t2.executed()
        assert not tm.is_blocked()
        t3 = tm.next_task()
        assert t3.get_target() == n3
        assert tm.is_blocked()

        t3.executed()
        assert not tm.is_blocked()
        t5 = tm.next_task()
        assert t5.get_target() == n5
        assert not tm.is_blocked()

        assert tm.next_task() == None

        
        n4 = Node("n4")
        n4.set_state(SCons.Node.executed)
        tm = SCons.Taskmaster.Taskmaster([n4])
        assert tm.next_task() == None
        
    def test_is_blocked(self):
        """Test whether a task is blocked

	Both default and overridden in a subclass.
	"""
	tm = SCons.Taskmaster.Taskmaster()
	assert not tm.is_blocked()

	class MyTM(SCons.Taskmaster.Taskmaster):
	    def is_blocked(self):
	        return 1
	tm = MyTM()
	assert tm.is_blocked() == 1

    def test_stop(self):
        """Test the stop() method

        Both default and overridden in a subclass.
        """
        global built

        n1 = Node("n1")
        n2 = Node("n2")
        n3 = Node("n3", [n1, n2])
        
        tm = SCons.Taskmaster.Taskmaster([n3])
        t = tm.next_task()
        t.execute()
        assert built == "n1 built"
        t.executed()

        tm.stop()
        assert tm.next_task() is None

        class MyTM(SCons.Taskmaster.Taskmaster):
            def stop(self):
                global built
                built = "MyTM.stop()"
                SCons.Taskmaster.Taskmaster.stop(self)

        n1 = Node("n1")
        n2 = Node("n2")
        n3 = Node("n3", [n1, n2])

        built = None
        tm = MyTM([n3])
        tm.next_task().execute()
        assert built == "n1 built"

        tm.stop()
        assert built == "MyTM.stop()"
        assert tm.next_task() is None

    #def test_add_pending(self):
    #    passs
    #
    #def test_remove_pending(self):
    #    passs



if __name__ == "__main__":
    suite = unittest.makeSuite(TaskmasterTestCase, 'test_')
    if not unittest.TextTestRunner().run(suite).wasSuccessful():
	sys.exit(1)