From 18a1bfd60f90dd0a7775644bf765f75884740e44 Mon Sep 17 00:00:00 2001 From: Benedek Racz Date: Sun, 5 May 2019 14:38:01 +0200 Subject: [PATCH] [ADD] first unittests copied from pexpect --- tests/PexpectTestCase.py | 105 +++++++++++++++++++++++++++++++ tests/__init__.py | 25 ++++++++ tests/test_command_list_split.py | 40 ++++++++++++ tests/test_delay.py | 45 +++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 tests/PexpectTestCase.py create mode 100644 tests/__init__.py create mode 100644 tests/test_command_list_split.py create mode 100644 tests/test_delay.py diff --git a/tests/PexpectTestCase.py b/tests/PexpectTestCase.py new file mode 100644 index 0000000..19ec92b --- /dev/null +++ b/tests/PexpectTestCase.py @@ -0,0 +1,105 @@ + +''' +PEXPECT LICENSE + + This license is approved by the OSI and FSF as GPL-compatible. + http://opensource.org/licenses/isc-license.txt + + Copyright (c) 2012, Noah Spurrier + PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY + PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE + COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +''' +from __future__ import print_function + +import contextlib +import unittest +import signal +import sys +import os + + +class PexpectTestCase(unittest.TestCase): + def setUp(self): + self.PYTHONBIN = sys.executable + self.original_path = os.getcwd() + tests_dir = os.path.dirname(__file__) + self.project_dir = project_dir = os.path.dirname(tests_dir) + + # all tests are executed in this folder; there are many auxiliary + # programs in this folder executed by spawn(). + os.chdir(tests_dir) + + # If the pexpect raises an exception after fork(), but before + # exec(), our test runner *also* forks. We prevent this by + # storing our pid and asserting equality on tearDown. + self.pid = os.getpid() + + coverage_rc = os.path.join(project_dir, '.coveragerc') + os.environ['COVERAGE_PROCESS_START'] = coverage_rc + os.environ['COVERAGE_FILE'] = os.path.join(project_dir, '.coverage') + print('\n', self.id(), end=' ') + sys.stdout.flush() + + # some build agents will ignore SIGHUP and SIGINT, which python + # inherits. This causes some of the tests related to terminate() + # to fail. We set them to the default handlers that they should + # be, and restore them back to their SIG_IGN value on tearDown. + # + # I'm not entirely convinced they need to be restored, only our + # test runner is affected. + self.restore_ignored_signals = [ + value for value in (signal.SIGINT,) + if signal.getsignal(value) == signal.SIG_IGN] + if signal.SIGINT in self.restore_ignored_signals: + # SIGINT should be set to signal.default_int_handler + signal.signal(signal.SIGINT, signal.default_int_handler) + unittest.TestCase.setUp(self) + + def tearDown(self): + # restore original working folder + os.chdir(self.original_path) + + if self.pid != os.getpid(): + # The build server pattern-matches phrase 'Test runner has forked!' + print("Test runner has forked! This means a child process raised " + "an exception before exec() in a test case, the error is " + "more than likely found above this line in stderr.", + file=sys.stderr) + exit(1) + + # restore signal handlers + for signal_value in self.restore_ignored_signals: + signal.signal(signal_value, signal.SIG_IGN) + + if sys.version_info < (2, 7): + # We want to use these methods, which are new/improved in 2.7, but + # we are still supporting 2.6 for the moment. This section can be + # removed when we drop Python 2.6 support. + @contextlib.contextmanager + def assertRaises(self, excClass): + try: + yield + except Exception as e: + assert isinstance(e, excClass) + else: + raise AssertionError("%s was not raised" % excClass) + + @contextlib.contextmanager + def assertRaisesRegexp(self, excClass, pattern): + import re + try: + yield + except Exception as e: + assert isinstance(e, excClass) + assert re.match(pattern, str(e)) + else: + raise AssertionError("%s was not raised" % excClass) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..f717d82 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,25 @@ + +''' +PEXPECT LICENSE + + This license is approved by the OSI and FSF as GPL-compatible. + http://opensource.org/licenses/isc-license.txt + + Copyright (c) 2012, Noah Spurrier + PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY + PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE + COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +''' + +# __init__.py +# The mere presence of this file makes the dir a package. +pass + diff --git a/tests/test_command_list_split.py b/tests/test_command_list_split.py new file mode 100644 index 0000000..9baa1c8 --- /dev/null +++ b/tests/test_command_list_split.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +''' +PEXPECT LICENSE + + This license is approved by the OSI and FSF as GPL-compatible. + http://opensource.org/licenses/isc-license.txt + + Copyright (c) 2012, Noah Spurrier + PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY + PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE + COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +''' +import wexpect +import unittest +from . import PexpectTestCase + +class SplitCommandLineTestCase(PexpectTestCase.PexpectTestCase): + def testSplitSizes(self): + assert len(wexpect.split_command_line(r'')) == 0 + assert len(wexpect.split_command_line(r'one')) == 1 + assert len(wexpect.split_command_line(r'one two')) == 2 + assert len(wexpect.split_command_line(r'one two')) == 2 + assert len(wexpect.split_command_line(r'one two')) == 2 + assert len(wexpect.split_command_line(r'one\ one')) == 1 + assert len(wexpect.split_command_line('\'one one\'')) == 1 + assert len(wexpect.split_command_line(r'one\"one')) == 1 + assert len(wexpect.split_command_line(r'This\' is a\'\ test')) == 3 + +if __name__ == '__main__': + unittest.main() + +suite = unittest.makeSuite(SplitCommandLineTestCase,'test') diff --git a/tests/test_delay.py b/tests/test_delay.py new file mode 100644 index 0000000..05c775c --- /dev/null +++ b/tests/test_delay.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +from . import PexpectTestCase +import wexpect + + +class TestCaseDelay(PexpectTestCase.PexpectTestCase): + """ + Tests for various delay attributes. + """ + def test_delaybeforesend(self): + """ + Test various values for delaybeforesend. + """ + p = wexpect.spawn("cat") + + p.delaybeforesend = 1 + p.sendline("line 1") + p.expect("line 1") + + p.delaybeforesend = 0.0 + p.sendline("line 2") + p.expect("line 2") + + p.delaybeforesend = None + p.sendline("line 3") + p.expect("line 3") + + def test_delayafterread(self): + """ + Test various values for delayafterread. + """ + p = wexpect.spawn("cat") + + p.delayafterread = 1 + p.sendline("line 1") + p.expect("line 1") + + p.delayafterread = 0.0 + p.sendline("line 2") + p.expect("line 2") + + p.delayafterread = None + p.sendline("line 3") + p.expect("line 3")