Sha256: 03c9a9feb7a51c5895cba71c067703700e1cb399b9fdb6cf07e75c0ba4d479a1

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'shell_mock/stub_registry'

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

    def initialize(command)
      @command     = command
      @side_effect = proc {}

      @reader, @writer = IO.pipe

      with_env({})
      with_options({})
      and_output(nil)
      and_succeed
    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 and_succeed
      and_exit(0)
    end

    def and_fail
      and_exit(1)
    end

    def runs
      @runs ||= 0

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

      @runs
    end

    def ran
      writer.write("R")
    end

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

    private

    attr_reader :reader, :writer
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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