Module: Brut::SpecSupport::CLICommandSupport

Defined in:
lib/brut/spec_support/cli_command_support.rb

Overview

Convienience methods included in tests for CLI commands

Defined Under Namespace

Classes: CapturingExecutor

Instance Method Summary collapse

Instance Method Details

#test_execution_context(argv: [], options: {}, env: {}, stdin: StringIO.new, stdout: StringIO.new, stderr: StringIO.new, executor: :default, logger: :default) {|executor| ... } ⇒ Object

Create an ExecutionContext suitable for any command, but which allows manipulating the data as needed for your test, or to access the various IO streams used by the command. don’t want to pass in a value for this. The default is an implementation that captures all the commands that were executed for your later inspection via the ‘have_executed` matcher.

Parameters:

  • argv (Array<String>) (defaults to: [])

    the arguments remaining on the command line after parsing

  • options (Hash<Symbol|String,String>) (defaults to: {})

    hash of options. The preceding dashes **must be omitted**. If strings are passed as keys, they are converted to symbols.

  • env (Hash<String,String>) (defaults to: {})

    The UNIX environment to use.

  • stdin (StringIO) (defaults to: StringIO.new)

    IO to use as the standard input.

  • stdout (StringIO) (defaults to: StringIO.new)

    IO to use as the standard output. Note that you usually do not want to specify this, as the default is to create a ‘StringIO` which you can access directly from the returned execution context.

  • stderr (StringIO) (defaults to: StringIO.new)

    IO to use as the standard error. Note that you usually do not want to specify this, as the default is to create a ‘StringIO` which you can access directly from the returned execution context.

  • executor (Brut::CLI::Executor) (defaults to: :default)

    executor to use to execute sub commands. Note that you likely

Yields:

  • (executor)

    If a block is passed, the executor is yielded to allow for configuration of its behavior.

Yield Parameters:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/brut/spec_support/cli_command_support.rb', line 73

def test_execution_context(
  argv: [],
  options: {},
  env: {},
  stdin: StringIO.new,
  stdout: StringIO.new,
  stderr: StringIO.new,
  executor: :default,
  logger: :default,
  &block
)
  logger = if logger == :default
             Brut::CLI::Logger.new(app_name: $0, stdout:, stderr:)
           else
             logger
           end
  executor = if executor == :default 
               CapturingExecutor.new(out: stdout, err: stderr, logger:)
             else
               executor
             end

  if block
    block.(executor)
  end
  options = options.map { |key,value|
    [ key.to_sym, value ]
  }.to_h
  Brut::CLI::Commands::ExecutionContext.new(
    argv:,
    options: Brut::CLI::Options.new({ "log-level": "error" }.merge(options)),
    env: { "NO_COLOR" => "1" }.merge(env),
    stdin:,
    stdout:,
    stderr:,
    executor:
  )
end