Module: PryTestCase::CommandHelpers

Defined in:
lib/pry_test_case/command_helpers.rb

Overview

Mixin providing methods for executing Pry commands in various contexts and environments.

Instance Method Summary collapse

Instance Method Details

#command_exec_cli(command_string, options = {}) ⇒ Object

Evaluates the given command string as the Pry CLI would evaluate it. Behaves more like executing the given command in a live Pry session would. This means that Pry will intercept some behaviors that may be valuable to your tests. For more direct access to the results and errors of a command execution, see #command_exec_direct.

Parameters:

  • command_string (String)

    The command string to execute including any arguments that the command string might take.

  • options (Hash) (defaults to: {})

    Optional arguments for manipulating the command execution environment.

Options Hash (options):

  • :target (Binding) — default: TOPLEVEL_BINDING

    The target Binding that the command should be executed in. Also aliased as :context.

  • :show_output (Boolean) — default: true

    Flag indicating whether or not the output of the command should be displayed.

  • :output (IO) — default: Pry.config.output

    The IO object that the output of the command should be written to. If :show_output is false, the given IO object will be ignored and a new StringIO object will be used instead.

  • :commands (Pry::CommandSet) — default: Pry.config.commands

    The command set that the generated Pry instance should be created with.

Returns:

  • (Object)

    The result of the command execution. The exact value returned varies depending on the command.

See Also:



27
28
29
30
31
# File 'lib/pry_test_case/command_helpers.rb', line 27

def command_exec_cli(command_string, options = {})
  Pry.run_command(command_string, options)
  result = Pry.current[:pry_cmd_result]
  result && result.retval != Pry::Command::VOID_VALUE ? result.retval : result
end

#command_exec_direct(command_string, options = {}) ⇒ Object

Evaluates the given command string and runs the command directly without going through the Pry CLI eval cycle. Allows more direct access to errors and other things the CLI can make hard to get direct access to. To execute a command in an environment more similar to a live Pry session, see #command_exec_cli.

Parameters:

  • command_string (String)

    The command string to execute including any arguments that the command string might take.

  • options (Hash) (defaults to: {})

    Optional arguments for manipulating the command execution environment.

Options Hash (options):

  • :target (Binding) — default: TOPLEVEL_BINDING

    The target Binding that the command should be executed in. Also aliased as :context.

  • :output (IO) — default: Pry.config.output

    The IO object that the output of the command should be written to.

  • :command_set (Pry::CommandSet) — default: Pry.config.commands

    The command set that should be passed to the specified command when it is initialized.

  • :pry_instance (Pry)

    The Pry instance that should be passed to the specified command when it is initialized. By default a new Pry instance will be generated from the given options.

Returns:

  • (Object)

    The result of the command execution. The exact value returned varies depending on the command.

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pry_test_case/command_helpers.rb', line 56

def command_exec_direct(command_string, options = {})
  exec_options = {
    :target => TOPLEVEL_BINDING,
    :output => Pry.config.output,
    :command_set => Pry.config.commands,
  }.merge!(options)
  exec_options[:eval_string] = command_string
  exec_options[:pry_instance] ||= Pry.new({
    :target => exec_options[:target],
    :output => exec_options[:output],
    :commands => exec_options[:command_set],
  })
  args = command_string.split(/\s+/)
  match = args.shift
  Pry.commands.run_command(exec_options, match, *args)
end