-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathinit.bats
More file actions
422 lines (346 loc) · 13.4 KB
/
init.bats
File metadata and controls
422 lines (346 loc) · 13.4 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env bats
# Tests for the init command (lib/commands/init.sh)
load test_helper
setup() {
source "$PROJECT_ROOT/lib/commands/init.sh"
}
# ── Default function name ────────────────────────────────────────────────────
@test "bash output defines gtr() function by default" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"gtr()"* ]]
}
@test "zsh output defines gtr() function by default" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"gtr()"* ]]
}
@test "fish output defines 'function gtr' by default" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"function gtr"* ]]
}
# ── --as flag ────────────────────────────────────────────────────────────────
@test "bash --as gwtr defines gwtr() function" {
run cmd_init bash --as gwtr
[ "$status" -eq 0 ]
[[ "$output" == *"gwtr()"* ]]
[[ "$output" != *"gtr()"* ]]
}
@test "zsh --as gwtr defines gwtr() function" {
run cmd_init zsh --as gwtr
[ "$status" -eq 0 ]
[[ "$output" == *"gwtr()"* ]]
[[ "$output" != *"gtr()"* ]]
}
@test "fish --as gwtr defines 'function gwtr'" {
run cmd_init fish --as gwtr
[ "$status" -eq 0 ]
[[ "$output" == *"function gwtr"* ]]
[[ "$output" != *"function gtr"* ]]
}
@test "--as replaces function name in completion registration (bash)" {
run cmd_init bash --as myfn
[ "$status" -eq 0 ]
[[ "$output" == *"complete -F _myfn_completion myfn"* ]]
}
@test "--as replaces function name in compdef (zsh)" {
run cmd_init zsh --as myfn
[ "$status" -eq 0 ]
[[ "$output" == *"compdef _myfn_completion myfn"* ]]
}
@test "--as replaces function name in fish completions" {
run cmd_init fish --as myfn
[ "$status" -eq 0 ]
[[ "$output" == *"complete -f -c myfn"* ]]
}
@test "--as replaces error message prefix" {
run cmd_init bash --as gwtr
[ "$status" -eq 0 ]
[[ "$output" == *"gwtr: postCd hook failed"* ]]
}
@test "--as can appear before shell argument" {
run cmd_init --as gwtr bash
[ "$status" -eq 0 ]
[[ "$output" == *"gwtr()"* ]]
}
# ── --as validation ──────────────────────────────────────────────────────────
@test "--as rejects name starting with digit" {
run cmd_init bash --as 123bad
[ "$status" -eq 1 ]
}
@test "--as rejects name with hyphens" {
run cmd_init bash --as foo-bar
[ "$status" -eq 1 ]
}
@test "--as rejects name with spaces" {
run cmd_init bash --as "foo bar"
[ "$status" -eq 1 ]
}
@test "--as accepts underscore-prefixed name" {
run cmd_init bash --as _my_func
[ "$status" -eq 0 ]
[[ "$output" == *"_my_func()"* ]]
}
@test "--as without value fails" {
run cmd_init bash --as
[ "$status" -eq 1 ]
}
# ── cd completions ───────────────────────────────────────────────────────────
@test "bash output includes cd in subcommand completions" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'"cd new go run'* ]]
}
@test "bash output uses git gtr list --porcelain for cd completion" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"git gtr list --porcelain"* ]]
}
@test "zsh output includes cd completion" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"cd:Change directory to worktree"* ]]
}
@test "zsh output uses git gtr list --porcelain for cd completion" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"git gtr list --porcelain"* ]]
}
@test "fish output includes cd subcommand completion" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"-a cd -d"* ]]
}
@test "fish output uses git gtr list --porcelain for cd completion" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"git gtr list --porcelain"* ]]
}
# ── Error cases ──────────────────────────────────────────────────────────────
@test "unknown shell fails" {
run cmd_init powershell
[ "$status" -eq 1 ]
}
@test "unknown flag fails" {
run cmd_init bash --unknown
[ "$status" -eq 1 ]
}
# ── fzf interactive picker ───────────────────────────────────────────────────
# ── fzf: general setup ──────────────────────────────────────────────────────
@test "bash output includes fzf detection for cd with no args" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"command -v fzf"* ]]
[[ "$output" == *"--prompt='Worktree> '"* ]]
[[ "$output" == *"--with-nth=2"* ]]
}
@test "zsh output includes fzf detection for cd with no args" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"command -v fzf"* ]]
[[ "$output" == *"--prompt='Worktree> '"* ]]
[[ "$output" == *"--with-nth=2"* ]]
}
@test "fish output includes fzf detection for cd with no args" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"type -q fzf"* ]]
[[ "$output" == *"--prompt='Worktree> '"* ]]
[[ "$output" == *"--with-nth=2"* ]]
}
# ── fzf: header shows all keybindings ───────────────────────────────────────
@test "bash fzf header lists all keybindings" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"enter:cd"* ]]
[[ "$output" == *"ctrl-e:editor"* ]]
[[ "$output" == *"ctrl-a:ai"* ]]
[[ "$output" == *"ctrl-d:delete"* ]]
[[ "$output" == *"ctrl-y:copy"* ]]
[[ "$output" == *"ctrl-r:refresh"* ]]
}
@test "zsh fzf header lists all keybindings" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"enter:cd"* ]]
[[ "$output" == *"ctrl-e:editor"* ]]
[[ "$output" == *"ctrl-a:ai"* ]]
[[ "$output" == *"ctrl-d:delete"* ]]
[[ "$output" == *"ctrl-y:copy"* ]]
[[ "$output" == *"ctrl-r:refresh"* ]]
}
@test "fish fzf header lists all keybindings" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"enter:cd"* ]]
[[ "$output" == *"ctrl-e:editor"* ]]
[[ "$output" == *"ctrl-a:ai"* ]]
[[ "$output" == *"ctrl-d:delete"* ]]
[[ "$output" == *"ctrl-y:copy"* ]]
[[ "$output" == *"ctrl-r:refresh"* ]]
}
# ── fzf: enter (cd) ─────────────────────────────────────────────────────────
@test "bash fzf enter extracts path from selection field 1 and cd" {
run cmd_init bash
[ "$status" -eq 0 ]
# Selection is parsed with cut -f1 to get path, then cd
[[ "$output" == *'cut -f1'* ]]
[[ "$output" == *'cd "$dir"'* ]]
}
@test "zsh fzf enter extracts path from selection field 1 and cd" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *'cut -f1'* ]]
[[ "$output" == *'cd "$dir"'* ]]
}
@test "fish fzf enter extracts path from selection and cd" {
run cmd_init fish
[ "$status" -eq 0 ]
# Fish uses string split or cut to extract path
[[ "$output" == *'cd '* ]]
}
# ── fzf: ctrl-e (editor) — via --expect ──────────────────────────────────────
@test "bash fzf ctrl-e handled via --expect for full terminal access" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-a,ctrl-e"* ]]
[[ "$output" == *'git gtr editor'* ]]
}
@test "zsh fzf ctrl-e handled via --expect for full terminal access" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-a,ctrl-e"* ]]
[[ "$output" == *'git gtr editor'* ]]
}
@test "fish fzf ctrl-e handled via --expect for full terminal access" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-a,ctrl-e"* ]]
[[ "$output" == *'git gtr editor'* ]]
}
# ── fzf: ctrl-a (ai) — via --expect ─────────────────────────────────────────
@test "bash fzf ctrl-a runs git gtr ai after fzf exits" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-a,ctrl-e"* ]]
[[ "$output" == *'git gtr ai'* ]]
}
@test "zsh fzf ctrl-a runs git gtr ai after fzf exits" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-a,ctrl-e"* ]]
[[ "$output" == *'git gtr ai'* ]]
}
@test "fish fzf ctrl-a runs git gtr ai after fzf exits" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-a,ctrl-e"* ]]
[[ "$output" == *'git gtr ai'* ]]
}
# ── fzf: ctrl-d (delete + reload) ───────────────────────────────────────────
@test "bash fzf ctrl-d runs git gtr rm and reloads list" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-d:execute(git gtr rm {2} > /dev/tty 2>&1 < /dev/tty)+reload(git gtr list --porcelain)"* ]]
}
@test "zsh fzf ctrl-d runs git gtr rm and reloads list" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-d:execute(git gtr rm {2} > /dev/tty 2>&1 < /dev/tty)+reload(git gtr list --porcelain)"* ]]
}
@test "fish fzf ctrl-d runs git gtr rm and reloads list" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-d:execute(git gtr rm {2} > /dev/tty 2>&1 < /dev/tty)+reload(git gtr list --porcelain)"* ]]
}
# ── fzf: ctrl-y (copy) ──────────────────────────────────────────────────────
@test "bash fzf ctrl-y runs git gtr copy on selected branch" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-y:execute(git gtr copy {2} > /dev/tty 2>&1 < /dev/tty)"* ]]
}
@test "zsh fzf ctrl-y runs git gtr copy on selected branch" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-y:execute(git gtr copy {2} > /dev/tty 2>&1 < /dev/tty)"* ]]
}
@test "fish fzf ctrl-y runs git gtr copy on selected branch" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-y:execute(git gtr copy {2} > /dev/tty 2>&1 < /dev/tty)"* ]]
}
# ── fzf: ctrl-r (refresh) ───────────────────────────────────────────────────
@test "bash fzf ctrl-r reloads worktree list" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-r:reload(git gtr list --porcelain)"* ]]
}
@test "zsh fzf ctrl-r reloads worktree list" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-r:reload(git gtr list --porcelain)"* ]]
}
@test "fish fzf ctrl-r reloads worktree list" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-r:reload(git gtr list --porcelain)"* ]]
}
# ── fzf: preview window ─────────────────────────────────────────────────────
@test "bash fzf preview shows git log and status" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"--preview="* ]]
[[ "$output" == *"git -C {1} log --oneline --graph --color=always"* ]]
[[ "$output" == *"git -C {1} status --short"* ]]
[[ "$output" == *"--preview-window=right:50%"* ]]
}
@test "zsh fzf preview shows git log and status" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"--preview="* ]]
[[ "$output" == *"git -C {1} log --oneline --graph --color=always"* ]]
[[ "$output" == *"git -C {1} status --short"* ]]
[[ "$output" == *"--preview-window=right:50%"* ]]
}
@test "fish fzf preview shows git log and status" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"--preview="* ]]
[[ "$output" == *"git -C {1} log --oneline --graph --color=always"* ]]
[[ "$output" == *"git -C {1} status --short"* ]]
[[ "$output" == *"--preview-window=right:50%"* ]]
}
# ── fzf: fallback messages ──────────────────────────────────────────────────
@test "bash output shows fzf install hint when no args and no fzf" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'Install fzf for an interactive picker'* ]]
}
@test "zsh output shows fzf install hint when no args and no fzf" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *'Install fzf for an interactive picker'* ]]
}
@test "fish output shows fzf install hint when no args and no fzf" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *'Install fzf for an interactive picker'* ]]
}
@test "--as replaces function name in fzf fallback message" {
run cmd_init bash --as gwtr
[ "$status" -eq 0 ]
[[ "$output" == *'Usage: gwtr cd <branch>'* ]]
}
# ── git gtr passthrough preserved ────────────────────────────────────────────
@test "bash output passes non-cd commands to git gtr" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'command git gtr "$@"'* ]]
}
@test "--as does not replace 'git gtr' invocations" {
run cmd_init bash --as myfn
[ "$status" -eq 0 ]
[[ "$output" == *"command git gtr"* ]]
[[ "$output" == *"git gtr list --porcelain"* ]]
}