Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add FakeRunner

  • Loading branch information...
commit f5e8acd5ce08c66bd0aa74c59cfdf94e7a99581e 1 parent 2881652
jon Yurek and Joel Quenneville authored jyurek committed
View
8 lib/cocaine/command_line.rb
@@ -29,6 +29,14 @@ def runner
@runner || best_runner
end
+ def fake!
+ @runner = FakeRunner.new
+ end
+
+ def unfake!
+ @runner = nil
+ end
+
private
def best_runner
View
1  lib/cocaine/command_line/runners.rb
@@ -1,3 +1,4 @@
require 'cocaine/command_line/runners/backticks_runner'
require 'cocaine/command_line/runners/process_runner'
require 'cocaine/command_line/runners/posix_runner'
+require 'cocaine/command_line/runners/fake_runner'
View
21 lib/cocaine/command_line/runners/fake_runner.rb
@@ -0,0 +1,21 @@
+module Cocaine
+ class CommandLine
+ class FakeRunner
+
+ attr_reader :commands
+
+ def initialize
+ @commands = []
+ end
+
+ def call(command, env = {})
+ commands << [command, env]
+ end
+
+ def ran?(predicate_command)
+ @commands.any?{|(command, env)| command =~ Regexp.new(predicate_command) }
+ end
+
+ end
+ end
+end
View
22 spec/cocaine/command_line/runners/fake_runner_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe Cocaine::CommandLine::FakeRunner do
+ it 'records commands' do
+ subject.call("some command", :environment)
+ subject.call("other command", :other_environment)
+ subject.commands.should eq [["some command", :environment], ["other command", :other_environment]]
+ end
+
+ it 'can tell if a command was run' do
+ subject.call("some command", :environment)
+ subject.call("other command", :other_environment)
+ subject.ran?("some command").should be_true
+ subject.ran?("no command").should be_false
+ end
+
+ it 'can tell if a command was run even if shell options were set' do
+ subject.call("something 2>/dev/null", :environment)
+ subject.ran?("something").should be_true
+ end
+
+end
View
22 spec/cocaine/command_line_spec.rb
@@ -232,5 +232,27 @@
cmd.runner.class.should == Cocaine::CommandLine::BackticksRunner
end
+ it "can go into 'Fake' mode" do
+ Cocaine::CommandLine.fake!
+
+ cmd = Cocaine::CommandLine.new("echo", "hello")
+ cmd.runner.class.should eq Cocaine::CommandLine::FakeRunner
+ end
+
+ it "can turn off Fake mode" do
+ Cocaine::CommandLine.fake!
+ Cocaine::CommandLine.unfake!
+
+ cmd = Cocaine::CommandLine.new("echo", "hello")
+ cmd.runner.class.should_not eq Cocaine::CommandLine::FakeRunner
+ end
+
+ it "can use a FakeRunner even if not in Fake mode" do
+ Cocaine::CommandLine.unfake!
+
+ cmd = Cocaine::CommandLine.new("echo", "hello", :runner => Cocaine::CommandLine::FakeRunner.new)
+ cmd.runner.class.should eq Cocaine::CommandLine::FakeRunner
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.