-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
201 lines (194 loc) · 6.68 KB
/
Copy pathhandler_test.go
File metadata and controls
201 lines (194 loc) · 6.68 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package shellshape
import (
"reflect"
"testing"
)
func TestEmitPositional(t *testing.T) {
tests := []struct {
name string
start []string
tok string
placeholder string
want []string
}{
{"plain token", nil, "foo", "<val>", []string{"<val>"}},
{"preserves prefix", []string{"pre"}, "foo", "<path>", []string{"pre", "<path>"}},
{"subshell preserved", nil, "$(date)", "<val>", []string{"$(date)"}},
{"subshell in middle", []string{"pre"}, "$(pwd)", "<path>", []string{"pre", "$(pwd)"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EmitPositional(tt.start, tt.tok, tt.placeholder)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("EmitPositional(%v, %q, %q) = %v, want %v", tt.start, tt.tok, tt.placeholder, got, tt.want)
}
})
}
}
func TestConsumeUntil(t *testing.T) {
terminators := map[string]bool{";": true, "+": true}
tests := []struct {
name string
args []string
i int
wantTerminator string
wantNext int
}{
{"semicolon terminator", []string{"grep", "-l", "foo", "{}", ";"}, 0, ";", 5},
{"plus terminator", []string{"rm", "{}", "+"}, 0, "+", 3},
{"mid-stream start", []string{"a", "b", "c", ";", "d"}, 2, ";", 4},
{"no terminator", []string{"grep", "-l", "foo"}, 0, "", 3},
{"empty slice", nil, 0, "", 0},
{"start past end", []string{"a"}, 5, "", 1},
{"terminator immediately", []string{";", "a"}, 0, ";", 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
term, next := ConsumeUntil(tt.args, tt.i, terminators)
if term != tt.wantTerminator || next != tt.wantNext {
t.Errorf("ConsumeUntil(%v, %d) = (%q, %d), want (%q, %d)",
tt.args, tt.i, term, next, tt.wantTerminator, tt.wantNext)
}
})
}
}
func TestRepairLeadingFlagSubcommand(t *testing.T) {
// Typical setup: a handler with --filter (val), -C (path), and some
// boolean flags. Mimics the pnpm/systemctl family.
valFlags := map[string]bool{"--filter": true, "-F": true}
pathFlags := map[string]bool{"-C": true, "--config": true}
categories := []FlagCategory{
{Flags: valFlags, Placeholder: "<val>"},
{Flags: pathFlags, Placeholder: "<path>"},
}
verbatimFlags := map[string]bool{"-t": true, "--type": true}
tests := []struct {
name string
subcommand string
args []string
startResult []string
categories []FlagCategory
verbatim map[string]bool
wantResult []string
wantRealSubcommand string
wantNextI int
}{
{
name: "not a flag: no-op",
subcommand: "install",
args: []string{"react"},
startResult: []string{"exe", "install"},
categories: categories,
wantResult: []string{"exe", "install"},
wantRealSubcommand: "install",
wantNextI: 0,
},
{
name: "leading val flag then real subcommand",
subcommand: "--filter",
args: []string{"infra", "exec", "tsgo", "--noEmit"},
startResult: []string{"exe", "--filter"},
categories: categories,
wantResult: []string{"exe", "--filter", "<val>", "exec"},
wantRealSubcommand: "exec",
wantNextI: 2,
},
{
name: "leading path flag then real subcommand",
subcommand: "-C",
args: []string{"/app", "add", "react"},
startResult: []string{"exe", "-C"},
categories: categories,
wantResult: []string{"exe", "-C", "<path>", "add"},
wantRealSubcommand: "add",
wantNextI: 2,
},
{
name: "two leading flags before subcommand",
subcommand: "--filter",
args: []string{"infra", "-C", "/app", "add"},
startResult: []string{"exe", "--filter"},
categories: categories,
wantResult: []string{"exe", "--filter", "<val>", "-C", "<path>", "add"},
wantRealSubcommand: "add",
wantNextI: 4,
},
{
name: "boolean flag leaked (no value)",
subcommand: "--dry-run",
args: []string{"enable"},
startResult: []string{"exe", "--dry-run"},
categories: nil,
wantResult: []string{"exe", "--dry-run", "enable"},
wantRealSubcommand: "enable",
wantNextI: 1,
},
{
name: "subshell as leading flag value preserved",
subcommand: "--filter",
args: []string{"$(pick-workspace)", "exec", "tsgo"},
startResult: []string{"exe", "--filter"},
categories: categories,
wantResult: []string{"exe", "--filter", "$(pick-workspace)", "exec"},
wantRealSubcommand: "exec",
wantNextI: 2,
},
{
name: "verbatim-value flag leaked",
subcommand: "-t",
args: []string{"service", "list-units"},
startResult: []string{"exe", "-t"},
categories: nil,
verbatim: verbatimFlags,
wantResult: []string{"exe", "-t", "service", "list-units"},
wantRealSubcommand: "list-units",
wantNextI: 2,
},
{
name: "fused flag leaked",
subcommand: "--filter=infra",
args: []string{"exec", "tsgo"},
startResult: []string{"exe", "--filter=infra"},
categories: categories,
wantResult: []string{"exe", "--filter=infra", "exec"},
wantRealSubcommand: "exec",
wantNextI: 1,
},
{
name: "exhausted without finding subcommand",
subcommand: "--filter",
args: []string{"infra"},
startResult: []string{"exe", "--filter"},
categories: categories,
wantResult: []string{"exe", "--filter", "<val>"},
wantRealSubcommand: "",
wantNextI: 1,
},
{
name: "intermediate subshell before subcommand",
subcommand: "--filter",
args: []string{"infra", "$(date)", "exec"},
startResult: []string{"exe", "--filter"},
categories: categories,
wantResult: []string{"exe", "--filter", "<val>", "$(date)", "exec"},
wantRealSubcommand: "exec",
wantNextI: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, realSub, nextI := RepairLeadingFlagSubcommand(
tt.subcommand, tt.args, 0, tt.startResult, tt.categories, tt.verbatim,
)
if !reflect.DeepEqual(got, tt.wantResult) {
t.Errorf("result = %v, want %v", got, tt.wantResult)
}
if realSub != tt.wantRealSubcommand {
t.Errorf("realSubcommand = %q, want %q", realSub, tt.wantRealSubcommand)
}
if nextI != tt.wantNextI {
t.Errorf("nextI = %d, want %d", nextI, tt.wantNextI)
}
})
}
}