-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrun_escape_test.go
More file actions
50 lines (44 loc) · 1.38 KB
/
Copy pathrun_escape_test.go
File metadata and controls
50 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package console
import (
"context"
"reflect"
"testing"
"github.com/spf13/cobra"
)
// TestRunCommandLineEscapeMode verifies that Console.SetEscapeMode flows all the
// way through to the argument vector a command actually receives.
func TestRunCommandLineEscapeMode(t *testing.T) {
tests := []struct {
name string
mode EscapeMode
line string
want []string
}{
{"shell default eats backslashes", EscapeShell, `run C:\Windows\Temp`, []string{`C:WindowsTemp`}},
{"literal preserves backslashes", EscapeLiteral, `run C:\Windows\Temp`, []string{`C:\Windows\Temp`}},
{"literal preserves trailing backslash", EscapeLiteral, `run C:\Windows\Temp\`, []string{`C:\Windows\Temp\`}},
{"literal still groups quotes", EscapeLiteral, `run "a b" C:\x`, []string{"a b", `C:\x`}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c := New("test")
c.SetEscapeMode(tc.mode)
menu := c.ActiveMenu()
var got []string
root := &cobra.Command{Use: "root"}
root.AddCommand(&cobra.Command{
Use: "run",
Run: func(_ *cobra.Command, args []string) {
got = args
},
})
menu.Command = root
if err := menu.RunCommandLine(context.Background(), tc.line); err != nil {
t.Fatalf("RunCommandLine(%q): %v", tc.line, err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("RunCommandLine(%q) args = %q, want %q", tc.line, got, tc.want)
}
})
}
}