Skip to content

Commit a2af371

Browse files
author
Jon Yurek
committed
Modify the environment instead of prepending a path.
1 parent 5150939 commit a2af371

5 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
source 'http://rubygems.org'
22
group :development do
3+
gem 'rake'
34
gemspec :name => "cocaine"
45
end

‎Gemfile.lock‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ GEM
1111
diff-lcs (1.1.2)
1212
mocha (0.9.8)
1313
rake
14-
rake (0.8.7)
14+
rake (0.9.1)
1515
rspec (2.6.0)
1616
rspec-core (~> 2.6.0)
1717
rspec-expectations (~> 2.6.0)
1818
rspec-mocks (~> 2.6.0)
19-
rspec-core (2.6.0)
19+
rspec-core (2.6.3)
2020
rspec-expectations (2.6.0)
2121
diff-lcs (~> 1.1.2)
2222
rspec-mocks (2.6.0)
@@ -28,4 +28,5 @@ DEPENDENCIES
2828
bourne
2929
cocaine!
3030
mocha
31+
rake
3132
rspec

‎lib/cocaine/command_line.rb‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ def initialize(binary, params = "", options = {})
1515

1616
def command
1717
cmd = []
18-
cmd << full_path(@binary)
18+
cmd << @binary
1919
cmd << interpolate(@params, @options)
2020
cmd << bit_bucket if @swallow_stderr
21-
cmd.join(" ")
21+
cmd.join(" ").strip
2222
end
2323

2424
def run
25+
output = ''
2526
begin
26-
output = self.class.send(:'`', command)
27+
with_modified_path do
28+
output = self.class.send(:'`', command)
29+
end
2730
rescue Errno::ENOENT
2831
raise Cocaine::CommandNotFoundError
2932
end
@@ -38,8 +41,15 @@ def run
3841

3942
private
4043

41-
def full_path(binary)
42-
[self.class.path, binary].compact.join("/")
44+
def with_modified_path
45+
begin
46+
saved_path = ENV['PATH']
47+
extra_path = [self.class.path].flatten
48+
ENV['PATH'] = [ENV['PATH'], *extra_path].join(File::PATH_SEPARATOR)
49+
yield
50+
ensure
51+
ENV['PATH'] = saved_path
52+
end
4353
end
4454

4555
def interpolate(pattern, vars)

‎spec/cocaine/command_line_spec.rb‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@
1111
cmd.command.should == "convert a.jpg b.png"
1212
end
1313

14-
it "can set a default path and produce commands with that path" do
15-
Cocaine::CommandLine.path = "/opt/bin"
16-
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
17-
cmd.command.should == "/opt/bin/convert a.jpg b.png"
14+
it "specifies the path where the command should be run" do
15+
Cocaine::CommandLine.path = "/path/to/command/dir"
16+
cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
17+
cmd.command.should == "ruby -e 'puts ENV[%{PATH}]'"
18+
output = cmd.run
19+
output.should match(%r{/path/to/command/dir})
20+
end
21+
22+
it "specifies more than one path where the command should be run" do
23+
Cocaine::CommandLine.path = ["/path/to/command/dir", "/some/other/path"]
24+
cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
25+
output = cmd.run
26+
output.should match(%r{/path/to/command/dir})
27+
output.should match(%r{/some/other/path})
1828
end
1929

2030
it "can interpolate quoted variables into the parameters" do

‎spec/spec_helper.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'rspec'
2-
require 'spec/support/with_exitstatus'
2+
require './spec/support/with_exitstatus'
33
require 'mocha'
44
require 'bourne'
55
require 'cocaine'

0 commit comments

Comments
 (0)