expect4r 0.0.6.dev → 0.0.7.dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/expect/io.rb +127 -20
  2. data/lib/router/error.rb +3 -2
  3. metadata +3 -3
data/lib/expect/io.rb CHANGED
@@ -9,9 +9,9 @@ class IO
9
9
  s = _io_string!
10
10
  exp_internal match_string
11
11
  exp_internal s
12
- return if no_echo
13
12
  s = s.chomp(ch) if ch
14
- _io_buf1 << s unless no_echo
13
+ _io_buf1 << s
14
+ _io_buf1
15
15
  end
16
16
  def _io_more? ; ! IO.select([self],nil,nil,0.20).nil? ; end
17
17
  def _io_exit? ; _io_buf0.last.nil? ; end
@@ -178,26 +178,130 @@ module Expect4r
178
178
  end
179
179
  lines.size==1 ? r[0] : r
180
180
  end
181
-
182
- def expect(match, ti=5)
183
- ev, buf = catch(:done) do
181
+
182
+ def expect(match, ti=5, matches=[])
183
+ t0 = Time.now
184
+ rc, buf = catch(:done) do
184
185
  @r.readbuf(ti) do |r|
185
186
  if r._io_exit?
186
- throw :done, [ :timeout, r._io_string!]
187
+ throw :done, [:abort, r._io_string!]
187
188
  end
188
189
  case r._io_string
189
190
  when match
190
- throw :done, [:ok, r._io_string!.chomp("\r\n")]
191
+ r._io_save false, "matching PROMPT"
192
+ # puts "debug IO BUF 1 #{r._io_buf1.inspect}"
193
+ throw(:done, [:ok, r._io_buf1])
194
+ # FIXME
195
+ # Watch out for the ping command !!!!
196
+ # throw :done, [:ok, r._io_string!.chomp("\r\n")]
197
+ when /(.+)\r\n/, "\r\n"
198
+ r._io_save false, "matching EOL"
199
+ else
200
+ matches.each do |match, arg|
201
+ if r._io_string =~ match
202
+ r._io_save false, "match #{match}"
203
+ if arg.is_a?(Proc)
204
+ arg.call(self)
205
+ else
206
+ exp_puts arg
207
+ end
208
+ end
209
+ end
191
210
  end
192
211
  end
193
212
  end
194
- exp_internal "#{ev.inspect} buf: #{buf.inspect}"
195
- raise ExpTimeoutError.new(buf, ti) if ev == :timeout
196
- [buf, ev]
213
+ case rc
214
+ when :abort
215
+ elapsed = Time.now - t0
216
+ if elapsed < ti
217
+ child_exit
218
+ raise ConnectionError.new(buf)
219
+ else
220
+ raise ExpTimeoutError.new(buf, elapsed)
221
+ end
222
+ else
223
+ @lp = buf.last
224
+ end
225
+ [buf, rc]
197
226
  end
198
227
 
