github.com/GGP1/kure@v0.8.4/commands/card/rm/rm_test.go (about) 1 package rm 2 3 import ( 4 "bytes" 5 "testing" 6 7 cmdutil "github.com/GGP1/kure/commands" 8 "github.com/GGP1/kure/db/card" 9 "github.com/GGP1/kure/pb" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestRm(t *testing.T) { 15 db := cmdutil.SetContext(t) 16 17 names := []string{"test", "directory/test"} 18 for _, name := range names { 19 err := card.Create(db, &pb.Card{Name: name}) 20 assert.NoErrorf(t, err, "Failed creating %q card", name) 21 } 22 23 cases := []struct { 24 desc string 25 name string 26 input string 27 }{ 28 { 29 desc: "Do not proceed", 30 name: "test", 31 input: "n", 32 }, 33 { 34 desc: "Remove", 35 name: "test", 36 input: "y", 37 }, 38 { 39 desc: "Remove directory", 40 name: "directory/", 41 input: "y", 42 }, 43 } 44 45 for _, tc := range cases { 46 t.Run(tc.desc, func(t *testing.T) { 47 buf := bytes.NewBufferString(tc.input) 48 49 cmd := NewCmd(db, buf) 50 cmd.SetArgs([]string{tc.name}) 51 52 err := cmd.Execute() 53 assert.NoError(t, err) 54 }) 55 } 56 } 57 58 func TestRmErrors(t *testing.T) { 59 db := cmdutil.SetContext(t) 60 61 cases := []struct { 62 desc string 63 name string 64 input string 65 }{ 66 { 67 desc: "Invalid name", 68 name: "", 69 }, 70 { 71 desc: "Non existent card", 72 name: "non-existent", 73 input: "y", 74 }, 75 } 76 77 for _, tc := range cases { 78 t.Run(tc.desc, func(t *testing.T) { 79 buf := bytes.NewBufferString(tc.input) 80 81 cmd := NewCmd(db, buf) 82 cmd.SetArgs([]string{tc.name}) 83 84 err := cmd.Execute() 85 assert.Error(t, err) 86 }) 87 } 88 }