| Index: recipe_engine/unittests/util_test.py
|
| diff --git a/recipe_engine/unittests/util_test.py b/recipe_engine/unittests/util_test.py
|
| new file mode 100755
|
| index 0000000000000000000000000000000000000000..fa31fbae8a06c56750453ffc16e5ff0556c6d044
|
| --- /dev/null
|
| +++ b/recipe_engine/unittests/util_test.py
|
| @@ -0,0 +1,85 @@
|
| +#!/usr/bin/env python
|
| +# Copyright 2015 The LUCI Authors. All rights reserved.
|
| +# Use of this source code is governed under the Apache License, Version 2.0
|
| +# that can be found in the LICENSE file.
|
| +
|
| +import unittest
|
| +
|
| +import test_env
|
| +
|
| +from recipe_engine import util
|
| +
|
| +
|
| +class TestMultiException(unittest.TestCase):
|
| +
|
| + def testNoExceptionsRaisesNothing(self):
|
| + mexc = util.MultiException()
|
| + with mexc.catch():
|
| + pass
|
| + mexc.raise_if_any()
|
| + self.assertEqual(str(mexc), 'MultiException(No exceptions)')
|
| +
|
| + def testExceptionsRaised(self):
|
| + fail_exc = Exception('fail!')
|
| + mexc = util.MultiException()
|
| + with mexc.catch():
|
| + raise fail_exc
|
| + self.assertRaises(util.MultiException, mexc.raise_if_any)
|
| + self.assertEqual(len(mexc), 1)
|
| + self.assertIs(mexc[0], fail_exc)
|
| + self.assertEqual(str(mexc), 'MultiException(fail!)')
|
| +
|
| + def testMultipleExceptions(self):
|
| + mexc = util.MultiException()
|
| + with mexc.catch():
|
| + raise KeyError('One')
|
| + with mexc.catch():
|
| + raise ValueError('Two')
|
| + self.assertEqual(len(mexc), 2)
|
| +
|
| + exceptions = list(mexc)
|
| + self.assertIsInstance(exceptions[0], KeyError)
|
| + self.assertIsInstance(exceptions[1], ValueError)
|
| + self.assertEqual(str(mexc), "MultiException('One', and 1 more...)")
|
| +
|
| + def testTargetedException(self):
|
| + mexc = util.MultiException()
|
| + def not_caught():
|
| + with mexc.catch(ValueError):
|
| + raise KeyError('One')
|
| + self.assertRaises(KeyError, not_caught)
|
| + self.assertFalse(mexc)
|
| +
|
| +
|
| +class TestDeferExceptionsFor(unittest.TestCase):
|
| +
|
| + def testNoExceptionsDoesNothing(self):
|
| + v = []
|
| + util.defer_exceptions_for([1, 2, 3], lambda e: v.append(e))
|
| + self.assertEqual(v, [1, 2, 3])
|
| +
|
| + def testCatchesExceptions(self):
|
| + v = []
|
| + def fn(e):
|
| + if e == 0:
|
| + raise ValueError('Zero')
|
| + v.append(e)
|
| +
|
| + mexc = None
|
| + try:
|
| + util.defer_exceptions_for([0, 1, 0, 0, 2, 0, 3, 0], fn)
|
| + except util.MultiException as e:
|
| + mexc = e
|
| +
|
| + self.assertEqual(v, [1, 2, 3])
|
| + self.assertIsNotNone(mexc)
|
| + self.assertEqual(len(mexc), 5)
|
| +
|
| + def testCatchesSpecificExceptions(self):
|
| + def fn(e):
|
| + raise ValueError('Zero')
|
| + self.assertRaises(ValueError, util.defer_exceptions_for, [1], fn, KeyError)
|
| +
|
| +
|
| +if __name__ == '__main__':
|
| + unittest.main()
|
|
|