Permalink
Browse files

Runners should be user-swappable

  • Loading branch information...
1 parent c33a48a commit b1e50737fde1830193435f56e8d603840cf11ca9 @jyurek jyurek committed Sep 12, 2012
Showing with 30 additions and 8 deletions.
  1. +14 −8 lib/cocaine/command_line.rb
  2. +16 −0 spec/cocaine/command_line_spec.rb
@@ -1,7 +1,7 @@
module Cocaine
class CommandLine
class << self
- attr_accessor :logger
+ attr_accessor :logger, :runner
def path
@supplemental_path
@@ -24,6 +24,18 @@ def posix_spawn_available?
def environment
@supplemental_environment ||= {}
end
+
+ def runner
+ @runner || best_runner
+ end
+
+ private
+
+ def best_runner
+ return PosixRunner.new if posix_spawn_available?
+ return ProcessRunner.new if Process.respond_to?(:spawn)
+ BackticksRunner.new
+ end
end
@environment = {}
@@ -33,7 +45,7 @@ def initialize(binary, params = "", options = {})
@binary = binary.dup
@params = params.dup
@options = options.dup
- @runner = best_runner
+ @runner = @options.delete(:runner) || self.class.runner
@logger = @options.delete(:logger) || self.class.logger
@swallow_stderr = @options.delete(:swallow_stderr)
@expected_outcodes = @options.delete(:expected_outcodes) || [0]
@@ -81,12 +93,6 @@ def environment
self.class.environment.merge(@environment)
end
- def best_runner
- return PosixRunner.new if self.class.posix_spawn_available?
- return ProcessRunner.new if Process.respond_to?(:spawn)
- BackticksRunner.new
- end
-
def interpolate(pattern, vars)
# interpolates :variables and :{variables}
pattern.gsub(%r#:(?:\w+|\{\w+\})#) do |match|
@@ -230,5 +230,21 @@
cmd = Cocaine::CommandLine.new("echo", "hello")
cmd.runner.class.should == Cocaine::CommandLine::PosixRunner
end
+
+ it "uses the BackticksRunner if the posix-spawn gem is available, but we told it to use Backticks all the time" do
+ Cocaine::CommandLine.stubs(:posix_spawn_available?).returns(true)
+ Cocaine::CommandLine.runner = Cocaine::CommandLine::BackticksRunner.new
+
+ cmd = Cocaine::CommandLine.new("echo", "hello")
+ cmd.runner.class.should == Cocaine::CommandLine::BackticksRunner
+ end
+
+ it "uses the BackticksRunner if the posix-spawn gem is available, but we told it to use Backticks" do
+ Cocaine::CommandLine.stubs(:posix_spawn_available?).returns(true)
+
+ cmd = Cocaine::CommandLine.new("echo", "hello", :runner => Cocaine::CommandLine::BackticksRunner.new)
+ cmd.runner.class.should == Cocaine::CommandLine::BackticksRunner
+ end
+
end
end

0 comments on commit b1e5073

Please sign in to comment.