ghost 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,13 +24,13 @@ class Host
24
24
  entries = []
25
25
  File.open(@@hosts_file).each do |line|
26
26
  next if line =~ /^#/
27
- if line =~ /^(\d+\.\d+\.\d+\.\d+) (.*)/
27
+ if line =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.*)$/
28
28
  ip = $1
29
- hosts = $2.split(' ')
30
- hosts.each {|host| entries << Host.new(host, ip) }
29
+ hosts = $2.split(/\s+/)
30
+ hosts.each { |host| entries << Host.new(host, ip) }
31
31
  end
32
32
  end
33
- entries.delete_if {|host| @@permanent_hosts.include? host }
33
+ entries.delete_if { |host| @@permanent_hosts.include? host }
34
34
  entries
35
35
  end
36
36
 
@@ -50,11 +50,11 @@ class Host
50
50
  end
51
51
 
52
52
  def find_by_host(hostname)
53
- list.find{|host| host.hostname == hostname }
53
+ list.find{ |host| host.hostname == hostname }
54
54
  end
55
55
 
56
56
  def find_by_ip(ip)
57
- list.find_all{|host| host.ip == ip }
57
+ list.find_all{ |host| host.ip == ip }
58
58
  end
59
59
 
60
60
  def empty!
@@ -63,16 +63,14 @@ class Host
63
63
 
64
64
  def delete(name)
65
65
  hosts = list
66
- hosts = hosts.delete_if {|host| host.name == name }
66
+ hosts = hosts.delete_if { |host| host.name == name }
67
67
  write_out!(hosts)
68
68
  end
69
69
 
70
70
  def delete_matching(pattern)
71
71
  pattern = Regexp.escape(pattern)
