ztk 1.0.0.rc.0 → 1.0.0.rc.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ztk/background.rb +53 -10
- data/lib/ztk/base.rb +16 -23
- data/lib/ztk/benchmark.rb +36 -8
- data/lib/ztk/command.rb +7 -7
- data/lib/ztk/dsl.rb +5 -2
- data/lib/ztk/dsl/base.rb +133 -1
- data/lib/ztk/dsl/core.rb +27 -0
- data/lib/ztk/dsl/core/actions.rb +3 -0
- data/lib/ztk/dsl/core/actions/find.rb +4 -0
- data/lib/ztk/dsl/core/actions/timestamps.rb +4 -0
- data/lib/ztk/dsl/core/attributes.rb +4 -0
- data/lib/ztk/dsl/core/dataset.rb +4 -12
- data/lib/ztk/dsl/core/io.rb +4 -0
- data/lib/ztk/dsl/core/relations.rb +4 -0
- data/lib/ztk/dsl/core/relations/belongs_to.rb +4 -0
- data/lib/ztk/dsl/core/relations/has_many.rb +4 -0
- data/lib/ztk/parallel.rb +66 -12
- data/lib/ztk/report.rb +9 -9
- data/lib/ztk/rescue_retry.rb +60 -11
- data/lib/ztk/spinner.rb +5 -5
- data/lib/ztk/ssh.rb +30 -30
- data/lib/ztk/tcp_socket_check.rb +7 -7
- data/lib/ztk/ui.rb +15 -8
- data/lib/ztk/version.rb +1 -1
- data/spec/spec_helper.rb +22 -4
- data/spec/ztk/background_spec.rb +0 -30
- data/spec/ztk/base_spec.rb +0 -6
- data/spec/ztk/benchmark_spec.rb +6 -16
- data/spec/ztk/command_spec.rb +23 -68
- data/spec/ztk/config_spec.rb +0 -6
- data/spec/ztk/dsl_spec.rb +0 -7
- data/spec/ztk/parallel_spec.rb +0 -30
- data/spec/ztk/rescue_retry_spec.rb +0 -6
- data/spec/ztk/spinner_spec.rb +0 -6
- data/spec/ztk/ssh_spec.rb +50 -102
- data/spec/ztk/tcp_socket_check_spec.rb +0 -30
- data/spec/ztk/template_spec.rb +0 -6
- data/spec/ztk/ui_spec.rb +70 -0
- data/spec/ztk/version_spec.rb +39 -0
- metadata +7 -3
data/lib/ztk/spinner.rb
CHANGED
@@ -60,16 +60,16 @@ module ZTK
|
|
60
60
|
options = Base.build_config({
|
61
61
|
:step => 0.1
|
62
62
|
}.merge(options))
|
63
|
-
options.logger.debug { "options(#{options.send(:table).inspect})" }
|
63
|
+
options.ui.logger.debug { "options(#{options.send(:table).inspect})" }
|
64
64
|
|
65
|
-
!block_given? and Base.log_and_raise(options.logger, SpinnerError, "You must supply a block!")
|
65
|
+
!block_given? and Base.log_and_raise(options.ui.logger, SpinnerError, "You must supply a block!")
|
66
66
|
|
67
67
|
count = 0
|
68
68
|
spinner = Thread.new do
|
69
69
|
while count do
|
70
|
-
options.stdout.print(CHARSET[(count += 1) % CHARSET.length])
|
71
|
-
options.stdout.print("\b")
|
72
|
-
options.stdout.respond_to?(:flush) and options.stdout.flush
|
70
|
+
options.ui.stdout.print(CHARSET[(count += 1) % CHARSET.length])
|
71
|
+
options.ui.stdout.print("\b")
|
72
|
+
options.ui.stdout.respond_to?(:flush) and options.ui.stdout.flush
|
73
73
|
sleep(options.step)
|
74
74
|
end
|
75
75
|
end
|
data/lib/ztk/ssh.rb
CHANGED
@@ -139,7 +139,7 @@ module ZTK
|
|
139
139
|
:timeout => 60,
|
140
140
|
:ignore_exit_status => false
|
141
141
|
}.merge(configuration))
|
142
|
-
config.logger.debug { "config=#{config.send(:table).inspect}" }
|
142
|
+
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
|
143
143
|
end
|
144
144
|
|
145
145
|
# Starts an SSH session. Can also be used to get the Net::SSH object.
|
@@ -158,7 +158,7 @@ module ZTK
|
|
158
158
|
|
159
159
|
# Close our session gracefully.
|
160
160
|
def close
|
161
|
-
config.logger.debug { "close" }
|
161
|
+
config.ui.logger.debug { "close" }
|
162
162
|
ssh and !ssh.closed? and ssh.close
|
163
163
|
end
|
164
164
|
|
@@ -174,8 +174,8 @@ module ZTK
|
|
174
174
|
# end
|
175
175
|
# ssh.console
|
176
176
|
def console
|
177
|
-
config.logger.debug { "config=#{config.send(:table).inspect}" }
|
178
|
-
config.logger.info { "console(#{console_command.inspect})" }
|
177
|
+
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
|
178
|
+
config.ui.logger.info { "console(#{console_command.inspect})" }
|
179
179
|
|
180
180
|
Kernel.exec(console_command)
|
181
181
|
end
|
@@ -204,9 +204,9 @@ module ZTK
|
|
204
204
|
def exec(command, options={})
|
205
205
|
options = OpenStruct.new({ :exit_code => 0, :silence => false }.merge(options))
|
206
206
|
|
207
|
-
config.logger.debug { "config=#{config.send(:table).inspect}" }
|
208
|
-
config.logger.debug { "options=#{options.send(:table).inspect}" }
|
209
|
-
config.logger.info { "exec(#{command.inspect})" }
|
207
|
+
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
|
208
|
+
config.ui.logger.debug { "options=#{options.send(:table).inspect}" }
|
209
|
+
config.ui.logger.info { "exec(#{command.inspect})" }
|
210
210
|
|
211
211
|
output = ""
|
212
212
|
exit_code = -1
|
@@ -220,7 +220,7 @@ module ZTK
|
|
220
220
|
@ssh = Net::SSH.start(config.host_name, config.user, ssh_options)
|
221
221
|
|
222
222
|
channel = ssh.open_channel do |chan|
|
223
|
-
config.logger.debug { "Channel opened." }
|
223
|
+
config.ui.logger.debug { "Channel opened." }
|
224
224
|
|
225
225
|
direct_log(:debug) { log_header("COMMAND") }
|
226
226
|
direct_log(:debug) { "#{command}\n" }
|
@@ -237,7 +237,7 @@ module ZTK
|
|
237
237
|
end
|
238
238
|
direct_log(:debug) { data }
|
239
239
|
|
240
|
-
config.stdout.print(data) unless options.silence
|
240
|
+
config.ui.stdout.print(data) unless options.silence
|
241
241
|
output += data
|
242
242
|
end
|
243
243
|
|
@@ -249,7 +249,7 @@ module ZTK
|
|
249
249
|
end
|
250
250
|
direct_log(:warn) { data }
|
251
251
|
|
252
|
-
config.stderr.print(data) unless options.silence
|
252
|
+
config.ui.stderr.print(data) unless options.silence
|
253
253
|
output += data
|
254
254
|
end
|
255
255
|
|
@@ -262,7 +262,7 @@ module ZTK
|
|
262
262
|
end
|
263
263
|
|
264
264
|
ch.on_open_failed do |c, code, desc|
|
265
|
-
config.logger.fatal { "Open failed! (#{code.inspect} - #{desc.inspect})" }
|
265
|
+
config.ui.logger.fatal { "Open failed! (#{code.inspect} - #{desc.inspect})" }
|
266
266
|
end
|
267
267
|
|
268
268
|
end
|
@@ -270,7 +270,7 @@ module ZTK
|
|
270
270
|
channel.wait
|
271
271
|
|
272
272
|
direct_log(:debug) { log_header("CLOSED") }
|
273
|
-
config.logger.debug { "Channel closed." }
|
273
|
+
config.ui.logger.debug { "Channel closed." }
|
274
274
|
end
|
275
275
|
end
|
276
276
|
|
@@ -284,7 +284,7 @@ module ZTK
|
|
284
284
|
(exit_signal.nil? ? nil : "exit_signal=#{exit_signal} (#{EXIT_SIGNALS[exit_signal]})")
|
285
285
|
].compact.join(", ")
|
286
286
|
|
287
|
-
config.logger.debug { message }
|
287
|
+
config.ui.logger.debug { message }
|
288
288
|
|
289
289
|
if !config.ignore_exit_status && (exit_code != options.exit_code)
|
290
290
|
log_and_raise(SSHError, message)
|
@@ -308,23 +308,23 @@ module ZTK
|
|
308
308
|
# remote = File.expand_path(File.join("/tmp", "id_rsa.pub"))
|
309
309
|
# ssh.upload(local, remote)
|
310
310
|
def upload(local, remote)
|
311
|
-
config.logger.debug { "config=#{config.send(:table).inspect}" }
|
312
|
-
config.logger.info { "upload(#{local.inspect}, #{remote.inspect})" }
|
311
|
+
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
|
312
|
+
config.ui.logger.info { "upload(#{local.inspect}, #{remote.inspect})" }
|
313
313
|
|
314
314
|
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
315
315
|
@sftp = Net::SFTP.start(config.host_name, config.user, ssh_options)
|
316
316
|
sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args|
|
317
317
|
case event
|
318
318
|
when :open
|
319
|
-
config.logger.debug { "upload(#{args[0].local} -> #{args[0].remote})" }
|
319
|
+
config.ui.logger.debug { "upload(#{args[0].local} -> #{args[0].remote})" }
|
320
320
|
when :close
|
321
|
-
config.logger.debug { "close(#{args[0].remote})" }
|
321
|
+
config.ui.logger.debug { "close(#{args[0].remote})" }
|
322
322
|
when :mkdir
|
323
|
-
config.logger.debug { "mkdir(#{args[0]})" }
|
323
|
+
config.ui.logger.debug { "mkdir(#{args[0]})" }
|
324
324
|
when :put
|
325
|
-
config.logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
|
325
|
+
config.ui.logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
|
326
326
|
when :finish
|
327
|
-
config.logger.debug { "finish" }
|
327
|
+
config.ui.logger.debug { "finish" }
|
328
328
|
end
|
329
329
|
end
|
330
330
|
end
|
@@ -348,23 +348,23 @@ module ZTK
|
|
348
348
|
# remote = File.expand_path(File.join(ENV["HOME"], ".ssh", "id_rsa.pub"))
|
349
349
|
# ssh.download(remote, local)
|
350
350
|
def download(remote, local)
|
351
|
-
config.logger.debug { "config=#{config.send(:table).inspect}" }
|
352
|
-
config.logger.info { "download(#{remote.inspect}, #{local.inspect})" }
|
351
|
+
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
|
352
|
+
config.ui.logger.info { "download(#{remote.inspect}, #{local.inspect})" }
|
353
353
|
|
354
354
|
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
355
355
|
@sftp = Net::SFTP.start(config.host_name, config.user, ssh_options)
|
356
356
|
sftp.download!(remote.to_s, local.to_s) do |event, downloader, *args|
|
357
357
|
case event
|
358
358
|
when :open
|
359
|
-
config.logger.debug { "download(#{args[0].remote} -> #{args[0].local})" }
|
359
|
+
config.ui.logger.debug { "download(#{args[0].remote} -> #{args[0].local})" }
|
360
360
|
when :close
|
361
|
-
config.logger.debug { "close(#{args[0].local})" }
|
361
|
+
config.ui.logger.debug { "close(#{args[0].local})" }
|
362
362
|
when :mkdir
|
363
|
-
config.logger.debug { "mkdir(#{args[0]})" }
|
363
|
+
config.ui.logger.debug { "mkdir(#{args[0]})" }
|
364
364
|
when :get
|
365
|
-
config.logger.debug { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
|
365
|
+
config.ui.logger.debug { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
|
366
366
|
when :finish
|
367
|
-
config.logger.debug { "finish" }
|
367
|
+
config.ui.logger.debug { "finish" }
|
368
368
|
end
|
369
369
|
end
|
370
370
|
end
|
@@ -389,7 +389,7 @@ module ZTK
|
|
389
389
|
command << [ "-o", "ProxyCommand=\"#{proxy_command}\"" ] if config.proxy_host_name
|
390
390
|
command << "#{config.user}@#{config.host_name}"
|
391
391
|
command = command.flatten.compact.join(" ")
|
392
|
-
config.logger.debug { "console_command(#{command.inspect})" }
|
392
|
+
config.ui.logger.debug { "console_command(#{command.inspect})" }
|
393
393
|
command
|
394
394
|
end
|
395
395
|
|
@@ -410,7 +410,7 @@ module ZTK
|
|
410
410
|
command << "#{config.proxy_user}@#{config.proxy_host_name}"
|
411
411
|
command << "nc %h %p"
|
412
412
|
command = command.flatten.compact.join(" ")
|
413
|
-
config.logger.debug { "proxy_command(#{command.inspect})" }
|
413
|
+
config.ui.logger.debug { "proxy_command(#{command.inspect})" }
|
414
414
|
command
|
415
415
|
end
|
416
416
|
|
@@ -441,7 +441,7 @@ module ZTK
|
|
441
441
|
# This is not plainly documented on the Net::SSH config class.
|
442
442
|
options.merge!(:password => config.password) if config.password
|
443
443
|
|
444
|
-
config.logger.debug { "ssh_options(#{options.inspect})" }
|
444
|
+
config.ui.logger.debug { "ssh_options(#{options.inspect})" }
|
445
445
|
options
|
446
446
|
end
|
447
447
|
|
data/lib/ztk/tcp_socket_check.rb
CHANGED
@@ -94,7 +94,7 @@ module ZTK
|
|
94
94
|
:timeout => 5,
|
95
95
|
:wait => 60
|
96
96
|
}.merge(configuration))
|
97
|
-
config.logger.debug { "config=#{config.send(:table).inspect}" }
|
97
|
+
config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
|
98
98
|
end
|
99
99
|
|
100
100
|
# Check to see if socket on the host and port specified is ready. This
|
@@ -110,15 +110,15 @@ module ZTK
|
|
110
110
|
socket = TCPSocket.new(config.host, config.port)
|
111
111
|
|
112
112
|
if config.data.nil?
|
113
|
-
config.logger.debug { "read(#{config.host}:#{config.port})" }
|
113
|
+
config.ui.logger.debug { "read(#{config.host}:#{config.port})" }
|
114
114
|
((IO.select([socket], nil, nil, config.timeout) && socket.gets) ? true : false)
|
115
115
|
else
|
116
|
-
config.logger.debug { "write(#{config.host}:#{config.port}, #{config.data.size} bytes)" }
|
116
|
+
config.ui.logger.debug { "write(#{config.host}:#{config.port}, #{config.data.size} bytes)" }
|
117
117
|
((IO.select(nil, [socket], nil, config.timeout) && socket.write(config.data)) ? true : false)
|
118
118
|
end
|
119
119
|
|
120
120
|
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH => e
|
121
|
-
config.logger.debug { "#{config.host}:#{config.port} - #{e.message}" }
|
121
|
+
config.ui.logger.debug { "#{config.host}:#{config.port} - #{e.message}" }
|
122
122
|
false
|
123
123
|
ensure
|
124
124
|
(socket && socket.close)
|
@@ -131,16 +131,16 @@ module ZTK
|
|
131
131
|
# @return [Boolean] Returns true or false depending on weither the socket
|
132
132
|
# became ready or not.
|
133
133
|
def wait
|
134
|
-
config.logger.debug { "Waiting for socket to become available; timeout after #{config.wait} seconds." }
|
134
|
+
config.ui.logger.debug { "Waiting for socket to become available; timeout after #{config.wait} seconds." }
|
135
135
|
Timeout.timeout(config.wait) do
|
136
136
|
until ready?
|
137
|
-
config.logger.debug { "Sleeping 1 second." }
|
137
|
+
config.ui.logger.debug { "Sleeping 1 second." }
|
138
138
|
sleep(1)
|
139
139
|
end
|
140
140
|
end
|
141
141
|
true
|
142
142
|
rescue Timeout::Error => e
|
143
|
-
config.logger.warn { "socket(#{config.host}:#{config.port}) timeout!" }
|
143
|
+
config.ui.logger.warn { "socket(#{config.host}:#{config.port}) timeout!" }
|
144
144
|
false
|
145
145
|
end
|
146
146
|
|
data/lib/ztk/ui.rb
CHANGED
@@ -22,24 +22,31 @@ require "base64"
|
|
22
22
|
|
23
23
|
module ZTK
|
24
24
|
|
25
|
-
# ZTK::
|
25
|
+
# ZTK::UI Error Class
|
26
26
|
#
|
27
27
|
# @author Zachary Patten <zachary@jovelabs.net>
|
28
28
|
class UIError < Error; end
|
29
29
|
|
30
|
+
# ZTK UI Wrapper Class
|
31
|
+
#
|
30
32
|
# @author Zachary Patten <zachary@jovelabs.net>
|
31
33
|
class UI < ZTK::Base
|
32
34
|
|
33
35
|
attr_accessor :stdout, :stderr, :stdin, :logger
|
34
36
|
|
35
37
|
def initialize(configuration={})
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
defined?(Rails) and (rails_logger = Rails.logger)
|
39
|
+
null_logger = (::ZTK::Logger.new("/dev/null") rescue ::Logger.new("/dev/null"))
|
40
|
+
|
41
|
+
@stdout = (configuration[:stdout] || $stdout || STDOUT)
|
42
|
+
@stderr = (configuration[:stderr] || $stderr || STDERR)
|
43
|
+
@stdin = (configuration[:stdin] || $stdin || STDIN)
|
44
|
+
@logger = (configuration[:logger] || $logger || rails_logger || null_logger)
|
45
|
+
|
46
|
+
(@stdout && @stdout.respond_to?(:sync=)) and @stdout.sync = true
|
47
|
+
(@stderr && @stderr.respond_to?(:sync=)) and @stderr.sync = true
|
48
|
+
(@stdin && @stdin.respond_to?(:sync=)) and @stdin.sync = true
|
49
|
+
(@logger && @logger.respond_to?(:sync=)) and @logger.sync = true
|
43
50
|
end
|
44
51
|
|
45
52
|
end
|
data/lib/ztk/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -32,10 +32,28 @@ ENV['LOG_LEVEL'] = "DEBUG"
|
|
32
32
|
|
33
33
|
WAIT_SMALL = 3
|
34
34
|
|
35
|
-
|
35
|
+
RSpec.configure do |config|
|
36
|
+
|
37
|
+
config.before(:all) do
|
38
|
+
$stdout = File.open("/dev/null", "w")
|
39
|
+
$stderr = File.open("/dev/null", "w")
|
40
|
+
$stdin = File.open("/dev/null", "r")
|
41
|
+
$logger = ZTK::Logger.new(File.join("/tmp", "test.log"))
|
42
|
+
|
43
|
+
$logger.info { "=" * 80 }
|
44
|
+
$logger.info { "STARTING ZTK v#{ZTK::VERSION} TEST RUN @ #{Time.now.utc}" }
|
45
|
+
$logger.info { "=" * 80 }
|
46
|
+
end
|
47
|
+
|
48
|
+
config.before(:each) do
|
49
|
+
$ui = ZTK::UI.new(
|
50
|
+
:stdout => StringIO.new,
|
51
|
+
:stderr => StringIO.new,
|
52
|
+
:stdin => StringIO.new
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
36
57
|
|
37
|
-
$logger.info { "=" * 80 }
|
38
|
-
$logger.info { "STARTING ZTK v#{ZTK::VERSION} TEST RUN @ #{Time.now.utc}" }
|
39
|
-
$logger.info { "=" * 80 }
|
40
58
|
|
41
59
|
################################################################################
|
data/spec/ztk/background_spec.rb
CHANGED
@@ -24,42 +24,12 @@ describe ZTK::Background do
|
|
24
24
|
|
25
25
|
subject { ZTK::Background.new }
|
26
26
|
|
27
|
-
before(:all) do
|
28
|
-
$stdout = File.open("/dev/null", "w")
|
29
|
-
$stderr = File.open("/dev/null", "w")
|
30
|
-
$stdin = File.open("/dev/null", "r")
|
31
|
-
end
|
32
|
-
|
33
27
|
describe "class" do
|
34
28
|
|
35
29
|
it "should be an instance of ZTK::Background" do
|
36
30
|
subject.should be_an_instance_of ZTK::Background
|
37
31
|
end
|
38
32
|
|
39
|
-
describe "default config" do
|
40
|
-
|
41
|
-
it "should use $stdout as the default" do
|
42
|
-
subject.config.stdout.should be_a_kind_of $stdout.class
|
43
|
-
subject.config.stdout.should == $stdout
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should use $stderr as the default" do
|
47
|
-
subject.config.stderr.should be_a_kind_of $stderr.class
|
48
|
-
subject.config.stderr.should == $stderr
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should use $stdin as the default" do
|
52
|
-
subject.config.stdin.should be_a_kind_of $stdin.class
|
53
|
-
subject.config.stdin.should == $stdin
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should use $logger as the default" do
|
57
|
-
subject.config.logger.should be_a_kind_of ZTK::Logger
|
58
|
-
subject.config.logger.should == $logger
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
33
|
end
|
64
34
|
|
65
35
|
describe "behaviour" do
|
data/spec/ztk/base_spec.rb
CHANGED
@@ -24,12 +24,6 @@ describe ZTK::Base do
|
|
24
24
|
|
25
25
|
subject { ZTK::Base }
|
26
26
|
|
27
|
-
before(:all) do
|
28
|
-
$stdout = File.open("/dev/null", "w")
|
29
|
-
$stderr = File.open("/dev/null", "w")
|
30
|
-
$stdin = File.open("/dev/null", "r")
|
31
|
-
end
|
32
|
-
|
33
27
|
describe "class" do
|
34
28
|
|
35
29
|
it "should be ZTK::Base" do
|
data/spec/ztk/benchmark_spec.rb
CHANGED
@@ -24,12 +24,6 @@ describe ZTK::Benchmark do
|
|
24
24
|
|
25
25
|
subject { ZTK::Benchmark }
|
26
26
|
|
27
|
-
before(:all) do
|
28
|
-
$stdout = File.open("/dev/null", "w")
|
29
|
-
$stderr = File.open("/dev/null", "w")
|
30
|
-
$stdin = File.open("/dev/null", "r")
|
31
|
-
end
|
32
|
-
|
33
27
|
describe "class" do
|
34
28
|
|
35
29
|
it "should be ZTK::Benchmark" do
|
@@ -54,33 +48,29 @@ describe ZTK::Benchmark do
|
|
54
48
|
end
|
55
49
|
|
56
50
|
it "should throw an exception if executed with a message but without a mark" do
|
57
|
-
stdout = StringIO.new
|
58
51
|
lambda {
|
59
|
-
ZTK::Benchmark.bench(:
|
52
|
+
ZTK::Benchmark.bench(:ui => $ui, :message => "Hello World")
|
60
53
|
}.should raise_error ZTK::BenchmarkError
|
61
54
|
end
|
62
55
|
|
63
56
|
it "should throw an exception if executed without a message but with a mark" do
|
64
|
-
stdout = StringIO.new
|
65
57
|
lambda {
|
66
|
-
ZTK::Benchmark.bench(:
|
58
|
+
ZTK::Benchmark.bench(:ui => $ui, :mark => "%0.4f")
|
67
59
|
}.should raise_error ZTK::BenchmarkError
|
68
60
|
end
|
69
61
|
|
70
62
|
it "should not write to STDOUT if not given a message or mark" do
|
71
|
-
|
72
|
-
ZTK::Benchmark.bench(:stdout => stdout) do
|
63
|
+
ZTK::Benchmark.bench(:ui => $ui) do
|
73
64
|
sleep(0.1)
|
74
65
|
end
|
75
|
-
stdout.size.should == 0
|
66
|
+
$ui.stdout.size.should == 0
|
76
67
|
end
|
77
68
|
|
78
69
|
it "should write to STDOUT if given a message and mark" do
|
79
|
-
|
80
|
-
ZTK::Benchmark.bench(:stdout => stdout, :message => "Hello World", :mark => "%0.4f") do
|
70
|
+
ZTK::Benchmark.bench(:ui => $ui, :message => "Hello World", :mark => "%0.4f") do
|
81
71
|
sleep(0.1)
|
82
72
|
end
|
83
|
-
stdout.size.should > 0
|
73
|
+
$ui.stdout.size.should > 0
|
84
74
|
end
|
85
75
|
|
86
76
|
end
|
data/spec/ztk/command_spec.rb
CHANGED
@@ -24,42 +24,12 @@ describe ZTK::Command do
|
|
24
24
|
|
25
25
|
subject { ZTK::Command.new }
|
26
26
|
|
27
|
-
before(:all) do
|
28
|
-
$stdout = File.open("/dev/null", "w")
|
29
|
-
$stderr = File.open("/dev/null", "w")
|
30
|
-
$stdin = File.open("/dev/null", "r")
|
31
|
-
end
|
32
|
-
|
33
27
|
describe "class" do
|
34
28
|
|
35
29
|
it "should be an instance of ZTK::Command" do
|
36
30
|
subject.should be_an_instance_of ZTK::Command
|
37
31
|
end
|
38
32
|
|
39
|
-
describe "default config" do
|
40
|
-
|
41
|
-
it "should use $stdout as the default" do
|
42
|
-
subject.config.stdout.should be_a_kind_of $stdout.class
|
43
|
-
subject.config.stdout.should == $stdout
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should use $stderr as the default" do
|
47
|
-
subject.config.stderr.should be_a_kind_of $stderr.class
|
48
|
-
subject.config.stderr.should == $stderr
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should use $stdin as the default" do
|
52
|
-
subject.config.stdin.should be_a_kind_of $stdin.class
|
53
|
-
subject.config.stdin.should == $stdin
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should use $logger as the default" do
|
57
|
-
subject.config.logger.should be_a_kind_of ZTK::Logger
|
58
|
-
subject.config.logger.should == $logger
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
33
|
end
|
64
34
|
|
65
35
|
describe "behaviour" do
|
@@ -67,21 +37,19 @@ describe ZTK::Command do
|
|
67
37
|
describe "execute" do
|
68
38
|
|
69
39
|
it "should be able to execute the command \"hostname -f\"" do
|
70
|
-
stdout = StringIO.new
|
71
40
|
subject.config do |config|
|
72
|
-
config.
|
41
|
+
config.ui = $ui
|
73
42
|
end
|
74
43
|
hostname = %x(hostname -f).chomp
|
75
44
|
status = subject.exec("hostname -f")
|
76
45
|
status.exit_code.should == 0
|
77
|
-
stdout.rewind
|
78
|
-
stdout.read.chomp.should == hostname
|
46
|
+
$ui.stdout.rewind
|
47
|
+
$ui.stdout.read.chomp.should == hostname
|
79
48
|
end
|
80
49
|
|
81
50
|
it "should timeout after the period specified" do
|
82
|
-
stdout = StringIO.new
|
83
51
|
subject.config do |config|
|
84
|
-
config.
|
52
|
+
config.ui = $ui
|
85
53
|
config.timeout = WAIT_SMALL
|
86
54
|
end
|
87
55
|
hostname = %x(hostname -f).chomp
|
@@ -89,26 +57,23 @@ describe ZTK::Command do
|
|
89
57
|
end
|
90
58
|
|
91
59
|
it "should throw an exception if the exit status is not as expected" do
|
92
|
-
stdout = StringIO.new
|
93
60
|
subject.config do |config|
|
94
|
-
config.
|
61
|
+
config.ui = $ui
|
95
62
|
end
|
96
63
|
lambda { subject.exec("/bin/bash -c 'exit 64'") }.should raise_error ZTK::CommandError
|
97
64
|
end
|
98
65
|
|
99
66
|
it "should return a instance of an OpenStruct object" do
|
100
|
-
stdout = StringIO.new
|
101
67
|
subject.config do |config|
|
102
|
-
config.
|
68
|
+
config.ui = $ui
|
103
69
|
end
|
104
70
|
result = subject.exec(%q{echo "Hello World"})
|
105
71
|
result.should be_an_instance_of OpenStruct
|
106
72
|
end
|
107
73
|
|
108
74
|
it "should return the exit code" do
|
109
|
-
stdout = StringIO.new
|
110
75
|
subject.config do |config|
|
111
|
-
config.
|
76
|
+
config.ui = $ui
|
112
77
|
end
|
113
78
|
data = 64
|
114
79
|
|
@@ -117,9 +82,8 @@ describe ZTK::Command do
|
|
117
82
|
end
|
118
83
|
|
119
84
|
it "should return the output" do
|
120
|
-
stdout = StringIO.new
|
121
85
|
subject.config do |config|
|
122
|
-
config.
|
86
|
+
config.ui = $ui
|
123
87
|
end
|
124
88
|
data = "Hello World @ #{Time.now.utc}"
|
125
89
|
|
@@ -128,9 +92,8 @@ describe ZTK::Command do
|
|
128
92
|
end
|
129
93
|
|
130
94
|
it "should allow us to change the expected exit code" do
|
131
|
-
stdout = StringIO.new
|
132
95
|
subject.config do |config|
|
133
|
-
config.
|
96
|
+
config.ui = $ui
|
134
97
|
end
|
135
98
|
data = 32
|
136
99
|
result = subject.exec(%Q{/bin/bash -c 'exit #{data}'}, :exit_code => data)
|
@@ -139,25 +102,21 @@ describe ZTK::Command do
|
|
139
102
|
describe "stdout" do
|
140
103
|
|
141
104
|
it "should capture STDOUT and send it to the appropriate pipe" do
|
142
|
-
stdout, stderr, stdin = StringIO.new, StringIO.new, StringIO.new
|
143
|
-
|
144
105
|
subject.config do |config|
|
145
|
-
config.
|
146
|
-
config.stderr = stderr
|
147
|
-
config.stdin = stdin
|
106
|
+
config.ui = $ui
|
148
107
|
end
|
149
108
|
data = "Hello World @ #{Time.now.utc}"
|
150
109
|
|
151
110
|
subject.exec(%Q{echo "#{data}" -f >&1})
|
152
111
|
|
153
|
-
stdout.rewind
|
154
|
-
stdout.read.match(data).should_not be nil
|
112
|
+
$ui.stdout.rewind
|
113
|
+
$ui.stdout.read.match(data).should_not be nil
|
155
114
|
|
156
|
-
stderr.rewind
|
157
|
-
stderr.read.match(data).should be nil
|
115
|
+
$ui.stderr.rewind
|
116
|
+
$ui.stderr.read.match(data).should be nil
|
158
117
|
|
159
|
-
stdin.rewind
|
160
|
-
stdin.read.match(data).should be nil
|
118
|
+
$ui.stdin.rewind
|
119
|
+
$ui.stdin.read.match(data).should be nil
|
161
120
|
end
|
162
121
|
|
163
122
|
end
|
@@ -165,25 +124,21 @@ describe ZTK::Command do
|
|
165
124
|
describe "stderr" do
|
166
125
|
|
167
126
|
it "should capture STDERR and send it to the appropriate pipe" do
|
168
|
-
stdout, stderr, stdin = StringIO.new, StringIO.new, StringIO.new
|
169
|
-
|
170
127
|
subject.config do |config|
|
171
|
-
config.
|
172
|
-
config.stderr = stderr
|
173
|
-
config.stdin = stdin
|
128
|
+
config.ui = $ui
|
174
129
|
end
|
175
130
|
data = "Hello World @ #{Time.now.utc}"
|
176
131
|
|
177
132
|
subject.exec(%Q{echo "#{data}" -f >&2})
|
178
133
|
|
179
|
-
stdout.rewind
|
180
|
-
stdout.read.match(data).should be nil
|
134
|
+
$ui.stdout.rewind
|
135
|
+
$ui.stdout.read.match(data).should be nil
|
181
136
|
|
182
|
-
stderr.rewind
|
183
|
-
stderr.read.match(data).should_not be nil
|
137
|
+
$ui.stderr.rewind
|
138
|
+
$ui.stderr.read.match(data).should_not be nil
|
184
139
|
|
185
|
-
stdin.rewind
|
186
|
-
stdin.read.match(data).should be nil
|
140
|
+
$ui.stdin.rewind
|
141
|
+
$ui.stdin.read.match(data).should be nil
|
187
142
|
end
|
188
143
|
end
|
189
144
|
|