def test_feeder(self): feeder = Feeder() p = capture_stdout([sys.executable, 'echoer.py'], input=feeder, async=True) try: lines = ('hello', 'goodbye') gen = iter(lines) # p.commands may not be set yet (separate thread) while not p.commands or p.commands[0].returncode is None: logger.debug('commands: %s', p.commands) try: data = next(gen) except StopIteration: break feeder.feed(data + '\n') if p.commands: p.commands[0].poll() time.sleep(0.05) # wait for child to return echo finally: # p.commands may not be set yet (separate thread) if p.commands: p.commands[0].terminate() feeder.close() self.assertEqual(p.stdout.text.splitlines(), ['hello hello', 'goodbye goodbye'])
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'))