From e2ee34ca3a123f28aa92f5db451ff796b930e39d Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Wed, 24 Jan 2024 21:47:54 -0700 Subject: [PATCH] remove test --- cmd/backup_test.go | 7 +++++ cmd/list_test.go | 8 ++++++ cmd/remove.go | 27 +++++++----------- cmd/remove_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 cmd/backup_test.go create mode 100644 cmd/remove_test.go diff --git a/cmd/backup_test.go b/cmd/backup_test.go new file mode 100644 index 0000000..a06c468 --- /dev/null +++ b/cmd/backup_test.go @@ -0,0 +1,7 @@ +package cmd + +import "testing" + +func TestBackup(t *testing.T) { + +} diff --git a/cmd/list_test.go b/cmd/list_test.go index 7bc3983..c1314b9 100644 --- a/cmd/list_test.go +++ b/cmd/list_test.go @@ -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")() diff --git a/cmd/remove.go b/cmd/remove.go index d63feee..d51ad40 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "net" "strings" @@ -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]) } @@ -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 @@ -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() } diff --git a/cmd/remove_test.go b/cmd/remove_test.go new file mode 100644 index 0000000..0e29afd --- /dev/null +++ b/cmd/remove_test.go @@ -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()) + }) +}