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 d74b71b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
14 changes: 10 additions & 4 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,13 @@ 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 Test_rfay(t *testing.T) {
hosts := newMacOSXDefault()
assert.Nil(t, hosts.Add("127.0.0.1", "xxx.ddev.site"))
hosts.HostsPerLine(8)
fmt.Println(hosts.String())
}

func BenchmarkHosts_Add(b *testing.B) {
Expand Down Expand Up @@ -720,16 +726,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
3 changes: 2 additions & 1 deletion hostsline.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ 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 == "" {
l.Comment = hostline.Comment
} else {
l.Comment = fmt.Sprintf("%s %s", l.Comment, hostline.Comment)
}
l.RegenRaw()
l.RemoveDuplicateHosts()
}

func (l *HostsLine) SortHosts() {
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", hl2.String())
}

0 comments on commit d74b71b

Please # to comment.