32a3e75851efdbb2bce7ca7c698a52c677bfa70f
[third_party/pexpect] / tests / PexpectTestCase.py
1
2 '''
3 PEXPECT LICENSE
4
5     This license is approved by the OSI and FSF as GPL-compatible.
6         http://opensource.org/licenses/isc-license.txt
7
8     Copyright (c) 2012, Noah Spurrier <noah@noah.org>
9     PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
10     PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
11     COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
12     THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13     WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14     MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15     ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16     WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17     ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 '''
21 from __future__ import print_function
22
23 import contextlib
24 import unittest
25 import signal
26 import sys
27 import os
28
29
30 class PexpectTestCase(unittest.TestCase):
31     def setUp(self):
32         self.PYTHONBIN = sys.executable
33         self.original_path = os.getcwd()
34         tests_dir = os.path.dirname(__file__)
35         self.project_dir = project_dir = os.path.dirname(tests_dir)
36
37         # all tests are executed in this folder; there are many auxiliary
38         # programs in this folder executed by spawn().
39         os.chdir(tests_dir)
40
41         coverage_rc = os.path.join(project_dir, '.coveragerc')
42         os.environ['COVERAGE_PROCESS_START'] = coverage_rc
43         os.environ['COVERAGE_FILE'] = os.path.join(project_dir, '.coverage')
44         print('\n', self.id(), end=' ')
45         sys.stdout.flush()
46
47         # some build agents will ignore SIGHUP and SIGINT, which python
48         # inherits.  This causes some of the tests related to terminate()
49         # to fail.  We set them to the default handlers that they should
50         # be, and restore them back to their SIG_IGN value on tearDown.
51         #
52         # I'm not entirely convinced they need to be restored, only our
53         # test runner is affected.
54         self.restore_ignored_signals = [
55             value for value in (signal.SIGHUP, signal.SIGINT,)
56             if signal.getsignal(value) == signal.SIG_IGN]
57         if signal.SIGHUP in self.store_ignored_signals:
58             # sighup should be set to default handler
59             signal.signal(signal.SIGHUP, signal.SIG_DFL)
60         if signal.SIGINT in self.restore_ignored_signals:
61             # SIGINT should be set to signal.default_int_handler
62             signal.signal(signal.SIGINT, signal.default_int_handler)
63         unittest.TestCase.setUp(self)
64
65     def tearDown(self):
66         # restore original working folder
67         os.chdir(self.original_path)
68
69         # restore signal handlers
70         for signal_name in self.restore_ignored_signals:
71             signal.signal(getattr(signal, signal_name), signal.SIG_IGN)
72
73     if sys.version_info < (2, 7):
74         # We want to use these methods, which are new/improved in 2.7, but
75         # we are still supporting 2.6 for the moment. This section can be
76         # removed when we drop Python 2.6 support.
77         @contextlib.contextmanager
78         def assertRaises(self, excClass):
79             try:
80                 yield
81             except Exception as e:
82                 assert isinstance(e, excClass)
83             else:
84                 raise AssertionError("%s was not raised" % excClass)
85
86         @contextlib.contextmanager
87         def assertRaisesRegexp(self, excClass, pattern):
88             import re
89             try:
90                 yield
91             except Exception as e:
92                 assert isinstance(e, excClass)
93                 assert re.match(pattern, str(e))
94             else:
95                 raise AssertionError("%s was not raised" % excClass)