Skip to content

Commit

Permalink
Use env vars instead of Etc module for local user
Browse files Browse the repository at this point in the history
This ensures that the code works on both Unix and Windows, while
Etc.getpwuid relied on /etc/passwd being present.
  • Loading branch information
felixbuenemann committed Nov 1, 2015
1 parent fec497a commit 4d8826b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/sshkit/host.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'ostruct'
require 'etc'

module SSHKit

Expand All @@ -26,7 +25,7 @@ def initialize(host_string_or_options_hash)
if host_string_or_options_hash == :local
@local = true
@hostname = "localhost"
@user = Etc.getpwuid.name
@user = ENV['USER'] || ENV['LOGNAME'] || ENV['USERNAME']
elsif !host_string_or_options_hash.is_a?(Hash)
suitable_parsers = [
SimpleHostParser,
Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_host.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'helper'
require 'etc'

module SSHKit

Expand Down Expand Up @@ -46,8 +45,9 @@ def test_host_local
h = Host.new :local
assert h.local?
assert_nil h.port
assert_equal Etc.getpwuid.name, h.username
assert_equal 'localhost', h.hostname
username_candidates = ENV['USER'] || ENV['LOGNAME'] || ENV['USERNAME']
assert_equal username_candidates, h.username
assert_equal 'localhost', h.hostname
end

def test_does_not_confuse_ipv6_hosts_with_port_specification
Expand Down

0 comments on commit 4d8826b

Please sign in to comment.