compacted class default value class methods in ssh, and tightened up the tests

This commit is contained in:
John Bender 2010-01-26 20:49:22 -08:00
parent 3fd2ef5e2b
commit 6ea3fe39b9
3 changed files with 25 additions and 24 deletions

View File

@ -1,28 +1,21 @@
module Hobo
class SSH
SCRIPT = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'hobo-ssh-expect.sh')
def self.connect(opts={})
Kernel.exec "#{SCRIPT} #{uname(opts)} #{pass(opts)} #{host(opts)} #{port(opts)}".strip
end
class << self
def connect(opts={})
Kernel.exec "#{SCRIPT} #{opts[:uname] || uname_default} #{opts[:pass] || pass_default} #{opts[:host] || host_default}".strip
end
private
module ClassMethods
private
def port_default
Hobo.config[:ssh][:port]
end
def host_default
Hobo.config[:ssh][:host]
end
def pass_default
Hobo.config[:ssh][:pass]
end
def uname_default
Hobo.config[:ssh][:uname]
[:port, :host, :pass, :uname].each do |method|
define_method(method) do |opts|
opts[method] || Hobo.config[:ssh][method]
end
end
end
extend ClassMethods
end
end

View File

@ -9,14 +9,14 @@ class SshTest < Test::Unit::TestCase
end
test "should call exec with defaults when no options are supplied" do
# NOTE HOBO_MOCK_CONFIG only contains the :uname at this stage, adding further params will break this test
Kernel.expects(:exec).with("#{@script} #{HOBO_MOCK_CONFIG[:ssh][:uname]}")
ssh = HOBO_MOCK_CONFIG[:ssh]
Kernel.expects(:exec).with("#{@script} #{ssh[:uname]} #{ssh[:pass]} #{ssh[:host]} #{ssh[:port]}")
Hobo::SSH.connect
end
test "should call exec with supplied params" do
args = {:uname => 'bar', :pass => 'baz', :host => 'bak'}
Kernel.expects(:exec).with("#{@script} #{args[:uname]} #{args[:pass]} #{args[:host]}")
args = {:uname => 'bar', :pass => 'baz', :host => 'bak', :port => 'bag'}
Kernel.expects(:exec).with("#{@script} #{args[:uname]} #{args[:pass]} #{args[:host]} #{args[:port]}")
Hobo::SSH.connect(args)
end
end

View File

@ -22,4 +22,12 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'hobo')
require 'contest'
require 'mocha'
HOBO_MOCK_CONFIG = { :ssh => { :uname => 'foo'} }
HOBO_MOCK_CONFIG =
{ :ssh =>
{
:uname => 'foo',
:pass => 'bar',
:host => 'baz',
:port => 'bak'
}
}