Permalink
Browse files

Restructure a tad to account for 1.8-series rubies

  • Loading branch information...
1 parent 94dbfa3 commit 5fd37aa7003bf26923c79896974c818e014561bb @jyurek jyurek committed Jun 29, 2012
View
@@ -1,6 +1,6 @@
require 'rbconfig'
-require 'cocaine/command_line/runners'
require 'cocaine/command_line'
+require 'cocaine/command_line/runners'
require 'cocaine/exceptions'
module Cocaine
@@ -1,21 +1,6 @@
module Cocaine
class CommandLine
class << self
- attr_accessor :spawner
- end
-
- # Check for posix-spawn gem. If it is available it will prevent the
- # invoked processes from getting a copy of the ruby heap which can
- # lead to significant performance gains.
-
- begin
- require 'posix/spawn'
- @@posix_spawn_available = true
- rescue LoadError => e
- @@posix_spawn_available = false
- end
-
- class << self
attr_accessor :logger
def path
@@ -28,7 +13,12 @@ def path=(supplemental_path)
end
def posix_spawn_available?
- @@posix_spawn_available
+ @posix_spawn_available ||= begin
+ require 'posix/spawn'
+ true
+ rescue LoadError => e
+ false
+ end
end
def environment
@@ -1,25 +1,27 @@
module Cocaine
class CommandLine
class PosixRunner
+ if Cocaine::CommandLine.posix_spawn_available?
- def call(command, env = {})
- input, output = IO.pipe
- pid = spawn(env, command, :out => output)
- waitpid(pid)
- output.close
- input.read
- end
+ def call(command, env = {})
+ input, output = IO.pipe
+ pid = spawn(env, command, :out => output)
+ waitpid(pid)
+ output.close
+ input.read
+ end
- private
+ private
- def spawn(*args)
- POSIX::Spawn.spawn(*args)
- end
+ def spawn(*args)
+ POSIX::Spawn.spawn(*args)
+ end
- def waitpid(pid)
- Process.waitpid(pid)
- end
+ def waitpid(pid)
+ Process.waitpid(pid)
+ end
+ end
end
end
end
@@ -1,25 +1,27 @@
module Cocaine
class CommandLine
class ProcessRunner
+ if Process.respond_to?(:spawn)
- def call(command, env = {})
- input, output = IO.pipe
- pid = spawn(env, command, :out => output)
- waitpid(pid)
- output.close
- input.read
- end
+ def call(command, env = {})
+ input, output = IO.pipe
+ pid = spawn(env, command, :out => output)
+ waitpid(pid)
+ output.close
+ input.read
+ end
- private
+ private
- def spawn(*args)
- Process.spawn(*args)
- end
+ def spawn(*args)
+ Process.spawn(*args)
+ end
- def waitpid(pid)
- Process.waitpid(pid)
- end
+ def waitpid(pid)
+ Process.waitpid(pid)
+ end
+ end
end
end
end
@@ -1,19 +1,21 @@
require 'spec_helper'
describe Cocaine::CommandLine::PosixRunner do
- it 'runs the command given' do
- subject.call("echo hello").should == "hello\n"
- end
+ if Cocaine::CommandLine::posix_spawn_available?
+ it 'runs the command given' do
+ subject.call("echo hello").should == "hello\n"
+ end
- it 'modifies the environment and runs the command given' do
- subject.call("echo $yes", {"yes" => "no"}).should == "no\n"
- end
+ it 'modifies the environment and runs the command given' do
+ subject.call("echo $yes", {"yes" => "no"}).should == "no\n"
+ end
- it 'sets the exitstatus when a command completes' do
- subject.call("ruby -e 'exit 0'")
- $?.exitstatus.should == 0
- subject.call("ruby -e 'exit 5'")
- $?.exitstatus.should == 5
+ it 'sets the exitstatus when a command completes' do
+ subject.call("ruby -e 'exit 0'")
+ $?.exitstatus.should == 0
+ subject.call("ruby -e 'exit 5'")
+ $?.exitstatus.should == 5
+ end
end
end
@@ -1,18 +1,20 @@
require 'spec_helper'
describe Cocaine::CommandLine::ProcessRunner do
- it 'runs the command given' do
- subject.call("echo hello").should == "hello\n"
- end
+ if Process.respond_to?(:spawn)
+ it 'runs the command given' do
+ subject.call("echo hello").should == "hello\n"
+ end
- it 'modifies the environment and runs the command given' do
- subject.call("echo $yes", {"yes" => "no"}).should == "no\n"
- end
+ it 'modifies the environment and runs the command given' do
+ subject.call("echo $yes", {"yes" => "no"}).should == "no\n"
+ end
- it 'sets the exitstatus when a command completes' do
- subject.call("ruby -e 'exit 0'")
- $?.exitstatus.should == 0
- subject.call("ruby -e 'exit 5'")
- $?.exitstatus.should == 5
+ it 'sets the exitstatus when a command completes' do
+ subject.call("ruby -e 'exit 0'")
+ $?.exitstatus.should == 0
+ subject.call("ruby -e 'exit 5'")
+ $?.exitstatus.should == 5
+ end
end
end

0 comments on commit 5fd37aa

Please sign in to comment.