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__"
46 def __call__(self, target, source, env):
47 global built_it, built_target, built_source, built_args, built_order
52 built_order = built_order + 1
53 self.order = built_order
56 def get_actions(self):
59 class MyNonGlobalAction:
63 self.built_target = None
64 self.built_source = None
66 def __call__(self, target, source, env):
67 # Okay, so not ENTIRELY non-global...
70 self.built_target = target
71 self.built_source = source
73 built_order = built_order + 1
74 self.order = built_order
77 def get_actions(self):
81 def Dictionary(self, *args):
83 def Override(self, overrides):
88 self.env = Environment()
90 self.action = MyAction()
93 def get_actions(self):
95 def get_contents(self, target, source, env):
98 class NoneBuilder(Builder):
99 def execute(self, target, source, env):
100 Builder.execute(self, target, source, env)
103 class ListBuilder(Builder):
104 def __init__(self, *nodes):
105 Builder.__init__(self)
107 def execute(self, target, source, env):
108 if hasattr(self, 'status'):
112 target = self.nodes[0]
113 self.status = Builder.execute(self, target, source, env)
116 def execute(self, target, source, env):
120 def execute(self, target, source, env):
121 raise SCons.Errors.BuildError
123 class ExceptBuilder2:
124 def execute(self, target, source, env):
129 def __call__(self, node):
131 return node.found_includes
133 class MyNode(SCons.Node.Node):
134 """The base Node class contains a number of do-nothing methods that
135 we expect to be overridden by real, functional Node subclasses. So
136 simulate a real, functional Node subclass.
138 def __init__(self, name):
139 SCons.Node.Node.__init__(self)
141 self.found_includes = []
144 def get_found_includes(self, env, scanner, target):
149 class NodeTestCase(unittest.TestCase):
151 def test_build(self):
152 """Test building a node
154 global built_it, built_order
156 # Make sure it doesn't blow up if no builder is set.
159 assert built_it == None
162 node.builder_set(Builder())
163 node.env_set(Environment())
165 node.sources = ["yyy", "zzz"]
168 assert built_target == [node], built_target
169 assert built_source == ["yyy", "zzz"], built_source
173 node.builder_set(NoneBuilder())
174 node.env_set(Environment())
176 node.sources = ["rrr", "sss"]
177 node.builder.overrides = { "foo" : 1, "bar" : 2 }
180 assert built_target == [node], built_target
181 assert built_source == ["rrr", "sss"], built_source
182 assert built_args["foo"] == 1, built_args
183 assert built_args["bar"] == 2, built_args
187 lb = ListBuilder(fff, ggg)
195 fff.sources = ["hhh", "iii"]
196 ggg.sources = ["hhh", "iii"]
197 # [Charles C. 1/7/2002] Uhhh, why are there no asserts here?
198 # [SK, 15 May 2003] I dunno, let's add some...
202 assert built_target == [fff], built_target
203 assert built_source == ["hhh", "iii"], built_source
207 assert built_target == [ggg], built_target
208 assert built_source == ["hhh", "iii"], built_source
214 # NOTE: No env_set()! We should pull the environment from the builder.
215 b.env = Environment()
216 b.overrides = { "on" : 3, "off" : 4 }
220 assert built_target[0] == jjj, built_target[0]
221 assert built_source == [], built_source
222 assert built_args["on"] == 3, built_args
223 assert built_args["off"] == 4, built_args
228 node.builder_set(Builder())
229 node.env_set(Environment())
230 node.sources = ["yyy", "zzz"]
231 pre1 = MyNonGlobalAction()
232 pre2 = MyNonGlobalAction()
233 post1 = MyNonGlobalAction()
234 post2 = MyNonGlobalAction()
235 node.add_pre_action(pre1)
236 node.add_pre_action(pre2)
237 node.add_post_action(post1)
238 node.add_post_action(post2)
243 assert post1.built_it
244 assert post2.built_it
245 assert pre1.order == 1, pre1.order
246 assert pre2.order == 2, pre1.order
247 # The action of the builder itself is order 3...
248 assert post1.order == 4, pre1.order
249 assert post2.order == 5, pre1.order
251 for act in [ pre1, pre2, post1, post2 ]:
252 assert type(act.built_target[0]) == type(MyNode("bar")), type(act.built_target[0])
253 assert str(act.built_target[0]) == "xxx", str(act.built_target[0])
254 assert act.built_source == ["yyy", "zzz"], act.built_source
256 def test_visited(self):
257 """Test the base visited() method
259 Just make sure it's there and we can call it.
261 n = SCons.Node.Node()
264 def test_depends_on(self):
265 """Test the depends_on() method
267 parent = SCons.Node.Node()
268 child = SCons.Node.Node()
269 parent.add_dependency([child])
270 assert parent.depends_on([child])
272 def test_builder_set(self):
273 """Test setting a Node's Builder
275 node = SCons.Node.Node()
278 assert node.builder == b
280 def test_has_builder(self):
281 """Test the has_builder() method
283 n1 = SCons.Node.Node()
284 assert n1.has_builder() == 0
285 n1.builder_set(Builder())
286 assert n1.has_builder() == 1
288 def test_is_derived(self):
289 """Test the is_derived() method
291 n1 = SCons.Node.Node()
292 n2 = SCons.Node.Node()
293 n3 = SCons.Node.Node()
295 n2.builder_set(Builder())
298 assert n1.is_derived() == 0
299 assert n2.is_derived() == 1
300 assert n3.is_derived() == 1
302 def test_alter_targets(self):
303 """Test the alter_targets() method
305 n = SCons.Node.Node()
306 t, m = n.alter_targets()
310 def test_current(self):
311 """Test the default current() method
313 node = SCons.Node.Node()
314 assert node.current() is None
316 def test_env_set(self):
317 """Test setting a Node's Environment
319 node = SCons.Node.Node()
324 def test_get_actions(self):
325 """Test fetching a Node's action list
327 node = SCons.Node.Node()
328 node.builder_set(Builder())
329 a = node.builder.get_actions()
330 assert isinstance(a[0], MyAction), a[0]
332 def test_set_bsig(self):
333 """Test setting a Node's signature
335 node = SCons.Node.Node()
337 assert node.bsig == 'www'
339 def test_get_bsig(self):
340 """Test fetching a Node's signature
342 node = SCons.Node.Node()
344 assert node.get_bsig() == 'xxx'
346 def test_set_csig(self):
347 """Test setting a Node's signature
349 node = SCons.Node.Node()
351 assert node.csig == 'yyy'
353 def test_get_csig(self):
354 """Test fetching a Node's signature
356 node = SCons.Node.Node()
358 assert node.get_csig() == 'zzz'
360 def test_store_bsig(self):
361 """Test calling the method to store a build signature
363 node = SCons.Node.Node()
366 def test_store_csig(self):
367 """Test calling the method to store a content signature
369 node = SCons.Node.Node()
372 def test_get_timestamp(self):
373 """Test calling the method to fetch a Node's timestamp
375 node = SCons.Node.Node()
376 assert node.get_timestamp() == 0
378 def test_store_timestamp(self):
379 """Test calling the method to store a timestamp
381 node = SCons.Node.Node()
382 node.store_timestamp()
384 def test_set_always_build(self):
385 """Test setting a Node's always_build value
387 node = SCons.Node.Node()
388 node.set_always_build()
389 assert node.always_build
390 node.set_always_build(3)
391 assert node.always_build == 3
393 def test_set_precious(self):
394 """Test setting a Node's precious value
396 node = SCons.Node.Node()
400 assert node.precious == 7
402 def test_exists(self):
403 """Test evaluating whether a Node exists.
405 node = SCons.Node.Node()
409 def test_exists(self):
410 """Test evaluating whether a Node exists locally or in a repository.
412 node = SCons.Node.Node()
416 class MyNode(SCons.Node.Node):
424 def test_prepare(self):
425 """Test preparing a node to be built
427 node = SCons.Node.Node()
429 n1 = SCons.Node.Node()
430 n1.builder_set(Builder())
432 node.implicit_dict = {}
433 node._add_child(node.implicit, node.implicit_dict, [n1])
435 node.prepare() # should not throw an exception
437 n2 = SCons.Node.Node()
440 node.implicit_dict = {}
441 node._add_child(node.implicit, node.implicit_dict, [n2])
443 node.prepare() # should not throw an exception
445 n3 = SCons.Node.Node()
447 node.implicit_dict = {}
448 node._add_child(node.implicit, node.implicit_dict, [n3])
450 node.prepare() # should not throw an exception
452 class MyNode(SCons.Node.Node):
457 node.implicit_dict = {}
458 node._add_child(node.implicit, node.implicit_dict, [n4])
462 except SCons.Errors.StopError:
464 assert exc_caught, "did not catch expected StopError"
466 def test_add_dependency(self):
467 """Test adding dependencies to a Node's list.
469 node = SCons.Node.Node()
470 assert node.depends == []
472 zero = SCons.Node.Node()
474 node.add_dependency(zero)
480 one = SCons.Node.Node()
481 two = SCons.Node.Node()
482 three = SCons.Node.Node()
483 four = SCons.Node.Node()
485 node.add_dependency([one])
486 assert node.depends == [one]
487 node.add_dependency([two, three])
488 assert node.depends == [one, two, three]
489 node.add_dependency([three, four, one])
490 assert node.depends == [one, two, three, four]
492 assert zero.get_parents() == []
493 assert one.get_parents() == [node]
494 assert two.get_parents() == [node]
495 assert three.get_parents() == [node]
496 assert four.get_parents() == [node]
499 def test_add_source(self):
500 """Test adding sources to a Node's list.
502 node = SCons.Node.Node()
503 assert node.sources == []
505 zero = SCons.Node.Node()
507 node.add_source(zero)
513 one = SCons.Node.Node()
514 two = SCons.Node.Node()
515 three = SCons.Node.Node()
516 four = SCons.Node.Node()
518 node.add_source([one])
519 assert node.sources == [one]
520 node.add_source([two, three])
521 assert node.sources == [one, two, three]
522 node.add_source([three, four, one])
523 assert node.sources == [one, two, three, four]
525 assert zero.get_parents() == []
526 assert one.get_parents() == [node]
527 assert two.get_parents() == [node]
528 assert three.get_parents() == [node]
529 assert four.get_parents() == [node]
531 def test_add_ignore(self):
532 """Test adding files whose dependencies should be ignored.
534 node = SCons.Node.Node()
535 assert node.ignore == []
537 zero = SCons.Node.Node()
539 node.add_ignore(zero)
545 one = SCons.Node.Node()
546 two = SCons.Node.Node()
547 three = SCons.Node.Node()
548 four = SCons.Node.Node()
550 node.add_ignore([one])
551 assert node.ignore == [one]
552 node.add_ignore([two, three])
553 assert node.ignore == [one, two, three]
554 node.add_ignore([three, four, one])
555 assert node.ignore == [one, two, three, four]
557 assert zero.get_parents() == []
558 assert one.get_parents() == [node]
559 assert two.get_parents() == [node]
560 assert three.get_parents() == [node]
561 assert four.get_parents() == [node]
563 def test_get_found_includes(self):
564 """Test the default get_found_includes() method
566 node = SCons.Node.Node()
567 target = SCons.Node.Node()
569 deps = node.get_found_includes(e, None, target)
570 assert deps == [], deps
572 def test_get_implicit_deps(self):
573 """Test get_implicit_deps()
576 target = MyNode("ttt")
579 # No scanner at all returns []
580 deps = node.get_implicit_deps(env, None, target)
581 assert deps == [], deps
585 node.found_includes = [d]
587 # Simple return of the found includes
588 deps = node.get_implicit_deps(env, s, target)
589 assert deps == [d], deps
591 # No "recursive" attribute on scanner doesn't recurse
593 d.found_includes = [e]
594 deps = node.get_implicit_deps(env, s, target)
595 assert deps == [d], map(str, deps)
597 # Explicit "recursive" attribute on scanner doesn't recurse
599 deps = node.get_implicit_deps(env, s, target)
600 assert deps == [d], map(str, deps)
602 # Explicit "recursive" attribute on scanner which does recurse
604 deps = node.get_implicit_deps(env, s, target)
605 assert deps == [d, e], map(str, deps)
607 # Recursive scanning eliminates duplicates
609 d.found_includes = [e, f]
610 e.found_includes = [f]
611 deps = node.get_implicit_deps(env, s, target)
612 assert deps == [d, e, f], map(str, deps)
615 """Test Scanner functionality
618 node.builder = Builder()
619 node.env_set(Environment())
623 node.found_includes = [d]
625 assert node.target_scanner == None, node.target_scanner
626 node.target_scanner = s
627 assert node.implicit is None
631 assert node.implicit == [d], node.implicit
633 def test_scanner_key(self):
634 """Test that a scanner_key() method exists"""
635 assert SCons.Node.Node().scanner_key() == None
637 def test_children(self):
638 """Test fetching the non-ignored "children" of a Node.
640 node = SCons.Node.Node()
641 n1 = SCons.Node.Node()
642 n2 = SCons.Node.Node()
643 n3 = SCons.Node.Node()
644 n4 = SCons.Node.Node()
645 n5 = SCons.Node.Node()
646 n6 = SCons.Node.Node()
647 n7 = SCons.Node.Node()
648 n8 = SCons.Node.Node()
649 n9 = SCons.Node.Node()
650 n10 = SCons.Node.Node()
651 n11 = SCons.Node.Node()
652 n12 = SCons.Node.Node()
654 node.add_source([n1, n2, n3])
655 node.add_dependency([n4, n5, n6])
657 node.implicit_dict = {}
658 node._add_child(node.implicit, node.implicit_dict, [n7, n8, n9])
659 node._add_child(node.implicit, node.implicit_dict, [n10, n11, n12])
660 node.add_ignore([n2, n5, n8, n11])
662 kids = node.children()
663 for kid in [n1, n3, n4, n6, n7, n9, n10, n12]:
664 assert kid in kids, kid
665 for kid in [n2, n5, n8, n11]:
666 assert not kid in kids, kid
668 def test_all_children(self):
669 """Test fetching all the "children" of a Node.
671 node = SCons.Node.Node()
672 n1 = SCons.Node.Node()
673 n2 = SCons.Node.Node()
674 n3 = SCons.Node.Node()
675 n4 = SCons.Node.Node()
676 n5 = SCons.Node.Node()
677 n6 = SCons.Node.Node()
678 n7 = SCons.Node.Node()
679 n8 = SCons.Node.Node()
680 n9 = SCons.Node.Node()
681 n10 = SCons.Node.Node()
682 n11 = SCons.Node.Node()
683 n12 = SCons.Node.Node()
685 node.add_source([n1, n2, n3])
686 node.add_dependency([n4, n5, n6])
688 node.implicit_dict = {}
689 node._add_child(node.implicit, node.implicit_dict, [n7, n8, n9])
690 node._add_child(node.implicit, node.implicit_dict, [n10, n11, n12])
691 node.add_ignore([n2, n5, n8, n11])
693 kids = node.all_children()
694 for kid in [n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12]:
695 assert kid in kids, kid
697 def test_state(self):
698 """Test setting and getting the state of a node
700 node = SCons.Node.Node()
701 assert node.get_state() == None
702 node.set_state(SCons.Node.executing)
703 assert node.get_state() == SCons.Node.executing
704 assert SCons.Node.pending < SCons.Node.executing
705 assert SCons.Node.executing < SCons.Node.up_to_date
706 assert SCons.Node.up_to_date < SCons.Node.executed
707 assert SCons.Node.executed < SCons.Node.failed
709 def test_walker(self):
710 """Test walking a Node tree.
715 nw = SCons.Node.Walker(n1)
716 assert not nw.is_done()
717 assert nw.next().name == "n1"
719 assert nw.next() == None
723 n1.add_source([n2, n3])
725 nw = SCons.Node.Walker(n1)
727 assert n.name == "n2", n.name
729 assert n.name == "n3", n.name
731 assert n.name == "n1", n.name
739 n2.add_source([n4, n5])
740 n3.add_dependency([n6, n7])
742 nw = SCons.Node.Walker(n1)
743 assert nw.next().name == "n4"
744 assert nw.next().name == "n5"
745 assert nw.history.has_key(n2)
746 assert nw.next().name == "n2"
747 assert nw.next().name == "n6"
748 assert nw.next().name == "n7"
749 assert nw.history.has_key(n3)
750 assert nw.next().name == "n3"
751 assert nw.history.has_key(n1)
752 assert nw.next().name == "n1"
753 assert nw.next() == None
756 n8.add_dependency([n3])
757 n7.add_dependency([n8])
759 def cycle(node, stack):
760 global cycle_detected
763 global cycle_detected
765 nw = SCons.Node.Walker(n3, cycle_func = cycle)
767 assert n.name == "n6", n.name
769 assert n.name == "n8", n.name
770 assert cycle_detected
771 cycle_detected = None
773 assert n.name == "n7", n.name
775 assert nw.next() == None
778 """Test the rstr() method."""
780 assert n1.rstr() == 'n1', n1.rstr()
782 def test_abspath(self):
783 """Test the get_abspath() method."""
785 assert n.get_abspath() == str(n), n.get_abspath()
787 def test_for_signature(self):
788 """Test the for_signature() method."""
790 assert n.for_signature() == str(n), n.get_abspath()
792 def test_get_string(self):
793 """Test the get_string() method."""
794 class TestNode(MyNode):
795 def __init__(self, name, sig):
796 MyNode.__init__(self, name)
799 def for_signature(self):
802 n = TestNode("foo", "bar")
803 assert n.get_string(0) == "foo", n.get_string(0)
804 assert n.get_string(1) == "bar", n.get_string(1)
806 def test_arg2nodes(self):
807 """Test the arg2nodes function."""
809 class X(SCons.Node.Node):
811 def Factory(name, directory = None, create = 1, dict=dict, X=X):
812 if not dict.has_key(name):
814 dict[name].name = name
817 nodes = SCons.Node.arg2nodes("Util.py UtilTests.py", Factory)
818 assert len(nodes) == 1, nodes
819 assert isinstance(nodes[0], X)
820 assert nodes[0].name == "Util.py UtilTests.py"
822 if hasattr(types, 'UnicodeType'):
824 nodes = SCons.Node.arg2nodes(u"Util.py UtilTests.py", Factory)
825 assert len(nodes) == 1, nodes
826 assert isinstance(nodes[0], X)
827 assert nodes[0].name == u"Util.py UtilTests.py"
829 exec code in globals(), locals()
831 nodes = SCons.Node.arg2nodes(["Util.py", "UtilTests.py"], Factory)
832 assert len(nodes) == 2, nodes
833 assert isinstance(nodes[0], X)
834 assert isinstance(nodes[1], X)
835 assert nodes[0].name == "Util.py"
836 assert nodes[1].name == "UtilTests.py"
838 n1 = Factory("Util.py")
839 nodes = SCons.Node.arg2nodes([n1, "UtilTests.py"], Factory)
840 assert len(nodes) == 2, nodes
841 assert isinstance(nodes[0], X)
842 assert isinstance(nodes[1], X)
843 assert nodes[0].name == "Util.py"
844 assert nodes[1].name == "UtilTests.py"
846 class SConsNode(SCons.Node.Node):
848 nodes = SCons.Node.arg2nodes(SConsNode())
849 assert len(nodes) == 1, nodes
850 assert isinstance(nodes[0], SConsNode), node
854 nodes = SCons.Node.arg2nodes(OtherNode())
855 assert len(nodes) == 1, nodes
856 assert isinstance(nodes[0], OtherNode), node
858 def lookup_a(str, F=Factory):
866 def lookup_b(str, F=Factory):
874 SCons.Node.arg2nodes_lookups.append(lookup_a)
875 SCons.Node.arg2nodes_lookups.append(lookup_b)
877 nodes = SCons.Node.arg2nodes(['aaa', 'bbb', 'ccc'], Factory)
878 assert len(nodes) == 3, nodes
880 assert nodes[0].name == 'aaa', nodes[0]
881 assert nodes[0].a == 1, nodes[0]
882 assert not hasattr(nodes[0], 'b'), nodes[0]
884 assert nodes[1].name == 'bbb'
885 assert not hasattr(nodes[1], 'a'), nodes[1]
886 assert nodes[1].b == 1, nodes[1]
888 assert nodes[2].name == 'ccc'
889 assert not hasattr(nodes[2], 'a'), nodes[1]
890 assert not hasattr(nodes[2], 'b'), nodes[1]
892 def lookup_bbbb(str, F=Factory):
900 def lookup_c(str, F=Factory):
908 nodes = SCons.Node.arg2nodes(['bbbb', 'ccc'], Factory,
909 [lookup_c, lookup_bbbb, lookup_b])
910 assert len(nodes) == 2, nodes
912 assert nodes[0].name == 'bbbb'
913 assert not hasattr(nodes[0], 'a'), nodes[1]
914 assert not hasattr(nodes[0], 'b'), nodes[1]
915 assert nodes[0].bbbb == 1, nodes[1]
916 assert not hasattr(nodes[0], 'c'), nodes[0]
918 assert nodes[1].name == 'ccc'
919 assert not hasattr(nodes[1], 'a'), nodes[1]
920 assert not hasattr(nodes[1], 'b'), nodes[1]
921 assert not hasattr(nodes[1], 'bbbb'), nodes[0]
922 assert nodes[1].c == 1, nodes[1]
924 def test_literal(self):
925 """Test the is_literal() function."""
927 assert n.is_literal()
929 def test_Annotate(self):
930 """Test using an interface-specific Annotate function."""
931 def my_annotate(node, self=self):
932 node.annotation = self.node_string
934 save_Annotate = SCons.Node.Annotate
935 SCons.Node.Annotate = my_annotate
938 self.node_string = '#1'
939 n = SCons.Node.Node()
940 assert n.annotation == '#1', n.annotation
942 self.node_string = '#2'
943 n = SCons.Node.Node()
944 assert n.annotation == '#2', n.annotation
946 SCons.Node.Annotate = save_Annotate
948 def test_clear(self):
949 """Test clearing all cached state information."""
950 n = SCons.Node.Node()
955 n.includes = 'testincludes'
956 n.found_include = {'testkey':'testvalue'}
957 n.implicit = 'testimplicit'
961 assert n.get_state() is None, n.get_state()
962 assert not hasattr(n, 'bsig'), n.bsig
963 assert not hasattr(n, 'csig'), n.csig
964 assert n.includes is None, n.includes
965 assert n.found_includes == {}, n.found_includes
966 assert n.implicit is None, n.implicit
968 def test_get_subst_proxy(self):
969 """Test the get_subst_proxy method."""
972 assert n.get_subst_proxy() == n, n.get_subst_proxy()
975 if __name__ == "__main__":
976 suite = unittest.makeSuite(NodeTestCase, 'test_')
977 if not unittest.TextTestRunner().run(suite).wasSuccessful():