class RSpec::Bash::Script

Constants

MAIN_SCRIPT_FILE
NOOP
NOOP_BEHAVIOR

Attributes

exit_code[R]
source[R]
source_file[R]
stderr[R]
stdout[R]
stubs[R]

Public Class Methods

load(path) click to toggle source
# File lib/rspec/bash/script.rb, line 11
def self.load(path)
  new(File.read(path))
end
new(source, path = 'Anonymous') click to toggle source
# File lib/rspec/bash/script.rb, line 17
def initialize(source, path = 'Anonymous')
  @conditional_stubs = []
  @conditional_stub_calls = []
  @source = source
  @source_file = path
  @stubs = {}
  @stub_calls = Hash.new { |h, k| h[k] = [] }
  @stdout = ""
  @stderr = ""
  @exit_code = nil
end

Public Instance Methods

calls_for(name) click to toggle source
# File lib/rspec/bash/script.rb, line 74
def calls_for(name)
  @stub_calls[name.to_sym]
end
conditional_calls_for(expr) click to toggle source
# File lib/rspec/bash/script.rb, line 78
def conditional_calls_for(expr)
  @conditional_stub_calls.select { |x| x.index(expr) == 0 }
end
exact_conditional_calls_for(fullexpr) click to toggle source
# File lib/rspec/bash/script.rb, line 82
def exact_conditional_calls_for(fullexpr)
  @conditional_stub_calls.select { |x| x == fullexpr }
end
has_conditional_stubs?() click to toggle source
# File lib/rspec/bash/script.rb, line 70
def has_conditional_stubs?
  @conditional_stubs.any?
end
has_stub?(name) click to toggle source
# File lib/rspec/bash/script.rb, line 66
def has_stub?(name)
  @stubs.key?(name.to_sym)
end
inspect() click to toggle source
# File lib/rspec/bash/script.rb, line 33
def inspect
  "Script(\"#{File.basename(@source_file)}\")"
end
stub(fn, behaviors:, call_original: false, subshell: true) click to toggle source
# File lib/rspec/bash/script.rb, line 37
def stub(fn, behaviors:, call_original: false, subshell: true)
  @stubs[fn.to_sym] = {
    behaviors: behaviors.map { |x| StubBehavior.new(x) },
    subshell: subshell,
    call_original: call_original
  }
end
stub_conditional(expr, behaviors:) click to toggle source
# File lib/rspec/bash/script.rb, line 45
def stub_conditional(expr, behaviors:)
  @conditional_stubs << {
    behaviors: behaviors.map { |x| StubBehavior.new(x) },
    expr: expr,
  }
end
stubbed(name, args) click to toggle source
# File lib/rspec/bash/script.rb, line 52
def stubbed(name, args)
  apply_matching_behavior @stubs[name.to_sym], args
end
stubbed_conditional(fullexpr) click to toggle source
# File lib/rspec/bash/script.rb, line 56
def stubbed_conditional(fullexpr)
  conditional_stub = @conditional_stubs.detect { |x| fullexpr.index(x[:expr]) == 0 }

  if conditional_stub
    apply_matching_behavior conditional_stub, fullexpr
  else
    ""
  end
end
to_s() click to toggle source
# File lib/rspec/bash/script.rb, line 29
def to_s
  ScriptGenerator.generate(self)
end
track_call(name, args) click to toggle source
# File lib/rspec/bash/script.rb, line 86
def track_call(name, args)
  fail "#{name} is not stubbed" unless @stubs.key?(name.to_sym)

  @stub_calls[name.to_sym].push({ args: args })
end
track_conditional_call(fullexpr) click to toggle source
# File lib/rspec/bash/script.rb, line 92
def track_conditional_call(fullexpr)
  @conditional_stub_calls.push(fullexpr)
end
track_exit_code(code) click to toggle source
# File lib/rspec/bash/script.rb, line 96
def track_exit_code(code)
  @exit_code = code
end

Private Instance Methods

apply_matching_behavior(stub, args) click to toggle source
# File lib/rspec/bash/script.rb, line 102
def apply_matching_behavior(stub, args)
  behavior = stub[:behaviors].detect { |x| x.usable? && x.applicable?(args) }
  behavior ||= stub[:behaviors].detect { |x| x.usable? && x.context_free? }
  behavior ||= NOOP_BEHAVIOR
  behavior.apply!(args)
end