From e30fb0b80b424224883aecf291db8dda8bd7224a Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Wed, 4 Feb 2015 09:40:06 -0800 Subject: [PATCH] Recursively remove extra whitespace --- hostess.go | 5 +++-- hostess_test.go | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/hostess.go b/hostess.go index 11eacaf..2d757d5 100644 --- a/hostess.go +++ b/hostess.go @@ -71,7 +71,9 @@ func parseLine(line string) []Hostname { // Replace tabs and multispaces with single spaces throughout line = strings.Replace(line, "\t", " ", -1) - line = strings.Replace(line, " ", " ", -1) + for strings.Contains(line, " ") { + line = strings.Replace(line, " ", " ", -1) + } // Break line into words words := strings.Split(line, " ") @@ -82,7 +84,6 @@ func parseLine(line string) []Hostname { if LooksLikeIpv4(ip) || LooksLikeIpv6(ip) { for _, v := range domains { - v = v hostnames = append(hostnames, Hostname{v, ip, enabled}) } } diff --git a/hostess_test.go b/hostess_test.go index e3fbd65..da69b32 100644 --- a/hostess_test.go +++ b/hostess_test.go @@ -187,13 +187,15 @@ func TestParseLine(t *testing.T) { } hosts = parseLine("#66.33.99.11 test.domain.com") - if !ContainsHostname(hosts, Hostname{"test.domain.com", "66.33.99.11", false}) { + if !ContainsHostname(hosts, Hostname{"test.domain.com", "66.33.99.11", false}) || + len(hosts) != 1 { t.Error("Expected to find test.domain.com (disabled)") } hosts = parseLine("# 66.33.99.11 test.domain.com domain.com") if !ContainsHostname(hosts, Hostname{"test.domain.com", "66.33.99.11", false}) || - !ContainsHostname(hosts, Hostname{"domain.com", "66.33.99.11", false}) { + !ContainsHostname(hosts, Hostname{"domain.com", "66.33.99.11", false}) || + len(hosts) != 2 { t.Error("Expected to find domain.com and test.domain.com (disabled)") t.Errorf("Found %s", hosts) } @@ -202,18 +204,21 @@ func TestParseLine(t *testing.T) { hosts = parseLine("255.255.255.255 broadcasthost test.domain.com domain.com") if !ContainsHostname(hosts, Hostname{"broadcasthost", "255.255.255.255", true}) || !ContainsHostname(hosts, Hostname{"test.domain.com", "255.255.255.255", true}) || - !ContainsHostname(hosts, Hostname{"domain.com", "255.255.255.255", true}) { + !ContainsHostname(hosts, Hostname{"domain.com", "255.255.255.255", true}) || + len(hosts) != 3 { t.Error("Expected to find broadcasthost, domain.com, and test.domain.com (enabled)") } // Ipv6 stuff hosts = parseLine("::1 localhost") - if !ContainsHostname(hosts, Hostname{"localhost", "::1", true}) { + if !ContainsHostname(hosts, Hostname{"localhost", "::1", true}) || + len(hosts) != 1 { t.Error("Expected to find localhost ipv6 (enabled)") } hosts = parseLine("ff02::1 ip6-allnodes") - if !ContainsHostname(hosts, Hostname{"ip6-allnodes", "ff02::1", true}) { + if !ContainsHostname(hosts, Hostname{"ip6-allnodes", "ff02::1", true}) || + len(hosts) != 1 { t.Error("Expected to find ip6-allnodes ipv6 (enabled)") } }