Skip to content
Browse files

Call ` method on CommandLine instances instead of the CommandLine cla…

…ss to aid testing.
  • Loading branch information...
1 parent aa36e45 commit 98dff3de80ab316033875c5c5bf93e8fa7366139 @bdurand bdurand committed Feb 29, 2012
Showing with 33 additions and 5 deletions.
  1. +1 −0 cocaine.gemspec
  2. +2 −2 lib/cocaine/command_line.rb
  3. +30 −3 spec/cocaine/command_line_spec.rb
View
1 cocaine.gemspec
@@ -20,5 +20,6 @@ Gem::Specification.new do |s|
s.add_development_dependency('bourne')
s.add_development_dependency('mocha')
s.add_development_dependency('rake')
+ s.add_development_dependency('posix-spawn')
end
View
4 lib/cocaine/command_line.rb
@@ -4,7 +4,6 @@ class CommandLine
# from getting a copy of the ruby heap which can lead to significant performance gains.
begin
require 'posix/spawn'
- extend POSIX::Spawn
rescue LoadError => e
# posix-spawn gem not available
end
@@ -21,6 +20,7 @@ def initialize(binary, params = "", options = {})
@swallow_stderr = @options.delete(:swallow_stderr)
@expected_outcodes = @options.delete(:expected_outcodes)
@expected_outcodes ||= [0]
+ extend(POSIX::Spawn) if defined?(POSIX::Spawn)
end
def command
@@ -36,7 +36,7 @@ def run
begin
with_modified_path do
@logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
- output = self.class.send(:'`', command)
+ output = send(:'`', command)
end
rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError
View
33 spec/cocaine/command_line_spec.rb
@@ -113,15 +113,15 @@
it "runs the command it's given and return the output" do
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
- cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
+ cmd.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(0) do
cmd.run.should == :correct_value
end
end
it "raises a CommandLineError if the result code from the command isn't expected" do
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
- cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
+ cmd.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do
lambda do
cmd.run
@@ -134,7 +134,7 @@
"a.jpg b.png",
:expected_outcodes => [0, 1],
:swallow_stderr => false)
- cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
+ cmd.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do
lambda do
cmd.run
@@ -175,4 +175,31 @@
cmd = Cocaine::CommandLine.new("echo", "'Logging!'", :logger => nil)
lambda { cmd.run }.should_not raise_error
end
+
+ describe "command execution" do
+ it "should use the ` method to invoke the command line" do
+ cmd = Cocaine::CommandLine.new("echo", "hello")
+ cmd.stubs(:`).with(anything).returns(nil)
+ cmd.run
+ cmd.should have_received(:`).with("echo hello")
+ end
+
+ it "should use POSIX::Spawn to create processes if it is available" do
+ cmd = Cocaine::CommandLine.new("echo", "hello")
+ cmd.method(:`).owner.should == POSIX::Spawn
+ cmd.run.chomp.should == "hello"
+ end
+
+ it "should use the default Kernel spawning to create processes if POSIX::Spawn is not available" do
+ spawn = POSIX::Spawn
+ POSIX.send(:remove_const, :Spawn)
+ begin
+ cmd = Cocaine::CommandLine.new("echo", "hello")
+ cmd.method(:`).owner.should == Kernel
+ cmd.run.chomp.should == "hello"
+ ensure
+ POSIX.const_set(:Spawn, spawn)
+ end
+ end
+ end
end

0 comments on commit 98dff3d

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