Expectit 没有找到我期望的字符串

Expectit doesn't find my expected string

我正在尝试将 expectit 库与 sshj 一起使用,如下所示:

final Session session = getSharedSession();
final Session.Command sessionCommand = session.exec(command);
try (Expect expect = new ExpectBuilder()
       .withOutput(sessionCommand.getOutputStream())
       .withInputs(sessionCommand.getInputStream(), sessionCommand.getErrorStream())
       .withInputFilters(removeColors(), removeNonPrintable())
       .withEchoInput(LoggingAppendableAdapter.getInstance())
       .withEchoOutput(LoggingAppendableAdapter.getInstance())
       .withExceptionOnFailure()
       .build()) {
    for (SshExpectations sshExpectation : sequence) {
        expect.expect(contains(sshExpectation.getExpectation()));
        expect.sendLine(sshExpectation.getReaction());
    }
}

我正在执行的命令是 "sleep 5; rm -i test.txt",我得到以下输出:

Jul 26, 2017 6:07:14 PM net.sf.expectit.SingleInputExpect start
FINE: Starting expect thread: input=< ChannelInputStream for Channel #1 >, charset=UTF-8, echoInput=LoggingAppendableAdapter@7a1099d7, filter=net.sf.expectit.filter.Filters@3afe1f22, bufferSize=1024
Jul 26, 2017 6:07:14 PM net.sf.expectit.SingleInputExpect start
FINE: Starting expect thread: input=< ChannelInputStream for Channel #1 >, charset=UTF-8, echoInput=LoggingAppendableAdapter@7a1099d7, filter=net.sf.expectit.filter.Filters@3afe1f22, bufferSize=1024
Jul 26, 2017 6:07:14 PM net.sf.expectit.ExpectImpl expectIn
FINE: Expect matcher 'contains('remove regular empty')' with timeout 30000 (ms) in input #0
Jul 26, 2017 6:07:14 PM net.sf.expectit.SingleInputExpect expect
FINE: Initial matcher contains('remove regular empty') result: SimpleResult{succeeded=false, before='null', group='null', input='', canStopMatching=false}
2017-07-26 18:07:19,574 INFO [expect-pool-26-thread-2] LoggingAppendableAdapter rm: remove regular empty file ‘test’? 
Jul 26, 2017 6:07:19 PM net.sf.expectit.InputStreamCopier call
FINE: Received from < ChannelInputStream for Channel #1 >: rm: remove regular empty file ‘test’? 
Jul 26, 2017 6:07:44 PM net.sf.expectit.SingleInputExpect expect
FINE: Selector returns 0 key
Jul 26, 2017 6:07:44 PM net.sf.expectit.SingleInputExpect stop
FINE: Releasing resources for input: < ChannelInputStream for Channel #1 >
Jul 26, 2017 6:07:44 PM net.sf.expectit.SingleInputExpect stop
FINE: Releasing resources for input: < ChannelInputStream for Channel #1 >
2017-07-26 18:07:46,800 ERROR Could not execute command
net.sf.expectit.ExpectIOException: Expect operation fails (timeout: 30000 ms) for matcher: contains('remove regular empty')
    at net.sf.expectit.ExpectImpl.expectIn(ExpectImpl.java:106)
    at net.sf.expectit.AbstractExpectImpl.expectIn(AbstractExpectImpl.java:57)
    at net.sf.expectit.AbstractExpectImpl.expect(AbstractExpectImpl.java:61)

人们会认为一切都应该正常工作,因为 LoggingAppendableAdapter 和内部日志报告字符串 "rm: remove regular empty file ‘test’?"。

对我可能做错的地方有什么建议吗?

问题是 rm -i test.txt 将其问题写入 stderr 而不是 stdout,但默认情况下 expectit 仅检查第一个输入流(在我的例子中是标准输出流).

通过使用

expect.expectIn(1, contains(sshExpectation.getExpectation()));

expectit 然后期望字符串在第二个给定的输入流上,在我的例子中是 stderr.