72
72
  hosts = list.select { |host| host.name.match(/#{pattern}/) }
73
- hosts.each do |host|
74
- delete(host.name)
75
- end
73
+ hosts.each { |host| delete(host.name) }
76
74
  hosts
77
75
  end
78
76
 
@@ -80,7 +78,6 @@ class Host
80
78
 
81
79
  def write_out!(hosts)
82
80
  hosts += @@permanent_hosts
83
- # Har har! inject!
84
81
  output = hosts.inject("") {|s, h| s + "#{h.ip} #{h.hostname}\n" }
85
82
  File.open(@@hosts_file, 'w') {|f| f.print output }
86
83
  end
@@ -0,0 +1,138 @@
1
+ require 'ghost/linux-host'
2
+
3
+ $hosts_file = File.expand_path(File.join(File.dirname(__FILE__), "etc_hosts"))
4
+
5
+ describe Host do
6
+ before(:all) do
7
+ class Host
8
+ @@hosts_file = $hosts_file
9
+ end
10
+ end
11
+ before { `touch #{$hosts_file.inspect}` }
12
+ after { `rm -f #{$hosts_file.inspect}` }
13
+
14
+ it "has an IP" do
15
+ hostname = 'ghost-test-hostname.local'
16
+
17
+ Host.add(hostname)
18
+ host = Host.list.first
19
+ host.ip.should eql('127.0.0.1')
20
+
21
+ Host.empty!
22
+
23
+ ip = '169.254.23.121'
24
+ host = Host.add(hostname, ip)
25
+ host.ip.should eql(ip)
26
+ end
27
+
28
+ it "has a hostname" do
29
+ hostname = 'ghost-test-hostname.local'
30
+
31
+ Host.add(hostname)
32
+ host = Host.list.first
33
+ host.hostname.should eql(hostname)
34
+
35
+ Host.empty!
36
+
37
+ ip = '169.254.23.121'
38
+ Host.add(hostname, ip)
39
+ host.hostname.should eql(hostname)
40
+ end
41
+
42
+ describe ".list" do
43
+ it "returns an array" do
44
+ Host.list.should be_instance_of(Array)
45
+ end
46
+
47
+ it "contains instances of Host" do
48
+ Host.add('ghost-test-hostname.local')
49
+ Host.list.first.should be_instance_of(Host)
50
+ end
51
+
52
+ it "gets all hosts on a single /etc/hosts line" do
53
+ example = "127.0.0.1\tproject_a.local\t\t\tproject_b.local project_c.local"
54
+ File.open($hosts_file, 'w') {|f| f << example}
55
+ hosts = Host.list
56
+ hosts.should have(3).items
57
+ hosts.map{|h|h.ip}.uniq.should == ['127.0.0.1']
58
+ hosts.map{|h|h.host}.sort.should == %w[project_a.local project_b.local project_c.local]
59
+ Host.add("project_d.local")
60
+ Host.list.should have(4).items
61
+ end
62
+ end
63
+
64
+ describe "#to_s" do
65
+ it "returns hostname" do
66
+ hostname = 'ghost-test-hostname.local'
67
+
68
+ Host.add(hostname)
69
+ host = Host.list.first
70
+ host.to_s.should eql(hostname)
71
+ end
72
+ end
73
+
74
+ describe "finder methods" do
75
+ before do
76
+ Host.add('abc.local')
77
+ Host.add('def.local')
78
+ Host.add('efg.local', '10.2.2.4')
79
+ end
80
+
81
+ it "returns valid Host when searching for host name" do
82
+ Host.find_by_host('abc.local').should be_instance_of(Host)
83
+ end
84
+ end
85
+
86
+ describe ".add" do
87
+ it "returns Host object when passed hostname" do
88
+ Host.add('ghost-test-hostname.local').should be_instance_of(Host)
89
+ end
90
+
91
+ it "returns Host object when passed hostname" do
92
+ Host.add('ghost-test-hostname.local', '10.0.0.2').should be_instance_of(Host)
93
+ end
94
+
95
+ it "raises error if hostname already exists and not add a duplicate" do
96
+ Host.empty!
97
+ Host.add('ghost-test-hostname.local')
98
+ lambda { Host.add('ghost-test-hostname.local') }.should raise_error
99
+ Host.list.should have(1).thing
100
+ end
101
+
102
+ it "overwrites existing hostname if forced" do
103
+ hostname = 'ghost-test-hostname.local'
104
+
105
+ Host.empty!
106
+ Host.add(hostname)
107
+
108
+ Host.list.first.hostname.should eql(hostname)
109
+ Host.list.first.ip.should eql('127.0.0.1')
110
+
111
+ Host.add(hostname, '10.0.0.1', true)
112
+ Host.list.first.hostname.should eql(hostname)
113
+ Host.list.first.ip.should eql('10.0.0.1')
114
+
115
+ Host.list.should have(1).thing
116
+ end
117
+ end
118
+
119
+ describe ".empty!" do
120
+ it "empties the hostnames" do
121
+ Host.add('ghost-test-hostname.local') # add a hostname to be sure
122
+ Host.empty!
123
+ Host.list.should have(0).things
124
+ end
125
+ end
126
+
127
+ describe ".delete_matching" do
128
+ it "deletes matching hostnames" do
129
+ keep = 'ghost-test-hostname-keep.local'
130
+ Host.add(keep)
131
+ Host.add('ghost-test-hostname-match1.local')
132
+ Host.add('ghost-test-hostname-match2.local')
133
+ Host.delete_matching('match')
134
+ Host.list.should have(1).thing
135
+ Host.list.first.hostname.should eql(keep)
136
+ end
137
+ end
138
+ end
@@ -1,4 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'ghost'
2
3
 
3
4
  # Warning: these tests will delete all hostnames in the system. Please back them up first
4
5
 
@@ -1,5 +1,4 @@
1
1
  $TESTING=true
2
2
  $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
3
  require 'rubygems'
4
- require 'spec'
5
- require 'ghost'
4
+ require 'spec'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bodaniel Jeanes
@@ -9,7 +9,7 @@ autorequire: ghost
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-14 00:00:00 +10:00
12
+ date: 2010-03-11 00:00:00 +10:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -32,6 +32,7 @@ files:
32
32
  - lib/ghost/linux-host.rb
33
33
  - lib/ghost/mac-host.rb
34
34
  - lib/ghost.rb
35
+ - spec/etc_hosts_spec.rb
35
36
  - spec/ghost_spec.rb
36
37
  - spec/spec.opts
37
38
  - spec/spec_helper.rb