Sha256: 22ec73719daa171ac2fbb94f487ea95506d95c754ead99b4d804da39f9cf359e

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module ShellMock

  # Raised when shell operations are invoked
  class ShellNotAllowedError < StandardError

    def initialize(command)
      msg =  "Shell operation is not allowed: #{command}\n\n"
      msg << "You can stub this request with the following snippet:\n\n"
      msg << "stub_shell(\"#{command}\", output: '', exitstatus: 0)\n "
      super msg
    end

  end

  # Applies shell mock functionality
  def self.apply!
    Object.any_instance.stub(:`) do |object, command|
      raise ShellNotAllowedError, command
    end

    Object.any_instance.stub(:system) do |object, command|
      raise ShellNotAllowedError, command
    end
  end

end

class Object

  # Stubs shell operations matching given command
  #
  # Options:
  #   :output     (defaults to '')
  #   :exitstatus (defaults to 0)
  #
  def stub_shell(command, options = {})
    output = options.delete(:output) || ''
    exitstatus = options.delete(:exitstatus) || 0

    block = lambda { |command|
      if exitstatus.nonzero?
        Kernel.send :`, "test"
      else
        Kernel.send :`, "test success"
      end
      output
    }

    stub(:`).with(command, &block)
    stub(:system).with(command, &block)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
waddup-0.2.2 spec/support/shell_mock.rb