コード例 #1
0
def test_PexpectEngine_read_all_reads_from_console(pty_pair):
    received = 'abcdef'
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    pty_pair.secondary.write(received)
    received_actual = engine.read_all()

    assert received_actual == received
コード例 #2
0
def test_PexpectEngine_read_all_returns_received_only_once(pty_pair):
    received = 'abcdef'
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    pty_pair.secondary.write(received)
    received1_actual = engine.read_all()
    received2_actual = engine.read_all()

    assert received1_actual == received
    assert received2_actual == ''
コード例 #3
0
def test_PexpectEngine_wait_for_match_return_received_if_not_matched(pty_pair):
    pattern = r'not going to match'
    received = 'abcd abcdyz'

    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    pty_pair.secondary.write(received)
    match = engine.wait_for_match(match=[pattern], timeout=0.5)

    assert match.regex_matched is None
    assert match.text_matched is None
    assert match.text_received == received
コード例 #4
0
def test_PexpectEngine_wait_for_match_return_match_and_if_matched(pty_pair):
    pattern = r'ab\S+yz'
    pattern_text = 'abcdyz'
    received = f'abcd {pattern_text}'

    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    pty_pair.secondary.write(received)
    match = engine.wait_for_match(match=[pattern], timeout=0.5)

    assert match.regex_matched == pattern
    assert match.text_matched == pattern_text
    assert match.text_received == received
コード例 #5
0
 def __init__(self,
              encoding: str = None,
              linesep: str = None,
              raw_logfile: str = None,
              system: SystemContext = None,
              engine: ConsoleEngine = None):
     self.engine = engine or PexpectEngine(
         linesep=linesep, encoding=encoding, raw_logfile=raw_logfile)
     self.system = system or SystemContext()
     self._requires_login = True
コード例 #6
0
def test_PexpectEngine_send_line_write_content_and_line_break(pty_pair):
    sent = 'abcdef'
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)
    engine.send_line(sent)

    assert pty_pair.secondary.read(timeout=0.5) == sent + '\n'
コード例 #7
0
def test_PexpectEngine_send_control_sends_correct_bytes(pty_pair_raw):
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair_raw.main.fd)

    engine.send_control('C')

    assert pty_pair_raw.main.read(timeout=0.5) == b'^C'
コード例 #8
0
def test_PexpectEngine_wait_for_match_should_return_after_timeout(
        pty_pair, timeout):
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    start_time = time.time()
    engine.wait_for_match(match=['abc'], timeout=timeout)

    assert 0.8 * timeout < time.time() - start_time < 1.2 * timeout
コード例 #9
0
def test_PexpectEngine_wait_for_match_should_return_immediately_on_match(
        pty_pair):
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)
    received = 'abc'
    pty_pair.secondary.write(received + '\n')

    start_time = time.time()
    engine.wait_for_match(match=[received], timeout=2)

    assert time.time() - start_time < 0.2
コード例 #10
0
def test_PexpectEngine_read_all_can_preserve_buffer(pty_pair):
    received = 'abcdef'
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    pty_pair.secondary.write(received)
    received1_actual = engine.read_all(preserve_read_buffer=True)
    received2_actual = engine.read_all()
    received3_actual = engine.read_all()

    assert received1_actual == received
    assert received2_actual == received
    assert received3_actual == ''
コード例 #11
0
def test_PexpectEngine_wait_for_match_should_match_only_once(pty_pair):
    pattern = r'ab\S+yz'
    pattern_text = 'abcdyz'
    received = f'abcd {pattern_text}'

    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)

    pty_pair.secondary.write(received)
    match = engine.wait_for_match(match=[pattern], timeout=0.5)

    assert match.regex_matched

    match = engine.wait_for_match(match=[pattern], timeout=0.5)

    assert match.regex_matched is None
コード例 #12
0
def test_PexpectEngine_open_shell_should_succeed():
    engine = PexpectEngine()
    engine.open(console_cmd='sh')
    assert engine.is_open
コード例 #13
0
def test_PexpectEngine_open_fd_should_succeed(pty_pair):
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair.main.fd)
    assert engine.is_open
コード例 #14
0
def test_PexpectEngine_error_on_invalid_code(pty_pair_raw):
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair_raw.main.fd)

    with pytest.raises(Exception):
        engine.send_control('!')
コード例 #15
0
def test_PexpectEngine_error_on_control_longer_than_one_char(pty_pair_raw):
    engine = PexpectEngine()
    engine.open(console_fd=pty_pair_raw.main.fd)

    with pytest.raises(ValueError):
        engine.send_control('CC')
コード例 #16
0
def test_PexpectEngine_close_shell_should_succeed():
    engine = PexpectEngine()
    engine.open(console_cmd='sh')
    engine.close()
    assert engine.is_open is False