diff options
author | Steven Silvester <steven.silvester@ieee.org> | 2014-12-01 21:00:21 -0600 |
---|---|---|
committer | Thomas Kluyver <takowl@gmail.com> | 2015-09-12 11:49:09 +0100 |
commit | 6407d849d4719c9aeda1eca011808044fb126764 (patch) | |
tree | bb035ce6955812cbb77f8003e5c0abd97b67ed58 | |
parent | ce7c7ed9ec8e5c9b985a51f911ba01bcb4c8d566 (diff) | |
download | pexpect-git-6407d849d4719c9aeda1eca011808044fb126764.tar.gz |
Add a sendeof method and tests
-rw-r--r-- | pexpect/popen_spawn.py | 10 | ||||
-rw-r--r-- | tests/test_popen_spawn.py | 28 |
2 files changed, 33 insertions, 5 deletions
diff --git a/pexpect/popen_spawn.py b/pexpect/popen_spawn.py index 0a186fe..389c505 100644 --- a/pexpect/popen_spawn.py +++ b/pexpect/popen_spawn.py @@ -26,7 +26,7 @@ class PopenSpawn(SpawnBase): logfile=None, cwd=None, env=None): super(PopenSpawn, self).__init__(timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, logfile=logfile) - + kwargs = dict(bufsize=0, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, cwd=cwd, env=env) @@ -106,7 +106,7 @@ class PopenSpawn(SpawnBase): def writelines(self, sequence): '''This calls write() for each element in the sequence. - + The sequence can be any iterable object producing strings, typically a list of strings. This does not add line separators. There is no return value. @@ -153,6 +153,12 @@ class PopenSpawn(SpawnBase): os.kill(self.proc.pid, sig) + def sendeof(self): + if sys.platform == 'win32': + self.kill(signal.CTRL_BREAK_EVENT) + else: + self.kill(signal.SIGTERM) + class PopenSpawnUnicode(SpawnBaseUnicode, PopenSpawn): def _send(self, s): diff --git a/tests/test_popen_spawn.py b/tests/test_popen_spawn.py index ce6b33e..3431e49 100644 --- a/tests/test_popen_spawn.py +++ b/tests/test_popen_spawn.py @@ -29,9 +29,31 @@ from . import PexpectTestCase class ExpectTestCase (PexpectTestCase.PexpectTestCase): + def test_expect_basic(self): + p = PopenSpawn('cat', timeout=5) + p.sendline(b'Hello') + p.sendline(b'there') + p.sendline(b'Mr. Python') + p.expect(b'Hello') + p.expect(b'there') + p.expect(b'Mr. Python') + p.sendeof() + p.expect(pexpect.EOF) + + def test_expect_exact_basic(self): + p = PopenSpawn('cat', timeout=5) + p.sendline(b'Hello') + p.sendline(b'there') + p.sendline(b'Mr. Python') + p.expect_exact(b'Hello') + p.expect_exact(b'there') + p.expect_exact(b'Mr. Python') + p.sendeof() + p.expect_exact(pexpect.EOF) + def test_expect(self): the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'], - stdout=subprocess.PIPE).communicate()[0].rstrip() + stdout=subprocess.PIPE).communicate()[0].rstrip() p = PopenSpawn('ls -l /bin') the_new_way = b'' while 1: @@ -45,7 +67,7 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_expect_exact(self): the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'], - stdout=subprocess.PIPE).communicate()[0].rstrip() + stdout=subprocess.PIPE).communicate()[0].rstrip() p = PopenSpawn('ls -l /bin') the_new_way = b'' while 1: @@ -64,7 +86,7 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_expect_eof(self): the_old_way = subprocess.Popen(args=['/bin/ls', '-l', '/bin'], - stdout=subprocess.PIPE).communicate()[0].rstrip() + stdout=subprocess.PIPE).communicate()[0].rstrip() p = PopenSpawn('/bin/ls -l /bin') # This basically tells it to read everything. Same as pexpect.run() # function. |