blue-shell 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/blue-shell.rb +16 -0
- data/lib/blue-shell/buffered_reader_expector.rb +2 -2
- data/lib/blue-shell/matchers.rb +2 -2
- data/lib/blue-shell/matchers/exit_code_matcher.rb +1 -1
- data/lib/blue-shell/matchers/output_matcher.rb +2 -5
- data/lib/blue-shell/runner.rb +14 -10
- data/lib/blue-shell/version.rb +1 -1
- data/spec/matchers/output_matcher_spec.rb +3 -2
- data/spec/matchers_spec.rb +0 -8
- data/spec/runner_spec.rb +51 -11
- metadata +3 -3
data/lib/blue-shell.rb
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
module BlueShell
|
2
|
+
class << self
|
3
|
+
attr_accessor :timeout
|
4
|
+
|
5
|
+
def with_timeout(timeout)
|
6
|
+
old_timeout = self.timeout
|
7
|
+
self.timeout = timeout
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
self.timeout = old_timeout
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
self.timeout = 30
|
15
|
+
end
|
16
|
+
|
1
17
|
require 'blue-shell/errors'
|
2
18
|
require 'blue-shell/matchers'
|
3
19
|
require 'blue-shell/matchers/exit_code_matcher'
|
@@ -9,7 +9,7 @@ module BlueShell
|
|
9
9
|
@output = ""
|
10
10
|
end
|
11
11
|
|
12
|
-
def expect(pattern
|
12
|
+
def expect(pattern)
|
13
13
|
case pattern
|
14
14
|
when String
|
15
15
|
pattern = Regexp.new(Regexp.quote(pattern))
|
@@ -18,7 +18,7 @@ module BlueShell
|
|
18
18
|
raise TypeError, "unsupported pattern class: #{pattern.class}"
|
19
19
|
end
|
20
20
|
|
21
|
-
result, buffer = read_pipe(timeout, pattern)
|
21
|
+
result, buffer = read_pipe(BlueShell.timeout, pattern)
|
22
22
|
|
23
23
|
@output << buffer
|
24
24
|
|
data/lib/blue-shell/matchers.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
module BlueShell
|
2
2
|
module Matchers
|
3
3
|
class OutputMatcher
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(expected_output, timeout = 30)
|
4
|
+
def initialize(expected_output)
|
7
5
|
@expected_output = expected_output
|
8
|
-
@timeout = timeout
|
9
6
|
end
|
10
7
|
|
11
8
|
def matches?(runner)
|
12
9
|
raise Errors::InvalidInputError unless runner.respond_to?(:expect)
|
13
|
-
@matched = runner.expect(@expected_output
|
10
|
+
@matched = runner.expect(@expected_output)
|
14
11
|
@full_output = runner.output
|
15
12
|
!!@matched
|
16
13
|
end
|
data/lib/blue-shell/runner.rb
CHANGED
@@ -3,6 +3,10 @@ require 'timeout'
|
|
3
3
|
|
4
4
|
module BlueShell
|
5
5
|
class Runner
|
6
|
+
class << self
|
7
|
+
alias_method :run, :new
|
8
|
+
end
|
9
|
+
|
6
10
|
def initialize(*args)
|
7
11
|
@stdout, slave = PTY.open
|
8
12
|
system('stty raw', :in => slave)
|
@@ -15,20 +19,20 @@ module BlueShell
|
|
15
19
|
if block_given?
|
16
20
|
yield self
|
17
21
|
else
|
18
|
-
wait_for_exit
|
22
|
+
wait_for_exit
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
|
-
|
23
|
-
|
26
|
+
def with_timeout(timeout, &block)
|
27
|
+
BlueShell.with_timeout(timeout, &block)
|
24
28
|
end
|
25
29
|
|
26
|
-
def expect(matcher
|
30
|
+
def expect(matcher)
|
27
31
|
case matcher
|
28
32
|
when Hash
|
29
|
-
expect_branches(matcher
|
33
|
+
expect_branches(matcher)
|
30
34
|
else
|
31
|
-
@expector.expect(matcher
|
35
|
+
@expector.expect(matcher)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
@@ -52,12 +56,12 @@ module BlueShell
|
|
52
56
|
@stdin.puts
|
53
57
|
end
|
54
58
|
|
55
|
-
def exit_code
|
59
|
+
def exit_code
|
56
60
|
return @code if @code
|
57
61
|
|
58
62
|
code = nil
|
59
63
|
begin
|
60
|
-
Timeout.timeout(timeout) do
|
64
|
+
Timeout.timeout(BlueShell.timeout) do
|
61
65
|
_, code = Process.waitpid2(@pid)
|
62
66
|
end
|
63
67
|
rescue Timeout::Error
|
@@ -97,9 +101,9 @@ module BlueShell
|
|
97
101
|
|
98
102
|
private
|
99
103
|
|
100
|
-
def expect_branches(branches
|
104
|
+
def expect_branches(branches)
|
101
105
|
branch_names = /#{branches.keys.collect { |k| Regexp.quote(k) }.join('|')}/
|
102
|
-
expected = @expector.expect(branch_names
|
106
|
+
expected = @expector.expect(branch_names)
|
103
107
|
return unless expected
|
104
108
|
|
105
109
|
data = expected.first.match(/(#{branch_names})$/)
|
data/lib/blue-shell/version.rb
CHANGED
@@ -4,9 +4,10 @@ module BlueShell
|
|
4
4
|
module Matchers
|
5
5
|
describe OutputMatcher, :ruby19 => true do
|
6
6
|
let(:expected_output) { "expected_output" }
|
7
|
-
let(:timeout) { 1 }
|
8
7
|
|
9
|
-
subject { OutputMatcher.new(expected_output
|
8
|
+
subject { OutputMatcher.new(expected_output) }
|
9
|
+
|
10
|
+
before { BlueShell.stub(:timeout).and_return(1) }
|
10
11
|
|
11
12
|
describe "#matches?" do
|
12
13
|
context "with something that isn't a runner" do
|
data/spec/matchers_spec.rb
CHANGED
@@ -12,14 +12,6 @@ module BlueShell
|
|
12
12
|
it "has synonyms" do
|
13
13
|
have_output("").should be_a(Matchers::OutputMatcher)
|
14
14
|
end
|
15
|
-
|
16
|
-
context "with an explicit timeout" do
|
17
|
-
it "returns an ExpectOutputMatcher" do
|
18
|
-
matcher = say("", 30)
|
19
|
-
matcher.should be_a(Matchers::OutputMatcher)
|
20
|
-
matcher.timeout.should == 30
|
21
|
-
end
|
22
|
-
end
|
23
15
|
end
|
24
16
|
|
25
17
|
describe "#have_exited_with" do
|
data/spec/runner_spec.rb
CHANGED
@@ -3,9 +3,17 @@ require 'tempfile'
|
|
3
3
|
|
4
4
|
module BlueShell
|
5
5
|
describe Runner do
|
6
|
-
|
6
|
+
around do |example|
|
7
|
+
begin
|
8
|
+
old_timeout = BlueShell.timeout
|
9
|
+
BlueShell.timeout = 1
|
10
|
+
example.call
|
11
|
+
ensure
|
12
|
+
BlueShell.timeout = old_timeout
|
13
|
+
end
|
14
|
+
end
|
7
15
|
|
8
|
-
describe "
|
16
|
+
describe ".run" do
|
9
17
|
let(:file) do
|
10
18
|
file = Tempfile.new('blue-shell-runner')
|
11
19
|
sleep 1 # wait one second to make sure touching the file does something measurable
|
@@ -18,6 +26,22 @@ module BlueShell
|
|
18
26
|
BlueShell::Runner.run("touch -a #{file.path}")
|
19
27
|
file.stat.atime.should > file.stat.mtime
|
20
28
|
end
|
29
|
+
|
30
|
+
context "wrapped by BlueShell.with_timeout" do
|
31
|
+
it "uses the given timeout" do
|
32
|
+
expect {
|
33
|
+
BlueShell.with_timeout(3) do
|
34
|
+
BlueShell::Runner.run("sleep 2")
|
35
|
+
end
|
36
|
+
}.to_not raise_error
|
37
|
+
|
38
|
+
expect {
|
39
|
+
BlueShell.with_timeout(1) do
|
40
|
+
BlueShell::Runner.run("sleep 2")
|
41
|
+
end
|
42
|
+
}.to raise_error(Timeout::Error)
|
43
|
+
end
|
44
|
+
end
|
21
45
|
end
|
22
46
|
|
23
47
|
describe "#success? and #successful?" do
|
@@ -56,7 +80,7 @@ module BlueShell
|
|
56
80
|
context "when the expected output never shows up" do
|
57
81
|
it "returns nil" do
|
58
82
|
BlueShell::Runner.run("echo the spanish inquisition") do |runner|
|
59
|
-
expect(runner.expect("something else"
|
83
|
+
expect(runner.expect("something else")).to be_nil
|
60
84
|
end
|
61
85
|
end
|
62
86
|
end
|
@@ -132,7 +156,7 @@ module BlueShell
|
|
132
156
|
context "and none of them match" do
|
133
157
|
it "returns nil when none of the branches match" do
|
134
158
|
BlueShell::Runner.run("echo not_a_number") do |runner|
|
135
|
-
expect(runner.expect({"1" => proc { 1 }}
|
159
|
+
expect(runner.expect({"1" => proc { 1 }})).to be_nil
|
136
160
|
end
|
137
161
|
end
|
138
162
|
end
|
@@ -219,8 +243,10 @@ module BlueShell
|
|
219
243
|
end
|
220
244
|
|
221
245
|
context "when the command doesn't finish within the timeout" do
|
246
|
+
before { BlueShell.stub(:timeout).and_return(3) }
|
247
|
+
|
222
248
|
it "raises a timeout error" do
|
223
|
-
BlueShell::Runner.run("sleep
|
249
|
+
BlueShell::Runner.run("sleep 4") do |runner|
|
224
250
|
expect { runner.exit_code }.to raise_error(Timeout::Error)
|
225
251
|
end
|
226
252
|
end
|
@@ -231,12 +257,6 @@ module BlueShell
|
|
231
257
|
end
|
232
258
|
end
|
233
259
|
end
|
234
|
-
|
235
|
-
it "uses the given timeout" do
|
236
|
-
BlueShell::Runner.run("sleep 2") do |runner|
|
237
|
-
expect { runner.exit_code(1) }.to raise_error(Timeout::Error)
|
238
|
-
end
|
239
|
-
end
|
240
260
|
end
|
241
261
|
|
242
262
|
context "#exited?" do
|
@@ -246,5 +266,25 @@ module BlueShell
|
|
246
266
|
end
|
247
267
|
end
|
248
268
|
end
|
269
|
+
|
270
|
+
describe "#with_timeout" do
|
271
|
+
it "uses the given timeout" do
|
272
|
+
BlueShell::Runner.run("sleep 2") do |runner|
|
273
|
+
expect {
|
274
|
+
runner.with_timeout(3) do
|
275
|
+
runner.exit_code
|
276
|
+
end
|
277
|
+
}.to_not raise_error
|
278
|
+
end
|
279
|
+
|
280
|
+
BlueShell::Runner.run("sleep 2") do |runner|
|
281
|
+
expect {
|
282
|
+
runner.with_timeout(1) do
|
283
|
+
runner.exit_code
|
284
|
+
end
|
285
|
+
}.to raise_error(Timeout::Error)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
249
289
|
end
|
250
290
|
end
|
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.
|
4
|
+
version: 0.3.0
|
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-
|
13
|
+
date: 2013-12-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.8.
|
96
|
+
rubygems_version: 1.8.23
|
97
97
|
signing_key:
|
98
98
|
specification_version: 3
|
99
99
|
summary: Friendly command-line test runner and matchers for shell scripting in ruby
|