OLD | NEW |
(Empty) | |
| 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 # If the pexpect raises an exception after fork(), but before |
| 42 # exec(), our test runner *also* forks. We prevent this by |
| 43 # storing our pid and asserting equality on tearDown. |
| 44 self.pid = os.getpid() |
| 45 |
| 46 coverage_rc = os.path.join(project_dir, '.coveragerc') |
| 47 os.environ['COVERAGE_PROCESS_START'] = coverage_rc |
| 48 os.environ['COVERAGE_FILE'] = os.path.join(project_dir, '.coverage') |
| 49 print('\n', self.id(), end=' ') |
| 50 sys.stdout.flush() |
| 51 |
| 52 # some build agents will ignore SIGHUP and SIGINT, which python |
| 53 # inherits. This causes some of the tests related to terminate() |
| 54 # to fail. We set them to the default handlers that they should |
| 55 # be, and restore them back to their SIG_IGN value on tearDown. |
| 56 # |
| 57 # I'm not entirely convinced they need to be restored, only our |
| 58 # test runner is affected. |
| 59 self.restore_ignored_signals = [ |
| 60 value for value in (signal.SIGHUP, signal.SIGINT,) |
| 61 if signal.getsignal(value) == signal.SIG_IGN] |
| 62 if signal.SIGHUP in self.restore_ignored_signals: |
| 63 # sighup should be set to default handler |
| 64 signal.signal(signal.SIGHUP, signal.SIG_DFL) |
| 65 if signal.SIGINT in self.restore_ignored_signals: |
| 66 # SIGINT should be set to signal.default_int_handler |
| 67 signal.signal(signal.SIGINT, signal.default_int_handler) |
| 68 unittest.TestCase.setUp(self) |
| 69 |
| 70 def tearDown(self): |
| 71 # restore original working folder |
| 72 os.chdir(self.original_path) |
| 73 |
| 74 if self.pid != os.getpid(): |
| 75 # The build server pattern-matches phrase 'Test runner has forked!' |
| 76 print("Test runner has forked! This means a child process raised " |
| 77 "an exception before exec() in a test case, the error is " |
| 78 "more than likely found above this line in stderr.", |
| 79 file=sys.stderr) |
| 80 exit(1) |
| 81 |
| 82 # restore signal handlers |
| 83 for signal_value in self.restore_ignored_signals: |
| 84 signal.signal(signal_value, signal.SIG_IGN) |
| 85 |
| 86 if sys.version_info < (2, 7): |
| 87 # We want to use these methods, which are new/improved in 2.7, but |
| 88 # we are still supporting 2.6 for the moment. This section can be |
| 89 # removed when we drop Python 2.6 support. |
| 90 @contextlib.contextmanager |
| 91 def assertRaises(self, excClass): |
| 92 try: |
| 93 yield |
| 94 except Exception as e: |
| 95 assert isinstance(e, excClass) |
| 96 else: |
| 97 raise AssertionError("%s was not raised" % excClass) |
| 98 |
| 99 @contextlib.contextmanager |
| 100 def assertRaisesRegexp(self, excClass, pattern): |
| 101 import re |
| 102 try: |
| 103 yield |
| 104 except Exception as e: |
| 105 assert isinstance(e, excClass) |
| 106 assert re.match(pattern, str(e)) |
| 107 else: |
| 108 raise AssertionError("%s was not raised" % excClass) |
OLD | NEW |