From 6ea3fe39b9ae5c7fcc3148bb302d9347a120ea3b Mon Sep 17 00:00:00 2001 From: John Bender Date: Tue, 26 Jan 2010 20:49:22 -0800 Subject: [PATCH] compacted class default value class methods in ssh, and tightened up the tests --- lib/hobo/ssh.rb | 31 ++++++++++++------------------- test/hobo/ssh_test.rb | 8 ++++---- test/test_helper.rb | 10 +++++++++- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/hobo/ssh.rb b/lib/hobo/ssh.rb index 25618510c..db25bca68 100644 --- a/lib/hobo/ssh.rb +++ b/lib/hobo/ssh.rb @@ -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 diff --git a/test/hobo/ssh_test.rb b/test/hobo/ssh_test.rb index 2a7e06c0d..a7c8ea2a8 100644 --- a/test/hobo/ssh_test.rb +++ b/test/hobo/ssh_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index a273ba404..c00f7f5df 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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' + } +}