process-helper 1.1.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/process-helper.rb +18 -16
- data/spec/process_helper_spec.rb +24 -20
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 07dac3b535cb2ec0ce43d8c9ead6f4a23a8b14683b4d234b643c942bb6f743dc
|
4
|
+
data.tar.gz: 3f2cf28692b50322e03293409c945d6a04ef619b226c2ff2c45475baeadea16f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6628d5907a0dd0cc684a6a2289ce707c1cfe0cf54f694e9b7162c4ef9bb06854cd3308ec0694d5fa093b974a17a45ed27c2a701338a2f7bf87c239a55440b750
|
7
|
+
data.tar.gz: 715afd5755003e0c7656bcc171495f61e4e11af76e4f5ba3d74e202c9d7e44c44301f1bc69cf6cb537347d16f3afa7f65ca4df711ec039dab71c317aa3c1717a
|
data/lib/process-helper.rb
CHANGED
@@ -20,27 +20,27 @@ module ProcessHelper
|
|
20
20
|
# output_to_wait_for:: Regex containing expected output, +start+ will block until a line of STDOUT matches this regex, if this is nil, then no waiting occurs.
|
21
21
|
# wait_timeout:: Timeout while waiting for particular output.
|
22
22
|
# env:: Hash of extra environment variables with which to start the process.
|
23
|
-
def start(command_and_args = [], output_to_wait_for = nil, wait_timeout = nil, env = {})
|
23
|
+
def start(command_and_args = [], output_to_wait_for = nil, wait_timeout = nil, env = {}, opts = {})
|
24
24
|
out_r, out_w = IO.pipe
|
25
|
-
|
25
|
+
@out_log = ProcessLog.new(out_r, @opts).start
|
26
|
+
if opts[:stderr]
|
27
|
+
err_r, err_w = IO.pipe
|
28
|
+
@err_log = ProcessLog.new(err_r, @opts).start
|
29
|
+
else
|
30
|
+
err_w = out_w
|
31
|
+
end
|
26
32
|
@pid = spawn(env, *command_and_args, :out => out_w, :err => err_w)
|
27
33
|
out_w.close
|
28
|
-
err_w.close
|
29
|
-
|
30
|
-
log = []
|
31
|
-
|
32
|
-
@out_log = ProcessLog.new(out_r, @opts, log).start
|
33
|
-
@err_log = ProcessLog.new(err_r, @opts).start
|
34
|
-
|
35
|
-
@out_log.wait_for_output(output_to_wait_for, :timeout => wait_timeout) if output_to_wait_for
|
34
|
+
err_w.close if opts[:stderr]
|
35
|
+
@out_log.wait_for_output(output_to_wait_for, :timeout => wait_timeout) unless output_to_wait_for.nil?
|
36
36
|
end
|
37
37
|
|
38
38
|
# returns true if the process exited with an exit code of 0.
|
39
39
|
def wait_for_exit
|
40
40
|
@out_log.wait
|
41
|
-
@err_log.wait
|
41
|
+
@err_log.wait unless @err_log.nil?
|
42
42
|
|
43
|
-
Process.wait
|
43
|
+
Process.wait(@pid)
|
44
44
|
@exit_status = $CHILD_STATUS
|
45
45
|
|
46
46
|
@pid = nil
|
@@ -57,7 +57,8 @@ module ProcessHelper
|
|
57
57
|
# * +:out+
|
58
58
|
# * +:err+
|
59
59
|
def get_log(which)
|
60
|
-
_get_log(which)
|
60
|
+
log = _get_log(which)
|
61
|
+
log.nil? ? [] : log.to_a
|
61
62
|
end
|
62
63
|
|
63
64
|
# Gets an array containing all the lines for the specified stream, emptying the stored buffer.
|
@@ -65,7 +66,8 @@ module ProcessHelper
|
|
65
66
|
# * +:out+
|
66
67
|
# * +:err+
|
67
68
|
def get_log!(which)
|
68
|
-
_get_log(which)
|
69
|
+
log = _get_log(which)
|
70
|
+
log.nil? ? [] : log.drain
|
69
71
|
end
|
70
72
|
|
71
73
|
# Blocks the current thread until the specified regex has been matched in the output.
|
@@ -121,10 +123,10 @@ module ProcessHelper
|
|
121
123
|
def wait_for_output(regex, opts = {})
|
122
124
|
opts = { :poll_rate => 0.25 }.merge(opts)
|
123
125
|
opts[:timeout] ||= 30
|
124
|
-
cutoff =
|
126
|
+
cutoff = Time.now + opts[:timeout].to_i
|
125
127
|
until _any_line_matches(regex)
|
126
128
|
sleep(opts[:poll_rate])
|
127
|
-
fail "Timeout of #{opts[:timeout]} seconds exceeded while waiting for output that matches '#{regex}'" if
|
129
|
+
fail "Timeout of #{opts[:timeout]} seconds exceeded while waiting for output that matches '#{regex}'" if Time.now > cutoff
|
128
130
|
fail "EOF encountered while waiting for output that matches '#{regex}'" if eof and !_any_line_matches(regex)
|
129
131
|
end
|
130
132
|
end
|
data/spec/process_helper_spec.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
def run(*args)
|
4
|
+
process = ProcessHelper::ProcessHelper.new
|
5
|
+
process.start(*args)
|
6
|
+
process.wait_for_exit
|
7
|
+
process
|
8
|
+
end
|
9
|
+
|
3
10
|
module ProcessHelper
|
11
|
+
|
4
12
|
describe ProcessHelper do
|
5
|
-
context '
|
13
|
+
context 'Tests' do
|
6
14
|
it 'It should start a process and capture the exit status (success)' do
|
7
15
|
process = ProcessHelper.new
|
8
16
|
process.start('true')
|
@@ -33,23 +41,20 @@ module ProcessHelper
|
|
33
41
|
end
|
34
42
|
|
35
43
|
it 'It should expose stdout' do
|
36
|
-
process =
|
37
|
-
process.start(['sh', '-c', 'echo hello ; echo there'])
|
38
|
-
process.wait_for_exit
|
44
|
+
process = run(['sh', '-c', 'echo hello ; echo there'])
|
39
45
|
out = process.get_log(:out)
|
40
|
-
expect(out
|
41
|
-
|
42
|
-
|
46
|
+
expect(out).to eq(["hello\n", "there\n"])
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'It should expose stdout and stderr' do
|
50
|
+
process = run(['sh', '-c', 'echo hello; echo there >&2'])
|
51
|
+
expect(process.get_log(:out)).to eq(["hello\n", "there\n"])
|
52
|
+
expect(process.get_log(:err)).to eq([])
|
43
53
|
end
|
44
54
|
|
45
55
|
it 'It should expose stderr' do
|
46
|
-
process =
|
47
|
-
process.
|
48
|
-
process.wait_for_exit
|
49
|
-
err = process.get_log(:err)
|
50
|
-
expect(err.count).to eq(2)
|
51
|
-
expect(err[0]).to eq("hello\n")
|
52
|
-
expect(err[1]).to eq("there\n")
|
56
|
+
process = run(['sh', '-c', 'echo hello >&2; echo there >&2'], nil, nil, {}, stderr: true)
|
57
|
+
expect(process.get_log(:err)).to eq(["hello\n", "there\n"])
|
53
58
|
end
|
54
59
|
|
55
60
|
it 'It should expose the pid' do
|
@@ -63,9 +68,7 @@ module ProcessHelper
|
|
63
68
|
|
64
69
|
it 'It should timestamp the logs' do
|
65
70
|
t0 = Time.now
|
66
|
-
process =
|
67
|
-
process.start(['sh', '-c', 'echo a ; sleep 1 ; echo b ; sleep 1 ; echo c'])
|
68
|
-
process.wait_for_exit
|
71
|
+
process = run(['sh', '-c', 'echo a ; sleep 1 ; echo b ; sleep 1 ; echo c'])
|
69
72
|
t1 = Time.now
|
70
73
|
|
71
74
|
out = process.get_log(:out)
|
@@ -85,7 +88,7 @@ module ProcessHelper
|
|
85
88
|
process = ProcessHelper.new
|
86
89
|
process.start(
|
87
90
|
['sh', '-c', 'echo frog >&2 ; sleep 1 ; echo cat ; sleep 1 ; echo dog ; sleep 1 ; echo frog'],
|
88
|
-
/fro
|
91
|
+
/fro/, nil, {}, stderr: true
|
89
92
|
)
|
90
93
|
t1 = Time.now
|
91
94
|
|
@@ -117,7 +120,7 @@ module ProcessHelper
|
|
117
120
|
process = ProcessHelper.new
|
118
121
|
process.start(
|
119
122
|
['sh', '-c', 'echo frog >&2 ; sleep 1 ; echo cat ; sleep 1 ; echo dog ; sleep 1 ; echo frog; sleep 3; echo goat'],
|
120
|
-
/fro
|
123
|
+
/fro/, nil, {}, stderr: true
|
121
124
|
)
|
122
125
|
t1 = Time.now
|
123
126
|
|
@@ -133,7 +136,8 @@ module ProcessHelper
|
|
133
136
|
it 'It should support draining the logs' do
|
134
137
|
process = ProcessHelper.new
|
135
138
|
process.start(
|
136
|
-
['bash', '-c', 'for ((i=0; $i<10; i=$i+1)) ; do echo out $i ; echo err $i >&2 ; sleep 1 ; done']
|
139
|
+
['bash', '-c', 'for ((i=0; $i<10; i=$i+1)) ; do echo out $i ; echo err $i >&2 ; sleep 1 ; done'],
|
140
|
+
nil, nil, {}, stderr: true
|
137
141
|
)
|
138
142
|
|
139
143
|
sleep 3
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: process-helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rachel evans
|
@@ -56,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
requirements: []
|
59
|
-
|
60
|
-
rubygems_version: 2.4.3
|
59
|
+
rubygems_version: 3.2.3
|
61
60
|
signing_key:
|
62
61
|
specification_version: 4
|
63
62
|
summary: Utility for managing sub processes.
|