From b1645b0f356e8d48b055deefacaff3cbeb6dc332 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Mon, 2 Mar 2015 01:46:12 -0800 Subject: [PATCH] Fixed a sort bug and added some more tests --- commands.go | 2 +- hostlist.go | 7 ++++--- hostlist_test.go | 33 ++++++++++++++++++++++++++++++--- hostname.go | 2 +- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/commands.go b/commands.go index a152379..1234330 100644 --- a/commands.go +++ b/commands.go @@ -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. diff --git a/hostlist.go b/hostlist.go index 37c2a1f..3181b00 100644 --- a/hostlist.go +++ b/hostlist.go @@ -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 } diff --git a/hostlist_test.go b/hostlist_test.go index 305caa2..8e74997 100644 --- a/hostlist_test.go +++ b/hostlist_test.go @@ -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() } diff --git a/hostname.go b/hostname.go index 4d49fa0..e8cbf95 100644 --- a/hostname.go +++ b/hostname.go @@ -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())