-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathinit.bats
More file actions
715 lines (593 loc) · 21.9 KB
/
init.bats
File metadata and controls
715 lines (593 loc) · 21.9 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#!/usr/bin/env bats
# Tests for the init command (lib/commands/init.sh)
load test_helper
setup() {
source "$PROJECT_ROOT/lib/commands/init.sh"
# Isolate cache to temp dir so tests don't pollute ~/.cache or each other
export XDG_CACHE_HOME="$BATS_TMPDIR/gtr-init-cache-$$"
export GTR_VERSION="test"
}
teardown() {
rm -rf "$BATS_TMPDIR/gtr-init-cache-$$"
}
run_generated_bash_wrapper_completion() {
local wrapper_name="$1"
local comp_line="$2"
local comp_cword="$3"
local comp_point="$4"
shift 4
bash -s -- "$PROJECT_ROOT" "$XDG_CACHE_HOME" "$wrapper_name" "$comp_line" "$comp_cword" "$comp_point" "$@" <<'BASH'
set -euo pipefail
PROJECT_ROOT="$1"
XDG_CACHE_HOME="$2"
wrapper_name="$3"
comp_line="$4"
comp_cword="$5"
comp_point="$6"
shift 6
words=("$@")
export XDG_CACHE_HOME
export GTR_VERSION="test"
log_info() { :; }
log_warn() { :; }
log_error() { :; }
show_command_help() { :; }
# shellcheck disable=SC1090
. "$PROJECT_ROOT/lib/commands/init.sh"
if [ "$wrapper_name" = "gtr" ]; then
eval "$(cmd_init bash)"
else
eval "$(cmd_init bash --as "$wrapper_name")"
fi
_git_gtr() {
printf 'WORDS=%s\n' "${COMP_WORDS[*]}"
printf 'CWORD=%s\n' "$COMP_CWORD"
printf 'LINE=%s\n' "$COMP_LINE"
printf 'POINT=%s\n' "$COMP_POINT"
COMPREPLY=(--from)
}
completion_fn="_${wrapper_name}_completion"
COMP_WORDS=("${words[@]}")
COMP_CWORD="$comp_cword"
COMP_LINE="$comp_line"
COMP_POINT="$comp_point"
COMPREPLY=()
"$completion_fn"
printf 'REPLY=%s\n' "${COMPREPLY[*]}"
BASH
}
# ── 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"* ]]
}
# ── new --cd wrapper support ────────────────────────────────────────────────
@test "bash output intercepts new --cd and strips flag before delegating" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'[ "$1" = "new" ]'* ]]
[[ "$output" == *'if [ "$_gtr_arg" = "--cd" ]'* ]]
[[ "$output" == *'command git gtr new "${_gtr_new_args[@]}"'* ]]
[[ "$output" == *'command git gtr "${_gtr_original_args[@]}"'* ]]
}
@test "zsh output intercepts new --cd and strips flag before delegating" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *'[ "$1" = "new" ]'* ]]
[[ "$output" == *'if [ "$_gtr_arg" = "--cd" ]'* ]]
[[ "$output" == *'command git gtr new "${_gtr_new_args[@]}"'* ]]
[[ "$output" == *'command git gtr "${_gtr_original_args[@]}"'* ]]
}
@test "fish output intercepts new --cd and strips flag before delegating" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *'test "$argv[1]" = "new"'* ]]
[[ "$output" == *'test "$_gtr_arg" = "--cd"'* ]]
[[ "$output" == *'command git gtr new $_gtr_new_args'* ]]
}
@test "bash output uses worktree diff to locate the new directory for --cd" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"git worktree list --porcelain"* ]]
[[ "$output" == *'run_post_cd_hooks "$_gtr_new_dir"'* ]]
[[ "$output" == *'could not determine new directory for --cd'* ]]
}
@test "zsh output uses worktree diff to locate the new directory for --cd" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"git worktree list --porcelain"* ]]
[[ "$output" == *'run_post_cd_hooks "$_gtr_new_dir"'* ]]
[[ "$output" == *'could not determine new directory for --cd'* ]]
}
@test "fish output uses worktree diff to locate the new directory for --cd" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"git worktree list --porcelain"* ]]
[[ "$output" == *'run_post_cd_hooks "$_gtr_new_paths[1]"'* ]]
[[ "$output" == *'could not determine new directory for --cd'* ]]
}
@test "bash wrapper completions include --cd for new" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'compgen -W "--cd"'* ]]
}
@test "bash wrapper completion rewrites delegated context for ai targets" {
run run_generated_bash_wrapper_completion gtr "gtr ai fe" 2 9 gtr ai fe
[ "$status" -eq 0 ]
[[ "$output" == *"WORDS=git gtr ai fe"* ]]
[[ "$output" == *"CWORD=3"* ]]
[[ "$output" == *"LINE=git gtr ai fe"* ]]
[[ "$output" == *"POINT=13"* ]]
[[ "$output" == *"REPLY=--from"* ]]
}
@test "bash wrapper completion rewrites delegated context for custom wrapper names" {
run run_generated_bash_wrapper_completion gwtr "gwtr ai fe" 2 10 gwtr ai fe
[ "$status" -eq 0 ]
[[ "$output" == *"WORDS=git gtr ai fe"* ]]
[[ "$output" == *"CWORD=3"* ]]
[[ "$output" == *"LINE=git gtr ai fe"* ]]
[[ "$output" == *"POINT=13"* ]]
[[ "$output" == *"REPLY=--from"* ]]
}
@test "bash wrapper completion preserves delegated replies and appends --cd for new flags" {
run run_generated_bash_wrapper_completion gtr "gtr new --c" 2 11 gtr new --c
[ "$status" -eq 0 ]
[[ "$output" == *"WORDS=git gtr new --c"* ]]
[[ "$output" == *"CWORD=3"* ]]
[[ "$output" == *"LINE=git gtr new --c"* ]]
[[ "$output" == *"POINT=15"* ]]
[[ "$output" == *"REPLY=--from --cd"* ]]
}
@test "zsh wrapper completions include --cd for new" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *'compadd -- --cd'* ]]
}
@test "fish wrapper completions include --cd for new" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"using_subcommand new"* ]]
[[ "$output" == *"-l cd -d 'Create and cd into the new worktree'"* ]]
}
# ── 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 to extract path, then cd
[[ "$output" == *'string split'* ]]
[[ "$output" == *'set dir'* ]]
[[ "$output" == *'cd $dir'* ]]
}
# ── 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-n,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-n,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-n,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-n,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-n,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-n,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>'* ]]
}
# ── ctrl-n (new worktree) in fzf picker ────────────────────────────────────
@test "bash output includes --expect=ctrl-n in fzf args" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-n"* ]]
}
@test "zsh output includes --expect=ctrl-n in fzf args" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-n"* ]]
}
@test "fish output includes --expect=ctrl-n in fzf args" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"--expect=ctrl-n"* ]]
}
@test "bash output includes ctrl-n:new in fzf header" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-n:new"* ]]
}
@test "zsh output includes ctrl-n:new in fzf header" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-n:new"* ]]
}
@test "fish output includes ctrl-n:new in fzf header" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *"ctrl-n:new"* ]]
}
@test "bash output includes git gtr new in ctrl-n handler" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'git gtr new "$_gtr_branch"'* ]]
}
@test "zsh output includes git gtr new in ctrl-n handler" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *'git gtr new "$_gtr_branch"'* ]]
}
@test "fish output includes git gtr new in ctrl-n handler" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *'git gtr new "$_gtr_branch"'* ]]
}
@test "bash output prompts for branch name on ctrl-n" {
run cmd_init bash
[ "$status" -eq 0 ]
[[ "$output" == *'Branch name: '* ]]
}
@test "zsh output prompts for branch name on ctrl-n" {
run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *'Branch name: '* ]]
}
@test "fish output prompts for branch name on ctrl-n" {
run cmd_init fish
[ "$status" -eq 0 ]
[[ "$output" == *'Branch name: '* ]]
}
# ── 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"* ]]
}
# ── caching (default behavior) ──────────────────────────────────────────────
@test "init creates cache file and returns output" {
GTR_VERSION="9.9.9" run cmd_init zsh
[ "$status" -eq 0 ]
[[ "$output" == *"gtr()"* ]]
[ -f "$XDG_CACHE_HOME/gtr/init-gtr.zsh" ]
}
@test "init returns cached output on second call" {
# First call: generates and caches
GTR_VERSION="9.9.9" run cmd_init bash
[ "$status" -eq 0 ]
local first_output="$output"
# Second call: reads from cache
GTR_VERSION="9.9.9" run cmd_init bash
[ "$status" -eq 0 ]
[ "$output" = "$first_output" ]
}
@test "cache invalidates when version changes" {
# Generate with version 1.0.0
GTR_VERSION="1.0.0" run cmd_init zsh
[ "$status" -eq 0 ]
# Check cache stamp
local stamp
stamp="$(head -1 "$XDG_CACHE_HOME/gtr/init-gtr.zsh")"
[[ "$stamp" == *"version=1.0.0"* ]]
# Regenerate with version 2.0.0
GTR_VERSION="2.0.0" run cmd_init zsh
[ "$status" -eq 0 ]
stamp="$(head -1 "$XDG_CACHE_HOME/gtr/init-gtr.zsh")"
[[ "$stamp" == *"version=2.0.0"* ]]
}
@test "cache uses --as func name in cache key" {
GTR_VERSION="9.9.9" run cmd_init bash --as myfn
[ "$status" -eq 0 ]
[[ "$output" == *"myfn()"* ]]
[ -f "$XDG_CACHE_HOME/gtr/init-myfn.bash" ]
}
@test "cache works for all shells" {
for sh in bash zsh fish; do
GTR_VERSION="9.9.9" run cmd_init "$sh"
[ "$status" -eq 0 ]
[ -f "$XDG_CACHE_HOME/gtr/init-gtr.${sh}" ]
done
}