buff-shell_out 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,10 +14,13 @@ module Buff
14
14
  #
15
15
  # @param [String] command
16
16
  # The command to execute
17
+ # @param [Hash] env
18
+ # A hash of enviroment variables to set during execution
17
19
  #
18
20
  # @return [ShellOut::Response]
19
- def shell_out(command)
20
- process_status, out, err = jruby? ? jruby_out(command) : mri_out(command)
21
+ def shell_out(command, env = {})
22
+ env = process_env_vars(env)
23
+ process_status, out, err = jruby? ? jruby_out(command, env) : mri_out(command, env)
21
24
  Response.new(process_status, out, err)
22
25
  end
23
26
 
@@ -25,11 +28,11 @@ module Buff
25
28
 
26
29
  # @param [String] command
27
30
  # The command to execute
28
- def mri_out(command)
31
+ def mri_out(command, env = {})
29
32
  out, err = Tempfile.new('mixin.shell_out.stdout'), Tempfile.new('mixin.shell_out.stderr')
30
33
 
31
34
  begin
32
- pid = Process.spawn(command, out: out.to_i, err: err.to_i)
35
+ pid = Process.spawn(env, command, out: out.to_i, err: err.to_i)
33
36
  pid, status = Process.waitpid2(pid)
34
37
  # Check if we're getting back a process status because win32-process 6.x was a fucking MURDERER.
35
38
  # https://github.com/djberg96/win32-process/blob/master/lib/win32/process.rb#L494-L519
@@ -48,10 +51,10 @@ module Buff
48
51
 
49
52
  # @param [String] command
50
53
  # The command to execute
51
- def jruby_out(command)
54
+ def jruby_out(command, env = {})
52
55
  out, err = StringIO.new, StringIO.new
53
56
  $stdout, $stderr = out, err
54
- system(command)
57
+ system(env, command)
55
58
 
56
59
  exitstatus = $?.exitstatus
57
60
  out.rewind
@@ -61,5 +64,17 @@ module Buff
61
64
  ensure
62
65
  $stdout, $stderr = STDOUT, STDERR
63
66
  end
67
+
68
+ # Builds a new hash from the given vars hash. The new hash's keys are all uppercase
69
+ # strings representing environment variables.
70
+ #
71
+ # @param [Hash] vars
72
+ # A hash of environment variables
73
+ def process_env_vars(vars)
74
+ vars.inject({}) do |acc, (key, val)|
75
+ acc[key.to_s.upcase] = val
76
+ acc
77
+ end
78
+ end
64
79
  end
65
80
  end
@@ -1,5 +1,5 @@
1
1
  module Buff
2
2
  module ShellOut
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ describe Buff::ShellOut do
20
20
  before { described_class.stub(jruby?: false) }
21
21
 
22
22
  it "delegates to #mri_out" do
23
- described_class.should_receive(:mri_out).with(command)
23
+ expect(described_class).to receive(:mri_out).with(command, {})
24
24
  result
25
25
  end
26
26
 
@@ -37,7 +37,7 @@ describe Buff::ShellOut do
37
37
  before { described_class.stub(jruby?: true) }
38
38
 
39
39
  it "delegates to #jruby_out" do
40
- described_class.should_receive(:jruby_out).with(command)
40
+ expect(described_class).to receive(:jruby_out).with(command, {})
41
41
  result
42
42
  end
43
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buff-shell_out
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-22 00:00:00.000000000 Z
12
+ date: 2014-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: buff-ruby_engine
@@ -216,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
216
  version: '0'
217
217
  segments:
218
218
  - 0
219
- hash: -3294574992538716380
219
+ hash: -1099429043491521833
220
220
  requirements: []
221
221
  rubyforge_project:
222
222
  rubygems_version: 1.8.23