Sha256: 4c5da0f88ca2423001b25fc11a5f91113df92f810ff3fc85b42cd4d0b6caaad3

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# 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

# Prevent invoking shell operations
class Object

  def `(command)
    raise ShellNotAllowedError, command
  end

  def system(command)
    raise ShellNotAllowedError, command
  end

  # 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

1 entries across 1 versions & 1 rubygems

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