Skip to content

Commit 2d7f65e

Browse files
Merge pull request #12 from guillaumerose/master
Verify hosts to delete instead of suffixes when cleaning hosts file
2 parents b3a9dc0 + d614eb6 commit 2d7f65e

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

pkg/hosts/hosts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ func (h *Hosts) Remove(hosts []string) error {
9797
}
9898

9999
func (h *Hosts) Clean(rawSuffixes []string) error {
100-
if err := h.verifyHosts(rawSuffixes); err != nil {
101-
return err
102-
}
103-
104100
if err := h.checkIsWritable(); err != nil {
105101
return err
106102
}
@@ -125,6 +121,10 @@ func (h *Hosts) Clean(rawSuffixes []string) error {
125121
}
126122
}
127123

124+
if err := h.verifyHosts(toDelete); err != nil {
125+
return err
126+
}
127+
128128
for _, host := range toDelete {
129129
if err := h.File.RemoveByHostname(host); err != nil {
130130
return err

pkg/hosts/hosts_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestSuffixFilter(t *testing.T) {
8484
defer os.RemoveAll(dir)
8585

8686
hostsFile := filepath.Join(dir, "hosts")
87-
assert.NoError(t, ioutil.WriteFile(hostsFile, []byte(`127.0.0.1 localhost`), 0600))
87+
assert.NoError(t, ioutil.WriteFile(hostsFile, []byte(`127.0.0.1 localhost localhost.localdomain`), 0600))
8888

8989
file, err := hostsfile.NewCustomHosts(hostsFile)
9090
assert.NoError(t, err)
@@ -99,6 +99,8 @@ func TestSuffixFilter(t *testing.T) {
9999
assert.Error(t, host.Add("127.0.0.1", []string{"host.poison"}))
100100
assert.Error(t, host.Add("127.0.0.1", []string{"CAPITAL.crc.testing"}))
101101
assert.Error(t, host.Remove([]string{"localhost"}))
102+
assert.NoError(t, host.Clean([]string{".crc.testing"}))
103+
assert.Error(t, host.Clean([]string{".localdomain"}))
102104
}
103105

104106
func hosts(t *testing.T, hostsFile string) Hosts {

0 commit comments

Comments
 (0)