cli-test 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a85c63f129a1c824466424bb4266a4cb2c3967bc
4
+ data.tar.gz: 066540802b20b65c8a47c97935a1340ad17dfe21
5
+ SHA512:
6
+ metadata.gz: 1ceea902ab6160735d224b6501d295a2da4db2098a8f8cc297e0e810d4c8d085d13d160689bbf40a9e260228fb190fabc24083ddfde89afaca1fe36117c48e40
7
+ data.tar.gz: ec009c08963aeed9f96a9842255fc1d618c7eb0627b8fd907a815c0a937c617993b5f9038bf46e592df984a286b71f06d13f3245a9720307ba66428e4ef231d7
data/lib/cli-test.rb ADDED
@@ -0,0 +1,2 @@
1
+ # to allow either 'cli_test' or 'cli-test' to be required to load code
2
+ require 'cli_test'
@@ -0,0 +1,47 @@
1
+
2
+ module CliTest
3
+ ##
4
+ # Encapsulates the execution results from the command
5
+ class Execution
6
+ extend Forwardable
7
+
8
+ ##
9
+ # @!attribute [r] stdout
10
+ # @return [String] the stdout from the execution
11
+ # @!attribute [r] stderr
12
+ # @return [String] the stderr from the execution
13
+ # @!attribute [r] status
14
+ # @return [Process::Status] the status of the execution
15
+ attr_reader :stdout, :stderr, :status
16
+
17
+ ##
18
+ # creates an object of type CliTest::Execution
19
+ # @param [String] stdout the stdout of the execution
20
+ # @param [String] stderr the stderr of the execution
21
+ # @param [Process::Status] status the status of the execution
22
+ # @return [CliTest::Execution] the execution object
23
+ def initialize(stdout, stderr, status)
24
+ @stdout = stdout
25
+ @stderr = stderr
26
+ @status = status
27
+ end
28
+
29
+ ##
30
+ # returns true if the status code for the execution is
31
+ # is zero
32
+ # @return [Boolean] whether the execution was successful
33
+ def successful?
34
+ @status.success?
35
+ end
36
+
37
+ ##
38
+ # returns the stdout and stderr combined with a newline (<tt>\n</tt>)
39
+ # separator.
40
+ # @return [String] the combined stdout and stderr of the execution
41
+ def output
42
+ @stdout + "\n" + @stderr
43
+ end
44
+
45
+ def_delegator :@status, :exitstatus
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ module CliTest
2
+ ##
3
+ # Version constant that is loaded as part of the gemspec for
4
+ # gem version
5
+ VERSION = '1.0.0' unless defined? VERSION
6
+ end
data/lib/cli_test.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'open3'
2
+
3
+ require 'cli-test/version'
4
+ require 'cli-test/execution'
5
+
6
+ module CliTest
7
+
8
+ ##
9
+ # Error class that will be used for all errors raise by library
10
+ class Error < StandardError
11
+ end
12
+
13
+ ##
14
+ # executes the supplied script and returns the results. If stdin_data
15
+ # is supplied it is passed to the command.
16
+ # @param [String] command the command to be executed
17
+ # @param [Hash] options a hash of options for the execution. valid values are:
18
+ #
19
+ # - `stdin_data`: data to be passed into the STDIN of the command. If an array
20
+ # is passed in then the data is concatenated using <tt>\n</tt> to simulate
21
+ # multiple STDIN entries, as most scripts consider <tt>\n</tt> to be the end
22
+ # character for input.
23
+ # @return [CliTest::Execution] an Execution object containing the results
24
+ def execute(command, options={})
25
+ # if stdin_data is array then convert into a newline delineated string otherwise return as is
26
+ stdin_data = options[:stdin_data].respond_to?(:join) ? options[:stdin_data].join("\n") : options[:stdin_data]
27
+ stdout,stderr,status = Open3.capture3(command, stdin_data: stdin_data)
28
+ @last_execution = Execution.new(stdout,stderr,status)
29
+ rescue Exception => e
30
+ raise Error.new("command or file path: #{command} could not be executed. Error: #{e.message}")
31
+ end
32
+
33
+ ##
34
+ # executes the supplied Ruby script. Because Windows can't use shebangs to
35
+ # to figure out the correct intepreter 'ruby' is explicitly added to the command.
36
+ # If `use_bundler` is true then the script is executed with `bundle exec`.
37
+ # @param [String] script_path the relative or absolute path of a script to execute
38
+ # @param [Hash] options a hash of options to be used during the execution
39
+ # valid options are:
40
+ #
41
+ # - `use_bundler`: if set executes the script within the context of bundler
42
+ # - `stdin_data`: data to be passed in to the STDIN of the execution
43
+ # - `args`: any command line arguments to be supplied to the script. If an array is
44
+ # supplied then each entry is treated as a separate argument. Any arguments supplied
45
+ # are converted to strings using the `to_s` method before the script is executed
46
+ # @return [CliTest::Execution] the execution object from running the script
47
+ def execute_script(script_path, options={})
48
+ # if args is an array convert into space delineated string otherwise return as is
49
+ args = options[:args].respond_to?(:join) ? options[:args].join(" ") : options[:args]
50
+
51
+ cmd = script_path
52
+ cmd = "ruby #{cmd}" if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
53
+ cmd = "bundle exec #{cmd}" if options[:use_bundler]
54
+ cmd = "#{cmd} #{args}" if args
55
+
56
+ execute(cmd, options)
57
+ end
58
+
59
+ ##
60
+ # convenience method to return last execution object for testing
61
+ # @return [CliTest::Execution] the last execution object
62
+ def last_execution
63
+ unless @last_execution
64
+ raise Error.new('No executions have occured. Try running execute or execute_script')
65
+ end
66
+
67
+ @last_execution
68
+ end
69
+
70
+ alias_method :run, :execute
71
+ alias_method :run_script, :execute_script
72
+ alias_method :last_run, :last_execution
73
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Hoiberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A rack-test inspired gem that provides helper methods to make testing
14
+ command line scripts easier
15
+ email: tim.hoiberg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/cli-test.rb
21
+ - lib/cli-test/execution.rb
22
+ - lib/cli-test/version.rb
23
+ - lib/cli_test.rb
24
+ homepage: https://github.com/thoiberg/cli-test
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.6
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: gem to make testing command line scripts easier
48
+ test_files: []
49
+ has_rdoc: