Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | from mozpack.errors import ( |
michael@0 | 6 | errors, |
michael@0 | 7 | ErrorMessage, |
michael@0 | 8 | AccumulatedErrors, |
michael@0 | 9 | ) |
michael@0 | 10 | import unittest |
michael@0 | 11 | import mozunit |
michael@0 | 12 | import sys |
michael@0 | 13 | from cStringIO import StringIO |
michael@0 | 14 | |
michael@0 | 15 | |
michael@0 | 16 | class TestErrors(object): |
michael@0 | 17 | def setUp(self): |
michael@0 | 18 | errors.out = StringIO() |
michael@0 | 19 | errors.ignore_errors(False) |
michael@0 | 20 | |
michael@0 | 21 | def tearDown(self): |
michael@0 | 22 | errors.out = sys.stderr |
michael@0 | 23 | |
michael@0 | 24 | def get_output(self): |
michael@0 | 25 | return [l.strip() for l in errors.out.getvalue().splitlines()] |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | class TestErrorsImpl(TestErrors, unittest.TestCase): |
michael@0 | 29 | def test_plain_error(self): |
michael@0 | 30 | errors.warn('foo') |
michael@0 | 31 | self.assertRaises(ErrorMessage, errors.error, 'foo') |
michael@0 | 32 | self.assertRaises(ErrorMessage, errors.fatal, 'foo') |
michael@0 | 33 | self.assertEquals(self.get_output(), ['Warning: foo']) |
michael@0 | 34 | |
michael@0 | 35 | def test_ignore_errors(self): |
michael@0 | 36 | errors.ignore_errors() |
michael@0 | 37 | errors.warn('foo') |
michael@0 | 38 | errors.error('bar') |
michael@0 | 39 | self.assertRaises(ErrorMessage, errors.fatal, 'foo') |
michael@0 | 40 | self.assertEquals(self.get_output(), ['Warning: foo', 'Warning: bar']) |
michael@0 | 41 | |
michael@0 | 42 | def test_no_error(self): |
michael@0 | 43 | with errors.accumulate(): |
michael@0 | 44 | errors.warn('1') |
michael@0 | 45 | |
michael@0 | 46 | def test_simple_error(self): |
michael@0 | 47 | with self.assertRaises(AccumulatedErrors): |
michael@0 | 48 | with errors.accumulate(): |
michael@0 | 49 | errors.error('1') |
michael@0 | 50 | self.assertEquals(self.get_output(), ['Error: 1']) |
michael@0 | 51 | |
michael@0 | 52 | def test_error_loop(self): |
michael@0 | 53 | with self.assertRaises(AccumulatedErrors): |
michael@0 | 54 | with errors.accumulate(): |
michael@0 | 55 | for i in range(3): |
michael@0 | 56 | errors.error('%d' % i) |
michael@0 | 57 | self.assertEquals(self.get_output(), |
michael@0 | 58 | ['Error: 0', 'Error: 1', 'Error: 2']) |
michael@0 | 59 | |
michael@0 | 60 | def test_multiple_errors(self): |
michael@0 | 61 | with self.assertRaises(AccumulatedErrors): |
michael@0 | 62 | with errors.accumulate(): |
michael@0 | 63 | errors.error('foo') |
michael@0 | 64 | for i in range(3): |
michael@0 | 65 | if i == 2: |
michael@0 | 66 | errors.warn('%d' % i) |
michael@0 | 67 | else: |
michael@0 | 68 | errors.error('%d' % i) |
michael@0 | 69 | errors.error('bar') |
michael@0 | 70 | self.assertEquals(self.get_output(), |
michael@0 | 71 | ['Error: foo', 'Error: 0', 'Error: 1', |
michael@0 | 72 | 'Warning: 2', 'Error: bar']) |
michael@0 | 73 | |
michael@0 | 74 | def test_errors_context(self): |
michael@0 | 75 | with self.assertRaises(AccumulatedErrors): |
michael@0 | 76 | with errors.accumulate(): |
michael@0 | 77 | self.assertEqual(errors.get_context(), None) |
michael@0 | 78 | with errors.context('foo', 1): |
michael@0 | 79 | self.assertEqual(errors.get_context(), ('foo', 1)) |
michael@0 | 80 | errors.error('a') |
michael@0 | 81 | with errors.context('bar', 2): |
michael@0 | 82 | self.assertEqual(errors.get_context(), ('bar', 2)) |
michael@0 | 83 | errors.error('b') |
michael@0 | 84 | self.assertEqual(errors.get_context(), ('foo', 1)) |
michael@0 | 85 | errors.error('c') |
michael@0 | 86 | self.assertEqual(self.get_output(), [ |
michael@0 | 87 | 'Error: foo:1: a', |
michael@0 | 88 | 'Error: bar:2: b', |
michael@0 | 89 | 'Error: foo:1: c', |
michael@0 | 90 | ]) |
michael@0 | 91 | |
michael@0 | 92 | if __name__ == '__main__': |
michael@0 | 93 | mozunit.main() |