github.com/xgoffin/jenkins-library@v1.154.0/cmd/shellExecute_test.go (about) 1 package cmd 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/SAP/jenkins-library/pkg/mock" 10 ) 11 12 type shellExecuteMockUtils struct { 13 t *testing.T 14 config *shellExecuteOptions 15 *mock.ExecMockRunner 16 *mock.FilesMock 17 } 18 19 type shellExecuteFileMock struct { 20 *mock.FilesMock 21 fileReadContent map[string]string 22 fileReadErr map[string]error 23 } 24 25 func (f *shellExecuteFileMock) FileRead(path string) ([]byte, error) { 26 if f.fileReadErr[path] != nil { 27 return []byte{}, f.fileReadErr[path] 28 } 29 return []byte(f.fileReadContent[path]), nil 30 } 31 32 func (f *shellExecuteFileMock) FileExists(path string) (bool, error) { 33 return strings.EqualFold(path, "path/to/script/script.sh"), nil 34 } 35 36 func newShellExecuteTestsUtils() shellExecuteMockUtils { 37 utils := shellExecuteMockUtils{ 38 ExecMockRunner: &mock.ExecMockRunner{}, 39 FilesMock: &mock.FilesMock{}, 40 } 41 return utils 42 } 43 44 func (v *shellExecuteMockUtils) GetConfig() *shellExecuteOptions { 45 return v.config 46 } 47 48 func TestRunShellExecute(t *testing.T) { 49 50 t.Run("negative case - script isn't present", func(t *testing.T) { 51 c := &shellExecuteOptions{ 52 Sources: []string{"path/to/script.sh"}, 53 } 54 u := newShellExecuteTestsUtils() 55 56 err := runShellExecute(c, nil, u) 57 assert.EqualError(t, err, "the script 'path/to/script.sh' could not be found") 58 }) 59 60 t.Run("success case - script is present", func(t *testing.T) { 61 o := &shellExecuteOptions{} 62 u := newShellExecuteTestsUtils() 63 64 err := runShellExecute(o, nil, u) 65 assert.NoError(t, err) 66 }) 67 68 t.Run("success case - script run successfully", func(t *testing.T) { 69 o := &shellExecuteOptions{} 70 u := newShellExecuteTestsUtils() 71 72 err := runShellExecute(o, nil, u) 73 assert.NoError(t, err) 74 }) 75 76 }