python/mozbuild/mozpack/test/test_errors.py

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial