github.com/GGP1/kure@v0.8.4/commands/file/ls/ls_test.go (about) 1 package ls 2 3 import ( 4 "testing" 5 "time" 6 7 cmdutil "github.com/GGP1/kure/commands" 8 "github.com/GGP1/kure/db/file" 9 "github.com/GGP1/kure/pb" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestLs(t *testing.T) { 15 db := cmdutil.SetContext(t) 16 17 err := file.Create(db, &pb.File{Name: "test.txt"}) 18 assert.NoError(t, err, "Failed creating file") 19 20 cases := []struct { 21 desc string 22 name string 23 filter string 24 }{ 25 { 26 desc: "List one", 27 name: "test.txt", 28 }, 29 { 30 desc: "Filter by name", 31 name: "test*", 32 filter: "true", 33 }, 34 { 35 desc: "List all", 36 name: "", 37 }, 38 } 39 40 cmd := NewCmd(db) 41 42 for _, tc := range cases { 43 t.Run(tc.desc, func(t *testing.T) { 44 f := cmd.Flags() 45 cmd.SetArgs([]string{tc.name}) 46 f.Set("filter", tc.filter) 47 48 err := cmd.Execute() 49 assert.NoError(t, err) 50 }) 51 } 52 } 53 54 func TestLsErrors(t *testing.T) { 55 db := cmdutil.SetContext(t) 56 57 err := file.Create(db, &pb.File{Name: "test.txt"}) 58 assert.NoError(t, err, "Failed creating file") 59 60 cases := []struct { 61 desc string 62 name string 63 filter string 64 }{ 65 { 66 desc: "File does not exist", 67 name: "non-existent", 68 }, 69 { 70 desc: "No files found", 71 name: "non-existent", 72 filter: "true", 73 }, 74 { 75 desc: "Filter syntax error", 76 name: "[error", 77 filter: "true", 78 }, 79 } 80 81 cmd := NewCmd(db) 82 83 for _, tc := range cases { 84 t.Run(tc.desc, func(t *testing.T) { 85 f := cmd.Flags() 86 cmd.SetArgs([]string{tc.name}) 87 f.Set("filter", tc.filter) 88 89 err := cmd.Execute() 90 assert.Error(t, err) 91 }) 92 } 93 } 94 95 func TestPrintFile(t *testing.T) { 96 cases := []struct { 97 desc string 98 size int64 99 }{ 100 { 101 desc: "Bytes", 102 size: 100, 103 }, 104 { 105 desc: "Kilo bytes", 106 size: KB, 107 }, 108 { 109 desc: "Mega bytes", 110 size: MB, 111 }, 112 { 113 desc: "Giga bytes", 114 size: GB, 115 }, 116 { 117 desc: "Tera bytes", 118 size: TB, 119 }, 120 } 121 122 for _, tc := range cases { 123 t.Run(tc.desc, func(t *testing.T) { 124 printFile(&pb.FileCheap{Size: tc.size}) 125 }) 126 } 127 } 128 129 func TestPrintFileUpdatedAt(t *testing.T) { 130 cases := []struct { 131 desc string 132 time int64 133 }{ 134 { 135 desc: "Without updated at", 136 time: time.Time{}.Unix(), 137 }, 138 { 139 desc: "With updated at", 140 time: 100, 141 }, 142 } 143 144 for _, tc := range cases { 145 t.Run(tc.desc, func(t *testing.T) { 146 printFile(&pb.FileCheap{UpdatedAt: tc.time}) 147 }) 148 } 149 } 150 151 func TestPostRun(t *testing.T) { 152 NewCmd(nil).PostRun(nil, nil) 153 }