Skip to content
Browse files

Modify the environment instead of prepending a path.

  • Loading branch information...
1 parent 5150939 commit a2af3711ab656c7d8b0f8785fe0c57c9dc074ed8 @jyurek jyurek committed Jun 5, 2011
Showing with 34 additions and 12 deletions.
  1. +1 −0 Gemfile
  2. +3 −2 Gemfile.lock
  3. +15 −5 lib/cocaine/command_line.rb
  4. +14 −4 spec/cocaine/command_line_spec.rb
  5. +1 −1 spec/spec_helper.rb
View
1 Gemfile
@@ -1,4 +1,5 @@
source 'http://rubygems.org'
group :development do
+ gem 'rake'
gemspec :name => "cocaine"
end
View
5 Gemfile.lock
@@ -11,12 +11,12 @@ GEM
diff-lcs (1.1.2)
mocha (0.9.8)
rake
- rake (0.8.7)
+ rake (0.9.1)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
- rspec-core (2.6.0)
+ rspec-core (2.6.3)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
@@ -28,4 +28,5 @@ DEPENDENCIES
bourne
cocaine!
mocha
+ rake
rspec
View
20 lib/cocaine/command_line.rb
@@ -15,15 +15,18 @@ def initialize(binary, params = "", options = {})
def command
cmd = []
- cmd << full_path(@binary)
+ cmd << @binary
cmd << interpolate(@params, @options)
cmd << bit_bucket if @swallow_stderr
- cmd.join(" ")
+ cmd.join(" ").strip
end
def run
+ output = ''
begin
- output = self.class.send(:'`', command)
+ with_modified_path do
+ output = self.class.send(:'`', command)
+ end
rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError
end
@@ -38,8 +41,15 @@ def run
private
- def full_path(binary)
- [self.class.path, binary].compact.join("/")
+ def with_modified_path
+ 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
def interpolate(pattern, vars)
View
18 spec/cocaine/command_line_spec.rb
@@ -11,10 +11,20 @@
cmd.command.should == "convert a.jpg b.png"
end
- it "can set a default path and produce commands with that path" do
- Cocaine::CommandLine.path = "/opt/bin"
- cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
- cmd.command.should == "/opt/bin/convert a.jpg b.png"
+ it "specifies the path where the command should be run" do
+ Cocaine::CommandLine.path = "/path/to/command/dir"
+ cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
+ 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
it "can interpolate quoted variables into the parameters" do
View
2 spec/spec_helper.rb
@@ -1,5 +1,5 @@
require 'rspec'
-require 'spec/support/with_exitstatus'
+require './spec/support/with_exitstatus'
require 'mocha'
require 'bourne'
require 'cocaine'

0 comments on commit a2af371

Please sign in to comment.
Something went wrong with that request. Please try again.