Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SSHKit::Host comparison methods #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/sshkit/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module SSHKit
UnparsableHostStringError = Class.new(SSHKit::StandardError)

class Host
DEFAULT_SSH_PORT = 22

attr_accessor :password, :hostname, :port, :user, :ssh_options

Expand Down Expand Up @@ -60,10 +61,12 @@ def username
end

def eql?(other_host)
other_host.hash == hash
other_host &&
user == other_host.user &&
hostname == other_host.hostname &&
port_with_default == other_host.send(:port_with_default)
end
alias :== :eql?
alias :equal? :eql?

def to_s
hostname
Expand All @@ -84,6 +87,12 @@ def properties
@properties ||= OpenStruct.new
end

private

def port_with_default
port || DEFAULT_SSH_PORT
end

end

# @private
Expand Down
18 changes: 17 additions & 1 deletion test/unit/test_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,23 @@ def test_assert_hosts_hash_equally
def test_assert_hosts_compare_equal
assert Host.new('example.com') == Host.new('example.com')
assert Host.new('example.com').eql? Host.new('example.com')
assert Host.new('example.com').equal? Host.new('example.com')

assert Host.new('example.com:22') == Host.new('example.com')
assert Host.new('example.com:22').eql? Host.new('example.com')

assert Host.new('example.com:22') != Host.new('example.com:23')
assert !Host.new('example.com:22').eql?(Host.new('example.com:23'))

assert Host.new('[email protected]') == Host.new('[email protected]')
assert Host.new('[email protected]').eql? Host.new('[email protected]')

assert Host.new('[email protected]') != Host.new('[email protected]')
assert !Host.new('[email protected]').eql?(Host.new('[email protected]'))

a = Host.new('example.com')
b = Host.new('example.com')
assert a.equal? a
assert !a.equal?(b)
end

def test_arbitrary_host_properties
Expand Down