Skip to content

Commit

Permalink
Merge pull request #31 from luthermonson/fix-regression
Browse files Browse the repository at this point in the history
  • Loading branch information
luthermonson authored Jan 6, 2022
2 parents 8c1bb74 + 208c672 commit 6e99443
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ func (h *Hosts) Load() error {
}
defer file.Close()

// if you're reloading from disk confirm you refresh the hash maps and lines
if len(h.Lines) != 0 {
h.ips = lookup{l: make(map[string][]int)}
h.hosts = lookup{l: make(map[string][]int)}
h.Lines = []HostsLine{}
}

scanner := bufio.NewScanner(utfbom.SkipOnly(file))
for scanner.Scan() {
hl := NewHostsLine(scanner.Text())
Expand Down
18 changes: 18 additions & 0 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,21 @@ func benchmarkHosts_Flush(c int, b *testing.B) {
assert.Nil(b, hosts.Flush())
assert.Nil(b, os.Remove("hostsfile"))
}

func TestHosts_Flush(t *testing.T) {
f, err := os.Create("hostsfile")
defer func() {
assert.Nil(t, f.Close())
assert.Nil(t, os.Remove("hostsfile"))
}()

assert.Nil(t, err)
hosts, err := NewCustomHosts("./hostsfile")
assert.Nil(t, err)
assert.Nil(t, hosts.Add("127.0.0.2", "host1"))
assert.Equal(t, 1, len(hosts.Lines))
assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
assert.Nil(t, hosts.Flush())
assert.Equal(t, 1, len(hosts.Lines))
assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
}

0 comments on commit 6e99443

Please sign in to comment.