Skip to content

Commit 6e99443

Browse files
authored
Merge pull request #31 from luthermonson/fix-regression
2 parents 8c1bb74 + 208c672 commit 6e99443

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

hosts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ func (h *Hosts) Load() error {
7272
}
7373
defer file.Close()
7474

75+
// if you're reloading from disk confirm you refresh the hash maps and lines
76+
if len(h.Lines) != 0 {
77+
h.ips = lookup{l: make(map[string][]int)}
78+
h.hosts = lookup{l: make(map[string][]int)}
79+
h.Lines = []HostsLine{}
80+
}
81+
7582
scanner := bufio.NewScanner(utfbom.SkipOnly(file))
7683
for scanner.Scan() {
7784
hl := NewHostsLine(scanner.Text())

hosts_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,21 @@ func benchmarkHosts_Flush(c int, b *testing.B) {
263263
assert.Nil(b, hosts.Flush())
264264
assert.Nil(b, os.Remove("hostsfile"))
265265
}
266+
267+
func TestHosts_Flush(t *testing.T) {
268+
f, err := os.Create("hostsfile")
269+
defer func() {
270+
assert.Nil(t, f.Close())
271+
assert.Nil(t, os.Remove("hostsfile"))
272+
}()
273+
274+
assert.Nil(t, err)
275+
hosts, err := NewCustomHosts("./hostsfile")
276+
assert.Nil(t, err)
277+
assert.Nil(t, hosts.Add("127.0.0.2", "host1"))
278+
assert.Equal(t, 1, len(hosts.Lines))
279+
assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
280+
assert.Nil(t, hosts.Flush())
281+
assert.Equal(t, 1, len(hosts.Lines))
282+
assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
283+
}

0 commit comments

Comments
 (0)