diff options
author | Samuel Bancal <Samuel.Bancal@epfl.ch> | 2015-02-12 15:23:02 +0100 |
---|---|---|
committer | Samuel Bancal <Samuel.Bancal@epfl.ch> | 2015-02-12 15:23:02 +0100 |
commit | 37665f9add48760c1d4ad8c1909da4d8645e3146 (patch) | |
tree | 73ef5b5638a9d5bb24a8c40d7d27b5e5aa1d1195 | |
parent | 450f390eaecf88ad95f429ce8b5d03fc8381f158 (diff) | |
download | pexpect-git-37665f9add48760c1d4ad8c1909da4d8645e3146.tar.gz |
Allows also method callback for events argument in pexpect.run()
Updated test_run.py for this case
-rw-r--r-- | pexpect/__init__.py | 3 | ||||
-rwxr-xr-x | tests/test_run.py | 52 |
2 files changed, 54 insertions, 1 deletions
diff --git a/pexpect/__init__.py b/pexpect/__init__.py index c906e89..2bed4e6 100644 --- a/pexpect/__init__.py +++ b/pexpect/__init__.py @@ -206,7 +206,8 @@ def _run(command, timeout, withexitstatus, events, extra_args, logfile, cwd, child_result_list.append(child.before) if isinstance(responses[index], child.allowed_string_types): child.send(responses[index]) - elif isinstance(responses[index], types.FunctionType): + elif isinstance(responses[index], types.FunctionType) or \ + isinstance(responses[index], types.MethodType): callback_result = responses[index](locals()) sys.stdout.flush() if isinstance(callback_result, child.allowed_string_types): diff --git a/tests/test_run.py b/tests/test_run.py index c018b4d..286bf69 100755 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -35,6 +35,20 @@ def timeout_callback (d): return 1 return 0 +def function_events_callback (d): + try: + previous_echoed = d["child_result_list"][-1].decode().split("\n")[-2].strip() + if previous_echoed.endswith("foo1"): + return "echo foo2\n" + elif previous_echoed.endswith("foo2"): + return "echo foo3\n" + elif previous_echoed.endswith("foo3"): + return "exit\n" + else: + raise Exception("Unexpected output {0}".format(previous_echoed)) + except IndexError: + return "echo foo1\n" + class RunFuncTestCase(PexpectTestCase.PexpectTestCase): runfunc = staticmethod(pexpect.run) cr = b'\r' @@ -88,6 +102,44 @@ class RunFuncTestCase(PexpectTestCase.PexpectTestCase): timeout=10) assert exitstatus == 0 + def test_run_function (self): + events = [ + ('GO:', function_events_callback) + ] + + (data, exitstatus) = pexpect.run( + 'bash --rcfile {0}'.format(self.rcfile), + withexitstatus=True, + events=events, + timeout=10) + assert exitstatus == 0 + + def test_run_method (self): + events = [ + ('GO:', self.method_events_callback) + ] + + (data, exitstatus) = pexpect.run( + 'bash --rcfile {0}'.format(self.rcfile), + withexitstatus=True, + events=events, + timeout=10) + assert exitstatus == 0 + + def method_events_callback (self, d): + try: + previous_echoed = d["child_result_list"][-1].decode().split("\n")[-2].strip() + if previous_echoed.endswith("foo1"): + return "echo foo2\n" + elif previous_echoed.endswith("foo2"): + return "echo foo3\n" + elif previous_echoed.endswith("foo3"): + return "exit\n" + else: + raise Exception("Unexpected output {0}".format(previous_echoed)) + except IndexError: + return "echo foo1\n" + class RunUnicodeFuncTestCase(RunFuncTestCase): runfunc = staticmethod(pexpect.runu) cr = b'\r'.decode('ascii') |