Skip to content

Commit

Permalink
adding hostsline coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
luthermonson committed Oct 28, 2023
1 parent 74bc203 commit c55e734
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
9 changes: 4 additions & 5 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ func TestHosts_HostsPerLine(t *testing.T) {
assert.Len(t, hosts.hosts.l, 10)

assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))

}

func BenchmarkHosts_Add(b *testing.B) {
Expand Down Expand Up @@ -720,16 +719,16 @@ func TestHosts_Clear(t *testing.T) {

func TestHosts_RemoveDuplicateHosts(t *testing.T) {
h := newHosts()
assert.Nil(t, h.loadString(`127.0.0.1 test1 test1 test2 test2`))
assert.Len(t, h.Lines, 1)
assert.Nil(t, h.loadString(`127.0.0.1 test1 test1 test2 test2`+eol+`# comment`))
assert.Len(t, h.Lines, 2)
assert.Len(t, h.ips.l, 1)
assert.Len(t, h.hosts.l, 2)

h.RemoveDuplicateHosts()
assert.Len(t, h.Lines, 1)
assert.Len(t, h.Lines, 2)
assert.Len(t, h.ips.l, 1)
assert.Len(t, h.hosts.l, 2)
assert.Equal(t, "127.0.0.1 test1 test2"+eol, h.String())
assert.Equal(t, `127.0.0.1 test1 test2`+eol+`# comment`+eol, h.String())

h = newHosts()
assert.Nil(t, h.loadString(`127.0.0.1 test1 test1 test2 test2`+eol+`127.0.0.2 test1 test1 test2 test2`+eol))
Expand Down
1 change: 1 addition & 0 deletions hostsline.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (l *HostsLine) RemoveDuplicateHosts() {
func (l *HostsLine) Combine(hostline HostsLine) {
l.combine(hostline)
}

func (l *HostsLine) combine(hostline HostsLine) {
l.Hosts = append(l.Hosts, hostline.Hosts...)
if l.Comment == "" {
Expand Down
26 changes: 26 additions & 0 deletions hostsline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hostsfile

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHostsline_String(t *testing.T) {
hl := HostsLine{IP: "127.0.0.1", Hosts: []string{"localhost"}}
assert.Equal(t, "127.0.0.1 localhost", hl.String())
}

func TestHosts_combine(t *testing.T) {
hl1 := HostsLine{IP: "127.0.0.1", Hosts: []string{"test1"}}
hl2 := HostsLine{IP: "127.0.0.1", Hosts: []string{"test2"}}
assert.Len(t, hl1.Hosts, 1)

hl1.combine(hl2)
assert.Len(t, hl1.Hosts, 2)
assert.Equal(t, "127.0.0.1 test1 test2", hl1.String())

// change to combine when deprecated
hl2.Combine(hl1) // should have dupes removed
assert.Equal(t, "127.0.0.1 test2 test1 test2", hl2.String())
}

0 comments on commit c55e734

Please sign in to comment.