Module: GitReflow::RSpec::CommandLineHelpers

Defined in:
lib/git_reflow/rspec/command_line_helpers.rb

Instance Method Summary collapse

Instance Method Details

#reset_stubbed_command_lineObject



50
51
52
53
54
55
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 50

def reset_stubbed_command_line
  $commands_ran = []
  $stubbed_commands = {}
  $output = []
  $says = []
end

#stub_command(command:, return_value: "", options: {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 57

def stub_command(command:, return_value: "", options: {})
  $stubbed_commands[command] = return_value
  $stubbed_runners.each do |runner|
    allow(runner).to receive(:run).with(command, options) do |command, options|
      options = { loud: true, blocking: true }.merge(options || {})
      $commands_ran << Hashie::Mash.new(command: command, options: options)
      $stubbed_commands[command] = return_value
      raise GitReflow::Sandbox::CommandError.new(return_value, "\"#{command}\" failed to run.") if options[:raise]
    end
  end
end

#stub_command_lineObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 7

def stub_command_line
  $commands_ran     = []
  $stubbed_commands = {}
  $stubbed_runners  = Set.new
  $output           = []
  $says             = []

  stub_run_for GitReflow
  stub_run_for GitReflow::Sandbox
  stub_run_for GitReflow::Workflow
  stub_run_for GitReflow::Workflows::Core if defined? GitReflow::Workflows

  stub_output_for(GitReflow)
  stub_output_for(GitReflow::Sandbox)
  stub_output_for(GitReflow::Workflow)

  allow_any_instance_of(GitReflow::GitServer::PullRequest).to receive(:printf) do |format, *output|
    $output << Array(output).join(" ")
    output = ''
  end.and_return("")
end

#stub_command_line_inputs(inputs) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 77

def stub_command_line_inputs(inputs)
  allow_any_instance_of(HighLine).to receive(:ask) do |terminal, question|
    return_value = inputs[question]
    question = ""
    return_value
  end
end

#stub_command_line_inputs_for(module_to_stub, inputs) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 69

def stub_command_line_inputs_for(module_to_stub, inputs)
  allow(module_to_stub).to receive(:ask) do |terminal, question|
    return_value = inputs[question]
    question = ""
    return_value
  end
end

#stub_output_for(object_to_stub, method_to_stub = :puts) ⇒ Object



29
30
31
32
33
34
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 29

def stub_output_for(object_to_stub, method_to_stub = :puts)
  allow_any_instance_of(object_to_stub).to receive(method_to_stub) do |output|
    $output << output
    output = ''
  end
end

#stub_run_for(module_to_stub) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_reflow/rspec/command_line_helpers.rb', line 36

def stub_run_for(module_to_stub)
  $stubbed_runners << module_to_stub
  allow(module_to_stub).to receive(:run) do |command, options|
    options = { loud: true, blocking: true }.merge(options || {})
    $commands_ran << Hashie::Mash.new(command: command, options: options)
    ret_value = $stubbed_commands[command] || ""
    command = "" # we need this due to a bug in rspec that will keep this assignment on subsequent runs of the stub
    ret_value
  end
  allow(module_to_stub).to receive(:say) do |output, type|
    $says << {message: output, type: type}
  end
end