mirror of
https://github.com/fleetdm/fleet
synced 2026-05-18 22:49:19 +00:00
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
|
|
// Based on https://github.com/kballard/go-shellquote
|
||
|
|
|
||
|
|
package shellquote
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"reflect"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSimpleSplit(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
for _, elem := range simpleSplitTest {
|
||
|
|
output, err := Split(elem.input)
|
||
|
|
if err != nil {
|
||
|
|
t.Errorf("Input %q, got error %#v", elem.input, err)
|
||
|
|
} else if !reflect.DeepEqual(output, elem.output) {
|
||
|
|
t.Errorf("Input %q, got %q, expected %q", elem.input, output, elem.output)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestErrorSplit(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
for _, elem := range errorSplitTest {
|
||
|
|
_, err := Split(elem.input)
|
||
|
|
if !errors.Is(err, elem.error) {
|
||
|
|
t.Errorf("Input %q, got error %#v, expected error %#v", elem.input, err, elem.error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var simpleSplitTest = []struct {
|
||
|
|
input string
|
||
|
|
output []string
|
||
|
|
}{
|
||
|
|
{"hello", []string{"hello"}},
|
||
|
|
{"hello goodbye", []string{"hello", "goodbye"}},
|
||
|
|
{"hello goodbye", []string{"hello", "goodbye"}},
|
||
|
|
{"glob* test?", []string{"glob*", "test?"}},
|
||
|
|
{"don\\'t you know the dewey decimal system\\?", []string{"don't", "you", "know", "the", "dewey", "decimal", "system?"}},
|
||
|
|
{"'don'\\''t you know the dewey decimal system?'", []string{"don't you know the dewey decimal system?"}},
|
||
|
|
{"one '' two", []string{"one", "", "two"}},
|
||
|
|
{"text with\\\na backslash-escaped newline", []string{"text", "witha", "backslash-escaped", "newline"}},
|
||
|
|
{"text \"with\na\" quoted newline", []string{"text", "with\na", "quoted", "newline"}},
|
||
|
|
{"\"quoted\\d\\\\\\\" text with\\\na backslash-escaped newline\"", []string{"quoted\\d\\\" text witha backslash-escaped newline"}},
|
||
|
|
{"text with an escaped \\\n newline in the middle", []string{"text", "with", "an", "escaped", "newline", "in", "the", "middle"}},
|
||
|
|
{"foo\"bar\"baz", []string{"foobarbaz"}},
|
||
|
|
{"--foo 6mI74pVBAidu1bALjY0F+wN4mPQyu8DUap/9M/kHp8I=", []string{"--foo", "6mI74pVBAidu1bALjY0F+wN4mPQyu8DUap/9M/kHp8I="}},
|
||
|
|
}
|
||
|
|
|
||
|
|
var errorSplitTest = []struct {
|
||
|
|
input string
|
||
|
|
error error
|
||
|
|
}{
|
||
|
|
{"don't worry", UnterminatedSingleQuoteError},
|
||
|
|
{"'test'\\''ing", UnterminatedSingleQuoteError},
|
||
|
|
{"\"foo'bar", UnterminatedDoubleQuoteError},
|
||
|
|
{"foo\\", UnterminatedEscapeError},
|
||
|
|
{" \\", UnterminatedEscapeError},
|
||
|
|
}
|