restic/internal/backend/shell_split_test.go

106 lines
1.7 KiB
Go
Raw Normal View History

package backend
2017-04-03 06:57:33 +00:00
import (
"reflect"
"testing"
)
func TestShellSplitter(t *testing.T) {
var tests = []struct {
data string
2017-04-03 19:05:42 +00:00
args []string
2017-04-03 06:57:33 +00:00
}{
{
`foo`,
2018-03-13 19:50:37 +00:00
[]string{"foo"},
2017-04-03 06:57:33 +00:00
},
{
`'foo'`,
2018-03-13 19:50:37 +00:00
[]string{"foo"},
2017-04-03 06:57:33 +00:00
},
{
`foo bar baz`,
2018-03-13 19:50:37 +00:00
[]string{"foo", "bar", "baz"},
2017-04-03 06:57:33 +00:00
},
{
`foo 'bar' baz`,
2018-03-13 19:50:37 +00:00
[]string{"foo", "bar", "baz"},
2017-04-03 06:57:33 +00:00
},
{
2017-04-03 19:05:42 +00:00
`'bar box' baz`,
2018-03-13 19:50:37 +00:00
[]string{"bar box", "baz"},
2017-04-03 06:57:33 +00:00
},
{
`"bar 'box'" baz`,
2018-03-13 19:50:37 +00:00
[]string{"bar 'box'", "baz"},
2017-04-03 06:57:33 +00:00
},
{
`'bar "box"' baz`,
2018-03-13 19:50:37 +00:00
[]string{`bar "box"`, "baz"},
2017-04-03 06:57:33 +00:00
},
{
`\"bar box baz`,
2018-03-13 19:50:37 +00:00
[]string{`"bar`, "box", "baz"},
2017-04-03 06:57:33 +00:00
},
{
`"bar/foo/x" "box baz"`,
2018-03-13 19:50:37 +00:00
[]string{"bar/foo/x", "box baz"},
2017-04-03 06:57:33 +00:00
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
2018-03-13 19:50:37 +00:00
args, err := SplitShellStrings(test.data)
2017-04-03 06:57:33 +00:00
if err != nil {
t.Fatal(err)
}
2017-04-03 19:05:42 +00:00
if !reflect.DeepEqual(args, test.args) {
t.Fatalf("wrong args returned, want:\n %#v\ngot:\n %#v",
test.args, args)
2017-04-03 06:57:33 +00:00
}
})
}
}
func TestShellSplitterInvalid(t *testing.T) {
var tests = []struct {
data string
err string
}{
{
"foo'",
"single-quoted string not terminated",
},
{
`foo"`,
"double-quoted string not terminated",
},
{
"foo 'bar",
"single-quoted string not terminated",
},
{
`foo "bar`,
"double-quoted string not terminated",
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
2018-03-13 19:50:37 +00:00
args, err := SplitShellStrings(test.data)
2017-04-03 06:57:33 +00:00
if err == nil {
t.Fatalf("expected error not found: %v", test.err)
}
if err.Error() != test.err {
t.Fatalf("expected error not found, want:\n %q\ngot:\n %q", test.err, err.Error())
}
2017-04-03 19:05:42 +00:00
if len(args) > 0 {
t.Fatalf("splitter returned fields from invalid data: %v", args)
2017-04-03 06:57:33 +00:00
}
})
}
}