class Rspec::Bash::StubbedEnv

Constants

BASH_STUB
DISALLOWED_COMMANDS
RUBY_STUB
STUB_MARSHALLER_MAPPINGS
STUB_SCRIPT_MAPPINGS

Public Class Methods

new(stub_type) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 18
def initialize(stub_type)
  start_stub_server(stub_type)
end

Public Instance Methods

execute(command, env_vars = {}) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 34
def execute(command, env_vars = {})
  script_runner = "command source #{command}"
  script_wrapper = wrap_script(script_runner)
  execute_script(env_vars, script_wrapper)
end
execute_function(script, command, env_vars = {}) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 40
def execute_function(script, command, env_vars = {})
  script_runner = "command source #{script}\n#{command}"
  script_wrapper = wrap_script(script_runner)
  execute_script(env_vars, script_wrapper)
end
execute_inline(command_string, env_vars = {}) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 46
def execute_inline(command_string, env_vars = {})
  temp_command_file = Tempfile.new('inline-')
  temp_command_path = temp_command_file.path
  write_file(temp_command_path, command_string)
  stdout, stderr, status = execute(temp_command_path, env_vars)
  delete_file(temp_command_path)
  [stdout, stderr, status]
end
start_stub_server(stub_type) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 22
def start_stub_server(stub_type)
  tcp_server = create_tcp_server
  stub_server = create_stub_server(stub_type)
  stub_server.start(tcp_server)
end
stub_command(command) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 28
def stub_command(command)
  check_if_command_is_allowed(command)
  add_override_for_command(command)
  create_stubbed_command(command)
end

Private Instance Methods

add_override_for_command(command) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 113
def add_override_for_command(command)
  function_override = @stub_function.script(command).chomp
  @stub_wrapper.add_override(function_override)
end
check_if_command_is_allowed(command) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 107
def check_if_command_is_allowed(command)
  error_message = "Not able to stub command #{command}. Reserved for use by test wrapper."

  raise(error_message) if DISALLOWED_COMMANDS.include? command
end
create_stub_server(stub_type) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 73
def create_stub_server(stub_type)
  stub_marshaller = STUB_MARSHALLER_MAPPINGS[stub_type].new
  stub_script_class = STUB_SCRIPT_MAPPINGS[stub_type]
  @stub_wrapper = BashWrapper.new(@stub_server_port)
  @stub_function = StubFunction.new(@stub_server_port, stub_script_class)
  @call_log_manager = CallLogManager.new
  @call_conf_manager = CallConfigurationManager.new
  StubServer.new(@call_log_manager, @call_conf_manager, stub_marshaller)
end
create_stubbed_command(command) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 83
def create_stubbed_command(command)
  StubbedCommand.new(
    command,
    @call_log_manager,
    @call_conf_manager
  )
end
create_tcp_server() click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 67
def create_tcp_server
  tcp_server = TCPServer.new('localhost', 0)
  @stub_server_port = tcp_server.addr[1]
  tcp_server
end
delete_file(file_path) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 99
def delete_file(file_path)
  File.delete(file_path)
end
execute_script(env_vars, script) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 91
def execute_script(env_vars, script)
  Open3.capture3(env_vars, script)
end
wrap_script(script) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 95
def wrap_script(script)
  @stub_wrapper.wrap_script(script)
end
write_file(file_path, contents) click to toggle source
# File lib/rspec/bash/stubbed_env.rb, line 103
def write_file(file_path, contents)
  File.write(file_path, contents)
end