afurmanov-clicase 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +16 -0
- data/Rakefile +18 -0
- data/VERSION.yml +4 -0
- data/clicase.gemspec +54 -0
- data/lib/clicase.rb +109 -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 +69 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009, Aleksandr Furmanov <afurmanov@rushpost.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,16 @@
|
|
1
|
+
= CliCase - Macros for testing Ruby command line apps with Shoulda or Test::Unit
|
2
|
+
|
3
|
+
When you start thinking about writing ruby command line utility you likely want to start with writing test. Assuming command line parsing is in CliApp#run you prabably want to write something like:
|
4
|
+
|
5
|
+
class CliTest < Test::Unit::TestCase
|
6
|
+
run_command_line_as {CliApp.run}
|
7
|
+
|
8
|
+
run_with_options ["--help"] do
|
9
|
+
assert_successful_run
|
10
|
+
assert_out_contains /Usage:/
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
Work in progress...
|
16
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gemspec|
|
6
|
+
gemspec.name = "clicase"
|
7
|
+
gemspec.summary = "Macros for testing Ruby command line apps with Shoulda or Test::Unit."
|
8
|
+
gemspec.email = "afurmanov@rushpost.com"
|
9
|
+
gemspec.homepage = "http://github.com/afurmanov/clicase"
|
10
|
+
gemspec.description = <<END
|
11
|
+
CliCase extends TestCase with macros simplifying testing command line apps written in Ruby. These macros allow
|
12
|
+
to specify command line entry point, run command line and assert execution results. Works with Test::Unit or with Shoulda.
|
13
|
+
END
|
14
|
+
gemspec.authors = ["Aleksandr Furmanov"]
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
data/VERSION.yml
ADDED
data/clicase.gemspec
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{clicase}
|
5
|
+
s.version = "0.1.5"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Aleksandr Furmanov"]
|
9
|
+
s.date = %q{2009-07-29}
|
10
|
+
s.description = %q{CliCase extends TestCase with macros simplifying testing command line apps written in Ruby. These macros allow
|
11
|
+
to specify command line entry point, run command line and assert execution results. Works with Test::Unit or with Shoulda.
|
12
|
+
}
|
13
|
+
s.email = %q{afurmanov@rushpost.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.rdoc"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"MIT-LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION.yml",
|
23
|
+
"clicase.gemspec",
|
24
|
+
"lib/clicase.rb",
|
25
|
+
"test/cli_app.rb",
|
26
|
+
"test/shoulda/cli_test.rb",
|
27
|
+
"test/test.rb",
|
28
|
+
"test/test_helper.rb",
|
29
|
+
"test/unit/cli_test.rb"
|
30
|
+
]
|
31
|
+
s.has_rdoc = true
|
32
|
+
s.homepage = %q{http://github.com/afurmanov/clicase}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.2}
|
36
|
+
s.summary = %q{Macros for testing Ruby command line apps with Shoulda or Test::Unit.}
|
37
|
+
s.test_files = [
|
38
|
+
"test/cli_app.rb",
|
39
|
+
"test/shoulda/cli_test.rb",
|
40
|
+
"test/test.rb",
|
41
|
+
"test/test_helper.rb",
|
42
|
+
"test/unit/cli_test.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
else
|
51
|
+
end
|
52
|
+
else
|
53
|
+
end
|
54
|
+
end
|
data/lib/clicase.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
module CliCase
|
2
|
+
module ClassMethods
|
3
|
+
attr_accessor :exit_status, :cli_output, :error, :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
|
+
Test::Unit::TestCase.exit_status = 0
|
45
|
+
Test::Unit::TestCase.error = ""
|
46
|
+
Test::Unit::TestCase.error_stack = ""
|
47
|
+
Test::Unit::TestCase.cli_output = ""
|
48
|
+
#It used to be $stdout = StringIO.new(...), but it does not work well with rdebug
|
49
|
+
class <<$stdout
|
50
|
+
alias_method :original_write, :write
|
51
|
+
def write(*s)
|
52
|
+
#original_write(s)
|
53
|
+
Test::Unit::TestCase.cli_output << s.to_s if Test::Unit::TestCase.cli_output
|
54
|
+
end
|
55
|
+
end
|
56
|
+
execute
|
57
|
+
rescue SystemExit => exit_error
|
58
|
+
Test::Unit::TestCase.exit_status = exit_error.status
|
59
|
+
rescue Exception => error
|
60
|
+
Test::Unit::TestCase.error_stack = error.backtrace.join("\n")
|
61
|
+
Test::Unit::TestCase.error = error.message
|
62
|
+
ensure
|
63
|
+
class <<$stdout
|
64
|
+
remove_method :write, :original_write
|
65
|
+
end
|
66
|
+
end
|
67
|
+
instance_eval(&block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
def generate_test(name, &block)
|
74
|
+
if defined? Shoulda
|
75
|
+
should(name, &block)
|
76
|
+
else
|
77
|
+
test_name = ("test_".concat name).strip.to_sym
|
78
|
+
self.send(:define_method, test_name, &block)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end #Macros
|
82
|
+
|
83
|
+
module Assertions
|
84
|
+
def assert_successful_run
|
85
|
+
expect_message = "Should run successfully, but"
|
86
|
+
assert_equal 0, Test::Unit::TestCase.exit_status, "#{expect_message} exit status is #{Test::Unit::TestCase.exit_status}"
|
87
|
+
assert Test::Unit::TestCase.error.empty?, "#{expect_message} exception '#{Test::Unit::TestCase.error}' has been thrown. Exception stack:\n" + Test::Unit::TestCase.error_stack
|
88
|
+
end
|
89
|
+
|
90
|
+
def assert_failed_run
|
91
|
+
flunk "Expects either thrown error or exit status not 0." if 0 == Test::Unit::TestCase.exit_status && Test::Unit::TestCase.error.empty?
|
92
|
+
end
|
93
|
+
|
94
|
+
def assert_out_contains regex
|
95
|
+
assert_match regex, Test::Unit::TestCase.cli_output
|
96
|
+
end
|
97
|
+
end #Assertions
|
98
|
+
end
|
99
|
+
|
100
|
+
module Test
|
101
|
+
module Unit
|
102
|
+
class TestCase
|
103
|
+
extend CliCase::ClassMethods
|
104
|
+
include CliCase::InstanceMethods
|
105
|
+
extend CliCase::Macros
|
106
|
+
include CliCase::Assertions
|
107
|
+
end
|
108
|
+
end
|
109
|
+
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,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: afurmanov-clicase
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksandr Furmanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-29 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: CliCase extends TestCase with macros simplifying testing command line apps written in Ruby. These macros allow to specify command line entry point, run command line and assert execution results. Works with Test::Unit or with Shoulda.
|
17
|
+
email: afurmanov@rushpost.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
|
+
- clicase.gemspec
|
31
|
+
- lib/clicase.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: true
|
38
|
+
homepage: http://github.com/afurmanov/clicase
|
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: Macros for testing Ruby command line apps with Shoulda or Test::Unit.
|
64
|
+
test_files:
|
65
|
+
- test/cli_app.rb
|
66
|
+
- test/shoulda/cli_test.rb
|
67
|
+
- test/test.rb
|
68
|
+
- test/test_helper.rb
|
69
|
+
- test/unit/cli_test.rb
|