Skip to content
This repository has been archived by the owner on Feb 7, 2018. It is now read-only.

Commit

Permalink
Modify the environment instead of prepending a path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jun 5, 2011
1 parent 5150939 commit a2af371
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions Gemfile
@@ -1,4 +1,5 @@
source 'http://rubygems.org' source 'http://rubygems.org'
group :development do group :development do
gem 'rake'
gemspec :name => "cocaine" gemspec :name => "cocaine"
end end
5 changes: 3 additions & 2 deletions Gemfile.lock
Expand Up @@ -11,12 +11,12 @@ GEM
diff-lcs (1.1.2) diff-lcs (1.1.2)
mocha (0.9.8) mocha (0.9.8)
rake rake
rake (0.8.7) rake (0.9.1)
rspec (2.6.0) rspec (2.6.0)
rspec-core (~> 2.6.0) rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0) rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0) rspec-mocks (~> 2.6.0)
rspec-core (2.6.0) rspec-core (2.6.3)
rspec-expectations (2.6.0) rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2) diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0) rspec-mocks (2.6.0)
Expand All @@ -28,4 +28,5 @@ DEPENDENCIES
bourne bourne
cocaine! cocaine!
mocha mocha
rake
rspec rspec
20 changes: 15 additions & 5 deletions lib/cocaine/command_line.rb
Expand Up @@ -15,15 +15,18 @@ def initialize(binary, params = "", options = {})


def command def command
cmd = [] cmd = []
cmd << full_path(@binary) cmd << @binary
cmd << interpolate(@params, @options) cmd << interpolate(@params, @options)
cmd << bit_bucket if @swallow_stderr cmd << bit_bucket if @swallow_stderr
cmd.join(" ") cmd.join(" ").strip
end end


def run def run
output = ''
begin begin
output = self.class.send(:'`', command) with_modified_path do
output = self.class.send(:'`', command)
end
rescue Errno::ENOENT rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError raise Cocaine::CommandNotFoundError
end end
Expand All @@ -38,8 +41,15 @@ def run


private private


def full_path(binary) def with_modified_path
[self.class.path, binary].compact.join("/") begin
saved_path = ENV['PATH']
extra_path = [self.class.path].flatten
ENV['PATH'] = [ENV['PATH'], *extra_path].join(File::PATH_SEPARATOR)
yield
ensure
ENV['PATH'] = saved_path
end
end end


def interpolate(pattern, vars) def interpolate(pattern, vars)
Expand Down
18 changes: 14 additions & 4 deletions spec/cocaine/command_line_spec.rb
Expand Up @@ -11,10 +11,20 @@
cmd.command.should == "convert a.jpg b.png" cmd.command.should == "convert a.jpg b.png"
end end


it "can set a default path and produce commands with that path" do it "specifies the path where the command should be run" do
Cocaine::CommandLine.path = "/opt/bin" Cocaine::CommandLine.path = "/path/to/command/dir"
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false) cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
cmd.command.should == "/opt/bin/convert a.jpg b.png" cmd.command.should == "ruby -e 'puts ENV[%{PATH}]'"
output = cmd.run
output.should match(%r{/path/to/command/dir})
end

it "specifies more than one path where the command should be run" do
Cocaine::CommandLine.path = ["/path/to/command/dir", "/some/other/path"]
cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
output = cmd.run
output.should match(%r{/path/to/command/dir})
output.should match(%r{/some/other/path})
end end


it "can interpolate quoted variables into the parameters" do it "can interpolate quoted variables into the parameters" do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,5 +1,5 @@
require 'rspec' require 'rspec'
require 'spec/support/with_exitstatus' require './spec/support/with_exitstatus'
require 'mocha' require 'mocha'
require 'bourne' require 'bourne'
require 'cocaine' require 'cocaine'
Expand Down

0 comments on commit a2af371

Please sign in to comment.