Sha256: 9c50a373904a9e0c76d7409697eae62a8f9dd19177de249783e2dde0d782461b

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'shell_mock/call_verifier'
require 'shell_mock/stub_registry'

module ShellMock
  class CommandStub
    attr_reader :command, :output, :exitstatus, :env, :options, :side_effect

    def initialize(command)
      @command     = command
      @env         = {}
      @options     = {}
      @side_effect = proc {}
      @exitstatus  = 0

      @reader, @writer = IO.pipe
    end

    def with_env(env)
      @env = env

      self
    end

    def with_options(options)
      @options = options

      self
    end

    def and_output(output)
      @output = output

      self
    end

    def and_return(output)
      self.
        and_output(output).
        and_exit(0)
    end

    def and_exit(exitstatus)
      @exitstatus = exitstatus

      self
    end

    def calls
      @calls ||= 0

      marshaled_signatures.each do |marshaled_signature|
        @calls += 1
      end

      @calls
    end

    def called_with(env, command, options)
      writer.puts("called\n")
    end

    def to_oneliner
      "echo '#{output}' && exit #{exitstatus}"
    end

    private

    attr_reader :reader, :writer

    def marshaled_signatures
      messages = ""

      loop do
        begin
          messages += reader.read_nonblock(1)
        rescue IO::WaitReadable
          break
        end
      end

      messages.split("\n")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shell_mock-0.5.0 lib/shell_mock/command_stub.rb