Skip to content

Commit

Permalink
Merge pull request #12 from guillaumerose/master
Browse files Browse the repository at this point in the history
Verify hosts to delete instead of suffixes when cleaning hosts file
  • Loading branch information
guillaumerose authored Mar 17, 2021
2 parents b3a9dc0 + d614eb6 commit 2d7f65e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ func (h *Hosts) Remove(hosts []string) error {
}

func (h *Hosts) Clean(rawSuffixes []string) error {
if err := h.verifyHosts(rawSuffixes); err != nil {
return err
}

if err := h.checkIsWritable(); err != nil {
return err
}
Expand All @@ -125,6 +121,10 @@ func (h *Hosts) Clean(rawSuffixes []string) error {
}
}

if err := h.verifyHosts(toDelete); err != nil {
return err
}

for _, host := range toDelete {
if err := h.File.RemoveByHostname(host); err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion pkg/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestSuffixFilter(t *testing.T) {
defer os.RemoveAll(dir)

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

file, err := hostsfile.NewCustomHosts(hostsFile)
assert.NoError(t, err)
Expand All @@ -99,6 +99,8 @@ func TestSuffixFilter(t *testing.T) {
assert.Error(t, host.Add("127.0.0.1", []string{"host.poison"}))
assert.Error(t, host.Add("127.0.0.1", []string{"CAPITAL.crc.testing"}))
assert.Error(t, host.Remove([]string{"localhost"}))
assert.NoError(t, host.Clean([]string{".crc.testing"}))
assert.Error(t, host.Clean([]string{".localdomain"}))
}

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

0 comments on commit 2d7f65e

Please sign in to comment.