github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/utils_test.go (about) 1 package command 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/pkg/errors" 10 "gotest.tools/v3/assert" 11 ) 12 13 func TestStringSliceReplaceAt(t *testing.T) { 14 out, ok := StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1) 15 assert.Assert(t, ok) 16 assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out) 17 18 out, ok = StringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1) 19 assert.Assert(t, !ok) 20 assert.DeepEqual(t, []string{"foo"}, out) 21 22 out, ok = StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0) 23 assert.Assert(t, !ok) 24 assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out) 25 26 out, ok = StringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0) 27 assert.Assert(t, ok) 28 assert.DeepEqual(t, []string{"baz", "bax"}, out) 29 30 out, ok = StringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1) 31 assert.Assert(t, ok) 32 assert.DeepEqual(t, []string{"abc", "baz"}, out) 33 34 out, ok = StringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1) 35 assert.Assert(t, !ok) 36 assert.DeepEqual(t, []string{"foo"}, out) 37 } 38 39 func TestValidateOutputPath(t *testing.T) { 40 basedir, err := ioutil.TempDir("", "TestValidateOutputPath") 41 assert.NilError(t, err) 42 defer os.RemoveAll(basedir) 43 dir := filepath.Join(basedir, "dir") 44 notexist := filepath.Join(basedir, "notexist") 45 err = os.MkdirAll(dir, 0755) 46 assert.NilError(t, err) 47 file := filepath.Join(dir, "file") 48 err = ioutil.WriteFile(file, []byte("hi"), 0644) 49 assert.NilError(t, err) 50 var testcases = []struct { 51 path string 52 err error 53 }{ 54 {basedir, nil}, 55 {file, nil}, 56 {dir, nil}, 57 {dir + string(os.PathSeparator), nil}, 58 {notexist, nil}, 59 {notexist + string(os.PathSeparator), nil}, 60 {filepath.Join(notexist, "file"), errors.New("does not exist")}, 61 } 62 63 for _, testcase := range testcases { 64 t.Run(testcase.path, func(t *testing.T) { 65 err := ValidateOutputPath(testcase.path) 66 if testcase.err == nil { 67 assert.NilError(t, err) 68 } else { 69 assert.ErrorContains(t, err, testcase.err.Error()) 70 } 71 }) 72 } 73 }