process-helper 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5801c46c90163e46beb528091ad2dfa932cafd0
4
- data.tar.gz: c8fa6a8f6b5c503b3ea2369bd0b15264badef55d
3
+ metadata.gz: 2ce039ab19c243fb4798183b2ae3acab432de81c
4
+ data.tar.gz: 440ba72e2b87290a0f1f3a00e220cad071aefd8e
5
5
  SHA512:
6
- metadata.gz: f78e7ad32fd2720831db9ff75982665e305f975c91be07c659f4618c885d83e86543705efcfc56d4b2b90f516b6bda43db36724082fd1ff3e787c6fd94fb390b
7
- data.tar.gz: 4088d5ed29f3ad6ab9c6a01c38655c318a33b9fe35bfafb59ea6299ad6ef11278a7f2d1f6229870591d1a5456c7732450bf2e1b5ee37a2870777c5e7614ded23
6
+ metadata.gz: 8fd6ac4b78c2c8709e6d14380474ecbba215f7be92f4cf3dbcce805823e407c2540539cd5da087201c1e567be461789a790496bb6076cae2bae2c164f23f8ebc
7
+ data.tar.gz: 9aad6802e56a4b0ce403c44e345e41bceecf57ba9bd9353568edeead4a83f580daf2ba2780a7bee6d5f8f60c8b50bfac513b9ffe924a5a26db618a0375fc68e0
@@ -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
- err_r, err_w = IO.pipe
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 @pid
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).to_a
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).drain
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 = DateTime.now + Rational(opts[:timeout].to_i, 86_400)
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 DateTime.now > cutoff
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
@@ -1,6 +1,14 @@
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
13
  context 'Things...' do
6
14
  it 'It should start a process and capture the exit status (success)' do
@@ -33,23 +41,20 @@ module ProcessHelper
33
41
  end
34
42
 
35
43
  it 'It should expose stdout' do
36
- process = ProcessHelper.new
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.count).to eq(2)
41
- expect(out[0]).to eq("hello\n")
42
- expect(out[1]).to eq("there\n")
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 = ProcessHelper.new
47
- process.start(['sh', '-c', 'echo hello >&2; echo there >&2'])
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 = ProcessHelper.new
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.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rachel evans
@@ -57,10 +57,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  version: '0'
58
58
  requirements: []
59
59
  rubyforge_project:
60
- rubygems_version: 2.4.3
60
+ rubygems_version: 2.5.1
61
61
  signing_key:
62
62
  specification_version: 4
63
63
  summary: Utility for managing sub processes.
64
64
  test_files:
65
- - spec/process_helper_spec.rb
66
65
  - spec/spec_helper.rb
66
+ - spec/process_helper_spec.rb