bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4797.32.2
by John Arbash Meinel
merge 2.1, resolving NEWS conflict. |
1 |
# Copyright (C) 2009, 2010 Canonical Ltd
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
17 |
import re |
|
18 |
||
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
19 |
from ..cleanup import ( |
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
20 |
_do_with_cleanups, |
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
21 |
_run_cleanup, |
|
5158.8.2
by Martin Pool
Add simple test case for ObjectWithCleanups |
22 |
ObjectWithCleanups, |
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
23 |
OperationWithCleanups, |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
24 |
)
|
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
25 |
from .. import ( |
|
4634.85.5
by Andrew Bennetts
Add unit test for -Dcleanup behaviour. |
26 |
debug, |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
27 |
tests, |
|
4634.85.5
by Andrew Bennetts
Add unit test for -Dcleanup behaviour. |
28 |
)
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
29 |
|
30 |
||
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
31 |
class ErrorA(Exception): |
32 |
"""Sample exception type A.""" |
|
33 |
||
34 |
||
35 |
class ErrorB(Exception): |
|
36 |
"""Sample exception type B.""" |
|
37 |
||
38 |
||
39 |
class CleanupsTestCase(tests.TestCase): |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
40 |
|
41 |
def setUp(self): |
|
42 |
super(CleanupsTestCase, self).setUp() |
|
43 |
self.call_log = [] |
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
44 |
|
45 |
def no_op_cleanup(self): |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
46 |
self.call_log.append('no_op_cleanup') |
47 |
||
48 |
def assertLogContains(self, regex): |
|
|
4794.1.15
by Robert Collins
Review feedback. |
49 |
self.assertContainsRe(self.get_log(), regex, re.DOTALL) |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
50 |
|
51 |
def failing_cleanup(self): |
|
52 |
self.call_log.append('failing_cleanup') |
|
53 |
raise Exception("failing_cleanup goes boom!") |
|
54 |
||
55 |
||
56 |
class TestRunCleanup(CleanupsTestCase): |
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
57 |
|
58 |
def test_no_errors(self): |
|
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
59 |
"""The function passed to _run_cleanup is run.""" |
60 |
self.assertTrue(_run_cleanup(self.no_op_cleanup)) |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
61 |
self.assertEqual(['no_op_cleanup'], self.call_log) |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
62 |
|
|
4634.85.4
by Andrew Bennetts
Another test. |
63 |
def test_cleanup_with_args_kwargs(self): |
64 |
def func_taking_args_kwargs(*args, **kwargs): |
|
65 |
self.call_log.append(('func', args, kwargs)) |
|
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
66 |
_run_cleanup(func_taking_args_kwargs, 'an arg', kwarg='foo') |
|
4634.85.4
by Andrew Bennetts
Another test. |
67 |
self.assertEqual( |
68 |
[('func', ('an arg',), {'kwarg': 'foo'})], self.call_log) |
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
69 |
|
70 |
def test_cleanup_error(self): |
|
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
71 |
"""An error from the cleanup function is logged by _run_cleanup, but not |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
72 |
propagated.
|
73 |
||
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
74 |
This is there's no way for _run_cleanup to know if there's an existing
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
75 |
exception in this situation::
|
76 |
try:
|
|
77 |
some_func()
|
|
78 |
finally:
|
|
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
79 |
_run_cleanup(cleanup_func)
|
80 |
So, the best _run_cleanup can do is always log errors but never raise
|
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
81 |
them.
|
82 |
"""
|
|
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
83 |
self.assertFalse(_run_cleanup(self.failing_cleanup)) |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
84 |
self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom') |
85 |
||
|
4634.85.5
by Andrew Bennetts
Add unit test for -Dcleanup behaviour. |
86 |
def test_cleanup_error_debug_flag(self): |
|
4634.85.6
by Andrew Bennetts
More tests. |
87 |
"""The -Dcleanup debug flag causes cleanup errors to be reported to the |
88 |
user.
|
|
89 |
"""
|
|
|
4634.85.5
by Andrew Bennetts
Add unit test for -Dcleanup behaviour. |
90 |
debug.debug_flags.add('cleanup') |
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
91 |
self.assertFalse(_run_cleanup(self.failing_cleanup)) |
|
4634.85.5
by Andrew Bennetts
Add unit test for -Dcleanup behaviour. |
92 |
self.assertContainsRe( |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
93 |
self.get_log(), |
|
6681.2.5
by Jelmer Vernooij
Fix tests. |
94 |
"brz: warning: Cleanup failed:.*failing_cleanup goes boom") |
|
4634.85.5
by Andrew Bennetts
Add unit test for -Dcleanup behaviour. |
95 |
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
96 |
def test_prior_error_cleanup_succeeds(self): |
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
97 |
"""Calling _run_cleanup from a finally block will not interfere with an |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
98 |
exception from the try block.
|
99 |
"""
|
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
100 |
def failing_operation(): |
101 |
try: |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
102 |
1 / 0 |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
103 |
finally: |
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
104 |
_run_cleanup(self.no_op_cleanup) |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
105 |
self.assertRaises(ZeroDivisionError, failing_operation) |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
106 |
self.assertEqual(['no_op_cleanup'], self.call_log) |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
107 |
|
108 |
def test_prior_error_cleanup_fails(self): |
|
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
109 |
"""Calling _run_cleanup from a finally block will not interfere with an |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
110 |
exception from the try block even when the cleanup itself raises an
|
111 |
exception.
|
|
112 |
||
113 |
The cleanup exception will be logged.
|
|
114 |
"""
|
|
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
115 |
def failing_operation(): |
116 |
try: |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
117 |
1 / 0 |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
118 |
finally: |
|
4744.3.1
by Andrew Bennetts
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch. |
119 |
_run_cleanup(self.failing_cleanup) |
|
4634.85.1
by Andrew Bennetts
Begin defining cleanup helpers and their tests. |
120 |
self.assertRaises(ZeroDivisionError, failing_operation) |
121 |
self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom') |
|
122 |
||
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
123 |
|
124 |
class TestDoWithCleanups(CleanupsTestCase): |
|
125 |
||
126 |
def trivial_func(self): |
|
127 |
self.call_log.append('trivial_func') |
|
128 |
return 'trivial result' |
|
129 |
||
130 |
def test_runs_func(self): |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
131 |
"""_do_with_cleanups runs the function it is given, and returns the |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
132 |
result.
|
133 |
"""
|
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
134 |
result = _do_with_cleanups([], self.trivial_func) |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
135 |
self.assertEqual('trivial result', result) |
136 |
||
137 |
def test_runs_cleanups(self): |
|
138 |
"""Cleanup functions are run (in the given order).""" |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
139 |
cleanup_func_1 = (self.call_log.append, ('cleanup 1',), {}) |
140 |
cleanup_func_2 = (self.call_log.append, ('cleanup 2',), {}) |
|
141 |
_do_with_cleanups([cleanup_func_1, cleanup_func_2], self.trivial_func) |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
142 |
self.assertEqual( |
143 |
['trivial_func', 'cleanup 1', 'cleanup 2'], self.call_log) |
|
144 |
||
145 |
def failing_func(self): |
|
146 |
self.call_log.append('failing_func') |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
147 |
1 / 0 |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
148 |
|
149 |
def test_func_error_propagates(self): |
|
150 |
"""Errors from the main function are propagated (after running |
|
151 |
cleanups).
|
|
152 |
"""
|
|
153 |
self.assertRaises( |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
154 |
ZeroDivisionError, _do_with_cleanups, |
155 |
[(self.no_op_cleanup, (), {})], self.failing_func) |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
156 |
self.assertEqual(['failing_func', 'no_op_cleanup'], self.call_log) |
157 |
||
158 |
def test_func_error_trumps_cleanup_error(self): |
|
159 |
"""Errors from the main function a propagated even if a cleanup raises |
|
160 |
an error.
|
|
161 |
||
162 |
The cleanup error is be logged.
|
|
163 |
"""
|
|
164 |
self.assertRaises( |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
165 |
ZeroDivisionError, _do_with_cleanups, |
166 |
[(self.failing_cleanup, (), {})], self.failing_func) |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
167 |
self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom') |
168 |
||
169 |
def test_func_passes_and_error_from_cleanup(self): |
|
170 |
"""An error from a cleanup is propagated when the main function doesn't |
|
171 |
raise an error. Later cleanups are still executed.
|
|
172 |
"""
|
|
173 |
exc = self.assertRaises( |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
174 |
Exception, _do_with_cleanups, |
175 |
[(self.failing_cleanup, (), {}), (self.no_op_cleanup, (), {})], |
|
176 |
self.trivial_func) |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
177 |
self.assertEqual('failing_cleanup goes boom!', exc.args[0]) |
178 |
self.assertEqual( |
|
179 |
['trivial_func', 'failing_cleanup', 'no_op_cleanup'], |
|
180 |
self.call_log) |
|
181 |
||
182 |
def test_multiple_cleanup_failures(self): |
|
183 |
"""When multiple cleanups fail (as tends to happen when something has |
|
184 |
gone wrong), the first error is propagated, and subsequent errors are
|
|
185 |
logged.
|
|
186 |
"""
|
|
|
4634.85.6
by Andrew Bennetts
More tests. |
187 |
cleanups = self.make_two_failing_cleanup_funcs() |
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
188 |
self.assertRaises(ErrorA, _do_with_cleanups, cleanups, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
189 |
self.trivial_func) |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
190 |
self.assertLogContains('Cleanup failed:.*ErrorB') |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
191 |
# Error A may appear in the log (with Python 3 exception chaining), but
|
192 |
# Error B should be the last error recorded.
|
|
193 |
self.assertContainsRe( |
|
194 |
self.get_log(), |
|
195 |
'Traceback \\(most recent call last\\):\n( .*\n)+' |
|
|
6779
by Jelmer Vernooij
Merge lp:~gz/brz/py3_test_cleanup |
196 |
'.*ErrorB: Error B\n$') |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
197 |
|
|
4634.85.6
by Andrew Bennetts
More tests. |
198 |
def make_two_failing_cleanup_funcs(self): |
199 |
def raise_a(): |
|
200 |
raise ErrorA('Error A') |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
201 |
|
|
4634.85.6
by Andrew Bennetts
More tests. |
202 |
def raise_b(): |
203 |
raise ErrorB('Error B') |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
204 |
return [(raise_a, (), {}), (raise_b, (), {})] |
|
4634.85.6
by Andrew Bennetts
More tests. |
205 |
|
206 |
def test_multiple_cleanup_failures_debug_flag(self): |
|
207 |
debug.debug_flags.add('cleanup') |
|
208 |
cleanups = self.make_two_failing_cleanup_funcs() |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
209 |
self.assertRaises(ErrorA, _do_with_cleanups, cleanups, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
210 |
self.trivial_func) |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
211 |
trace_value = self.get_log() |
|
4634.85.6
by Andrew Bennetts
More tests. |
212 |
self.assertContainsRe( |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
213 |
trace_value, "brz: warning: Cleanup failed:.*Error B\n") |
214 |
self.assertEqual(1, trace_value.count('brz: warning:')) |
|
|
4634.85.6
by Andrew Bennetts
More tests. |
215 |
|
216 |
def test_func_and_cleanup_errors_debug_flag(self): |
|
217 |
debug.debug_flags.add('cleanup') |
|
218 |
cleanups = self.make_two_failing_cleanup_funcs() |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
219 |
self.assertRaises(ZeroDivisionError, _do_with_cleanups, cleanups, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
220 |
self.failing_func) |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
221 |
trace_value = self.get_log() |
222 |
self.assertContainsRe( |
|
223 |
trace_value, "brz: warning: Cleanup failed:.*Error A\n") |
|
224 |
self.assertContainsRe( |
|
225 |
trace_value, "brz: warning: Cleanup failed:.*Error B\n") |
|
226 |
self.assertEqual(2, trace_value.count('brz: warning:')) |
|
|
4634.85.6
by Andrew Bennetts
More tests. |
227 |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
228 |
def test_func_may_mutate_cleanups(self): |
229 |
"""The main func may mutate the cleanups before it returns. |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
230 |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
231 |
This allows a function to gradually add cleanups as it acquires
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
232 |
resources, rather than planning all the cleanups up-front. The
|
233 |
OperationWithCleanups helper relies on this working.
|
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
234 |
"""
|
235 |
cleanups_list = [] |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
236 |
|
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
237 |
def func_that_adds_cleanups(): |
238 |
self.call_log.append('func_that_adds_cleanups') |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
239 |
cleanups_list.append((self.no_op_cleanup, (), {})) |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
240 |
return 'result' |
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
241 |
result = _do_with_cleanups(cleanups_list, func_that_adds_cleanups) |
|
4634.85.3
by Andrew Bennetts
Lots more tests. |
242 |
self.assertEqual('result', result) |
243 |
self.assertEqual( |
|
244 |
['func_that_adds_cleanups', 'no_op_cleanup'], self.call_log) |
|
|
4634.85.6
by Andrew Bennetts
More tests. |
245 |
|
246 |
def test_cleanup_error_debug_flag(self): |
|
247 |
"""The -Dcleanup debug flag causes cleanup errors to be reported to the |
|
248 |
user.
|
|
249 |
"""
|
|
250 |
debug.debug_flags.add('cleanup') |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
251 |
self.assertRaises(ZeroDivisionError, _do_with_cleanups, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
252 |
[(self.failing_cleanup, (), {})], self.failing_func) |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
253 |
trace_value = self.get_log() |
|
4634.85.6
by Andrew Bennetts
More tests. |
254 |
self.assertContainsRe( |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
255 |
trace_value, |
|
6681.2.5
by Jelmer Vernooij
Fix tests. |
256 |
"brz: warning: Cleanup failed:.*failing_cleanup goes boom") |
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
257 |
self.assertEqual(1, trace_value.count('brz: warning:')) |
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
258 |
|
259 |
||
260 |
class TestOperationWithCleanups(CleanupsTestCase): |
|
261 |
||
262 |
def test_cleanup_ordering(self): |
|
263 |
"""Cleanups are added in LIFO order. |
|
264 |
||
265 |
So cleanups added before run is called are run last, and the last
|
|
266 |
cleanup added during the func is run first.
|
|
267 |
"""
|
|
268 |
call_log = [] |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
269 |
|
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
270 |
def func(op, foo): |
271 |
call_log.append(('func called', foo)) |
|
272 |
op.add_cleanup(call_log.append, 'cleanup 2') |
|
273 |
op.add_cleanup(call_log.append, 'cleanup 1') |
|
274 |
return 'result' |
|
275 |
owc = OperationWithCleanups(func) |
|
276 |
owc.add_cleanup(call_log.append, 'cleanup 4') |
|
277 |
owc.add_cleanup(call_log.append, 'cleanup 3') |
|
278 |
result = owc.run('foo') |
|
279 |
self.assertEqual('result', result) |
|
280 |
self.assertEqual( |
|
281 |
[('func called', 'foo'), 'cleanup 1', 'cleanup 2', 'cleanup 3', |
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
282 |
'cleanup 4'], call_log) |
|
4744.3.4
by Andrew Bennetts
Make OperationWithCleanups the only public API in bzrlib.cleanup, add test for it, add support for *args and **kwargs for func and for cleanups, use deque.appendleft rather than list.insert(0, ...). |
283 |
|
|
5158.8.2
by Martin Pool
Add simple test case for ObjectWithCleanups |
284 |
|
285 |
class SampleWithCleanups(ObjectWithCleanups): |
|
|
6770.3.1
by Martin
Fix test_cleanup on Python 3 |
286 |
"""Minimal ObjectWithCleanups subclass.""" |
287 |
||
288 |
||
289 |
class TestObjectWithCleanups(tests.TestCase): |
|
|
5158.8.2
by Martin Pool
Add simple test case for ObjectWithCleanups |
290 |
|
291 |
def test_object_with_cleanups(self): |
|
292 |
a = [] |
|
293 |
s = SampleWithCleanups() |
|
294 |
s.add_cleanup(a.append, 42) |
|
295 |
s.cleanup_now() |
|
296 |
self.assertEqual(a, [42]) |