fkocherga-cmd_line_test 0.1.2
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.
- data/.gitignore +5 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +28 -0
- data/Rakefile +28 -0
- data/VERSION.yml +4 -0
- data/cmd_line_test.gemspec +49 -0
- data/lib/cmd_line_test.rb +110 -0
- data/test/cli_app.rb +25 -0
- data/test/shoulda/cli_test.rb +20 -0
- data/test/test.rb +34 -0
- data/test/test_helper.rb +3 -0
- data/test/unit/cli_test.rb +3 -0
- metadata +66 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009, Fedor Kocherga <fkocherga@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= cmd_line_test - Extends Shoulda or Test::Unit with macros for testing command line apps
|
2
|
+
|
3
|
+
= Basic Usage
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
sudo gem install fkocherga-cmd_line_test
|
7
|
+
|
8
|
+
== Example
|
9
|
+
If you are about to start writing new command line utility MyCommandLineApp, you probably want to specify its options and behavior, likely in form of tests. This gem is aimed to simplify creation of such tests:
|
10
|
+
|
11
|
+
|
12
|
+
requrie 'cmd_line_test'
|
13
|
+
|
14
|
+
class CliTest < Test::Unit::TestCase
|
15
|
+
run_command_line_as {MyCommandLineApp.run}
|
16
|
+
|
17
|
+
run_with_options ["--help"] do
|
18
|
+
assert_successful_run
|
19
|
+
assert_out_contains /Usage:/
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
= Shortcumings
|
24
|
+
Only tested on the 1.8.7 Ruby implementation and with Shoulda 2.10.1
|
25
|
+
|
26
|
+
= License
|
27
|
+
cmd_line_test is released under the MIT license.
|
28
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['test/shoulda/cli_test.rb', 'test/unit/cli_test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'jeweler'
|
13
|
+
gemspec = Gem::Specification.new do |s|
|
14
|
+
|
15
|
+
s.name = "cmd_line_test"
|
16
|
+
s.summary = "Extends Shoulda or Test::Unit with macros for testing command line apps."
|
17
|
+
s.email = "fkocherga@gmail.com"
|
18
|
+
s.homepage = "http://github.com/fkocherga/cml_line_test"
|
19
|
+
s.description = <<END
|
20
|
+
Macros specific to command line Ruby apps testing. Works with Test::Unit or Shoulda.
|
21
|
+
END
|
22
|
+
s.authors = ["Fedor Kocherga"]
|
23
|
+
s.test_files = ['test/shoulda/cli_test.rb', 'test/unit/cli_test.rb']
|
24
|
+
end
|
25
|
+
Jeweler::Tasks.new gemspec
|
26
|
+
rescue LoadError
|
27
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
28
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{cmd_line_test}
|
5
|
+
s.version = "0.1.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Fedor Kocherga"]
|
9
|
+
s.date = %q{2009-09-16}
|
10
|
+
s.description = %q{Macros specific to command line Ruby apps testing. Works with Test::Unit or Shoulda.
|
11
|
+
}
|
12
|
+
s.email = %q{fkocherga@gmail.com}
|
13
|
+
s.extra_rdoc_files = [
|
14
|
+
"README.rdoc"
|
15
|
+
]
|
16
|
+
s.files = [
|
17
|
+
".gitignore",
|
18
|
+
"MIT-LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"cmd_line_test.gemspec",
|
23
|
+
"lib/cmd_line_test.rb",
|
24
|
+
"test/cli_app.rb",
|
25
|
+
"test/shoulda/cli_test.rb",
|
26
|
+
"test/test.rb",
|
27
|
+
"test/test_helper.rb",
|
28
|
+
"test/unit/cli_test.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/fkocherga/cml_line_test}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.5}
|
34
|
+
s.summary = %q{Extends Shoulda or Test::Unit with macros for testing command line apps.}
|
35
|
+
s.test_files = [
|
36
|
+
"test/shoulda/cli_test.rb",
|
37
|
+
"test/unit/cli_test.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
else
|
46
|
+
end
|
47
|
+
else
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module CmdLineTest
|
2
|
+
module ClassMethods
|
3
|
+
attr_accessor :cli_exit_status, :cli_output, :cli_error, :cli_error_stack, :cli_block
|
4
|
+
|
5
|
+
def run_command_line_as(&block)
|
6
|
+
self.cli_block = block
|
7
|
+
end
|
8
|
+
end #ClassMethods
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
def run_with_options(options)
|
12
|
+
with_ARGV_set_to(options) {execute}
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def execute
|
17
|
+
self.class.cli_block.call
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_ARGV_set_to(options)
|
21
|
+
ignore_assignment_to_const_warning { Object.const_set(:ARGV, options) }
|
22
|
+
yield
|
23
|
+
end
|
24
|
+
|
25
|
+
def ignore_assignment_to_const_warning
|
26
|
+
warnings_option = $-w
|
27
|
+
$-w = nil
|
28
|
+
begin
|
29
|
+
yield
|
30
|
+
ensure
|
31
|
+
$-w = warnings_option
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end #InstanceMethods
|
35
|
+
|
36
|
+
module Macros
|
37
|
+
def run_with_options(options, name = nil, &block)
|
38
|
+
raise RuntimeError, "Command is not defined: 'run_command_line_as()' must be called first." unless
|
39
|
+
self.cli_block
|
40
|
+
test_name = name || "after running with options '#{options.join(', ')}'"
|
41
|
+
generate_test test_name do
|
42
|
+
with_ARGV_set_to options do
|
43
|
+
begin
|
44
|
+
self.class.cli_exit_status = 0
|
45
|
+
self.class.cli_error = ""
|
46
|
+
self.class.cli_error_stack = ""
|
47
|
+
self.class.cli_output = ""
|
48
|
+
$test_class = self.class
|
49
|
+
#It used to be $stdout = StringIO.new(...), but it does not work well with rdebug
|
50
|
+
class <<$stdout
|
51
|
+
alias_method :original_write, :write
|
52
|
+
def write(*s)
|
53
|
+
#original_write(s)
|
54
|
+
$test_class.cli_output << s.to_s if $test_class.cli_output
|
55
|
+
end
|
56
|
+
end
|
57
|
+
execute
|
58
|
+
rescue SystemExit => exit_error
|
59
|
+
self.class.cli_exit_status = exit_error.status
|
60
|
+
rescue Exception => error
|
61
|
+
self.class.cli_error_stack = error.backtrace.join("\n")
|
62
|
+
self.class.cli_error = error.message
|
63
|
+
ensure
|
64
|
+
class <<$stdout
|
65
|
+
remove_method :write, :original_write
|
66
|
+
end
|
67
|
+
end
|
68
|
+
instance_eval(&block)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
def generate_test(name, &block)
|
75
|
+
if defined? Shoulda
|
76
|
+
should(name, &block)
|
77
|
+
else
|
78
|
+
test_name = ("test_".concat name).strip.to_sym
|
79
|
+
self.send(:define_method, test_name, &block)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end #Macros
|
83
|
+
|
84
|
+
module Assertions
|
85
|
+
def assert_successful_run
|
86
|
+
expect_message = "Should run successfully, but"
|
87
|
+
assert_equal 0, self.class.cli_exit_status, "#{expect_message} exit status is #{self.class.cli_exit_status}"
|
88
|
+
assert self.class.cli_error.empty?, "#{expect_message} exception '#{self.class.cli_error}' has been thrown. Exception stack:\n" + self.class.cli_error_stack
|
89
|
+
end
|
90
|
+
|
91
|
+
def assert_failed_run
|
92
|
+
flunk "Expects either thrown error or exit status not 0." if 0 == self.class.cli_exit_status && self.class.cli_error.empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
def assert_out_contains regex
|
96
|
+
assert_match regex, self.class.cli_output
|
97
|
+
end
|
98
|
+
end #Assertions
|
99
|
+
end
|
100
|
+
|
101
|
+
module Test
|
102
|
+
module Unit
|
103
|
+
class TestCase
|
104
|
+
extend CmdLineTest::ClassMethods
|
105
|
+
include CmdLineTest::InstanceMethods
|
106
|
+
extend CmdLineTest::Macros
|
107
|
+
include CmdLineTest::Assertions
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/test/cli_app.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module CliApp
|
4
|
+
class <<self
|
5
|
+
def run
|
6
|
+
OptionParser.new do |opts|
|
7
|
+
opts.banner = "Usage: cli.rb [options]\nCommand line test app."
|
8
|
+
|
9
|
+
opts.on( "--option OPTIONIAL", "run with OPTIONIAL") do |option|
|
10
|
+
puts option
|
11
|
+
end
|
12
|
+
|
13
|
+
opts.on_tail("-h", "--help", "show help") do
|
14
|
+
puts opts
|
15
|
+
#exit NB!
|
16
|
+
end
|
17
|
+
end.parse!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
if $0 == __FILE__
|
24
|
+
CliApp.run
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'shoulda'
|
3
|
+
require File.dirname(__FILE__) + '/../test'
|
4
|
+
|
5
|
+
class CliShouldaSpecificTest < Test::Unit::TestCase
|
6
|
+
run_command_line_as {CliApp.run}
|
7
|
+
|
8
|
+
context "outer" do
|
9
|
+
run_with_options ["--option", "OUTER"] do
|
10
|
+
assert_out_contains /OUTER/
|
11
|
+
end
|
12
|
+
context "inner" do
|
13
|
+
run_with_options ["--option", "INNER"] do
|
14
|
+
assert_out_contains /INNER/
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
data/test/test.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class CliUsageTest < Test::Unit::TestCase
|
4
|
+
run_command_line_as {CliApp.run}
|
5
|
+
|
6
|
+
run_with_options ["--help"] do
|
7
|
+
assert_successful_run
|
8
|
+
assert_out_contains /Usage:/
|
9
|
+
end
|
10
|
+
|
11
|
+
run_with_options [], "running with no arguments" do
|
12
|
+
assert_successful_run
|
13
|
+
end
|
14
|
+
|
15
|
+
run_with_options ["--option", "PRINT_THIS"] do
|
16
|
+
assert_successful_run
|
17
|
+
assert_out_contains /PRINT_THIS/
|
18
|
+
end
|
19
|
+
|
20
|
+
run_with_options [], "run_with_options could be used within block" do
|
21
|
+
assert_successful_run
|
22
|
+
run_with_options []
|
23
|
+
end
|
24
|
+
|
25
|
+
run_with_options ["--invalid-option"], "running with invalid option" do
|
26
|
+
assert_failed_run
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class CliTestNoCommandLineDefined < Test::Unit::TestCase
|
31
|
+
def test_failure_when_command_line_defined
|
32
|
+
assert_raise(RuntimeError) {self.class.run_with_options []}
|
33
|
+
end
|
34
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fkocherga-cmd_line_test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fedor Kocherga
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Macros specific to command line Ruby apps testing. Works with Test::Unit or Shoulda.
|
17
|
+
email: fkocherga@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- cmd_line_test.gemspec
|
31
|
+
- lib/cmd_line_test.rb
|
32
|
+
- test/cli_app.rb
|
33
|
+
- test/shoulda/cli_test.rb
|
34
|
+
- test/test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- test/unit/cli_test.rb
|
37
|
+
has_rdoc: false
|
38
|
+
homepage: http://github.com/fkocherga/cml_line_test
|
39
|
+
licenses:
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Extends Shoulda or Test::Unit with macros for testing command line apps.
|
64
|
+
test_files:
|
65
|
+
- test/shoulda/cli_test.rb
|
66
|
+
- test/unit/cli_test.rb
|