199
- def readline(ti=2)
200
- expect(/(.+)\r\n/, ti)
228
+ def get_prompt
229
+ putline '', :no_trim=>true, :no_echo=>true
230
+ end
231
+
232
+ def putline(line, arg={})
233
+ raise ConnectionError.new(line) if child_exited?
234
+
235
+ arg = {:ti=>13, :no_echo=>false, :debug=>0, :sync=> false, :no_trim=>false}.merge(arg)
236
+ no_echo = arg[:no_echo]
237
+ ti = arg[:ti]
238
+ unless arg[:no_trim]==true
239
+ line = line.gsub(/\s+/,' ').gsub(/^\s+/,'')
240
+ if line.size==0
241
+ log "DEBUG PUTLINE: NOT SENDING **** line = '#{line.inspect}' arg=#{arg.inspect}"
242
+ return [[], :empty_line]
243
+ end
244
+ end
245
+ sync if arg[:sync]
246
+ t0 = Time.now
247
+ exp_puts line
248
+ output=[]
249
+ rc, buf = catch(:done) do
250
+ @r.readbuf(arg[:ti]) do |r|
251
+ if r._io_exit?
252
+ r._io_save(no_echo)
253
+ throw :done, [ :abort, r._io_buf1]
254
+ end
255
+ case r._io_string
256
+ when @ps1, @ps1_bis
257
+ unless r._io_more?
258
+ r._io_save no_echo, "matching PROMPT"
259
+ # puts "debug IO BUF 1 #{r._io_buf1.inspect}"
260
+ throw(:done, [:ok, r._io_buf1])
261
+ end
262
+ exp_internal "more..."
263
+ when /(.+)\r\n/, "\r\n"
264
+ r._io_save no_echo, "matching EOL"
265
+ when @more
266
+ r._io_save no_echo, "matching MORE"
267
+ putc ' '
268
+ else
269
+ # For objects that include Expect4r but do not subclass base Login class.
270
+ @matches ||= []
271
+ @matches.each { |match, _send|
272
+ if r._io_string =~ match
273
+ r._io_save no_echo, "match #{match}"
274
+ if _send.is_a?(Proc)
275
+ _send.call(self)
276
+ else
277
+ exp_puts _send
278
+ end
279
+ end
280
+ }
281
+ end
282
+ end
283
+ end
284
+
285
+ case rc
286
+ when :abort
287
+ elapsed = Time.now - t0
288
+ if elapsed < ti
289
+ child_exit
290
+ raise ConnectionError.new(line)
291
+ else
292
+ raise ExpTimeoutError.new(line, elapsed)
293
+ end
294
+ else
295
+ @lp = buf.last
296
+ end
297
+ [buf, rc]
298
+ end
299
+
300
+ def readline(ti=0.2, matches=[])
301
+ ret = expect(/(.+)\r\n/, ti, matches)
302
+ ret[0][0].chomp
303
+ rescue ExpTimeoutError => ex
304
+ ''
201
305
  end
202
306
 
203
307
  def connected?
@@ -453,9 +557,12 @@ module Expect4r
453
557
  include Expect4r
454
558
  class << self
455
559
  attr_reader :routers
560
+ def all
561
+ @arr
562
+ end
456
563
  def add(r)
457
- @routers ||=[]
458
- @routers << r
564
+ @arr ||=[]
565
+ @arr << r
459
566
  end
460
567
  end
461
568
  def initialize(*args)
@@ -578,11 +685,11 @@ end
578
685
  if __FILE__ != $0
579
686
 
580
687
  at_exit {
581
- if Expect4r::Base.routers
582
- Expect4r::Base.routers.each { |r| r.logout if r.respond_to? :logout }
688
+ if Expect4r::Base.all
689
+ Expect4r::Base.all.each { |o| o.logout if o.respond_to? :logout }
583
690
  end
584
691
  }
585
-
692
+
586
693
  else
587
694
 
588
695
  require "test/unit"
@@ -597,11 +704,11 @@ else
597
704
  include Expect4r
598
705
 
599
706
  def test_add
600
- assert [], Base.routers
707
+ assert [], Base.all
601
708
  Base.new
602
- assert 1, Base.routers.size
709
+ assert 1, Base.all.size
603
710
  Base.new
604
- assert_equal 2, Base.routers.size
711
+ assert_equal 2, Base.all.size
605
712
  end
606
713
  end
607
714
 
data/lib/router/error.rb CHANGED
@@ -23,9 +23,10 @@ module Error
23
23
  end
24
24
 
25
25
  class PingError < RuntimeError
26
- attr_reader :rname, :dest, :exp_pct, :act_pct, :sent, :recv
26
+ attr_reader :rname, :dest, :exp_pct, :act_pct, :sent, :recv, :ping_output
27
27
  def initialize(rname, dest, exp_pct, act_pct, sent, recv, output)
28
- @rname, @dest, @exp_pct, @act_pct, @sent, @recv = rname, dest, exp_pct, act_pct, sent, recv
28
+ @rname, @dest, @exp_pct, @act_pct, @sent, @recv, @ping_output = \
29
+ rname, dest, exp_pct, act_pct, sent, recv, output
29
30
  end
30
31
  def err_msg
31
32
  "#{@rname} [PingError] : failed to ping #{@dest}, expected/actual pct: #{@exp_pct}/#{@act_pct}"
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
8
+ - 7
9
9
  - dev
10
- version: 0.0.6.dev
10
+ version: 0.0.7.dev
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jean-Michel Esnault
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-29 00:00:00 -08:00
18
+ date: 2010-12-16 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency