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
26 class ExampleTree(object):
32 self
.got
.append("foo")
35 self
.got
.append("bar")
37 def baz_unpunned_name(self
, log
):
38 self
.got
.append("baz")
40 @action_tree.action_node
44 ("baz", self
.baz_unpunned_name
)]
47 self
.got
.append("qux")
50 self
.got
.append("quux")
52 @action_tree.action_node
54 return [self
.qux
, self
.quux
]
56 @action_tree.action_node
58 return [self
.subtree1
, self
.subtree2
]
61 class TreeWithFailure(object):
63 def failer(self
, log
):
64 raise Exception("lose")
66 @action_tree.action_node
68 return [self
.leaf1
, self
.leaf2
]
76 @action_tree.action_node
78 return [self
.failer
, self
.subtree
]
81 class SimpleLog(object):
83 def __init__(self
, name
="top"):
91 def child_log(self
, name
, do_start
=True):
92 child
= SimpleLog(name
)
93 self
._sublogs
.append(child
)
96 def finish(self
, 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)
111 # This is a bit friendlier than unittest's assertEquals() when
112 # printing non-matching multi-line strings.
113 def assert_equals(x
, y
):
115 raise AssertionError('"%s" != "%s"' % (x
, y
))
119 stream
= StringIO
.StringIO()
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()
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"])
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(), """\
180 stream
= StringIO
.StringIO()
181 action_tree
.action_main(tree
, ["-f", "subtree2"], stdout
=stream
)
182 assert_equals(stream
.getvalue(), """\
189 def test_logging(self
):
190 tree
= ExampleTree().all_steps
192 action_tree
.action_main(tree
, ["0"], log
=log
)
193 assert_equals(iostring(log
.format
), """\
204 def test_logging_enrol_up_front(self
):
205 tree
= TreeWithFailure().all_steps
208 action_tree
.action_main(tree
, ["0"], log
=log
)
211 assert_equals(iostring(log
.format
), """\
220 if __name__
== "__main__":