Skip to content

Commit

Permalink
Merge pull request #17 from nerab/SupportDefaultHost
Browse files Browse the repository at this point in the history
Implement `default` (read-only)
  • Loading branch information
geemus committed Mar 18, 2014
2 parents 1d40620 + 831e293 commit b02d378
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source :rubygems
source 'https://rubygems.org'

gemspec
9 changes: 9 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
guard 'bundler' do
watch('Gemfile')
watch(/^.+\.gemspec/)
end

guard 'minitest' do
watch(%r|^test/test_(.*)\.rb|){|m| "test/test_#{m[1]}.rb"}
watch(%r|^lib/*\.rb|){'test'}
end
4 changes: 4 additions & 0 deletions data/default_only.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this is my netrc with only a default
default
login ld # this is my default username
password pd
8 changes: 8 additions & 0 deletions data/sample_multi.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# this is my netrc with multiple machines
machine m
login lm # this is my m-username
password pm

machine n
login ln # this is my n-username
password pn
12 changes: 12 additions & 0 deletions data/sample_multi_with_default.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# this is my netrc with multiple machines and a default
machine m
login lm # this is my m-username
password pm

machine n
login ln # this is my n-username
password pn

default
login ld # this is my default username
password pd
8 changes: 8 additions & 0 deletions data/sample_with_default.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# this is my netrc with default
machine m
login l # this is my username
password p

default
login default_login # this is my default username
password default_password
26 changes: 22 additions & 4 deletions lib/netrc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,29 @@ def ts.readto
return l.join
end

pre = ts.readto{|t| t == "machine"}
pre = ts.readto{|t| t == "machine" || t == "default"}

while ts.length > 0
cur << ts.take + ts.readto{|t| ! skip?(t)}
cur << ts.take
if ts[0] == 'default'
cur << ts.take.to_sym
cur << ''
else
cur << ts.take + ts.readto{|t| ! skip?(t)}
cur << ts.take
end

if ts.include?('login')
cur << ts.readto{|t| t == "login"} + ts.take + ts.readto{|t| ! skip?(t)}
cur << ts.take
end

if ts.include?('password')
cur << ts.readto{|t| t == "password"} + ts.take + ts.readto{|t| ! skip?(t)}
cur << ts.take
end
cur << ts.readto{|t| t == "machine"}

cur << ts.readto{|t| t == "machine" || t == "default"}

item << cur
cur = []
end
Expand All @@ -125,13 +135,21 @@ def initialize(path, data)
@new_item_prefix = ''
@path = path
@pre, @data = data

if @data && @data.last && :default == @data.last[0]
@default = @data.pop
else
@default = nil
end
end

attr_accessor :new_item_prefix

def [](k)
if item = @data.detect {|datum| datum[1] == k}
Entry.new(item[3], item[5])
elsif @default
Entry.new(@default[3], @default[5])
end
end

Expand Down
27 changes: 26 additions & 1 deletion test/test_netrc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestNetrc < Test::Unit::TestCase

def setup
File.chmod(0600, "data/sample.netrc")
Dir.glob('data/*.netrc').each{|f| File.chmod(0600, f)}
File.chmod(0644, "data/permissive.netrc")
end

Expand Down Expand Up @@ -209,4 +209,29 @@ def test_entry_implicit_splat
assert_equal("pass", pass)
end

def test_with_default
netrc = Netrc.read('data/sample_with_default.netrc')
assert_equal(['l', 'p'], netrc['m'].to_a)
assert_equal(['default_login', 'default_password'], netrc['unknown'].to_a)
end

def test_multi_without_default
netrc = Netrc.read('data/sample_multi.netrc')
assert_equal(['lm', 'pm'], netrc['m'].to_a)
assert_equal(['ln', 'pn'], netrc['n'].to_a)
assert_equal([], netrc['other'].to_a)
end

def test_multi_with_default
netrc = Netrc.read('data/sample_multi_with_default.netrc')
assert_equal(['lm', 'pm'], netrc['m'].to_a)
assert_equal(['ln', 'pn'], netrc['n'].to_a)
assert_equal(['ld', 'pd'], netrc['other'].to_a)
end

def test_default_only
netrc = Netrc.read('data/default_only.netrc')
assert_equal(['ld', 'pd'], netrc['m'].to_a)
assert_equal(['ld', 'pd'], netrc['other'].to_a)
end
end

0 comments on commit b02d378

Please sign in to comment.