compacted class default value class methods in ssh, and tightened up the tests
This commit is contained in:
parent
3fd2ef5e2b
commit
6ea3fe39b9
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue