github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/shellquote/shellstring_unix_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package shellquote 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestSplit(t *testing.T) { 14 tests := []struct { 15 name string 16 line string 17 want []string 18 wantErr bool 19 }{ 20 { 21 name: "Empty", 22 line: "", 23 want: nil, 24 wantErr: false, 25 }, 26 { 27 name: "single quoted", 28 line: `'one quoted' 'two quoted'`, 29 want: []string{`one quoted`, `two quoted`}, 30 wantErr: false, 31 }, 32 { 33 name: "escape in single quoted", 34 line: `'\one'`, 35 want: []string{`\one`}, 36 wantErr: false, 37 }, 38 { 39 name: "escape in single quoted", 40 line: `'\'one'`, // unbalanced. There's no escape in single quote 41 want: nil, 42 wantErr: true, 43 }, 44 { 45 name: "single quoted concat", 46 line: `'one quoted''two quoted'`, 47 want: []string{`one quotedtwo quoted`}, 48 wantErr: false, 49 }, 50 { 51 name: "double quoted", 52 line: `"one quoted" "two quoted"`, 53 want: []string{`one quoted`, `two quoted`}, 54 wantErr: false, 55 }, 56 { 57 name: "double quoted concat", 58 line: `"one quoted""two quoted"`, 59 want: []string{`one quotedtwo quoted`}, 60 wantErr: false, 61 }, 62 { 63 name: "double quoted with escaped quote", 64 line: `"one \"quoted\"" "two quoted"`, 65 want: []string{`one "quoted"`, `two quoted`}, 66 wantErr: false, 67 }, 68 { 69 name: "double quoted with unbalanced escaped quote", 70 line: `"one \"quoted\" "two quoted"`, 71 want: nil, 72 wantErr: true, 73 }, 74 { 75 name: "double quoted with escaped dollar", 76 line: `"\$32.0"`, 77 want: []string{`$32.0`}, 78 wantErr: false, 79 }, 80 { 81 name: "double quoted with escaped escape", 82 line: `"the \\ character"`, 83 want: []string{`the \ character`}, 84 wantErr: false, 85 }, 86 { 87 name: "double quoted with escaped newline", 88 line: "\"the line \\\ncontinues here\"", 89 want: []string{`the line continues here`}, 90 wantErr: false, 91 }, 92 { 93 name: "double quoted with escaped newline", 94 line: "\"the line \\\ncontinues here\"", 95 want: []string{`the line continues here`}, 96 wantErr: false, 97 }, 98 { 99 name: "not quoted", 100 line: `not quoted`, 101 want: []string{`not`, `quoted`}, 102 wantErr: false, 103 }, 104 { 105 name: "backslash escape", 106 line: `one\ two three\ four`, 107 want: []string{`one two`, `three four`}, 108 wantErr: false, 109 }, 110 { 111 name: "double and singe quoted concat", 112 line: `"one quoted"'two quoted'`, 113 want: []string{`one quotedtwo quoted`}, 114 wantErr: false, 115 }, 116 } 117 for _, tt := range tests { 118 t.Run(tt.name, func(t *testing.T) { 119 got, err := Split(tt.line) 120 if tt.wantErr { 121 assert.Error(t, err) 122 } else { 123 require.NoError(t, err) 124 assert.Equal(t, tt.want, got) 125 got, err := Split(ShellArgsString(got)) 126 require.NoError(t, err) 127 assert.Equal(t, tt.want, got) 128 } 129 }) 130 } 131 }