execute_shell 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -1
- data/Gemfile.lock +3 -3
- data/README +10 -6
- data/execute_shell.gemspec +8 -5
- data/lib/execute_shell.rb +3 -1
- data/lib/execute_shell/execute_shell.rb +1 -1
- data/test/execute_shell_test.rb +3 -2
- data/test/require.rb +0 -6
- metadata +11 -10
- data/test/lib/test_case.rb +0 -502
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,10 +2,10 @@ GEM
|
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
4
|
Platform (0.4.0)
|
5
|
-
app_mode (
|
5
|
+
app_mode (1.0.1)
|
6
6
|
open4 (1.0.1)
|
7
7
|
rake (0.8.7)
|
8
|
-
rake_tasks (0.0.
|
8
|
+
rake_tasks (0.0.4)
|
9
9
|
rake (~> 0.8.7)
|
10
10
|
win32-open3-19 (0.0.1)
|
11
11
|
|
@@ -14,7 +14,7 @@ PLATFORMS
|
|
14
14
|
|
15
15
|
DEPENDENCIES
|
16
16
|
Platform (~> 0.4.0)
|
17
|
-
app_mode (~> 0.0
|
17
|
+
app_mode (~> 1.0.0)
|
18
18
|
open4 (~> 1.0.1)
|
19
19
|
rake_tasks (~> 0.0.1)
|
20
20
|
win32-open3-19 (~> 0.0.1)
|
data/README
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
== Welcome to ExecuteShell
|
2
2
|
|
3
|
-
ExecuteShell
|
3
|
+
ExecuteShell provides cross-platform shell command execution.
|
4
|
+
In other words, it lays the smack down like a hungry t-rex
|
5
|
+
in the octagon with a pig.
|
4
6
|
|
5
7
|
== Getting Started
|
6
8
|
|
@@ -8,19 +10,21 @@ ExecuteShell allows shell commands to be executed the same across all platforms.
|
|
8
10
|
|
9
11
|
gem install execute_shell
|
10
12
|
|
11
|
-
2. Require the gem in your
|
13
|
+
2. Require the gem in your Gemfile:
|
12
14
|
|
13
|
-
gem 'execute_shell', '~> 0.0.
|
15
|
+
gem 'execute_shell', '~> 0.0.5'
|
14
16
|
|
15
|
-
3. Require the gem wherever you need
|
17
|
+
3. Require the gem wherever you need to use it:
|
16
18
|
|
17
19
|
require 'execute_shell'
|
18
20
|
|
19
|
-
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
1. Include the ExecuteShell module in your class or module:
|
20
24
|
|
21
25
|
include ExecuteShell
|
22
26
|
|
23
|
-
|
27
|
+
2. Call a shell command:
|
24
28
|
|
25
29
|
success, output = shell('ls ~')
|
26
30
|
|
data/execute_shell.gemspec
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'execute_shell'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.5'
|
4
4
|
|
5
5
|
s.summary = 'Cross-platform shell commands.'
|
6
|
-
s.description =
|
7
|
-
|
6
|
+
s.description = %Q{
|
7
|
+
ExecuteShell provides cross-platform shell command execution.
|
8
|
+
In other words, it lays the smack down like a hungry t-rex
|
9
|
+
in the octagon with a pig.
|
10
|
+
}.strip
|
8
11
|
|
9
12
|
s.author = 'Travis Herrick'
|
10
13
|
s.email = 'tthetoad@gmail.com'
|
11
14
|
s.homepage = 'http://www.bitbucket.org/ToadJamb/gems_execute_shell'
|
12
15
|
|
13
|
-
s.license = '
|
16
|
+
s.license = 'GPLv3'
|
14
17
|
|
15
18
|
s.extra_rdoc_files << 'README'
|
16
19
|
|
@@ -21,7 +24,7 @@ Gem::Specification.new do |s|
|
|
21
24
|
s.add_dependency 'Platform', '~> 0.4.0'
|
22
25
|
s.add_dependency 'open4', '~> 1.0.1'
|
23
26
|
s.add_dependency 'win32-open3-19', '~> 0.0.1'
|
24
|
-
s.add_dependency 'app_mode', '~> 0.0
|
27
|
+
s.add_dependency 'app_mode', '~> 1.0.0'
|
25
28
|
|
26
29
|
s.add_development_dependency 'rake_tasks', '~> 0.0.1'
|
27
30
|
|
data/lib/execute_shell.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
gem_name = File.basename(__FILE__, '.rb')
|
2
|
+
|
1
3
|
require 'app_mode'
|
2
4
|
require 'platform'
|
3
5
|
require 'open4' if [:linux].include?(Platform::IMPL)
|
4
6
|
require 'open3' if [:mingw].include?(Platform::IMPL)
|
5
7
|
|
6
|
-
require_relative
|
8
|
+
require_relative File.join(gem_name, gem_name)
|
@@ -35,7 +35,7 @@
|
|
35
35
|
# Contains methods for returning output from console commands.
|
36
36
|
module ExecuteShell
|
37
37
|
# Indicates the mode that ExecuteShell is running in.
|
38
|
-
ExecuteShellMode =
|
38
|
+
ExecuteShellMode = StateManager.new(:production)
|
39
39
|
|
40
40
|
# Returns output from a console command.
|
41
41
|
# ==== Input
|
data/test/execute_shell_test.rb
CHANGED
@@ -34,6 +34,7 @@ require File.join(File.dirname(File.expand_path(__FILE__)), 'require')
|
|
34
34
|
|
35
35
|
class ExecuteShellTest < Test::Unit::TestCase
|
36
36
|
def setup
|
37
|
+
@class = ExecuteShell
|
37
38
|
@obj = Object.new
|
38
39
|
@obj.extend(@class)
|
39
40
|
|
@@ -72,7 +73,7 @@ class ExecuteShellTest < Test::Unit::TestCase
|
|
72
73
|
assert output.index(@user),
|
73
74
|
"'#{command}' does not appear to include " +
|
74
75
|
"the current user's folder (#{@user})."
|
75
|
-
|
76
|
+
assert success, "#{command} was not successful"
|
76
77
|
end
|
77
78
|
|
78
79
|
def test_stderr
|
@@ -99,7 +100,7 @@ class ExecuteShellTest < Test::Unit::TestCase
|
|
99
100
|
assert_equal result, output if Platform::IMPL == :linux
|
100
101
|
|
101
102
|
assert output.index(result)
|
102
|
-
|
103
|
+
assert !success
|
103
104
|
end
|
104
105
|
|
105
106
|
def test_grep
|
data/test/require.rb
CHANGED
@@ -2,12 +2,6 @@ root_path = File.join(File.dirname(File.expand_path(__FILE__)), '..')
|
|
2
2
|
root_path = File.expand_path(root_path)
|
3
3
|
|
4
4
|
Bundler.require :test
|
5
|
-
|
6
5
|
require 'test/unit'
|
7
6
|
|
8
7
|
require_relative '../lib/execute_shell'
|
9
|
-
|
10
|
-
file_list = Dir[File.join(root_path, 'test', 'lib', '*.rb')]
|
11
|
-
file_list.each do |file|
|
12
|
-
require file
|
13
|
-
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Herrick
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-08-
|
17
|
+
date: 2011-08-10 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -70,10 +70,10 @@ dependencies:
|
|
70
70
|
- - ~>
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
segments:
|
73
|
+
- 1
|
73
74
|
- 0
|
74
75
|
- 0
|
75
|
-
|
76
|
-
version: 0.0.1
|
76
|
+
version: 1.0.0
|
77
77
|
type: :runtime
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: *id004
|
@@ -92,7 +92,10 @@ dependencies:
|
|
92
92
|
type: :development
|
93
93
|
prerelease: false
|
94
94
|
version_requirements: *id005
|
95
|
-
description:
|
95
|
+
description: |-
|
96
|
+
ExecuteShell provides cross-platform shell command execution.
|
97
|
+
In other words, it lays the smack down like a hungry t-rex
|
98
|
+
in the octagon with a pig.
|
96
99
|
email: tthetoad@gmail.com
|
97
100
|
executables: []
|
98
101
|
|
@@ -109,12 +112,11 @@ files:
|
|
109
112
|
- rakefile
|
110
113
|
- execute_shell.gemspec
|
111
114
|
- test/require.rb
|
112
|
-
- test/lib/test_case.rb
|
113
115
|
- test/execute_shell_test.rb
|
114
116
|
has_rdoc: true
|
115
117
|
homepage: http://www.bitbucket.org/ToadJamb/gems_execute_shell
|
116
118
|
licenses:
|
117
|
-
-
|
119
|
+
- GPLv3
|
118
120
|
post_install_message:
|
119
121
|
rdoc_options: []
|
120
122
|
|
@@ -125,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
127
|
requirements:
|
126
128
|
- - ">="
|
127
129
|
- !ruby/object:Gem::Version
|
128
|
-
hash: -
|
130
|
+
hash: -1239307810502746702
|
129
131
|
segments:
|
130
132
|
- 0
|
131
133
|
version: "0"
|
@@ -146,5 +148,4 @@ specification_version: 3
|
|
146
148
|
summary: Cross-platform shell commands.
|
147
149
|
test_files:
|
148
150
|
- test/require.rb
|
149
|
-
- test/lib/test_case.rb
|
150
151
|
- test/execute_shell_test.rb
|
data/test/lib/test_case.rb
DELETED
@@ -1,502 +0,0 @@
|
|
1
|
-
# This file contains a monkey patch of Test::Unit::TestCase.
|
2
|
-
|
3
|
-
#--
|
4
|
-
################################################################################
|
5
|
-
# Copyright (C) 2011 Travis Herrick #
|
6
|
-
################################################################################
|
7
|
-
# #
|
8
|
-
# \v^V,^!v\^/ #
|
9
|
-
# ~% %~ #
|
10
|
-
# { _ _ } #
|
11
|
-
# ( * - ) #
|
12
|
-
# | / | #
|
13
|
-
# \ _, / #
|
14
|
-
# \__.__/ #
|
15
|
-
# #
|
16
|
-
################################################################################
|
17
|
-
# This program is free software: you can redistribute it #
|
18
|
-
# and/or modify it under the terms of the GNU General Public License #
|
19
|
-
# as published by the Free Software Foundation, #
|
20
|
-
# either version 3 of the License, or (at your option) any later version. #
|
21
|
-
# #
|
22
|
-
# Commercial licensing may be available for a fee under a different license. #
|
23
|
-
################################################################################
|
24
|
-
# This program is distributed in the hope that it will be useful, #
|
25
|
-
# but WITHOUT ANY WARRANTY; #
|
26
|
-
# without even the implied warranty of MERCHANTABILITY #
|
27
|
-
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
28
|
-
# See the GNU General Public License for more details. #
|
29
|
-
# #
|
30
|
-
# You should have received a copy of the GNU General Public License #
|
31
|
-
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
32
|
-
################################################################################
|
33
|
-
#++
|
34
|
-
|
35
|
-
# Monkey patch Test::Unit::TestCase to make it do cool stuff.
|
36
|
-
Test::Unit::TestCase.class_eval do
|
37
|
-
# Since this is in a class_eval, instance methods need to be wrapped up
|
38
|
-
# in class_variable_set or ruby will throw warnings.
|
39
|
-
|
40
|
-
# Indicates whether the class has already been initialized.
|
41
|
-
# This combined with @@class_name prevents duplicate patching.
|
42
|
-
class_variable_set(:@@initialized, false)
|
43
|
-
|
44
|
-
# Keeps track of the class that has most recently been initialized.
|
45
|
-
# This combined with @@initialized prevents duplicate patching.
|
46
|
-
class_variable_set(:@@class_name, '')
|
47
|
-
|
48
|
-
# Initializes the class
|
49
|
-
# and exposes private methods and variables of the class that is being tested.
|
50
|
-
def initialize(*args)
|
51
|
-
# Call initialize on the superclass.
|
52
|
-
super
|
53
|
-
|
54
|
-
@obj = nil
|
55
|
-
|
56
|
-
reset_io
|
57
|
-
reset_trace
|
58
|
-
|
59
|
-
# This block ensures that tests still work if there is not a class that
|
60
|
-
# corresponds with the test file/class.
|
61
|
-
@class = nil
|
62
|
-
begin
|
63
|
-
# Get the class that is being tested.
|
64
|
-
# Assume that the name of the class is found by removing 'Test'
|
65
|
-
# from the test class.
|
66
|
-
@class = Kernel.const_get(self.class.name.gsub(/Test$/, ''))
|
67
|
-
@@initialized = ((@class.name == @@class_name) && @@initialized)
|
68
|
-
@@class_name = @class.name
|
69
|
-
rescue
|
70
|
-
@@initialized = true
|
71
|
-
@@class_name = ''
|
72
|
-
end
|
73
|
-
|
74
|
-
# Only patch if this code has not yet been run.
|
75
|
-
if !@@initialized and @class.class.name != 'Module'
|
76
|
-
set_instance_method_wrappers
|
77
|
-
|
78
|
-
# Expose private class methods.
|
79
|
-
# We will only expose the methods we are responsible for creating.
|
80
|
-
# (i.e. subtracting the superclass's private methods)
|
81
|
-
expose_private_methods(:class,
|
82
|
-
@class.private_methods -
|
83
|
-
@class.superclass.private_methods)
|
84
|
-
|
85
|
-
# Expose private instance methods.
|
86
|
-
# We will only expose the methods we are responsible for creating.
|
87
|
-
# (i.e. subtracting the superclass's private methods)
|
88
|
-
expose_private_methods(:instance,
|
89
|
-
@class.private_instance_methods -
|
90
|
-
@class.superclass.private_instance_methods)
|
91
|
-
|
92
|
-
# Expose variables.
|
93
|
-
# Requires that variables are assigned to in the constructor.
|
94
|
-
wrap_output {
|
95
|
-
expose_variables(@class.class_variables +
|
96
|
-
@class.new([]).instance_variables)
|
97
|
-
}
|
98
|
-
|
99
|
-
# Indicate that this code has been run.
|
100
|
-
@@initialized = true
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Sets up functionality for all tests.
|
105
|
-
#
|
106
|
-
# Tracing is set up here so that it is only running during tests.
|
107
|
-
#
|
108
|
-
# If you want to disable tracing, simply override the setup method
|
109
|
-
# without calling super. (It would be good form to also override teardown).
|
110
|
-
def setup
|
111
|
-
set_trace_func proc { |event, file, line, id, binding, class_name|
|
112
|
-
if class_name == @class and
|
113
|
-
@stack_trace.last != {:class => class_name.name, :method => id}
|
114
|
-
@stack_trace << {
|
115
|
-
:class => class_name.name,
|
116
|
-
:method => id,
|
117
|
-
}
|
118
|
-
end
|
119
|
-
}
|
120
|
-
end
|
121
|
-
|
122
|
-
# Clean up after each test.
|
123
|
-
#
|
124
|
-
# If you disable tracing, it would be good form to override this method
|
125
|
-
# as well without calling super.
|
126
|
-
def teardown
|
127
|
-
set_trace_func nil
|
128
|
-
end
|
129
|
-
|
130
|
-
############################################################################
|
131
|
-
private
|
132
|
-
############################################################################
|
133
|
-
|
134
|
-
############################################################################
|
135
|
-
# Monkey patching methods for the class being tested.
|
136
|
-
############################################################################
|
137
|
-
|
138
|
-
# Monkey patch the class's initializer to enable tracing
|
139
|
-
# with parameters and results.
|
140
|
-
def set_initializer
|
141
|
-
@class.class_eval do
|
142
|
-
attr_accessor :trace
|
143
|
-
|
144
|
-
alias :test_case_initialize :initialize
|
145
|
-
def initialize(*args)
|
146
|
-
@trace = []
|
147
|
-
result = test_case_initialize(*args)
|
148
|
-
return result
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Loop through the instance methods, calling set_instance_methods for each.
|
154
|
-
def set_instance_method_wrappers
|
155
|
-
[
|
156
|
-
:public_instance_methods,
|
157
|
-
:protected_instance_methods,
|
158
|
-
:private_instance_methods
|
159
|
-
].each do |method_id|
|
160
|
-
|
161
|
-
scope = method_id.to_s.gsub(/_.*/, '')
|
162
|
-
|
163
|
-
set_instance_methods(@class.send(method_id) -
|
164
|
-
@class.superclass.send(method_id), scope)
|
165
|
-
end
|
166
|
-
|
167
|
-
# If this is not at the end, the loop will attempt to do it's thing
|
168
|
-
# with the constructor created in this method, which is not necessary.
|
169
|
-
set_initializer
|
170
|
-
end
|
171
|
-
|
172
|
-
# Loop through the list of methods that are passed in,
|
173
|
-
# creating a wrapper method that enables tracing.
|
174
|
-
#
|
175
|
-
# Tracing data includes method name, parameters, and result.
|
176
|
-
# ==== Input
|
177
|
-
# [method_list : Array] A list of methods that will have wrapping functions
|
178
|
-
# created to enable tracing.
|
179
|
-
# [scope : String] The scope of the original function.
|
180
|
-
def set_instance_methods(method_list, scope)
|
181
|
-
method_list.each do |method_id|
|
182
|
-
# Setters and methods that accept blocks do not appear to work.
|
183
|
-
next if method_id =~ /=/ or method_id =~ /wrap_output/
|
184
|
-
|
185
|
-
# Build the method.
|
186
|
-
new_method = <<-DOC
|
187
|
-
alias :test_case_#{method_id} :#{method_id}
|
188
|
-
def #{method_id}(*args)
|
189
|
-
result = test_case_#{method_id}(*args)
|
190
|
-
@trace << {
|
191
|
-
:method => '#{method_id}',
|
192
|
-
:args => args,
|
193
|
-
:result => result
|
194
|
-
}
|
195
|
-
return result
|
196
|
-
end
|
197
|
-
#{scope} :#{method_id}
|
198
|
-
DOC
|
199
|
-
|
200
|
-
# Add the method to the class.
|
201
|
-
@class.class_eval do
|
202
|
-
eval(new_method)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
# Expose the private methods that are passed in. New methods will be created
|
208
|
-
# with the old method name followed by '_public_test'. If the original
|
209
|
-
# method contained a '?', it will be removed in the new method.
|
210
|
-
# ==== Input
|
211
|
-
# [type : Symbol] Indicates whether to handle instance or class methods.
|
212
|
-
#
|
213
|
-
# Only :class and :instance are supported.
|
214
|
-
# [methods : Array] An array of the methods to expose.
|
215
|
-
def expose_private_methods(type, methods)
|
216
|
-
# Get the text that the method should be wrapped in.
|
217
|
-
method_wrapper = wrapper(type)
|
218
|
-
|
219
|
-
# Loop through the methods.
|
220
|
-
methods.each do |method|
|
221
|
-
# Remove ?s.
|
222
|
-
new_method = method.to_s.gsub(/\?/, '')
|
223
|
-
|
224
|
-
# This is the new method.
|
225
|
-
new_method = <<-DOC
|
226
|
-
def #{new_method}_public_test(*args)
|
227
|
-
#{method}(*args)
|
228
|
-
end
|
229
|
-
DOC
|
230
|
-
|
231
|
-
# Add the wrapping text.
|
232
|
-
new_method = method_wrapper % [new_method]
|
233
|
-
|
234
|
-
# Add the method to the class.
|
235
|
-
@class.class_eval do
|
236
|
-
eval(new_method)
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
# Expose the variables.
|
242
|
-
#
|
243
|
-
# New methods will be created (a getter and a setter) for each variable.
|
244
|
-
#
|
245
|
-
# Regardless of the type of variable, these methods are only available
|
246
|
-
# via an instance.
|
247
|
-
# ==== Input
|
248
|
-
# [variables : Array] An array of variables to expose.
|
249
|
-
def expose_variables(variables)
|
250
|
-
# Get the text that the methods should be wrapped in.
|
251
|
-
var_wrapper = wrapper(:instance)
|
252
|
-
|
253
|
-
# Loop through the variables
|
254
|
-
variables.each do |var|
|
255
|
-
# Remove any @s.
|
256
|
-
new_method = var.to_s.gsub(/@/, '')
|
257
|
-
|
258
|
-
# These are the new getter and setters.
|
259
|
-
new_method = <<-DOC
|
260
|
-
def #{new_method}_variable_method
|
261
|
-
#{var}
|
262
|
-
end
|
263
|
-
|
264
|
-
def #{new_method}_variable_method=(value)
|
265
|
-
#{var} = value
|
266
|
-
end
|
267
|
-
DOC
|
268
|
-
|
269
|
-
# Add the wrapping text.
|
270
|
-
new_method = var_wrapper % [new_method]
|
271
|
-
|
272
|
-
# Add the methods to the class.
|
273
|
-
@class.class_eval do
|
274
|
-
eval(new_method)
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
# Returns the wrapping text for the specified type of method.
|
280
|
-
# ==== Input
|
281
|
-
# [type : Symbol] Indicates whether to handle instance or class methods.
|
282
|
-
#
|
283
|
-
# Only :class & :instance are supported.
|
284
|
-
# ==== Output
|
285
|
-
# [String] The text that the specified type of method should be wrapped in.
|
286
|
-
def wrapper(type)
|
287
|
-
case type
|
288
|
-
when :class then 'class << self;%s;end'
|
289
|
-
when :instance then '%s'
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
############################################################################
|
294
|
-
# I/O support methods.
|
295
|
-
############################################################################
|
296
|
-
|
297
|
-
# Return the actual output to stdout and stderr.
|
298
|
-
# ==== Output
|
299
|
-
# [Array] Two element array of strings.
|
300
|
-
#
|
301
|
-
# The first element is from stdout.
|
302
|
-
#
|
303
|
-
# The second element is from stderr.
|
304
|
-
def real_finis
|
305
|
-
return out, err
|
306
|
-
end
|
307
|
-
|
308
|
-
# Wrap a block to capture the output to stdout and stderr.
|
309
|
-
# ==== Input
|
310
|
-
# [&block : Block] The block of code that will have stdout and stderr trapped.
|
311
|
-
def wrap_output(&block)
|
312
|
-
begin
|
313
|
-
$stdout = @out
|
314
|
-
$stderr = @err
|
315
|
-
yield
|
316
|
-
ensure
|
317
|
-
$stdout = STDOUT
|
318
|
-
$stderr = STDERR
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
# Returns the output from stdout as a string.
|
323
|
-
# ==== Output
|
324
|
-
# [String] The output from stdout.
|
325
|
-
#
|
326
|
-
# All trailing line feeds are removed.
|
327
|
-
def out
|
328
|
-
@out.respond_to?(:string) ? @out.string.gsub(/\n*\z/, '') : ''
|
329
|
-
end
|
330
|
-
|
331
|
-
# Returns the output from stderr as a string.
|
332
|
-
# ==== Output
|
333
|
-
# [String] The output from stderr.
|
334
|
-
#
|
335
|
-
# All trailing line feeds are removed.
|
336
|
-
def err
|
337
|
-
@err.respond_to?(:string) ? @err.string.gsub(/\n*\z/, '') : ''
|
338
|
-
end
|
339
|
-
|
340
|
-
# Reset the stdout and stderr stream variables.
|
341
|
-
def reset_io
|
342
|
-
@out = StringIO.new
|
343
|
-
@err = StringIO.new
|
344
|
-
end
|
345
|
-
|
346
|
-
############################################################################
|
347
|
-
# Support methods.
|
348
|
-
############################################################################
|
349
|
-
|
350
|
-
# Indicates whether the specified method has been called on a given class.
|
351
|
-
# ==== Input
|
352
|
-
# [method_name : String] The name of the method.
|
353
|
-
#
|
354
|
-
# This value may be a string or a symbol.
|
355
|
-
# [class_name : String : @class.name] The name of the class that the method
|
356
|
-
# should have been invoked from.
|
357
|
-
def method_called?(method_name, class_name = @class.name)
|
358
|
-
!@stack_trace.index(
|
359
|
-
{:method => method_name.to_sym, :class => class_name}).nil?
|
360
|
-
end
|
361
|
-
|
362
|
-
# Resets the trace arrays.
|
363
|
-
#
|
364
|
-
# This is intended for use in cases where code may be called multiple
|
365
|
-
# times in a single test.
|
366
|
-
def reset_trace
|
367
|
-
@stack_trace = []
|
368
|
-
@obj.trace = [] if @obj.respond_to?(:trace=)
|
369
|
-
end
|
370
|
-
|
371
|
-
# Shows the trace history as it stands, if the object supports it.
|
372
|
-
def show_trace
|
373
|
-
return unless defined? @obj
|
374
|
-
puts @obj.trace.join("\n" + '-' * 80 + "\n") if @obj.respond_to?(:trace)
|
375
|
-
end
|
376
|
-
|
377
|
-
############################################################################
|
378
|
-
# Assertions.
|
379
|
-
############################################################################
|
380
|
-
|
381
|
-
# Asserts that a value is equal to false.
|
382
|
-
# ==== Input
|
383
|
-
# [value : Any] The value to check for equality against false.
|
384
|
-
# [message : String : nil] The message to display if the value is not false.
|
385
|
-
def assert_false(value, message = nil)
|
386
|
-
assert_equal false, value, message
|
387
|
-
end
|
388
|
-
|
389
|
-
# Asserts that a value is equal to true.
|
390
|
-
# ==== Input
|
391
|
-
# [value : Any] The value to check for equality against true.
|
392
|
-
# [message : String : nil] The message to display if the value is not true.
|
393
|
-
def assert_true(value, message = nil)
|
394
|
-
assert_equal true, value, message
|
395
|
-
end
|
396
|
-
|
397
|
-
# Asserts that the negation of a value is true.
|
398
|
-
# ==== Input
|
399
|
-
# [value : Any] The value which will be negated and then asserted.
|
400
|
-
# [message : String : nil] The message to display if the assertion fails.
|
401
|
-
def assert_not(value, message = nil)
|
402
|
-
assert !value, message
|
403
|
-
end
|
404
|
-
|
405
|
-
# Assert that an array has a specified number of elements.
|
406
|
-
# ==== Input
|
407
|
-
# [array : Array] The array that will have it's length checked.
|
408
|
-
# [length : Fixnum] The length that the array should be.
|
409
|
-
# [message : String : nil] The message to display if the assertion fails.
|
410
|
-
def assert_array_count(array, length, message = nil)
|
411
|
-
if message.nil?
|
412
|
-
message = "#{array} has #{array.length} item(s), " +
|
413
|
-
"but was expected to have #{length}."
|
414
|
-
end
|
415
|
-
|
416
|
-
assert array.length == length, message
|
417
|
-
end
|
418
|
-
|
419
|
-
############################################################################
|
420
|
-
# Assertions - Stack trace.
|
421
|
-
############################################################################
|
422
|
-
|
423
|
-
# Asserts that a method was called on a class.
|
424
|
-
# ==== Input
|
425
|
-
# [method_name : String] The name of the method to check for.
|
426
|
-
# [class_name : String : @class.name] The name of the class
|
427
|
-
# on which <tt>method_name</tt>
|
428
|
-
# should have been invoked.
|
429
|
-
def assert_method(method_name, class_name = @class.name)
|
430
|
-
assert method_called?(method_name.to_sym, class_name),
|
431
|
-
"#{class_name}.#{method_name} has not been called."
|
432
|
-
end
|
433
|
-
|
434
|
-
# Asserts that a method was not called on a class.
|
435
|
-
# ==== Input
|
436
|
-
# [method_name : String] The name of the method to check for.
|
437
|
-
# [class_name : String : @class.name] The name of the class
|
438
|
-
# on which <tt>method_name</tt>
|
439
|
-
# should not have been invoked.
|
440
|
-
def assert_not_method(method_name, class_name = @class.name)
|
441
|
-
assert !method_called?(method_name.to_sym, class_name),
|
442
|
-
"#{class_name}.#{method_name} should not be called."
|
443
|
-
end
|
444
|
-
|
445
|
-
# Asserts that a method was called with the specified parameters.
|
446
|
-
# ==== Input
|
447
|
-
# [method_name : String] The name of the method to check.
|
448
|
-
# [*args : Array] The parameters that were passed in to the method.
|
449
|
-
def assert_trace_args(method_name, *args)
|
450
|
-
match = false
|
451
|
-
|
452
|
-
list = []
|
453
|
-
|
454
|
-
# Loop through the stack trace to see if the method was called
|
455
|
-
# with the specified arguments.
|
456
|
-
@obj.trace.each do |trace|
|
457
|
-
if trace[:method] == method_name and trace[:args] == args
|
458
|
-
match = true
|
459
|
-
break
|
460
|
-
elsif trace[:method] == method_name
|
461
|
-
list << trace[:args]
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
assert match,
|
466
|
-
"#{method_name} was not called with the following parameters:\n" +
|
467
|
-
"#{args.join("\n" + '-' * 80 + "\n")}\n" +
|
468
|
-
'*' * 80 + "\n" +
|
469
|
-
"#{method_name} was recorded as follows:\n" +
|
470
|
-
"#{list.join("\n" + '-' * 80 + "\n")}"
|
471
|
-
end
|
472
|
-
|
473
|
-
# Asserts that a method was called with the specified parameters
|
474
|
-
# and returned the specified result.
|
475
|
-
# ==== Input
|
476
|
-
# [method_name : String] The name of the method to check.
|
477
|
-
# [result : Any] The expected result of the method call.
|
478
|
-
# [*args : Array] The parameters that were passed in to the method.
|
479
|
-
def assert_trace_info(method_name, result, *args)
|
480
|
-
match = (@obj.trace.index(
|
481
|
-
{:methd => method_name, :args => args, :result => result}))
|
482
|
-
|
483
|
-
list = []
|
484
|
-
|
485
|
-
# Only get a list of possible results if a match was not found.
|
486
|
-
unless match
|
487
|
-
@obj.trace.each do |trace|
|
488
|
-
if trace[:method] == method_name
|
489
|
-
list << {:args => trace[:args], :result => trace[:result]}
|
490
|
-
end
|
491
|
-
end
|
492
|
-
end
|
493
|
-
|
494
|
-
assert match,
|
495
|
-
"#{method_name} was not called with the following parameters:\n" +
|
496
|
-
"#{args}\n" +
|
497
|
-
"or did not return the following result:\n" +
|
498
|
-
"#{result}\n" +
|
499
|
-
"#{method_name} was recorded as follows:\n" +
|
500
|
-
"#{list.join("\n" + '-' * 80 + "\n")}"
|
501
|
-
end
|
502
|
-
end
|