module Dk::HasTheStubs::InstanceMethods

Public Instance Methods

local_cmd_stubs() click to toggle source

cmd stub api

# File lib/dk/has_the_stubs.rb, line 19
def local_cmd_stubs
  @local_cmd_stubs ||= []
end
remote_cmd_stubs() click to toggle source

ssh stub API

# File lib/dk/has_the_stubs.rb, line 41
def remote_cmd_stubs
  @remote_cmd_stubs ||= []
end
stub_cmd(cmd_str, args = nil, &block) click to toggle source
# File lib/dk/has_the_stubs.rb, line 23
def stub_cmd(cmd_str, args = nil, &block)
  args ||= {}

  cmd_str_proc    = get_cmd_ssh_proc(cmd_str)
  input_proc      = get_cmd_ssh_proc(args[:input])
  given_opts_proc = get_cmd_ssh_proc(args[:opts])

  local_cmd_stubs.unshift(
    Stub.new(cmd_str_proc, input_proc, given_opts_proc, block)
  )
end
stub_ssh(cmd_str, args = nil, &block) click to toggle source
# File lib/dk/has_the_stubs.rb, line 45
def stub_ssh(cmd_str, args = nil, &block)
  args ||= {}

  cmd_str_proc    = get_cmd_ssh_proc(cmd_str)
  input_proc      = get_cmd_ssh_proc(args[:input])
  given_opts_proc = get_cmd_ssh_proc(args[:opts])

  remote_cmd_stubs.unshift(
    Stub.new(cmd_str_proc, input_proc, given_opts_proc, block)
  )
end
unstub_all_cmds() click to toggle source
# File lib/dk/has_the_stubs.rb, line 35
def unstub_all_cmds
  local_cmd_stubs.clear
end
unstub_all_ssh() click to toggle source
# File lib/dk/has_the_stubs.rb, line 57
def unstub_all_ssh
  remote_cmd_stubs.clear
end

Private Instance Methods

build_local_cmd(task, cmd_str, input, given_opts) click to toggle source

if the cmd is stubbed, build a spy and apply the stub (or return the cached spy), otherwise let the runner decide how to handle the local cmd

# File lib/dk/has_the_stubs.rb, line 79
def build_local_cmd(task, cmd_str, input, given_opts)
  b = find_cmd_ssh_stub_block(local_cmd_stubs, task, cmd_str, input, given_opts)
  if b
    Local::CmdSpy.new(cmd_str, given_opts).tap(&b)
  else
    has_the_stubs_build_local_cmd(cmd_str, given_opts)
  end
end
build_remote_cmd(task, cmd_str, input, given_opts, ssh_opts) click to toggle source

if the cmd is stubbed, build a spy and apply the stub (or return the cached spy), otherwise let the runner decide how to handle the remote cmd; when building the spy use the ssh opts, this allows stubbing and calling ssh cmds with the same opts but also allows building a valid remote cmd that has an ssh host

# File lib/dk/has_the_stubs.rb, line 97
def build_remote_cmd(task, cmd_str, input, given_opts, ssh_opts)
  b = find_cmd_ssh_stub_block(remote_cmd_stubs, task, cmd_str, input, given_opts)
  if b
    Remote::CmdSpy.new(cmd_str, ssh_opts).tap(&b)
  else
    has_the_stubs_build_remote_cmd(cmd_str, ssh_opts)
  end
end
find_cmd_ssh_stub_block(stubs, task, cmd_str, input, given_opts) click to toggle source
# File lib/dk/has_the_stubs.rb, line 67
def find_cmd_ssh_stub_block(stubs, task, cmd_str, input, given_opts)
  stub = stubs.find do |stub|
    task.instance_eval(&stub.cmd_str_proc)    == cmd_str    &&
    task.instance_eval(&stub.input_proc)      == input      &&
    task.instance_eval(&stub.given_opts_proc) == given_opts
  end
  stub ? stub.block : nil
end
get_cmd_ssh_proc(obj) click to toggle source
# File lib/dk/has_the_stubs.rb, line 63
def get_cmd_ssh_proc(obj)
  obj.kind_of?(::Proc) ? obj : proc{ obj }
end
has_the_stubs_build_local_cmd(cmd_str, given_opts) click to toggle source
# File lib/dk/has_the_stubs.rb, line 88
def has_the_stubs_build_local_cmd(cmd_str, given_opts)
  raise NotImplementedError
end
has_the_stubs_build_remote_cmd(cmd_str, ssh_opts) click to toggle source
# File lib/dk/has_the_stubs.rb, line 106
def has_the_stubs_build_remote_cmd(cmd_str, ssh_opts)
  raise NotImplementedError
end