Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added possibility to add env variables to the command.

Added missing test coverage for opening the actual pipe.
  • Loading branch information...
commit 9a5f71571c913f8aa72f07913b6c8bbfc90b412d 1 parent 6d83aff
@alloy alloy authored
Showing with 61 additions and 9 deletions.
  1. +1 −0  .gitignore
  2. +1 −1  lib/executioner.rb
  3. +59 −8 test/executioner_test.rb
View
1  .gitignore
@@ -0,0 +1 @@
+pkg/
View
2  lib/executioner.rb
@@ -16,7 +16,7 @@ def included(klass)
end
def execute(command, options={})
- options[:switch_stdout_and_stderr] = false if options[:switch_stdout_and_stderr].nil?
+ command = "#{options[:env].map { |k,v| "#{k}='#{v}'" }.join(' ')} #{command}" if options[:env]
Executioner.logger.debug("Executing: `#{command}'") if Executioner.logger
View
67 test/executioner_test.rb
@@ -6,18 +6,64 @@ class AClassThatUsesSubshells
executable :doesnotexistforsure
executable 'executable-with-dash'
executable :with_path, :path => '/path/to/executable'
+ executable :with_env, :path => '/path/to/executable', :env => { :foo => 'bar' }
+
+ public :execute
end
-describe "Executioner, the module" do
- it "should assign an optional logger" do
+describe "Executioner, when executing" do
+ before do
+ @object = AClassThatUsesSubshells.new
+ end
+
+ it "should open a pipe with the given command" do
+ Open3.expects(:popen3).with('/the/command')
+ @object.execute('/the/command')
+ end
+
+ it "should return the output received from stdout" do
+ stub_popen3 'stdout output', ''
+ @object.execute('/the/command').should == 'stdout output'
+ end
+
+ it "should return the output received from stderr if they should be reversed" do
+ stub_popen3 '', 'stderr output'
+ @object.execute('/the/command', :switch_stdout_and_stderr => true).should == 'stderr output'
+ end
+
+ it "should raise a Executioner::ProcessError if stdout is empty and stderr is not" do
+ stub_popen3 '', 'stderr output'
+ lambda { @object.execute('foo') }.should.raise Executioner::ProcessError
+ end
+
+ it "should raise a Executioner::ProcessError if stderr is empty and stdout is not and the streams are reversed" do
+ stub_popen3 'stdout output', ''
+ lambda { @object.execute('foo', :switch_stdout_and_stderr => true) }.should.raise Executioner::ProcessError
+ end
+
+ it "should prepend the given env variables" do
+ Open3.expects(:popen3).with("foo='bar' /the/command")
+ @object.execute('/the/command', :env => { :foo => :bar })
+ end
+
+ it "should log the command that's going to be executed if a logger is available" do
begin
- logger = Object.new
+ logger = mock('Logger')
Executioner.logger = logger
- Executioner.logger.should.be logger
+
+ logger.expects(:debug).with("Executing: `foo'")
+ stub_popen3
+ @object.execute('foo')
ensure
Executioner.logger = nil
end
end
+
+ private
+
+ def stub_popen3(stdout = '', stderr = '')
+ Open3.stubs(:popen3).yields(*['stdin', stdout, stderr].map { |s| StringIO.new(s) })
+ end
end
describe "Executioner, instance methods" do
@@ -76,7 +122,7 @@ class AClassThatUsesSubshells
end
it "should define an instance method which calls #execute with the correct path to the executable" do
- @object.expects(:execute).with('/bin/sh with some args', {:switch_stdout_and_stderr => false})
+ @object.expects(:execute).with('/bin/sh with some args', { :switch_stdout_and_stderr => false })
@object.sh 'with some args'
end
@@ -85,13 +131,13 @@ class AClassThatUsesSubshells
end
it "should be possible to switch stdin and stderr" do
- AClassThatUsesSubshells.class_eval { executable(:sh, {:switch_stdout_and_stderr => true}) }
- @object.expects(:execute).with('/bin/sh with some args', {:switch_stdout_and_stderr => true})
+ AClassThatUsesSubshells.class_eval { executable(:sh, { :switch_stdout_and_stderr => true }) }
+ @object.expects(:execute).with('/bin/sh with some args', { :switch_stdout_and_stderr => true })
@object.sh 'with some args'
end
it "should be possible to use the queue by default" do
- AClassThatUsesSubshells.class_eval { executable(:sh, {:use_queue => true}) }
+ AClassThatUsesSubshells.class_eval { executable(:sh, { :use_queue => true }) }
@object.expects(:execute).with('/bin/sh arg1 && /bin/sh arg2', {})
@object.sh 'arg1'
@object.sh 'arg2'
@@ -103,6 +149,11 @@ class AClassThatUsesSubshells
@object.with_path 'arg1'
end
+ it "should be possible to specify the env that's to be prepended to the command" do
+ @object.expects(:execute).with { |command, options| options[:env] == { :foo => 'bar' } }
+ @object.with_env 'arg1'
+ end
+
it "should be possible to find an executable" do
File.stubs(:exist?).with('/bin/sh').returns(true)
Executioner::ClassMethods.find_executable('sh').should == '/bin/sh'
Please sign in to comment.
Something went wrong with that request. Please try again.