Module: CliTest
- Defined in:
- lib/cli_test.rb,
lib/cli-test/version.rb,
lib/cli-test/execution.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
Version constant that is loaded as part of the gemspec for gem version
'1.0.0'
Instance Method Summary collapse
-
#execute(command, options = {}) ⇒ CliTest::Execution
(also: #run)
executes the supplied script and returns the results.
-
#execute_script(script_path, options = {}) ⇒ CliTest::Execution
(also: #run_script)
executes the supplied Ruby script.
-
#last_execution ⇒ CliTest::Execution
(also: #last_run)
convenience method to return last execution object for testing.
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.
24 25 26 27 28 29 30 31 |
# File 'lib/cli_test.rb', line 24 def execute(command, ={}) # if stdin_data is array then convert into a newline delineated string otherwise return as is stdin_data = [:stdin_data].respond_to?(:join) ? [:stdin_data].join("\n") : [: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.}") 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`.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cli_test.rb', line 47 def execute_script(script_path, ={}) # if args is an array convert into space delineated string otherwise return as is args = [:args].respond_to?(:join) ? [:args].join(" ") : [:args] cmd = script_path cmd = "ruby #{cmd}" if RUBY_PLATFORM =~ /mswin|mingw|cygwin/ cmd = "bundle exec #{cmd}" if [:use_bundler] cmd = "#{cmd} #{args}" if args execute(cmd, ) end |
#last_execution ⇒ CliTest::Execution Also known as: last_run
convenience method to return last execution object for testing
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 |