Class: Brut::SpecSupport::CLICommandSupport::CapturingExecutor

Inherits:
CLI::Executor
  • Object
show all
Defined in:
lib/brut/spec_support/cli_command_support.rb

Overview

A subclass of CLI::Executor that remembers the commands it was asked to execute, instead of actually executing them. This allows inspection later to see if expected commands had been run.

This is used by the ‘have_executed` matcher. It also is useful when yielding a block to `test_execution_context` so that you can configure command line executions to raise errors or provide specific output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CLI::Executor

#system

Constructor Details

#initializeCapturingExecutor

Returns a new instance of CapturingExecutor.



15
16
17
18
# File 'lib/brut/spec_support/cli_command_support.rb', line 15

def initialize(...)
  super
  @on_commands = {}
end

Instance Attribute Details

#commands_executedObject (readonly)

Returns the value of attribute commands_executed.



14
15
16
# File 'lib/brut/spec_support/cli_command_support.rb', line 14

def commands_executed
  @commands_executed
end

Instance Method Details

#on_command(command, raise_error: false, output: nil) ⇒ Object

Configure the behavior of a specific command. This must be called before any command line commands are invoked.

Parameters:

  • command (String)

    the exact command line invocation you want to configure.

  • raise_error (false|true) (defaults to: false)

    if true, an execption is raised when this command is executed.

  • output (String) (defaults to: nil)

    if not-nil, this output is produced by the command.



51
52
53
54
55
56
57
# File 'lib/brut/spec_support/cli_command_support.rb', line 51

def on_command(command, raise_error: false, output: nil)
  if raise_error
    @on_commands[command] = { raise_error: }
  elsif output
    @on_commands[command] = { output: }
  end
end

#system!(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/brut/spec_support/cli_command_support.rb', line 19

def system!(*args)
  @commands_executed ||= []
  command = if args.length == 1
              args[0]
            else
              args
            end
  @commands_executed << command
  on_command = @on_commands.select { |it,_|
    it == command || it.match?(command)
  }.map { |_,it| it }.first
  if on_command
    if on_command[:raise_error]
      raise Brut::CLI::SystemExecError.new(command,1)
    elsif on_command[:output]
      output = on_command[:output]
      if block_given?
        yield(output)
      else
        @out.puts output
      end
    end
  end
  nil
end