Add toolchain_test.py to naclbuild.py
[nacl-build.git] / action_tree_test.py
blob99da11b8ea946280b8c670d6d8e084c19f7c4fd2
2 # Copyright (C) 2008 Mark Seaborn
4 # chroot_build is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as
6 # published by the Free Software Foundation; either version 2.1 of the
7 # License, or (at your option) any later version.
9 # chroot_build is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with chroot_build; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
19 import StringIO
20 import unittest
22 import action_tree
23 import build_log
26 class ExampleTree(object):
28 def __init__(self):
29 self.got = []
31 def foo(self, log):
32 self.got.append("foo")
34 def bar(self, log):
35 self.got.append("bar")
37 def baz_unpunned_name(self, log):
38 self.got.append("baz")
40 @action_tree.action_node
41 def subtree1(self):
42 return [self.foo,
43 self.bar,
44 ("baz", self.baz_unpunned_name)]
46 def qux(self, log):
47 self.got.append("qux")
49 def quux(self, log):
50 self.got.append("quux")
52 @action_tree.action_node
53 def subtree2(self):
54 return [self.qux, self.quux]
56 @action_tree.action_node
57 def all_steps(self):
58 return [self.subtree1, self.subtree2]
61 class TreeWithFailure(object):
63 def failer(self, log):
64 raise Exception("lose")
66 @action_tree.action_node
67 def subtree(self):
68 return [self.leaf1, self.leaf2]
70 def leaf1(self, log):
71 pass
73 def leaf2(self, log):
74 pass
76 @action_tree.action_node
77 def all_steps(self):
78 return [self.failer, self.subtree]
81 class SimpleLog(object):
83 def __init__(self, name="top"):
84 self._name = name
85 self._sublogs = []
86 self._result = None
88 def start(self):
89 pass
91 def child_log(self, name, do_start=True):
92 child = SimpleLog(name)
93 self._sublogs.append(child)
94 return child
96 def finish(self, result):
97 self._result = result
99 def format(self, stream, indent=0):
100 stream.write("%s%s [%s]\n" % (" " * indent, self._name, self._result))
101 for sublog in self._sublogs:
102 sublog.format(stream, indent=indent + 2)
105 def pop_all(lst):
106 copy = lst[:]
107 lst[:] = []
108 return copy
111 # This is a bit friendlier than unittest's assertEquals() when
112 # printing non-matching multi-line strings.
113 def assert_equals(x, y):
114 if x != y:
115 raise AssertionError('"%s" != "%s"' % (x, y))
118 def iostring(func):
119 stream = StringIO.StringIO()
120 func(stream)
121 return stream.getvalue()
124 class ActionTreeTest(unittest.TestCase):
126 def test_running(self):
127 example = ExampleTree()
128 example.all_steps(build_log.DummyLogWriter())
129 self.assertEquals(example.got, ["foo", "bar", "baz", "qux", "quux"])
131 def test_running_subtrees(self):
132 example = ExampleTree()
133 # By number
134 action_tree.action_main(example.all_steps, ["0"])
135 self.assertEquals(pop_all(example.got),
136 ["foo", "bar", "baz", "qux", "quux"])
137 action_tree.action_main(example.all_steps, ["1"])
138 self.assertEquals(pop_all(example.got), ["foo", "bar", "baz"])
139 action_tree.action_main(example.all_steps, ["2"])
140 self.assertEquals(pop_all(example.got), ["foo"])
141 action_tree.action_main(example.all_steps, ["3"])
142 self.assertEquals(pop_all(example.got), ["bar"])
143 action_tree.action_main(example.all_steps, ["-t", "3"])
144 self.assertEquals(pop_all(example.got), ["bar", "baz", "qux", "quux"])
145 action_tree.action_main(example.all_steps, ["-t", "5"])
146 self.assertEquals(pop_all(example.got), ["qux", "quux"])
147 # By name
148 action_tree.action_main(example.all_steps, ["subtree1"])
149 self.assertEquals(pop_all(example.got), ["foo", "bar", "baz"])
150 action_tree.action_main(example.all_steps, ["foo"])
151 self.assertEquals(pop_all(example.got), ["foo"])
152 action_tree.action_main(example.all_steps, ["bar"])
153 self.assertEquals(pop_all(example.got), ["bar"])
155 action_tree.action_main(example.all_steps, ["-t", "bar"])
156 self.assertEquals(pop_all(example.got), ["bar", "baz", "qux", "quux"])
157 action_tree.action_main(example.all_steps, ["-t", "subtree2"])
158 self.assertEquals(pop_all(example.got), ["qux", "quux"])
160 action_tree.action_main(example.all_steps, ["-f", "subtree1", "0"])
161 self.assertEquals(pop_all(example.got), ["foo", "bar", "baz"])
162 action_tree.action_main(example.all_steps, ["-f", "-bar", "0"])
163 self.assertEquals(pop_all(example.got), ["foo", "baz", "qux", "quux"])
165 def test_formatting(self):
166 tree = ExampleTree().all_steps
167 stream = StringIO.StringIO()
168 action_tree.action_main(tree, [], stdout=stream)
169 assert_equals(stream.getvalue(), """\
170 0: all_steps
171 1: subtree1
172 2: foo
173 3: bar
174 4: baz
175 5: subtree2
176 6: qux
177 7: quux
178 """)
180 stream = StringIO.StringIO()
181 action_tree.action_main(tree, ["-f", "subtree2"], stdout=stream)
182 assert_equals(stream.getvalue(), """\
183 0: all_steps
184 1: subtree2
185 2: qux
186 3: quux
187 """)
189 def test_logging(self):
190 tree = ExampleTree().all_steps
191 log = SimpleLog()
192 action_tree.action_main(tree, ["0"], log=log)
193 assert_equals(iostring(log.format), """\
194 top [None]
195 subtree1 [0]
196 foo [0]
197 bar [0]
198 baz [0]
199 subtree2 [0]
200 qux [0]
201 quux [0]
202 """)
204 def test_logging_enrol_up_front(self):
205 tree = TreeWithFailure().all_steps
206 log = SimpleLog()
207 try:
208 action_tree.action_main(tree, ["0"], log=log)
209 except:
210 pass
211 assert_equals(iostring(log.format), """\
212 top [None]
213 failer [1]
214 subtree [None]
215 leaf1 [None]
216 leaf2 [None]
217 """)
220 if __name__ == "__main__":
221 unittest.main()