2019-04-17 18:09:29 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-10-30 15:07:18 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-04-17 18:09:29 -04:00
|
|
|
"testing"
|
|
|
|
|
2019-10-30 15:07:18 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2019-04-17 18:09:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStringSliceReplaceAt(t *testing.T) {
|
|
|
|
out, ok := StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
|
|
|
assert.Assert(t, ok)
|
|
|
|
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
|
|
|
|
|
|
|
|
out, ok = StringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
|
|
|
assert.Assert(t, !ok)
|
|
|
|
assert.DeepEqual(t, []string{"foo"}, out)
|
|
|
|
|
|
|
|
out, ok = StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
|
|
|
assert.Assert(t, !ok)
|
|
|
|
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
|
|
|
|
|
|
|
|
out, ok = StringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
|
|
|
assert.Assert(t, ok)
|
|
|
|
assert.DeepEqual(t, []string{"baz", "bax"}, out)
|
|
|
|
|
|
|
|
out, ok = StringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
|
|
|
|
assert.Assert(t, ok)
|
|
|
|
assert.DeepEqual(t, []string{"abc", "baz"}, out)
|
|
|
|
|
|
|
|
out, ok = StringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
|
|
|
|
assert.Assert(t, !ok)
|
|
|
|
assert.DeepEqual(t, []string{"foo"}, out)
|
|
|
|
}
|
2019-10-30 15:07:18 -04:00
|
|
|
|
|
|
|
func TestValidateOutputPath(t *testing.T) {
|
2022-02-25 08:35:28 -05:00
|
|
|
basedir := t.TempDir()
|
2019-10-30 15:07:18 -04:00
|
|
|
dir := filepath.Join(basedir, "dir")
|
|
|
|
notexist := filepath.Join(basedir, "notexist")
|
2022-02-25 08:35:28 -05:00
|
|
|
err := os.MkdirAll(dir, 0755)
|
2019-10-30 15:07:18 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
file := filepath.Join(dir, "file")
|
2022-02-25 08:35:28 -05:00
|
|
|
err = os.WriteFile(file, []byte("hi"), 0644)
|
2019-10-30 15:07:18 -04:00
|
|
|
assert.NilError(t, err)
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2019-10-30 15:07:18 -04:00
|
|
|
path string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{basedir, nil},
|
|
|
|
{file, nil},
|
|
|
|
{dir, nil},
|
|
|
|
{dir + string(os.PathSeparator), nil},
|
|
|
|
{notexist, nil},
|
|
|
|
{notexist + string(os.PathSeparator), nil},
|
|
|
|
{filepath.Join(notexist, "file"), errors.New("does not exist")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
t.Run(testcase.path, func(t *testing.T) {
|
|
|
|
err := ValidateOutputPath(testcase.path)
|
|
|
|
if testcase.err == nil {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.ErrorContains(t, err, testcase.err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|