2 # Copyright (c) 2001 Steven Knight
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
41 def execute(self, **kw):
42 global built_it, built_target, built_source
44 built_target = kw['target']
45 built_source = kw['source']
47 def get_contents(self, env, dir):
50 class NoneBuilder(Builder):
51 def execute(self, **kw):
52 apply(Builder.execute, (self,), kw)
56 def execute(self, **kw):
60 def execute(self, **kw):
61 raise SCons.Errors.BuildError
64 def execute(self, **kw):
68 def Dictionary(self, *args):
73 class NodeTestCase(unittest.TestCase):
75 def test_BuildException(self):
76 """Test throwing an exception on build failure.
78 node = SCons.Node.Node()
79 node.builder_set(FailBuilder())
80 node.env_set(Environment())
83 except SCons.Errors.BuildError:
86 raise TestFailed, "did not catch expected BuildError"
88 node = SCons.Node.Node()
89 node.builder_set(ExceptBuilder())
90 node.env_set(Environment())
93 except SCons.Errors.BuildError:
96 raise TestFailed, "did not catch expected BuildError"
98 node = SCons.Node.Node()
99 node.builder_set(ExceptBuilder2())
100 node.env_set(Environment())
103 except SCons.Errors.BuildError, e:
104 # On a generic (non-BuildError) exception from a Builder,
105 # the Node should throw a BuildError exception with
106 # the args set to the exception value, type, and traceback.
107 assert len(e.args) == 3, `e.args`
108 assert e.args[0] == 'foo', e.args[0]
109 assert e.args[1] is None
110 assert type(e.args[2]) is type(sys.exc_traceback), e.args[2]
112 raise TestFailed, "did not catch expected BuildError"
114 def test_build(self):
115 """Test building a node
119 class MyNode(SCons.Node.Node):
122 # Make sure it doesn't blow up if no builder is set.
125 assert built_it == None
128 node.builder_set(Builder())
129 node.env_set(Environment())
131 node.sources = ["yyy", "zzz"]
134 assert type(built_target) == type(MyNode()), type(built_target)
135 assert str(built_target) == "xxx", str(built_target)
136 assert built_source == ["yyy", "zzz"], built_source
140 node.builder_set(NoneBuilder())
141 node.env_set(Environment())
143 node.sources = ["rrr", "sss"]
146 assert type(built_target) == type(MyNode()), type(built_target)
147 assert str(built_target) == "qqq", str(built_target)
148 assert built_source == ["rrr", "sss"], built_source
150 def test_builder_set(self):
151 """Test setting a Node's Builder
153 node = SCons.Node.Node()
156 assert node.builder == b
158 def test_builder_sig_adapter(self):
159 """Test the node's adapter for builder signatures
161 node = SCons.Node.Node()
162 node.builder_set(Builder())
163 node.env_set(Environment())
164 c = node.builder_sig_adapter().get_contents()
167 def test_current(self):
168 """Test the default current() method
170 node = SCons.Node.Node()
171 assert node.current() is None
173 def test_env_set(self):
174 """Test setting a Node's Environment
176 node = SCons.Node.Node()
181 def test_set_bsig(self):
182 """Test setting a Node's signature
184 node = SCons.Node.Node()
186 assert node.bsig == 'www'
188 def test_get_bsig(self):
189 """Test fetching a Node's signature
191 node = SCons.Node.Node()
193 assert node.get_bsig() == 'xxx'
195 def test_set_csig(self):
196 """Test setting a Node's signature
198 node = SCons.Node.Node()
200 assert node.csig == 'yyy'
202 def test_get_csig(self):
203 """Test fetching a Node's signature
205 node = SCons.Node.Node()
207 assert node.get_csig() == 'zzz'
209 def test_set_precious(self):
210 """Test setting a Node's precious value
212 node = SCons.Node.Node()
216 assert node.precious == 7
218 def test_add_dependency(self):
219 """Test adding dependencies to a Node's list.
221 node = SCons.Node.Node()
222 assert node.depends == []
224 zero = SCons.Node.Node()
226 node.add_dependency(zero)
232 one = SCons.Node.Node()
233 two = SCons.Node.Node()
234 three = SCons.Node.Node()
235 four = SCons.Node.Node()
237 node.add_dependency([one])
238 assert node.depends == [one]
239 node.add_dependency([two, three])
240 assert node.depends == [one, two, three]
241 node.add_dependency([three, four, one])
242 assert node.depends == [one, two, three, four]
244 assert zero.get_parents() == []
245 assert one.get_parents() == [node]
246 assert two.get_parents() == [node]
247 assert three.get_parents() == [node]
248 assert four.get_parents() == [node]
251 def test_add_source(self):
252 """Test adding sources to a Node's list.
254 node = SCons.Node.Node()
255 assert node.sources == []
257 zero = SCons.Node.Node()
259 node.add_source(zero)
265 one = SCons.Node.Node()
266 two = SCons.Node.Node()
267 three = SCons.Node.Node()
268 four = SCons.Node.Node()
270 node.add_source([one])
271 assert node.sources == [one]
272 node.add_source([two, three])
273 assert node.sources == [one, two, three]
274 node.add_source([three, four, one])
275 assert node.sources == [one, two, three, four]
277 assert zero.get_parents() == []
278 assert one.get_parents() == [node]
279 assert two.get_parents() == [node]
280 assert three.get_parents() == [node]
281 assert four.get_parents() == [node]
283 def test_add_implicit(self):
284 """Test adding implicit (scanned) dependencies to a Node's list.
286 node = SCons.Node.Node()
287 assert node.implicit == {}
289 zero = SCons.Node.Node()
291 node.add_source(zero)
297 one = SCons.Node.Node()
298 two = SCons.Node.Node()
299 three = SCons.Node.Node()
300 four = SCons.Node.Node()
302 node.add_implicit([one], 1)
303 assert node.implicit[1] == [one]
304 node.add_implicit([two, three], 1)
305 assert node.implicit[1] == [one, two, three]
306 node.add_implicit([three, four, one], 1)
307 assert node.implicit[1] == [one, two, three, four]
309 assert zero.get_parents() == []
310 assert one.get_parents() == [node]
311 assert two.get_parents() == [node]
312 assert three.get_parents() == [node]
313 assert four.get_parents() == [node]
315 node.add_implicit([one], 2)
316 node.add_implicit([two, three], 3)
317 node.add_implicit([three, four, one], 4)
319 assert node.implicit[1] == [one, two, three, four]
320 assert node.implicit[2] == [one]
321 assert node.implicit[3] == [two, three]
322 assert node.implicit[4] == [three, four, one]
324 def test_add_ignore(self):
325 """Test adding files whose dependencies should be ignored.
327 node = SCons.Node.Node()
328 assert node.ignore == []
330 zero = SCons.Node.Node()
332 node.add_ignore(zero)
338 one = SCons.Node.Node()
339 two = SCons.Node.Node()
340 three = SCons.Node.Node()
341 four = SCons.Node.Node()
343 node.add_ignore([one])
344 assert node.ignore == [one]
345 node.add_ignore([two, three])
346 assert node.ignore == [one, two, three]
347 node.add_ignore([three, four, one])
348 assert node.ignore == [one, two, three, four]
350 assert zero.get_parents() == []
351 assert one.get_parents() == [node]
352 assert two.get_parents() == [node]
353 assert three.get_parents() == [node]
354 assert four.get_parents() == [node]
357 """Test Scanner functionality"""
361 node = SCons.Node.Node()
362 assert node.scanners == [], node.scanners
364 assert node.scanners == [ ds ], node.scanners
366 assert node.scanned[ds] == 1, node.scanned
368 def test_children(self):
369 """Test fetching the "children" of a Node.
371 node = SCons.Node.Node()
372 one = SCons.Node.Node()
373 two = SCons.Node.Node()
374 three = SCons.Node.Node()
375 four = SCons.Node.Node()
376 five = SCons.Node.Node()
377 six = SCons.Node.Node()
379 node.add_source([one, two, three])
380 node.add_dependency([four, five, six])
381 kids = node.children()
382 assert len(kids) == 6
390 def test_add_parent(self):
391 """Test adding parents to a Node."""
392 node = SCons.Node.Node()
393 parent = SCons.Node.Node()
394 node._add_parent(parent)
395 assert node.get_parents() == [parent]
396 node._add_parent(parent)
397 assert node.get_parents() == [parent]
399 def test_state(self):
400 """Test setting and getting the state of a node
402 node = SCons.Node.Node()
403 assert node.get_state() == None
404 node.set_state(SCons.Node.executing)
405 assert node.get_state() == SCons.Node.executing
406 assert SCons.Node.pending < SCons.Node.executing
407 assert SCons.Node.executing < SCons.Node.up_to_date
408 assert SCons.Node.up_to_date < SCons.Node.executed
409 assert SCons.Node.executed < SCons.Node.failed
411 def test_walker(self):
412 """Test walking a Node tree.
415 class MyNode(SCons.Node.Node):
416 def __init__(self, name):
417 SCons.Node.Node.__init__(self)
422 nw = SCons.Node.Walker(n1)
423 assert not nw.is_done()
424 assert nw.next().name == "n1"
426 assert nw.next() == None
430 n1.add_source([n2, n3])
432 nw = SCons.Node.Walker(n1)
433 assert nw.next().name == "n2"
434 assert nw.next().name == "n3"
435 assert nw.next().name == "n1"
436 assert nw.next() == None
442 n2.add_source([n4, n5])
443 n3.add_dependency([n6, n7])
445 nw = SCons.Node.Walker(n1)
446 assert nw.next().name == "n4"
447 assert nw.next().name == "n5"
448 assert nw.history.has_key(n2)
449 assert nw.next().name == "n2"
450 assert nw.next().name == "n6"
451 assert nw.next().name == "n7"
452 assert nw.history.has_key(n3)
453 assert nw.next().name == "n3"
454 assert nw.history.has_key(n1)
455 assert nw.next().name == "n1"
456 assert nw.next() == None
459 n8.add_dependency([n3])
460 n7.add_dependency([n8])
462 def cycle(node, stack):
463 global cycle_detected
466 global cycle_detected
468 nw = SCons.Node.Walker(n3, cycle_func = cycle)
470 assert n.name == "n6", n.name
472 assert n.name == "n8", n.name
473 assert cycle_detected
474 cycle_detected = None
476 assert n.name == "n7", n.name
478 assert nw.next() == None
480 def test_children_are_executed(self):
481 n1 = SCons.Node.Node()
482 n2 = SCons.Node.Node()
483 n3 = SCons.Node.Node()
484 n4 = SCons.Node.Node()
487 n3.add_source([n1, n2])
489 assert not n4.children_are_executed()
490 assert not n3.children_are_executed()
491 assert n2.children_are_executed()
492 assert n1.children_are_executed()
494 n1.set_state(SCons.Node.executed)
495 assert not n4.children_are_executed()
496 assert not n3.children_are_executed()
497 assert n2.children_are_executed()
498 assert n1.children_are_executed()
500 n2.set_state(SCons.Node.executed)
501 assert not n4.children_are_executed()
502 assert n3.children_are_executed()
503 assert n2.children_are_executed()
504 assert n1.children_are_executed()
506 n3.set_state(SCons.Node.executed)
507 assert n4.children_are_executed()
508 assert n3.children_are_executed()
509 assert n2.children_are_executed()
510 assert n1.children_are_executed()
512 def test_rescan(self):
513 """Test that built nodes are rescanned."""
517 class TestNode(SCons.Node.Node):
519 for scn in self.scanners:
520 if not self.scanned.has_key(scn):
523 self.add_implicit([ n ], scn)
524 self.scanned[scn] = 1
526 tn.builder_set(Builder())
527 tn.env_set(Environment())
531 map(lambda x: x.scan(), tn.depends)
532 assert tn.scanned[ds]
533 assert len(tn.implicit[ds]) == 1, tn.implicit
535 assert len(tn.implicit[ds]) == 2, tn.implicit
536 for dep in tn.implicit[ds]:
537 assert dep.scanned[ds] == 1
539 if __name__ == "__main__":
540 suite = unittest.makeSuite(NodeTestCase, 'test_')
541 if not unittest.TextTestRunner().run(suite).wasSuccessful():