Sha256: 7047e2db80706f762a3035efaca0c92e2de9e428085aae81bbd536bcc8691e3a

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

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

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

    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_option(option)
      @options = options

      self
    end

    def and_return(expected_output)
      @expected_output = expected_output
      @exitstatus      = 0

      self
    end

    def and_exit(exitstatus)
      @exitstatus = exitstatus

      self
    end

    def calls
      @calls ||= []

      marshaled_signatures.each do |marshaled_signature|
        @calls << Marshal.load(marshaled_signature)
      end

      @calls
    end

    def called_with(env, command, options)
      signature = Marshal.dump([env, command, options])

      writer.puts(signature)
    end

    private

    attr_reader :reader

    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

2 entries across 2 versions & 1 rubygems

Version Path
shell_mock-0.4.0 lib/shell_mock/command_stub.rb
shell_mock-0.3.3 lib/shell_mock/command_stub.rb