Browse Source

fix existing tests

tags/v3.0.0
Chris Wiegman 5 months ago
parent
commit
f1f4cea8b6
Signed by: chriswiegman <chris@chriswiegman.com> GPG Key ID: 2B23D7A74FDE2DF4
4 changed files with 23 additions and 12 deletions
  1. +17
    -5
      goodhosts_test.go
  2. +2
    -1
      hostsFile.go
  3. +2
    -4
      utils.go
  4. +2
    -2
      utils_test.go

+ 17
- 5
goodhosts_test.go View File

@@ -11,7 +11,7 @@ func TestHostsLineIsComment(t *testing.T) {
line := NewHostsLine(comment)
result := line.IsComment()
if !result {
t.Error(fmt.Sprintf("'%s' should be a comment"), comment)
t.Error(fmt.Sprintf("'%s' should be a comment", comment))
}
}

@@ -51,12 +51,20 @@ func TestHostsHasDoesntFindMissingEntry(t *testing.T) {
func TestHostsAddWhenIpHasOtherHosts(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada yadda")}
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 nada"),
NewHostsLine("10.0.0.7 yadda"),
}

hosts.Add("10.0.0.7", "brada", "yadda")

expectedLines := []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada yadda brada")}
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 nada"),
NewHostsLine("10.0.0.7 yadda"),
NewHostsLine("10.0.0.7 brada"),
NewHostsLine("10.0.0.7 yadda"),
}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
t.Error("Add entry failed to append entry.")
@@ -66,12 +74,16 @@ func TestHostsAddWhenIpHasOtherHosts(t *testing.T) {
func TestHostsAddWhenIpDoesntExist(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda")}
NewHostsLine("127.0.0.1 yadda"),
}

hosts.Add("10.0.0.7", "brada", "yadda")

expectedLines := []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 brada yadda")}
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 brada"),
NewHostsLine("10.0.0.7 yadda"),
}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
t.Error("Add entry failed to append entry.")


+ 2
- 1
hostsFile.go View File

@@ -83,7 +83,8 @@ func (h *Hosts) Add(ip string, hosts ...string) error {
}

for _, host := range hosts {
endLine := NewHostsLine(buildRawLine(ip, []string{host}))

endLine := NewHostsLine(buildRawLine(ip, host))
h.Lines = append(h.Lines, endLine)
}



+ 2
- 4
utils.go View File

@@ -12,11 +12,9 @@ func itemInSlice(item string, list []string) bool {
return false
}

func buildRawLine(ip string, hosts []string) string {
func buildRawLine(ip string, host string) string {
output := ip
for _, host := range hosts {
output = fmt.Sprintf("%s %s", output, host)
}
output = fmt.Sprintf("%s %s", output, host)

return output
}

+ 2
- 2
utils_test.go View File

@@ -10,12 +10,12 @@ func TestItemInSlice(t *testing.T) {
list := []string{"hello", "brah"}
result := itemInSlice("goodbye", list)
if result {
t.Error(fmt.Sprintf("'%' should not have been found in slice.", item))
t.Error(fmt.Sprintf("'%s' should not have been found in slice.", item))
}

item = "hello"
result = itemInSlice(item, list)
if !result {
t.Error(fmt.Sprintf("'%' should have been found in slice.", item))
t.Error(fmt.Sprintf("'%s' should have been found in slice.", item))
}
}

Loading…
Cancel
Save