cli_test 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/.gemtest +0 -0
- data/History.txt +6 -0
- data/Manifest.txt +7 -0
- data/README.rdoc +51 -0
- data/Rakefile +11 -0
- data/lib/cli_test.rb +80 -0
- data/test/test_cli_test.rb +111 -0
- metadata +74 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= cli_test
|
2
|
+
|
3
|
+
* http://github.com/julik/cli_test
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Assists in testing commandline applications (run an application qucikly, read out stderr, stdout and status, check option combinations)
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
Uses in-process loading on non-forking architectures
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
@app = CLITest.new("/usr/local/bin/ruby")
|
16
|
+
status, out, err = @app.run("--version")
|
17
|
+
out #=> "ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-darwin9.8.0]\n"
|
18
|
+
assert_match /revision/, out, "Should output revision information"
|
19
|
+
|
20
|
+
== REQUIREMENTS:
|
21
|
+
|
22
|
+
* FIX (list of requirements)
|
23
|
+
|
24
|
+
== INSTALL:
|
25
|
+
|
26
|
+
* FIX (sudo gem install, anything else)
|
27
|
+
|
28
|
+
== LICENSE:
|
29
|
+
|
30
|
+
(The MIT License)
|
31
|
+
|
32
|
+
Copyright (c) 2011 FIX
|
33
|
+
|
34
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
35
|
+
a copy of this software and associated documentation files (the
|
36
|
+
'Software'), to deal in the Software without restriction, including
|
37
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
38
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
39
|
+
permit persons to whom the Software is furnished to do so, subject to
|
40
|
+
the following conditions:
|
41
|
+
|
42
|
+
The above copyright notice and this permission notice shall be
|
43
|
+
included in all copies or substantial portions of the Software.
|
44
|
+
|
45
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
46
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
47
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
48
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
49
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
50
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
51
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/cli_test.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require "stringio"
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "open3"
|
5
|
+
rescue LoadError
|
6
|
+
end
|
7
|
+
|
8
|
+
class CLITest
|
9
|
+
VERSION = '1.0.0'
|
10
|
+
|
11
|
+
def initialize(binary_path)
|
12
|
+
@binary_path = File.expand_path(binary_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(commandline_arguments)
|
16
|
+
arguments_as_argv = cli_arguments_to_argv(commandline_arguments)
|
17
|
+
(not_unix? || !ruby19?) ? in_process(arguments_as_argv) : in_fork(arguments_as_argv)
|
18
|
+
end
|
19
|
+
|
20
|
+
def not_unix?
|
21
|
+
RUBY_PLATFORM =~ /mswin|jruby/
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def ruby19?
|
26
|
+
!(RUBY_VERSION < "1.9")
|
27
|
+
end
|
28
|
+
|
29
|
+
# Run the binary under test with passed options, and return [exit_code, stdout_content, stderr_content]
|
30
|
+
# There is a limitation however: your app should be using $stdout and $stdeer and NOT their constant
|
31
|
+
# equivalents.
|
32
|
+
def in_process(commandline_arguments)
|
33
|
+
old_stdout, old_stderr, old_argv = $stdout, $stderr, ARGV.dup
|
34
|
+
os, es = StringIO.new, StringIO.new
|
35
|
+
begin
|
36
|
+
$stdout, $stderr, verbosity = os, es, $VERBOSE
|
37
|
+
ARGV.replace(commandline_arguments)
|
38
|
+
$VERBOSE = false
|
39
|
+
|
40
|
+
load(@binary_path)
|
41
|
+
|
42
|
+
return [0, os.string, es.string]
|
43
|
+
rescue SystemExit => boom # The binary uses exit(), we use that to preserve the output code
|
44
|
+
return [boom.status, os.string, es.string]
|
45
|
+
ensure
|
46
|
+
$VERBOSE = verbosity
|
47
|
+
ARGV.replace(old_argv)
|
48
|
+
$stdout, $stderr = old_stdout, old_stderr
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def cli_arguments_to_argv(arguments_string_or_array)
|
53
|
+
return arguments_string_or_array if arguments_string_or_array.is_a?(Array)
|
54
|
+
arguments_string_or_array.split
|
55
|
+
end
|
56
|
+
|
57
|
+
# Run the binary under test with passed options, and return [exit_code, stdout_content, stderr_content]
|
58
|
+
# There is a limitation however: your app should be using $stdout and $stdeer and NOT their constant
|
59
|
+
# equivalents.
|
60
|
+
def in_fork(args)
|
61
|
+
args.unshift(@binary_path)
|
62
|
+
|
63
|
+
if ruby19?
|
64
|
+
Open3.popen3(ENV, args.join(' ')) do |stdin, stdout, stderr, wait_thr|
|
65
|
+
# Process::Status object returned in Ruby 1.9
|
66
|
+
[make_signed(wait_thr.value.exitstatus), stdout.read, stderr.read]
|
67
|
+
end
|
68
|
+
else
|
69
|
+
Open3.popen3(args.join(' ')) do |stdin, stdout, stderr|
|
70
|
+
[make_signed($?.to_i), stdout.read, stderr.read]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# yuck!
|
76
|
+
def make_signed(retcode)
|
77
|
+
((retcode > 128)) && ((retcode = retcode - 256))
|
78
|
+
retcode
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require File.expand_path(File.dirname(__FILE__)) + "/../lib/cli_test"
|
3
|
+
require "tempfile"
|
4
|
+
|
5
|
+
class TestCliTest < Test::Unit::TestCase
|
6
|
+
class InProcess < CLITest
|
7
|
+
|
8
|
+
def not_unix?
|
9
|
+
true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@tfs = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def make_tempfile
|
18
|
+
tf = Tempfile.new("experiment")
|
19
|
+
@tfs.push(tf)
|
20
|
+
tf
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
@tfs.each{|tf| tf.close! }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_cli_passes_ARGV
|
28
|
+
f = make_tempfile
|
29
|
+
f.puts('#!/usr/bin/env ruby')
|
30
|
+
f.puts("print ARGV.inspect")
|
31
|
+
f.chmod(0x777)
|
32
|
+
f.flush
|
33
|
+
|
34
|
+
@app = CLITest.new(f.path)
|
35
|
+
s, o, e = @app.run("--help")
|
36
|
+
assert_equal %w( --help ).inspect, o, "Should be the same STDOUT"
|
37
|
+
assert_equal "", e, "Should be the same STDERR"
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_cli_returns_stderr
|
41
|
+
f = make_tempfile
|
42
|
+
f.puts('#!/usr/bin/env ruby')
|
43
|
+
f.puts("$stderr.write ARGV.inspect")
|
44
|
+
f.chmod(0x777)
|
45
|
+
f.flush
|
46
|
+
|
47
|
+
@app = CLITest.new(f.path)
|
48
|
+
s, o, e = @app.run("--help")
|
49
|
+
assert_equal %w( --help ).inspect, e, "Should be the same STDERR"
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_cli_passes_exit_status
|
53
|
+
f = Tempfile.new("experiment")
|
54
|
+
f.puts('#!/usr/bin/env ruby')
|
55
|
+
f.puts("exit(12)")
|
56
|
+
f.chmod(0x777)
|
57
|
+
f.flush
|
58
|
+
|
59
|
+
@app = CLITest.new(f.path)
|
60
|
+
s, o, e = @app.run("")
|
61
|
+
assert_equal 12, s, "Should pass the proper exit status"
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_cli_passes_exit_status_with_inprocess
|
65
|
+
f = Tempfile.new("experiment")
|
66
|
+
f.puts('#!/usr/bin/env ruby')
|
67
|
+
f.puts("exit(12)")
|
68
|
+
f.chmod(0x777)
|
69
|
+
f.flush
|
70
|
+
|
71
|
+
@app = InProcess.new(f.path)
|
72
|
+
s, o, e = @app.run("")
|
73
|
+
assert_equal 12, s, "Should pass the proper exit status"
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_cli_returns_stderr_with_inprocess
|
77
|
+
f = make_tempfile
|
78
|
+
f.puts('#!/usr/bin/env ruby')
|
79
|
+
f.puts("$stderr.write ARGV.inspect")
|
80
|
+
f.chmod(0x777)
|
81
|
+
f.flush
|
82
|
+
|
83
|
+
@app = InProcess.new(f.path)
|
84
|
+
s, o, e = @app.run("--help")
|
85
|
+
assert_equal %w( --help ).inspect, e, "Should be the same STDERR"
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_cli_returns_stdout_with_inprocess
|
89
|
+
f = make_tempfile
|
90
|
+
f.puts('#!/usr/bin/env ruby')
|
91
|
+
f.puts("$stdout.write ARGV.inspect")
|
92
|
+
f.chmod(0x777)
|
93
|
+
f.flush
|
94
|
+
|
95
|
+
@app = InProcess.new(f.path)
|
96
|
+
s, o, e = @app.run("--help")
|
97
|
+
assert_equal %w( --help ).inspect, o, "Should be the same STDOUT"
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_cli_passes_negative_exit_status
|
101
|
+
f = Tempfile.new("experiment")
|
102
|
+
f.puts('#!/usr/bin/env ruby')
|
103
|
+
f.puts("exit(-12)")
|
104
|
+
f.chmod(0x777)
|
105
|
+
f.flush
|
106
|
+
|
107
|
+
@app = CLITest.new(f.path)
|
108
|
+
s, o, e = @app.run("")
|
109
|
+
assert_equal -12, s, "Should pass the proper exit status"
|
110
|
+
end
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cli_test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Julik Tarkhanov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-24 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.9.4
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
description: Assists in testing commandline applications (run an application qucikly, read out stderr, stdout and status, check option combinations)
|
27
|
+
email:
|
28
|
+
- me@julik.nl
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- History.txt
|
35
|
+
- Manifest.txt
|
36
|
+
files:
|
37
|
+
- .autotest
|
38
|
+
- History.txt
|
39
|
+
- Manifest.txt
|
40
|
+
- README.rdoc
|
41
|
+
- Rakefile
|
42
|
+
- lib/cli_test.rb
|
43
|
+
- test/test_cli_test.rb
|
44
|
+
- .gemtest
|
45
|
+
homepage: http://github.com/julik/cli_test
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --main
|
51
|
+
- README.rdoc
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: cli_test
|
69
|
+
rubygems_version: 1.8.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Assists in testing commandline applications (run an application qucikly, read out stderr, stdout and status, check option combinations)
|
73
|
+
test_files:
|
74
|
+
- test/test_cli_test.rb
|