michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: from mozpack.errors import ( michael@0: errors, michael@0: ErrorMessage, michael@0: AccumulatedErrors, michael@0: ) michael@0: import unittest michael@0: import mozunit michael@0: import sys michael@0: from cStringIO import StringIO michael@0: michael@0: michael@0: class TestErrors(object): michael@0: def setUp(self): michael@0: errors.out = StringIO() michael@0: errors.ignore_errors(False) michael@0: michael@0: def tearDown(self): michael@0: errors.out = sys.stderr michael@0: michael@0: def get_output(self): michael@0: return [l.strip() for l in errors.out.getvalue().splitlines()] michael@0: michael@0: michael@0: class TestErrorsImpl(TestErrors, unittest.TestCase): michael@0: def test_plain_error(self): michael@0: errors.warn('foo') michael@0: self.assertRaises(ErrorMessage, errors.error, 'foo') michael@0: self.assertRaises(ErrorMessage, errors.fatal, 'foo') michael@0: self.assertEquals(self.get_output(), ['Warning: foo']) michael@0: michael@0: def test_ignore_errors(self): michael@0: errors.ignore_errors() michael@0: errors.warn('foo') michael@0: errors.error('bar') michael@0: self.assertRaises(ErrorMessage, errors.fatal, 'foo') michael@0: self.assertEquals(self.get_output(), ['Warning: foo', 'Warning: bar']) michael@0: michael@0: def test_no_error(self): michael@0: with errors.accumulate(): michael@0: errors.warn('1') michael@0: michael@0: def test_simple_error(self): michael@0: with self.assertRaises(AccumulatedErrors): michael@0: with errors.accumulate(): michael@0: errors.error('1') michael@0: self.assertEquals(self.get_output(), ['Error: 1']) michael@0: michael@0: def test_error_loop(self): michael@0: with self.assertRaises(AccumulatedErrors): michael@0: with errors.accumulate(): michael@0: for i in range(3): michael@0: errors.error('%d' % i) michael@0: self.assertEquals(self.get_output(), michael@0: ['Error: 0', 'Error: 1', 'Error: 2']) michael@0: michael@0: def test_multiple_errors(self): michael@0: with self.assertRaises(AccumulatedErrors): michael@0: with errors.accumulate(): michael@0: errors.error('foo') michael@0: for i in range(3): michael@0: if i == 2: michael@0: errors.warn('%d' % i) michael@0: else: michael@0: errors.error('%d' % i) michael@0: errors.error('bar') michael@0: self.assertEquals(self.get_output(), michael@0: ['Error: foo', 'Error: 0', 'Error: 1', michael@0: 'Warning: 2', 'Error: bar']) michael@0: michael@0: def test_errors_context(self): michael@0: with self.assertRaises(AccumulatedErrors): michael@0: with errors.accumulate(): michael@0: self.assertEqual(errors.get_context(), None) michael@0: with errors.context('foo', 1): michael@0: self.assertEqual(errors.get_context(), ('foo', 1)) michael@0: errors.error('a') michael@0: with errors.context('bar', 2): michael@0: self.assertEqual(errors.get_context(), ('bar', 2)) michael@0: errors.error('b') michael@0: self.assertEqual(errors.get_context(), ('foo', 1)) michael@0: errors.error('c') michael@0: self.assertEqual(self.get_output(), [ michael@0: 'Error: foo:1: a', michael@0: 'Error: bar:2: b', michael@0: 'Error: foo:1: c', michael@0: ]) michael@0: michael@0: if __name__ == '__main__': michael@0: mozunit.main()