blue-shell 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,8 +10,6 @@ module BlueShell
10
10
  end
11
11
 
12
12
  def expect(pattern, timeout = 5)
13
- buffer = ''
14
-
15
13
  case pattern
16
14
  when String
17
15
  pattern = Regexp.new(Regexp.quote(pattern))
@@ -20,6 +18,22 @@ module BlueShell
20
18
  raise TypeError, "unsupported pattern class: #{pattern.class}"
21
19
  end
22
20
 
21
+ result, buffer = read_pipe(timeout, pattern)
22
+
23
+ @output << buffer
24
+
25
+ result
26
+ end
27
+
28
+ def read_to_end
29
+ _, buffer = read_pipe(0.01)
30
+ @output << buffer
31
+ end
32
+
33
+ private
34
+
35
+ def read_pipe(timeout, pattern = nil)
36
+ buffer = ""
23
37
  result = nil
24
38
  position = 0
25
39
  @unused ||= ""
@@ -53,19 +67,15 @@ module BlueShell
53
67
  end
54
68
  end
55
69
 
56
- if matches = pattern.match(buffer)
70
+ if pattern && matches = pattern.match(buffer)
57
71
  result = [buffer, *matches.to_a[1..-1]]
58
72
  break
59
73
  end
60
74
  end
61
75
 
62
- @output << buffer
63
-
64
- result
76
+ return result, buffer
65
77
  end
66
78
 
67
- private
68
-
69
79
  def output_ended?(timeout)
70
80
  (@out.is_a?(IO) && !IO.select([@out], nil, nil, timeout)) || @out.eof?
71
81
  end
@@ -41,12 +41,16 @@ module BlueShell
41
41
  @stdin.puts
42
42
  end
43
43
 
44
- def exit_code
44
+ def exit_code(timeout = 5)
45
45
  return @code if @code
46
46
 
47
47
  code = nil
48
- Timeout.timeout(5) do
49
- _, code = Process.waitpid2(@pid)
48
+ begin
49
+ Timeout.timeout(timeout) do
50
+ _, code = Process.waitpid2(@pid)
51
+ end
52
+ rescue Timeout::Error
53
+ raise ::Timeout::Error.new("execution expired, output was:\n#{@expector.read_to_end}")
50
54
  end
51
55
 
52
56
  @code = numeric_exit_code(code)
@@ -1,3 +1,3 @@
1
1
  module BlueShell
2
- VERSION = "0.0.2".freeze
2
+ VERSION = "0.0.3".freeze
3
3
  end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe BlueShell::BufferedReaderExpector do
4
+ let(:pipe) { IO::pipe }
5
+ let(:read) { pipe[0] }
6
+ let(:write) { pipe[1] }
7
+
8
+ subject { BlueShell::BufferedReaderExpector.new(read) }
9
+
10
+ describe "#read_to_end" do
11
+ it "captures the output" do
12
+ hash = %w|the never ending story|
13
+ hash.each do |thing|
14
+ write.puts thing
15
+ end
16
+
17
+ subject.read_to_end.should include(hash.last)
18
+
19
+ hash.each do |thing|
20
+ subject.output.should include(thing)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -191,6 +191,26 @@ module BlueShell
191
191
  end
192
192
  end
193
193
  end
194
+
195
+ context "when the command doesn't finish within the timeout" do
196
+ it "raises a timeout error" do
197
+ BlueShell::Runner.run("sleep 10") do |runner|
198
+ expect { runner.exit_code }.to raise_error(Timeout::Error)
199
+ end
200
+ end
201
+
202
+ it "prints the output so far" do
203
+ BlueShell::Runner.run("echo 'everything is coming up wankershim' && sleep 10") do |runner|
204
+ expect { runner.exit_code }.to raise_error(Timeout::Error, /everything is coming up wankershim/)
205
+ end
206
+ end
207
+ end
208
+
209
+ it "uses the given timeout" do
210
+ BlueShell::Runner.run("sleep 2") do |runner|
211
+ expect { runner.exit_code(1) }.to raise_error(Timeout::Error)
212
+ end
213
+ end
194
214
  end
195
215
 
196
216
  context "#exited?" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blue-shell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-09 00:00:00.000000000 Z
13
+ date: 2013-04-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -79,6 +79,7 @@ files:
79
79
  - lib/blue-shell.rb
80
80
  - spec/assets/input.rb
81
81
  - spec/assets/pause.rb
82
+ - spec/buffered_reader_expector_spec.rb
82
83
  - spec/matchers/exit_code_matcher_spec.rb
83
84
  - spec/matchers/output_matcher_spec.rb
84
85
  - spec/matchers_spec.rb
@@ -114,6 +115,7 @@ summary: Friendly command-line test runner and matchers for shell scripting in r
114
115
  test_files:
115
116
  - spec/assets/input.rb
116
117
  - spec/assets/pause.rb
118
+ - spec/buffered_reader_expector_spec.rb
117
119
  - spec/matchers/exit_code_matcher_spec.rb
118
120
  - spec/matchers/output_matcher_spec.rb
119
121
  - spec/matchers_spec.rb