Module: CliTest

Defined in:
lib/cli_test.rb,
lib/cli-test/version.rb,
lib/cli-test/execution.rb

Defined Under Namespace

Classes: Error, Execution

Constant Summary collapse

VERSION =

Version constant that is loaded as part of the gemspec for gem version

'1.0.0'

Instance Method Summary collapse

Instance Method Details

#execute(command, options = {}) ⇒ CliTest::Execution Also known as: run

executes the supplied script and returns the results. If stdin_data is supplied it is passed to the command.

is passed in then the data is concatenated using <tt>\n</tt>  to simulate
multiple STDIN entries, as most scripts consider <tt>\n</tt> to be the end 
character for input.

Parameters:

  • command (String)

    the command to be executed

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

    a hash of options for the execution. valid values are:

    • ‘stdin_data`: data to be passed into the STDIN of the command. If an array

Returns:



24
25
26
27
28
29
30
31
# File 'lib/cli_test.rb', line 24

def execute(command, options={})
  # if stdin_data is array then convert into a newline delineated string otherwise return as is
  stdin_data = options[:stdin_data].respond_to?(:join) ? options[:stdin_data].join("\n") : options[:stdin_data]
  stdout,stderr,status = Open3.capture3(command, stdin_data: stdin_data)
  @last_execution = Execution.new(stdout,stderr,status)
rescue Exception => e
  raise Error.new("command or file path: #{command} could not be executed. Error: #{e.message}")
end

#execute_script(script_path, options = {}) ⇒ CliTest::Execution Also known as: run_script

executes the supplied Ruby script. Because Windows can’t use shebangs to to figure out the correct intepreter ‘ruby’ is explicitly added to the command. If ‘use_bundler` is true then the script is executed with `bundle exec`.

Parameters:

  • script_path (String)

    the relative or absolute path of a script to execute

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

    a hash of options to be used during the execution valid options are:

    • ‘use_bundler`: if set executes the script within the context of bundler

    • ‘stdin_data`: data to be passed in to the STDIN of the execution

    • ‘args`: any command line arguments to be supplied to the script. If an array is

    supplied then each entry is treated as a separate argument. Any arguments supplied are converted to strings using the ‘to_s` method before the script is executed

Returns:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cli_test.rb', line 47

def execute_script(script_path, options={})
  # if args is an array convert into space delineated string otherwise return as is
  args = options[:args].respond_to?(:join) ? options[:args].join(" ") : options[:args]
  
  cmd = script_path
  cmd = "ruby #{cmd}" if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
  cmd = "bundle exec #{cmd}" if options[:use_bundler]
  cmd = "#{cmd} #{args}" if args

  execute(cmd, options)
end

#last_executionCliTest::Execution Also known as: last_run

convenience method to return last execution object for testing

Returns:



62
63
64
65
66
67
68
# File 'lib/cli_test.rb', line 62

def last_execution
  unless @last_execution
    raise Error.new('No executions have occured. Try running execute or execute_script')
  end

  @last_execution
end