Method: SshHelper::TmcSsh#expect

Defined in:
lib/helpers/tmc_helpers/ssh_helper/ssh_helper.rb

#expect(data, timeout: 10.sec) ⇒ Object

Public: Waits for some output in the active PTY.

data - String or Regexp output to expect. timeout - Integer timeout in milliseconds (default: 10.sec).

Returns a String data buffered before the expected output.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/helpers/tmc_helpers/ssh_helper/ssh_helper.rb', line 128

def expect(data, timeout: 10.sec)
  raise 'No active pty.' if @pty.nil? && !@pty_starting
  data ||= @pty_prompt
  logdebug("Expecting: #{data.inspect}")
  reason = nil
  begin
    Timeout.timeout(timeout / 1000.0) do
      @ssh.loop(0.1) do
        ready = false
        if @pty.nil?
          unless @pty_starting
            reason = 'no pty'
            ready = true
          end
        elsif @pty.eof?
          reason = 'pty is eof'
          ready = true
        elsif search_buffer(data)
          reason = 'pty is ready'
          ready = true
        end
        !ready
      end
    end
  rescue TimeoutError
    @buffer_lock.synchronize do
      raise "#{session_info} Timed out waiting for expected output in buffer:\n#{@buffer}"
    end
  end
  puts "*ready (#{reason})" if @debug
  if @pty.nil?
    @buffer_lock.synchronize do
      puts "*buffer: #{@buffer.inspect}" if @debug
      raw_output = @buffer
    end
  else
    puts "*before: #{@before.inspect}" if @debug
    puts "*match: #{@match.inspect}" if @debug
    raw_output = @before
  end
  raw_output ||= ''
  logdebug("Output:\n#{@last_match || ''}#{raw_output.rstrip}")
  re = /^#{@last_sent_data.gsub(/\r?\n/, '\r?\n')}/
  puts "*trimming start: #{re.inspect}" if @debug
  raw_output.sub(re, '')
end