module AwesomeSpawn::SpecHelper

Public Class Methods

disable_spawning(config, file_path: nil) click to toggle source

Disable spawning for specs

@example Disable spawning for all specs

RSpec.configure do |config|
  AwesomeSpawn::SpecHelper.disable_spawning(config)
end

@example Disable spawning for specs in a specific path

RSpec.configure do |config|
  AwesomeSpawn::SpecHelper.disable_spawning(config, file_path: "spec/models")
end

@param config [RSpec::Core::Configuration] RSpec configuration @param file_path [String] Restrict the disabling to a specific set of specs in the given path

# File lib/awesome_spawn/spec_helper.rb, line 19
def self.disable_spawning(config, file_path: nil)
  if file_path
    config.define_derived_metadata(:file_path => file_path) do |metadata|
      metadata[:uses_awesome_spawn] = true
    end
    config.include AwesomeSpawn::SpecHelper, :uses_awesome_spawn => true
    config.before(:each, :uses_awesome_spawn) { disable_spawning }
  else
    config.include AwesomeSpawn::SpecHelper
    config.before { disable_spawning }
  end
  config
end

Public Instance Methods

disable_spawning() click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 33
def disable_spawning
  allow(Open3).to receive(:capture3)
    .and_raise("Spawning is not permitted in specs.  Please change your spec to use expectations/stubs.")
end
enable_spawning() click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 38
def enable_spawning
  allow(Open3).to receive(:capture3).and_call_original
end
stub_bad_run(command, options = {}) click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 46
def stub_bad_run(command, options = {})
  stub_run(:bad, :run, command, options)
end
stub_bad_run!(command, options = {}) click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 54
def stub_bad_run!(command, options = {})
  stub_run(:bad, :run!, command, options)
end
stub_good_run(command, options = {}) click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 42
def stub_good_run(command, options = {})
  stub_run(:good, :run, command, options)
end
stub_good_run!(command, options = {}) click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 50
def stub_good_run!(command, options = {})
  stub_run(:good, :run!, command, options)
end

Private Instance Methods

stub_run(mode, method, command, options) click to toggle source
# File lib/awesome_spawn/spec_helper.rb, line 60
def stub_run(mode, method, command, options)
  options = options.dup
  output = options.delete(:output) || ""
  error  = options.delete(:error)  || (mode == :bad ? "Failure" : "")
  pid    = options.delete(:pid)    || ""
  exit_status = options.delete(:exit_status) || (mode == :bad ? 1 : 0)

  command_line = AwesomeSpawn.build_command_line(command, options[:params])

  args = [command, options]

  result = CommandResult.new(command_line, output, error, pid, exit_status)
  if method == :run! && mode == :bad
    error_message = CommandResultError.default_message(command, exit_status)
    error = CommandResultError.new(error_message, result)
    expect(AwesomeSpawn).to receive(method).with(*args).and_raise(error)
  else
    expect(AwesomeSpawn).to receive(method).with(*args).and_return(result)
  end
  result
end