Sha256: bb219f451ec74a47fbc24749b26ee019562cfda9347d863a5992b779c14c3ea7

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 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(:`).and_return do |foo|
      raise ShellNotAllowedError, foo
    end

    Object.any_instance.stub(:system).and_return do |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 {
      if exitstatus.nonzero?
        Kernel.send :`, "test"
      else
        Kernel.send :`, "test success"
      end
      output
    }

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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
waddup-0.2.1 spec/support/shell_mock.rb
waddup-0.2.0 spec/support/shell_mock.rb
waddup-0.1.0 spec/support/shell_mock.rb