def test_expect(self): cap = Capture(buffer_size=-1) # line buffered p = run("%s lister.py -d 0.01" % sys.executable, async=True, stdout=cap) timeout = 1.0 m1 = cap.expect("^line 1\r?$", timeout) self.assertTrue(m1) m2 = cap.expect("^line 5\r?$", timeout) self.assertTrue(m2) m3 = cap.expect("^line 1.*\r?$", timeout) self.assertTrue(m3) cap.close(True) p.commands[0].kill() data = cap.bytes self.assertEqual(data[m1.start() : m1.end()].rstrip(), b"line 1") self.assertEqual(data[m2.start() : m2.end()].rstrip(), b"line 5") self.assertEqual(data[m3.start() : m3.end()].rstrip(), b"line 10")
def test_expect(self): cap = Capture(buffer_size=-1) # line buffered p = run('%s lister.py -d 0.01' % sys.executable, async=True, stdout=cap) timeout = 1.0 m1 = cap.expect('^line 1\r?$', timeout) self.assertTrue(m1) m2 = cap.expect('^line 5\r?$', timeout) self.assertTrue(m2) m3 = cap.expect('^line 1.*\r?$', timeout) self.assertTrue(m3) cap.close(True) p.commands[0].kill() data = cap.bytes self.assertEqual(data[m1.start():m1.end()].rstrip(), b'line 1') self.assertEqual(data[m2.start():m2.end()].rstrip(), b'line 5') self.assertEqual(data[m3.start():m3.end()].rstrip(), b'line 10')
import logging from sarge import run, Capture import time logger = logging.getLogger(__name__) logging.basicConfig(filename='test_expect.log', filemode='w', level=logging.INFO, format='%(asctime)s %(levelname)-8s %(name)s %(threadName)s %(lineno)4d %(message)s') cap = Capture(buffer_size=-1) # line buffered p = run('python lister.py -d 0.01 -i "<head|body>" docs/_build/html/tutorial.html', async=True, stdout=cap) stime = time.time() logger.info('Calling expect for head') cap.expect('<head>', 60.0) logger.info('Returned from expect for head') elapsed = time.time() - stime if not cap.match: print('<head> not found within time limit.') else: print('<head> found at %s in %.1f seconds.' % (cap.match.span(), elapsed)) stime = time.time() logger.info('Calling expect for body') cap.expect('<body>', 60.0) logger.info('Returned from expect for body') elapsed = time.time() - stime if not cap.match: print('<body> not found within time limit.') else: print('<body> found at %s in %.1f seconds.' % (cap.match.span(), elapsed)) logger.debug('Killing subprocess')
import re from sarge import Capture, Feeder, run f = Feeder() c = Capture(buffer_size=1) p = run('python login_test.py', async_=True, stdout=c, input=f) c.expect('Username:'******'input username') f.feed('user\n') c.expect('Password:'******'input password') f.feed('pass\n') VERIFICATION_CODE_REGEX = re.compile(rb'Input verification code \((\d{4})\): ') match = c.expect(VERIFICATION_CODE_REGEX) print('input verification code', match.group(1)) f.feed(match.group(1) + b'\n') c.expect('>>>', timeout=5) f.feed('print(1 + 1)\n') f.feed('exit()\n') p.wait() print('final output:\n', b''.join(c.readlines()).decode('utf-8'))
import time logger = logging.getLogger(__name__) logging.basicConfig( filename='test_expect.log', filemode='w', level=logging.INFO, format='%(asctime)s %(levelname)-8s %(name)s %(threadName)s %(lineno)4d %(message)s') cap = Capture(buffer_size=-1) # line buffered p = run('python lister.py -d 0.01 -i "<head|body>" docs/_build/html/tutorial.html', async_=True, stdout=cap) stime = time.time() logger.info('Calling expect for head') cap.expect('<head>', 60.0) logger.info('Returned from expect for head') elapsed = time.time() - stime if not cap.match: print('<head> not found within time limit.') else: print('<head> found at %s in %.1f seconds.' % (cap.match.span(), elapsed)) stime = time.time() logger.info('Calling expect for body') cap.expect('<body>', 60.0) logger.info('Returned from expect for body') elapsed = time.time() - stime if not cap.match: print('<body> not found within time limit.') else: print('<body> found at %s in %.1f seconds.' % (cap.match.span(), elapsed))