File: test_errors.py

package info (click to toggle)
mozjs78 78.15.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 739,892 kB
  • sloc: javascript: 1,344,214; cpp: 1,215,708; python: 526,544; ansic: 433,835; xml: 118,736; sh: 26,176; asm: 16,664; makefile: 11,537; yacc: 4,486; perl: 2,564; ada: 1,681; lex: 1,414; pascal: 1,139; cs: 879; exp: 499; java: 164; ruby: 68; sql: 45; csh: 35; sed: 18; lisp: 2
file content (96 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function, unicode_literals

from mozpack.errors import (
    errors,
    ErrorMessage,
    AccumulatedErrors,
)
import unittest
import mozunit
import six
import sys


class TestErrors(object):
    def setUp(self):
        errors.out = six.moves.cStringIO()
        errors.ignore_errors(False)

    def tearDown(self):
        errors.out = sys.stderr

    def get_output(self):
        return [l.strip() for l in errors.out.getvalue().splitlines()]


class TestErrorsImpl(TestErrors, unittest.TestCase):
    def test_plain_error(self):
        errors.warn('foo')
        self.assertRaises(ErrorMessage, errors.error, 'foo')
        self.assertRaises(ErrorMessage, errors.fatal, 'foo')
        self.assertEquals(self.get_output(), ['Warning: foo'])

    def test_ignore_errors(self):
        errors.ignore_errors()
        errors.warn('foo')
        errors.error('bar')
        self.assertRaises(ErrorMessage, errors.fatal, 'foo')
        self.assertEquals(self.get_output(), ['Warning: foo', 'Warning: bar'])

    def test_no_error(self):
        with errors.accumulate():
            errors.warn('1')

    def test_simple_error(self):
        with self.assertRaises(AccumulatedErrors):
            with errors.accumulate():
                errors.error('1')
        self.assertEquals(self.get_output(), ['Error: 1'])

    def test_error_loop(self):
        with self.assertRaises(AccumulatedErrors):
            with errors.accumulate():
                for i in range(3):
                    errors.error('%d' % i)
        self.assertEquals(self.get_output(),
                          ['Error: 0', 'Error: 1', 'Error: 2'])

    def test_multiple_errors(self):
        with self.assertRaises(AccumulatedErrors):
            with errors.accumulate():
                errors.error('foo')
                for i in range(3):
                    if i == 2:
                        errors.warn('%d' % i)
                    else:
                        errors.error('%d' % i)
                errors.error('bar')
        self.assertEquals(self.get_output(),
                          ['Error: foo', 'Error: 0', 'Error: 1',
                           'Warning: 2', 'Error: bar'])

    def test_errors_context(self):
        with self.assertRaises(AccumulatedErrors):
            with errors.accumulate():
                self.assertEqual(errors.get_context(), None)
                with errors.context('foo', 1):
                    self.assertEqual(errors.get_context(), ('foo', 1))
                    errors.error('a')
                    with errors.context('bar', 2):
                        self.assertEqual(errors.get_context(), ('bar', 2))
                        errors.error('b')
                    self.assertEqual(errors.get_context(), ('foo', 1))
                    errors.error('c')
        self.assertEqual(self.get_output(), [
            'Error: foo:1: a',
            'Error: bar:2: b',
            'Error: foo:1: c',
        ])


if __name__ == '__main__':
    mozunit.main()