python/mozbuild/mozpack/test/test_errors.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/python/mozbuild/mozpack/test/test_errors.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +from mozpack.errors import (
     1.9 +    errors,
    1.10 +    ErrorMessage,
    1.11 +    AccumulatedErrors,
    1.12 +)
    1.13 +import unittest
    1.14 +import mozunit
    1.15 +import sys
    1.16 +from cStringIO import StringIO
    1.17 +
    1.18 +
    1.19 +class TestErrors(object):
    1.20 +    def setUp(self):
    1.21 +        errors.out = StringIO()
    1.22 +        errors.ignore_errors(False)
    1.23 +
    1.24 +    def tearDown(self):
    1.25 +        errors.out = sys.stderr
    1.26 +
    1.27 +    def get_output(self):
    1.28 +        return [l.strip() for l in errors.out.getvalue().splitlines()]
    1.29 +
    1.30 +
    1.31 +class TestErrorsImpl(TestErrors, unittest.TestCase):
    1.32 +    def test_plain_error(self):
    1.33 +        errors.warn('foo')
    1.34 +        self.assertRaises(ErrorMessage, errors.error, 'foo')
    1.35 +        self.assertRaises(ErrorMessage, errors.fatal, 'foo')
    1.36 +        self.assertEquals(self.get_output(), ['Warning: foo'])
    1.37 +
    1.38 +    def test_ignore_errors(self):
    1.39 +        errors.ignore_errors()
    1.40 +        errors.warn('foo')
    1.41 +        errors.error('bar')
    1.42 +        self.assertRaises(ErrorMessage, errors.fatal, 'foo')
    1.43 +        self.assertEquals(self.get_output(), ['Warning: foo', 'Warning: bar'])
    1.44 +
    1.45 +    def test_no_error(self):
    1.46 +        with errors.accumulate():
    1.47 +            errors.warn('1')
    1.48 +
    1.49 +    def test_simple_error(self):
    1.50 +        with self.assertRaises(AccumulatedErrors):
    1.51 +            with errors.accumulate():
    1.52 +                errors.error('1')
    1.53 +        self.assertEquals(self.get_output(), ['Error: 1'])
    1.54 +
    1.55 +    def test_error_loop(self):
    1.56 +        with self.assertRaises(AccumulatedErrors):
    1.57 +            with errors.accumulate():
    1.58 +                for i in range(3):
    1.59 +                    errors.error('%d' % i)
    1.60 +        self.assertEquals(self.get_output(),
    1.61 +                          ['Error: 0', 'Error: 1', 'Error: 2'])
    1.62 +
    1.63 +    def test_multiple_errors(self):
    1.64 +        with self.assertRaises(AccumulatedErrors):
    1.65 +            with errors.accumulate():
    1.66 +                errors.error('foo')
    1.67 +                for i in range(3):
    1.68 +                    if i == 2:
    1.69 +                        errors.warn('%d' % i)
    1.70 +                    else:
    1.71 +                        errors.error('%d' % i)
    1.72 +                errors.error('bar')
    1.73 +        self.assertEquals(self.get_output(),
    1.74 +                          ['Error: foo', 'Error: 0', 'Error: 1',
    1.75 +                           'Warning: 2', 'Error: bar'])
    1.76 +
    1.77 +    def test_errors_context(self):
    1.78 +        with self.assertRaises(AccumulatedErrors):
    1.79 +            with errors.accumulate():
    1.80 +                self.assertEqual(errors.get_context(), None)
    1.81 +                with errors.context('foo', 1):
    1.82 +                    self.assertEqual(errors.get_context(), ('foo', 1))
    1.83 +                    errors.error('a')
    1.84 +                    with errors.context('bar', 2):
    1.85 +                        self.assertEqual(errors.get_context(), ('bar', 2))
    1.86 +                        errors.error('b')
    1.87 +                    self.assertEqual(errors.get_context(), ('foo', 1))
    1.88 +                    errors.error('c')
    1.89 +        self.assertEqual(self.get_output(), [
    1.90 +            'Error: foo:1: a',
    1.91 +            'Error: bar:2: b',
    1.92 +            'Error: foo:1: c',
    1.93 +        ])
    1.94 +
    1.95 +if __name__ == '__main__':
    1.96 +    mozunit.main()

mercurial