Class: RSpec::Bash::ScriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/bash/script_generator.rb

Constant Summary collapse

NOOP =
lambda { |*| '' }
SCRIPTS =
{
  conditionals: File.expand_path('../script_generator/conditional.sh', __FILE__),
  controller: File.expand_path('../script_generator/controller.sh', __FILE__)
}
SPIES =
{
  builtin: lambda { |name|
    <<-EOF
      #{name}() {
        __rspec_bash_call_stubbed '#{name}' "${@}"

        builtin #{name} $@
      }
    EOF
  },
}
STUBS =
{
  function: lambda { |name|
    <<-EOF
      #{name}() {
        __rspec_bash_call_stubbed '#{name}' "${@}"
      }
    EOF
  },

  function_in_subshell: lambda { |name|
    "#{name}()(__rspec_bash_call_stubbed '#{name}' \"${@}\")\n"
  }
}

Class Method Summary collapse

Class Method Details

.generate(script) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec/bash/script_generator.rb', line 34

def self.generate(script)
  buffer = ""
  buffer << "builtin . '#{SCRIPTS[:controller]}'\n"
  buffer << "builtin . '#{SCRIPTS[:conditionals]}'\n" if script.has_conditional_stubs?
  buffer << "\n"

  script.stubs.keys.each do |name|
    stub_def = script.stubs[name]

    if stub_def[:call_original] then
      buffer << SPIES[:builtin].call(name)
    elsif stub_def[:subshell] == false then
      buffer << STUBS[:function].call(name)
    else
      buffer << STUBS[:function_in_subshell].call(name)
    end
  end

  buffer << "\n"
  buffer << script.source
  buffer
end