Skip to content

Commit

Permalink
remove test
Browse files Browse the repository at this point in the history
  • Loading branch information
luthermonson committed Jan 25, 2024
1 parent 94c5636 commit e2ee34c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
7 changes: 7 additions & 0 deletions cmd/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

import "testing"

func TestBackup(t *testing.T) {

}
8 changes: 8 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func TestList(t *testing.T) {
}
})

// this is really a noop but future proofs us a bit
t.Run("default action", func(t *testing.T) {
defer read(t, "test-list", "127.0.0.1 localhost")()
args, out := setup("-f", "test-list")
assert.Empty(t, App.Run(args))
assert.Equal(t, "127.0.0.1 localhost"+"\n", out.String())
})

// this is really a noop but future proofs us a bit
t.Run("hostsfile that is write accessible", func(t *testing.T) {
defer write(t, "test-list", "127.0.0.1 localhost")()
Expand Down
27 changes: 10 additions & 17 deletions cmd/remove.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"net"
"strings"

Expand Down Expand Up @@ -32,15 +33,16 @@ func Remove() *cli.Command {

func remove(c *cli.Context) error {
args := c.Args()

if args.Len() == 0 {
return errors.New("no input")
}

hf, err := loadHostsfile(c, false)
if err != nil {
return err
}

if args.Len() == 0 {
return cli.Exit("no input", 1)
}

if args.Len() == 1 { //could be ip or hostname
return processSingleArg(hf, args.Slice()[0])
}
Expand All @@ -60,11 +62,11 @@ func remove(c *cli.Context) error {
if hf.HasIP(args.Slice()[0]) {
err = hf.Remove(args.Slice()[0], hostEntries...)
if err != nil {
return cli.Exit(err.Error(), 2)
return err
}
}
} else {
hostEntries = append(hostEntries, args.Slice()[0])
hostEntries = append([]string{args.Slice()[0]}, hostEntries...)
for _, value := range hostEntries {
if err := hf.RemoveByHostname(value); err != nil {
return err
Expand Down Expand Up @@ -95,21 +97,12 @@ func processSingleArg(hf *hostsfile.Hosts, arg string) error {
if net.ParseIP(arg) != nil {
logrus.Infof("removing ip %s\n", arg)
hf.RemoveByIP(arg)
if err := hf.Flush(); err != nil {
return err
}

return nil
return hf.Flush()
}

logrus.Infof("removing hostname %s\n", arg)

if err := hf.RemoveByHostname(arg); err != nil {
return err
}
if err := hf.Flush(); err != nil {
return err
}

return nil
return hf.Flush()
}
71 changes: 71 additions & 0 deletions cmd/remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRemove(t *testing.T) {
// app run will return an error if the hostsfile does not exist
t.Run("hostsfile that doesn't exist", func(t *testing.T) {
args, _ := setup("-f", "no-existy", "remove", "127.0.0.1")
err := App.Run(args)
assert.NotEmpty(t, err)
if runtime.GOOS == "windows" {
assert.Equal(t, "open no-existy: The system cannot find the file specified.", err.Error())
} else {
assert.Equal(t, "open no-existy: no such file or directory", err.Error())
}
})

t.Run("no args", func(t *testing.T) {
args, _ := setup("remove")
assert.Equal(t, App.Run(args).Error(), "no input")
})

t.Run("remove an ip", func(t *testing.T) {
defer write(t, "test-list", "127.0.0.1 localhost")()
args, out := setup("-f", "test-list", "remove", "127.0.0.1")
assert.Empty(t, App.Run(args))
assert.Equal(t, "removing ip 127.0.0.1\n", out.String())

args, out = setup("-f", "test-list", "list")
assert.Empty(t, App.Run(args))
assert.Equal(t, "", out.String())
})

t.Run("remove a host", func(t *testing.T) {
defer write(t, "test-list", "127.0.0.1 localhost")()
args, out := setup("-f", "test-list", "remove", "localhost")
assert.Empty(t, App.Run(args))
assert.Equal(t, "removing hostname localhost\n", out.String())

args, out = setup("-f", "test-list", "list")
assert.Empty(t, App.Run(args))
assert.Equal(t, "", out.String())
})

t.Run("remove two hosts", func(t *testing.T) {
defer write(t, "test-list", "127.0.0.1 localhost host1 host2")()
args, out := setup("-f", "test-list", "remove", "127.0.0.1", "localhost", "host1")
assert.Empty(t, App.Run(args))
assert.Equal(t, "entry removed: localhost host1\n", out.String())

args, out = setup("-f", "test-list", "list")
assert.Empty(t, App.Run(args))
assert.Equal(t, "127.0.0.1 host2\n", out.String())
})

t.Run("remove two hosts from multiple lines", func(t *testing.T) {
defer write(t, "test-list", "127.0.0.1 localhost host1\n127.0.0.2 localhost host2")()
args, out := setup("-f", "test-list", "remove", "host1", "host2")
assert.Empty(t, App.Run(args))
assert.Equal(t, "entry removed: host1 host2\n", out.String())

args, out = setup("-f", "test-list", "list")
assert.Empty(t, App.Run(args))
assert.Equal(t, "127.0.0.1 localhost\n127.0.0.2 localhost\n", out.String())
})
}

0 comments on commit e2ee34c

Please sign in to comment.