Fixed a sort bug and added some more tests

pull/13/head
Chris Bednarski 9 years ago
parent bc9228e41b
commit b1645b0f35

@ -152,7 +152,7 @@ func Ls(c *cli.Context) {
// }
}
const fix_help = `Programmatically rewrite your hostsfile.
const fixHelp = `Programmatically rewrite your hostsfile.
Domains pointing to the same IP will be consolidated, sorted, and extra
whitespace and comments will be removed.

@ -72,10 +72,10 @@ func (h Hostlist) Less(i, j int) bool {
// case folding. There is a way to do this correctly but it's complicated
// so I'm not going to do it right now.
for c := 0; c < max; c++ {
if c > ilen {
if c >= ilen {
return true
}
if c > jlen {
if c >= jlen {
return false
}
if h[i].Domain[c] < h[j].Domain[c] {
@ -86,7 +86,8 @@ func (h Hostlist) Less(i, j int) bool {
}
}
// Seems like everything was the same, so it can't be Less
// Seems like everything was the same, so it can't be Less. Also since we
// can't Add something twice we should never end up here. Just in case...
return false
}

@ -91,24 +91,51 @@ func TestRemoveDomain(t *testing.T) {
}
func TestSort(t *testing.T) {
// Getting 100% coverage on this is kinda tricky. It's pretty close and
// this is already too long.
hosts := hostess.NewHostlist()
hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true))
hosts.Add(hostess.NewHostname("google3.com", "::1", true))
hosts.Add(hostess.NewHostname(domain, ip, false))
hosts.Add(hostess.NewHostname("google2.com", "8.8.4.4", true))
hosts.Add(hostess.NewHostname("blah2", "10.20.1.1", true))
hosts.Add(hostess.NewHostname("blah3", "10.20.1.1", true))
hosts.Add(hostess.NewHostname("blah33", "10.20.1.1", true))
hosts.Add(hostess.NewHostname("blah", "10.20.1.1", true))
hosts.Sort()
if (*hosts)[0].Domain != "localhost" {
t.Error("Expected localhost to be first")
t.Error(hosts.Format())
}
if (*hosts)[1].Domain != "google2.com" {
t.Error("Expected google2 to be second")
t.Error(hosts.Format())
}
if (*hosts)[2].Domain != "google.com" {
t.Error("Expected google3 to be third")
t.Error(hosts.Format())
}
if (*hosts)[3].Domain != "blah" {
t.Error("Expected blah to be fourth")
t.Error(hosts.Format())
}
if (*hosts)[4].Domain != "blah2" {
t.Error("Expected blah2 to be fifth")
t.Error(hosts.Format())
}
if (*hosts)[5].Domain != "blah3" {
t.Error("Expected blah3 to be sixth")
t.Error(hosts.Format())
}
if (*hosts)[6].Domain != "blah33" {
t.Error("Expected blah33 to be seventh")
t.Error(hosts.Format())
}
if (*hosts)[3].Domain != "google3.com" {
t.Error("Expected google3 to be last")
// IPv6 Domains
if (*hosts)[7].Domain != "google3.com" {
t.Error("Expected google3 to be eigth")
t.Error(hosts.Format())
}
hosts.Format()
}

@ -83,7 +83,7 @@ func (h *Hostname) FormatEnabled() string {
return "(Off)"
}
// Format outputs the Hostname in a more human-readable format:
// FormatHuman outputs the Hostname in a more human-readable format:
// blah.example.com -> 127.0.0.1 (Off)
func (h *Hostname) FormatHuman() string {
return fmt.Sprintf("%s -> %s %s", h.Domain, h.IP, h.FormatEnabled())

Loading…
Cancel
Save