From c55e734c21f13ee7d7f99fc4678bef5c67a53359 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Sat, 28 Oct 2023 09:08:56 -0700 Subject: [PATCH] adding hostsline coverage --- hosts_test.go | 9 ++++----- hostsline.go | 1 + hostsline_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 hostsline_test.go diff --git a/hosts_test.go b/hosts_test.go index 3b142db..841efd2 100644 --- a/hosts_test.go +++ b/hosts_test.go @@ -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) { @@ -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)) diff --git a/hostsline.go b/hostsline.go index 92a6e2b..d0b9f3b 100644 --- a/hostsline.go +++ b/hostsline.go @@ -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 == "" { diff --git a/hostsline_test.go b/hostsline_test.go new file mode 100644 index 0000000..e1dba18 --- /dev/null +++ b/hostsline_test.go @@ -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()) +}