Skip to content

Commit

Permalink
set default options before checking for nils
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneMorgan committed May 15, 2016
1 parent 74d7da5 commit bfcc0fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 11 additions & 7 deletions lib/net/ssh.rb
Expand Up @@ -207,6 +207,8 @@ def self.start(host, user=nil, options={}, &block)
raise ArgumentError, "invalid option(s): #{invalid_options.join(', ')}"
end

assign_defaults(options)

if options.values.include? nil
nil_options = options.keys.select { |k| options[k].nil? }
raise ArgumentError, "Value(s) have been set to nil: #{nil_options.join(', ')}"
Expand All @@ -216,17 +218,10 @@ def self.start(host, user=nil, options={}, &block)
options = configuration_for(host, options.fetch(:config, true)).merge(options)
host = options.fetch(:host_name, host)

if !options.key?(:logger)
options[:logger] = Logger.new(STDERR)
options[:logger].level = Logger::FATAL
end

if options[:non_interactive]
options[:number_of_password_prompts] = 0
end

options[:password_prompt] ||= Prompt.default(options)

if options[:verbose]
options[:logger].level = case options[:verbose]
when Fixnum then options[:verbose]
Expand Down Expand Up @@ -278,5 +273,14 @@ def self.configuration_for(host, use_ssh_config=true)

Net::SSH::Config.for(host, files)
end

def self.assign_defaults(options)
if !options[:logger]
options[:logger] = Logger.new(STDERR)
options[:logger].level = Logger::FATAL
end

options[:password_prompt] ||= Prompt.default(options)
end
end
end
11 changes: 9 additions & 2 deletions test/start/test_options.rb
Expand Up @@ -55,17 +55,24 @@ def test_start_should_accept_remote_user_option

def test_constructor_should_reject_options_set_to_nil
assert_raises(ArgumentError) do
options = { :remote_user => nil}
options = { :remote_user => nil }
Net::SSH.start('localhost', 'testuser', options)
end
end

def test_constructor_should_reject_invalid_options
assert_raises(ArgumentError) do
options = { :some_invalid_option => "some setting"}
options = { :some_invalid_option => "some setting" }
Net::SSH.start('localhost', 'testuser', options)
end
end

def test_constructor_should_set_default_options
options = { :logger => nil, :password_prompt => nil }
Net::SSH.start('localhost', 'testuser', options)
assert !options[:logger].nil?
assert !options[:password_prompt].nil?
end
end
end

0 comments on commit bfcc0fd

Please sign in to comment.