github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/pkg/common/cat_test.go (about) 1 package common 2 3 import ( 4 "errors" 5 "os" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestCat(t *testing.T) { 12 tcs := []struct { 13 name string 14 filename string 15 expectedValue string 16 expectedError error 17 }{ 18 { 19 "empty value", 20 "", 21 "", 22 errors.New("open : no such file or directory"), 23 }, 24 { 25 "txt file", 26 "a.txt", 27 "The quick brown fox jumped over the lazy dog.\n", 28 nil, 29 }, 30 { 31 "txt file in subdir", 32 "subdir/b.txt", 33 "Sphinx of black quartz, judge my vow.\n", 34 nil, 35 }, 36 { 37 "file not found", 38 "c.txt", 39 "", 40 errors.New("open c.txt: no such file or directory"), 41 }, 42 } 43 for _, tc := range tcs { 44 t.Run(tc.name, func(t *testing.T) { 45 assert := assert.New(t) 46 wd, _ := os.Getwd() 47 os.Chdir("testdata/cat") 48 49 c := Cat{} 50 output, err := c.Run("", "", []string{tc.filename}) 51 if tc.expectedError == nil { 52 assert.NoError(err) 53 } else { 54 assert.Error(tc.expectedError, err) 55 } 56 assert.Equal(tc.expectedValue, output) 57 os.Chdir(wd) 58 }) 59 } 60 }