-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_git.bats
More file actions
1321 lines (1057 loc) · 41.9 KB
/
Copy pathlib_git.bats
File metadata and controls
1321 lines (1057 loc) · 41.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bats
load ../../tests/test_helper.sh
setup() {
setup_test_tmpdir
source "$BASE_BASH_DIR/std/lib_std.sh"
declare -a setup_args=()
base_init setup_args --source "$BASE_BASH_DIR/git/tests/lib_git.bats" --
source "$BASE_BASH_DIR/git/lib_git.sh"
}
@test "lib_git can be sourced more than once" {
source "$BASE_BASH_DIR/git/lib_git.sh"
[ "$(type -t base_git_update_repo)" = "function" ]
}
@test "lib_git fails clearly when sourced without stdlib" {
bats_run bash -c 'source "$1"; rc=$?; printf "source-rc=%s\n" "$rc"; exit "$rc"' bash "$BASE_BASH_DIR/git/lib_git.sh"
[ "$status" -eq 1 ]
[[ "$output" == *"lib_git.sh requires lib_std.sh to be sourced first"* ]]
[[ "$output" == *"source-rc=1"* ]]
[[ "$output" != *"command not found"* ]]
}
@test "lib_git requires the stdlib loaded marker" {
bats_run bash -c 'base_std_log_error() { :; }; base_std_log_debug() { :; }; source "$1"; rc=$?; printf "source-rc=%s\n" "$rc"; exit "$rc"' bash "$BASE_BASH_DIR/git/lib_git.sh"
[ "$status" -eq 1 ]
[[ "$output" == *"lib_git.sh requires lib_std.sh to be sourced first"* ]]
[[ "$output" == *"source-rc=1"* ]]
}
@test "git required-argument APIs return usage errors under every caller option combination" {
local function_name mode
for mode in off e u p eu ep up eup; do
for function_name in \
base_git_detect_default_branch \
base_git_worktree_path_for_branch \
base_git_branch_upstream \
base_git_branch_merged_to_ref \
base_git_update_repo \
base_git_get_current_branch \
base_git_check_script_up_to_date; do
bats_run "$BASH" -c '
mode="$1"
case "$mode" in *e*) set -e ;; esac
case "$mode" in *u*) set -u ;; esac
case "$mode" in *p*) set -o pipefail ;; esac
source "$2"
declare -a app_args=()
base_init app_args --source "$0" --
source "$3"
"$4"
rc=$?
exit "$rc"
' bash "$mode" "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/git/lib_git.sh" "$function_name"
[ "$status" -eq 1 ]
[[ "$output" == *"Usage:"* ]]
[[ "$output" != *"unbound variable"* ]]
done
done
}
@test "git optional forms reject excess arguments and unsupported option placement" {
capture_command base_git_list_worktree_branches one two
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_list_worktree_branches [repo_dir]"* ]]
capture_command base_git_list_remote_branches one two
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_list_remote_branches [repo_dir]"* ]]
capture_command base_git_worktree_path_for_branch branch repo extra
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_worktree_path_for_branch <branch> [repo_dir]"* ]]
capture_command base_git_check_script_up_to_date --refresh script.sh
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_check_script_up_to_date [--fetch] <script_path>"* ]]
capture_command base_git_check_script_up_to_date --fetch
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_check_script_up_to_date [--fetch] <script_path>"* ]]
}
@test "git remote parsing is independent of and preserves caller IFS" {
local output_file="$TEST_TMPDIR/remote-branches.out"
local rc
git() {
if [[ "${1:-}" == "-C" && "${3:-}" == "ls-remote" ]]; then
printf 'abc123\trefs/heads/main\n'
printf 'def456\trefs/heads/feature/topic\n'
return 0
fi
command git "$@"
}
IFS=:
if base_git_list_remote_branches "$TEST_TMPDIR" >"$output_file"; then
rc=0
else
rc=$?
fi
unset -f git
[ "$rc" -eq 0 ]
[ "$IFS" = ":" ]
[ "$(cat "$output_file")" = $'main\nfeature/topic' ]
}
@test "git predicate status survives every caller option combination" {
local mode
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" checkout -b feature >/dev/null 2>&1
printf 'feature\n' > "$repo/feature.txt"
commit_all "$repo" "Feature commit"
git -C "$repo" checkout main >/dev/null 2>&1
for mode in off e u p eu ep up eup; do
bats_run "$BASH" -c '
mode="$1"
case "$mode" in *e*) set -e ;; esac
case "$mode" in *u*) set -u ;; esac
case "$mode" in *p*) set -o pipefail ;; esac
source "$2"
declare -a app_args=()
base_init app_args --
source "$3"
base_git_branch_merged_to_ref "$4" feature main
rc=$?
exit "$rc"
' bash "$mode" "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/git/lib_git.sh" "$repo"
[ "$status" -eq 1 ]
[[ "$output" != *"unbound variable"* ]]
done
}
@test "base_git_detect_default_branch resolves origin HEAD and fallback branches" {
local repo="$TEST_TMPDIR/repo"
local branch=""
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" update-ref refs/remotes/origin/trunk HEAD
git -C "$repo" symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/trunk
base_git_detect_default_branch "$repo" branch
[ "$branch" = "trunk" ]
git -C "$repo" symbolic-ref -d refs/remotes/origin/HEAD
base_git_detect_default_branch "$repo" branch
[ "$branch" = "main" ]
}
@test "git worktree helpers surface producer failures" {
git() {
printf 'worktree command failed\n' >&2
return 7
}
capture_command base_git_worktree_path_for_branch feature
[ "$status" -eq 1 ]
[[ "$output" == *"Unable to list Git worktrees."* ]]
capture_command base_git_list_worktree_branches
[ "$status" -eq 1 ]
[[ "$output" == *"Unable to list Git worktrees."* ]]
unset -f git
}
@test "git worktree helpers parse canonical porcelain output" {
git() {
if [[ "${1:-}" == "worktree" || "${3:-}" == "worktree" ]]; then
cat <<'EOF'
worktree /tmp/main
HEAD abc123
branch refs/heads/main
worktree /tmp/feature
HEAD def456
branch refs/heads/feature/test
EOF
return 0
fi
command git "$@"
}
capture_command base_git_worktree_path_for_branch feature/test
[ "$status" -eq 0 ]
[ "$output" = "/tmp/feature" ]
capture_command base_git_list_worktree_branches
[ "$status" -eq 0 ]
[[ "$output" == *$'/tmp/main\tmain'* ]]
[[ "$output" == *$'/tmp/feature\tfeature/test'* ]]
unset -f git
}
@test "git worktree command arrays are Bash 4.2 nounset-safe" {
bats_run "$BASH" -c '
set -u
source "$1"
declare -a app_args=()
base_init app_args --
source "$2"
git() {
printf "%s\n" \
"worktree /tmp/main" \
"HEAD abc123" \
"branch refs/heads/main" \
"" \
"worktree /tmp/feature" \
"HEAD def456" \
"branch refs/heads/feature/test"
}
base_git_worktree_path_for_branch feature/test
base_git_list_worktree_branches /tmp/repo
' bash "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/git/lib_git.sh"
[ "$status" -eq 0 ]
[[ "$output" == *"/tmp/feature"* ]]
[[ "$output" == *$'/tmp/main\tmain'* ]]
[[ "$output" == *$'/tmp/feature\tfeature/test'* ]]
[[ "$output" != *"unbound variable"* ]]
}
@test "git branch and remote helpers use generic names" {
local repo="$TEST_TMPDIR/repo"
local branch_output remote_output
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" branch feature
branch_output="$(base_git_branch_upstream "$repo" main)"
[ -z "$branch_output" ]
base_git_branch_merged_to_ref "$repo" feature main
git() {
if [[ "${1:-}" == "-C" && "${3:-}" == "ls-remote" ]]; then
printf 'abc123\trefs/heads/main\n'
printf 'def456\trefs/tags/v1\n'
return 0
fi
command git "$@"
}
remote_output="$(base_git_list_remote_branches "$repo")"
unset -f git
[ "$remote_output" = "main" ]
}
@test "base_git_get_current_branch returns the current branch name" {
local repo="$TEST_TMPDIR/repo"
local branch=""
init_git_repo "$repo"
base_git_get_current_branch "$repo" branch
[ "$branch" = "main" ]
}
@test "base_git_get_current_branch rejects readonly result variables" {
local repo="$TEST_TMPDIR/repo"
local branch="sentinel"
local stderr_file="$TEST_TMPDIR/git-readonly-output.err"
local rc
init_git_repo "$repo"
readonly branch
if base_git_get_current_branch "$repo" branch 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 1 ]
[ "$branch" = "sentinel" ]
[[ "$(cat "$stderr_file")" == *"result variable 'branch' is readonly"* ]]
}
@test "Git result helpers reject exact internal holder names before locals or mutation" {
local -r __base_bash_libs_git_detect_result_name=detected
local -r __base_bash_libs_git_branch_result_name=current
local detected="keep-detected"
local current="keep-current"
local stderr_file="$TEST_TMPDIR/git-internal-holder.err"
local rc
if base_git_detect_default_branch . __base_bash_libs_git_detect_result_name 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 1 ]
[ "$detected" = "keep-detected" ]
if base_git_get_current_branch . __base_bash_libs_git_branch_result_name 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 1 ]
[ "$current" = "keep-current" ]
[[ "$(cat "$stderr_file")" == *"uses the reserved '__' internal namespace"* ]]
[[ "$(cat "$stderr_file")" != *"readonly variable"* ]]
}
@test "base_git_get_current_branch supports shadowing-prone output variable names" {
local repo="$TEST_TMPDIR/repo"
local result_var_name=""
local branch_name=""
init_git_repo "$repo"
base_git_get_current_branch "$repo" result_var_name
base_git_get_current_branch "$repo" branch_name
[ "$result_var_name" = "main" ]
[ "$branch_name" = "main" ]
}
@test "base_git_get_current_branch reports detached head" {
local repo="$TEST_TMPDIR/repo"
local branch=""
init_git_repo "$repo"
printf 'hello\n' > "$repo/README.md"
commit_all "$repo" "Initial commit"
git -C "$repo" checkout --detach >/dev/null 2>&1
base_git_get_current_branch "$repo" branch
[ "$branch" = "detached head" ]
}
@test "base_git_get_current_branch leaves missing directories as empty success" {
local branch="sentinel"
local rc
if base_git_get_current_branch "$TEST_TMPDIR/missing" branch; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 0 ]
[ "$branch" = "" ]
}
@test "base_git_get_current_branch does not use pushd or popd" {
local repo="$TEST_TMPDIR/repo"
local branch="" rc
init_git_repo "$repo"
pushd() {
printf 'unexpected pushd\n' >&2
return 99
}
popd() {
printf 'unexpected popd\n' >&2
return 99
}
if base_git_get_current_branch "$repo" branch; then
rc=0
else
rc=$?
fi
unset -f pushd popd
[ "$rc" -eq 0 ]
[ "$branch" = "main" ]
}
@test "base_git_get_current_branch usage names the current function" {
bats_run base_git_get_current_branch
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_get_current_branch <directory> <result_variable_name>"* ]]
[[ "$output" != *"Usage: get_git_branch"* ]]
}
@test "base_git_get_current_branch rejects invalid result variable names" {
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
bats_run base_git_get_current_branch "$repo" "bad-name"
[ "$status" -eq 1 ]
[[ "$output" == *"base_git_get_current_branch: result variable name must be a valid Bash variable name"* ]]
[[ "$output" != *"invalid variable name"* ]]
}
@test "base_git_update_repo usage names the current function" {
capture_command base_git_update_repo
[ "$status" -eq 1 ]
[[ "$output" == *"Usage: base_git_update_repo /path/to/repo [allowed_dirty_path] [expected_branch]"* ]]
[[ "$output" != *"Usage: update_repo"* ]]
}
@test "base_git_update_repo skips dirty repositories when no dirty path is allowed" {
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
printf 'local change\n' > "$repo/data.txt"
base_std_set_log_level DEBUG
base_std_set_log_category_level -l base_bash_libs.git DEBUG
capture_command base_git_update_repo "$repo"
[ "$status" -eq 0 ]
[[ "$output" == *"has local changes; skipping auto-update"* ]]
}
@test "caller DEBUG does not enable reusable Git DEBUG by default" {
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
base_std_set_log_level DEBUG
capture_command base_git_update_repo "$repo" "" release
[ "$status" -eq 0 ]
[[ "$output" != *"not 'release'. Skipping update"* ]]
}
@test "Git child category DEBUG opt-in enables reusable Git diagnostics" {
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
base_std_set_log_level DEBUG
base_std_set_log_category_level -l base_bash_libs.git DEBUG
capture_command base_git_update_repo "$repo" "" release
[ "$status" -eq 0 ]
[[ "$output" == *"not 'release'. Skipping update"* ]]
}
@test "base_bash_libs parent DEBUG opt-in enables reusable Git diagnostics" {
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
base_std_set_log_level DEBUG
base_std_set_log_category_level -l base_bash_libs DEBUG
capture_command base_git_update_repo "$repo" "" release
[ "$status" -eq 0 ]
[[ "$output" == *"not 'release'. Skipping update"* ]]
}
@test "base_git_update_repo fails clearly when origin remote is missing" {
local before_head
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
before_head="$(git -C "$repo" rev-parse HEAD)"
capture_command base_git_update_repo "$repo" "" main
[ "$status" -eq 1 ]
[[ "$output" == *"git pull failed on repo '$repo'"* ]]
[ "$(git -C "$repo" rev-parse HEAD)" = "$before_head" ]
[ "$(cat "$repo/data.txt")" = "base" ]
[ -z "$(git -C "$repo" status --porcelain)" ]
}
@test "base_git_update_repo fails clearly when origin remote is unreachable" {
local before_head
local remote="$TEST_TMPDIR/remote.git"
local repo="$TEST_TMPDIR/repo"
create_tracked_repo_with_upstream "$repo" "$remote" "data.txt" "base"
before_head="$(git -C "$repo" rev-parse HEAD)"
git -C "$repo" remote set-url origin "$TEST_TMPDIR/missing-remote.git"
capture_command base_git_update_repo "$repo" "" main
[ "$status" -eq 1 ]
[[ "$output" == *"git pull failed on repo '$repo'"* ]]
[ "$(git -C "$repo" rev-parse HEAD)" = "$before_head" ]
[ "$(cat "$repo/data.txt")" = "base" ]
[ -z "$(git -C "$repo" status --porcelain)" ]
}
@test "base_git_update_repo fails on non-fast-forward updates without changing HEAD" {
local before_head
local other="$TEST_TMPDIR/other"
local remote="$TEST_TMPDIR/remote.git"
local repo="$TEST_TMPDIR/repo"
create_tracked_repo_with_upstream "$repo" "$remote" "data.txt" "base"
before_head="$(git -C "$repo" rev-parse HEAD)"
git clone "$remote" "$other" >/dev/null 2>&1
git -C "$other" config user.name "Bats Test"
git -C "$other" config user.email "bats@example.com"
printf 'rewritten remote\n' > "$other/data.txt"
git -C "$other" add data.txt
git -C "$other" commit --amend -m "Rewrite remote history" >/dev/null 2>&1
git -C "$other" push --force origin main >/dev/null 2>&1
capture_command base_git_update_repo "$repo" "" main
[ "$status" -eq 1 ]
[[ "$output" == *"git pull failed on repo '$repo'"* ]]
[ "$(git -C "$repo" rev-parse HEAD)" = "$before_head" ]
[ "$(cat "$repo/data.txt")" = "base" ]
[ -z "$(git -C "$repo" status --porcelain)" ]
}
@test "base_git_update_repo lets git protect untracked files from incoming tracked paths" {
local before_head
local other="$TEST_TMPDIR/other"
local remote="$TEST_TMPDIR/remote.git"
local repo="$TEST_TMPDIR/repo"
create_tracked_repo_with_upstream "$repo" "$remote" "data.txt" "base"
before_head="$(git -C "$repo" rev-parse HEAD)"
git clone "$remote" "$other" >/dev/null 2>&1
git -C "$other" config user.name "Bats Test"
git -C "$other" config user.email "bats@example.com"
printf 'incoming tracked\n' > "$other/local-notes.md"
git -C "$other" add local-notes.md
git -C "$other" commit -m "Add tracked notes" >/dev/null 2>&1
git -C "$other" push origin main >/dev/null 2>&1
printf 'local untracked\n' > "$repo/local-notes.md"
capture_command base_git_update_repo "$repo" "" main
[ "$status" -eq 1 ]
[[ "$output" == *"git pull failed on repo '$repo'"* ]]
[ "$(git -C "$repo" rev-parse HEAD)" = "$before_head" ]
[ "$(cat "$repo/local-notes.md")" = "local untracked" ]
! git -C "$repo" ls-files --error-unmatch local-notes.md >/dev/null 2>&1
}
@test "base_git_update_repo accepts main as the detected update branch" {
local repo="$TEST_TMPDIR/repo"
init_git_repo "$repo"
git -C "$repo" checkout -B main >/dev/null 2>&1
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
printf 'local change\n' > "$repo/data.txt"
base_std_set_log_level DEBUG
base_std_set_log_category_level -l base_bash_libs.git DEBUG
capture_command base_git_update_repo "$repo"
[ "$status" -eq 0 ]
[[ "$output" == *"has local changes; skipping auto-update"* ]]
[[ "$output" != *"not 'main'"* ]]
}
@test "__base_bash_libs_git_expected_update_branch__ returns main when origin has main" {
local repo="$TEST_TMPDIR/repo"
local branch
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" update-ref refs/remotes/origin/main HEAD
git -C "$repo" checkout --detach >/dev/null 2>&1
git -C "$repo" branch -D main >/dev/null 2>&1
pushd "$repo" >/dev/null
branch="$(__base_bash_libs_git_expected_update_branch__)"
popd >/dev/null
[ "$branch" = "main" ]
}
@test "__base_bash_libs_git_expected_update_branch__ returns master when origin only has master" {
local repo="$TEST_TMPDIR/repo"
local branch
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" update-ref refs/remotes/origin/master HEAD
git -C "$repo" checkout --detach >/dev/null 2>&1
git -C "$repo" branch -D main >/dev/null 2>&1
pushd "$repo" >/dev/null
branch="$(__base_bash_libs_git_expected_update_branch__)"
popd >/dev/null
[ "$branch" = "master" ]
}
@test "__base_bash_libs_git_expected_update_branch__ falls back to main without main or master refs" {
local repo="$TEST_TMPDIR/repo"
local branch
init_git_repo "$repo"
pushd "$repo" >/dev/null
branch="$(__base_bash_libs_git_expected_update_branch__)"
popd >/dev/null
[ "$branch" = "main" ]
}
@test "__base_bash_libs_git_only_path_dirty__ accepts multiple dirty files under an allowed directory" {
local repo="$TEST_TMPDIR/repo"
local rc
init_git_repo "$repo"
mkdir -p "$repo/shared"
printf 'one\n' > "$repo/shared/one.txt"
printf 'two\n' > "$repo/shared/two.txt"
commit_all "$repo" "Initial commit"
printf 'local one\n' > "$repo/shared/one.txt"
printf 'local two\n' > "$repo/shared/two.txt"
pushd "$repo" >/dev/null
__base_bash_libs_git_only_path_dirty__ "shared"
rc=$?
popd >/dev/null
[ "$rc" -eq 0 ]
}
@test "__base_bash_libs_git_only_path_dirty__ accepts tracked paths containing spaces" {
local repo="$TEST_TMPDIR/repo"
local rc
init_git_repo "$repo"
mkdir -p "$repo/shared"
printf 'one\n' > "$repo/shared/hello world.txt"
commit_all "$repo" "Initial commit"
printf 'local change\n' >> "$repo/shared/hello world.txt"
pushd "$repo" >/dev/null
__base_bash_libs_git_only_path_dirty__ "shared"
rc=$?
popd >/dev/null
[ "$rc" -eq 0 ]
}
@test "__base_bash_libs_git_only_path_dirty__ does not treat sibling path prefixes as allowed" {
local repo="$TEST_TMPDIR/repo"
local rc
init_git_repo "$repo"
mkdir -p "$repo/shared"
printf 'one\n' > "$repo/shared/one.txt"
printf 'other\n' > "$repo/shared-other.txt"
commit_all "$repo" "Initial commit"
printf 'local one\n' > "$repo/shared/one.txt"
printf 'local other\n' > "$repo/shared-other.txt"
pushd "$repo" >/dev/null
set +e
__base_bash_libs_git_only_path_dirty__ "shared"
rc=$?
set -e
popd >/dev/null
[ "$rc" -eq 1 ]
}
@test "__base_bash_libs_git_only_path_dirty__ rejects renames from outside the allowed path" {
local repo="$TEST_TMPDIR/repo"
local rc
init_git_repo "$repo"
mkdir -p "$repo/shared" "$repo/src"
printf 'one\n' > "$repo/src/one.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" mv src/one.txt shared/one.txt
pushd "$repo" >/dev/null
set +e
__base_bash_libs_git_only_path_dirty__ "shared"
rc=$?
set -e
popd >/dev/null
[ "$rc" -eq 1 ]
}
@test "__base_bash_libs_git_only_path_dirty__ accepts renames inside the allowed path" {
local repo="$TEST_TMPDIR/repo"
local rc
init_git_repo "$repo"
mkdir -p "$repo/shared"
printf 'one\n' > "$repo/shared/one.txt"
commit_all "$repo" "Initial commit"
git -C "$repo" mv shared/one.txt shared/two.txt
pushd "$repo" >/dev/null
__base_bash_libs_git_only_path_dirty__ "shared"
rc=$?
popd >/dev/null
[ "$rc" -eq 0 ]
}
@test "base_git_update_repo cleans up temp log without changing RETURN trap" {
local repo="$TEST_TMPDIR/repo"
local temp_dir="$TEST_TMPDIR/git-temp"
local return_trap
mkdir -p "$temp_dir"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
printf 'local change\n' > "$repo/data.txt"
trap 'printf "outer return trap\n"' RETURN
TMPDIR="$temp_dir" capture_command base_git_update_repo "$repo"
return_trap="$(trap -p RETURN)"
trap - RETURN
[ "$status" -eq 0 ]
[[ "$return_trap" == *"outer return trap"* ]]
! compgen -G "$temp_dir/git_log.*" >/dev/null
}
@test "base_git_update_repo registers temp log for cleanup" {
local repo="$TEST_TMPDIR/repo"
local registration_file="$TEST_TMPDIR/registered-cleanup-paths.txt"
init_git_repo "$repo"
printf 'base\n' > "$repo/data.txt"
commit_all "$repo" "Initial commit"
printf 'local change\n' > "$repo/data.txt"
eval "$(declare -f base_std_register_cleanup_path | sed '1s/base_std_register_cleanup_path/__orig_std_register_cleanup_path/')"
base_std_register_cleanup_path() {
printf '%s\n' "$@" >> "$registration_file"
__orig_std_register_cleanup_path "$@"
}
capture_command base_git_update_repo "$repo"
unset -f base_std_register_cleanup_path __orig_std_register_cleanup_path
[ "$status" -eq 0 ]
[[ "$(cat "$registration_file")" == *"git_log."* ]]
}
@test "__base_bash_libs_git_update_repo_finish__ removes temp log after success" {
local git_log="$TEST_TMPDIR/git.log"
printf 'pull output\n' > "$git_log"
bats_run __base_bash_libs_git_update_repo_finish__ "$git_log" false 0
[ "$status" -eq 0 ]
[ ! -e "$git_log" ]
}
@test "__base_bash_libs_git_update_repo_finish__ preserves an existing RETURN trap" {
local git_log="$TEST_TMPDIR/git.log"
local return_trap
printf 'pull output\n' > "$git_log"
trap 'printf "outer return trap\n"' RETURN
bats_run __base_bash_libs_git_update_repo_finish__ "$git_log" false 0
return_trap="$(trap -p RETURN)"
trap - RETURN
[ "$status" -eq 0 ]
[[ "$return_trap" == *"outer return trap"* ]]
[ ! -e "$git_log" ]
}
@test "__base_bash_libs_git_update_repo_finish__ unregisters removed temp logs" {
local git_log="$TEST_TMPDIR/git.log"
local unregister_file="$TEST_TMPDIR/unregistered-paths.txt"
printf 'pull output\n' > "$git_log"
eval "$(declare -f base_std_unregister_cleanup_path | sed '1s/base_std_unregister_cleanup_path/__orig_std_unregister_cleanup_path/')"
base_std_unregister_cleanup_path() {
printf '%s\n' "$@" >> "$unregister_file"
__orig_std_unregister_cleanup_path "$@"
}
__base_bash_libs_git_update_repo_finish__ "$git_log" false 0
unset -f base_std_unregister_cleanup_path __orig_std_unregister_cleanup_path
[ "$(cat "$unregister_file")" = "$git_log" ]
[ ! -e "$git_log" ]
}
@test "base_git_update_repo reports captured submodule diagnostics" {
local repo="$TEST_TMPDIR/repo"
local remote="$TEST_TMPDIR/remote.git"
create_tracked_repo_with_upstream "$repo" "$remote" "data.txt" "base"
git() {
if [[ "${1:-}" == "submodule" ]]; then
printf 'submodule exploded\n' >&2
return 1
fi
command git "$@"
}
capture_command base_git_update_repo "$repo"
unset -f git
[ "$status" -eq 1 ]
[[ "$output" == *"git submodule update failed on repo '$repo'"* ]]
[[ "$output" == *"submodule exploded"* ]]
! compgen -G "$TEST_TMPDIR/git-submodule-log.*" >/dev/null
}
@test "__base_bash_libs_git_pull_with_retry__ retries once after a transient pull failure" {
local git_log="$TEST_TMPDIR/git.log"
local pull_count="$TEST_TMPDIR/pull-count"
printf '0\n' > "$pull_count"
git() {
local count
if [[ "${1:-}" == "pull" ]]; then
count="$(cat "$pull_count")"
count=$((count + 1))
printf '%s\n' "$count" > "$pull_count"
printf 'pull attempt %s\n' "$count" >&2
[[ "$count" -ge 2 ]]
return $?
fi
command git "$@"
}
bats_run __base_bash_libs_git_pull_with_retry__ "$git_log"
unset -f git
[ "$status" -eq 0 ]
[ "$(cat "$pull_count")" = "2" ]
[[ "$output" == *"git pull failed on attempt 1; retrying once."* ]]
[ "$(cat "$git_log")" = "pull attempt 2" ]
}
@test "__base_bash_libs_git_pull_with_retry__ honors configured max attempts" {
local git_log="$TEST_TMPDIR/git.log"
local pull_count="$TEST_TMPDIR/pull-count"
printf '0\n' > "$pull_count"
git() {
local count
if [[ "${1:-}" == "pull" ]]; then
count="$(cat "$pull_count")"
count=$((count + 1))
printf '%s\n' "$count" > "$pull_count"
printf 'pull attempt %s\n' "$count" >&2
[[ "$count" -ge 3 ]]
return $?
fi
command git "$@"
}
BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS=3 bats_run __base_bash_libs_git_pull_with_retry__ "$git_log"
unset -f git
[ "$status" -eq 0 ]
[ "$(cat "$pull_count")" = "3" ]
[[ "$output" == *"git pull failed on attempt 2; retrying (attempt 3 of 3)."* ]]
[ "$(cat "$git_log")" = "pull attempt 3" ]
}
@test "__base_bash_libs_git_pull_with_retry__ falls back for invalid configured max attempts" {
local git_log="$TEST_TMPDIR/git.log"
local max_attempts
local pull_count="$TEST_TMPDIR/pull-count"
git() {
local count
if [[ "${1:-}" == "pull" ]]; then
count="$(cat "$pull_count")"
count=$((count + 1))
printf '%s\n' "$count" > "$pull_count"
printf 'pull attempt %s\n' "$count" >&2
[[ "$count" -ge 2 ]]
return $?
fi
command git "$@"
}
for max_attempts in abc 0 -1; do
printf '0\n' > "$pull_count"
: > "$git_log"
BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS="$max_attempts" bats_run __base_bash_libs_git_pull_with_retry__ "$git_log"
[ "$status" -eq 0 ]
[ "$(cat "$pull_count")" = "2" ]
[[ "$output" == *"BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS must be a positive integer; using 2."* ]]
[[ "$output" == *"git pull failed on attempt 1; retrying once."* ]]
[ "$(cat "$git_log")" = "pull attempt 2" ]
done
unset -f git
}
@test "__base_bash_libs_git_pull_with_retry__ fails after two pull attempts" {
local git_log="$TEST_TMPDIR/git.log"
local pull_count="$TEST_TMPDIR/pull-count"
printf '0\n' > "$pull_count"
git() {
local count
if [[ "${1:-}" == "pull" ]]; then
count="$(cat "$pull_count")"
count=$((count + 1))
printf '%s\n' "$count" > "$pull_count"
printf 'pull attempt %s\n' "$count" >&2
return 1
fi
command git "$@"
}
bats_run __base_bash_libs_git_pull_with_retry__ "$git_log"
unset -f git
[ "$status" -eq 1 ]
[ "$(cat "$pull_count")" = "2" ]
[[ "$output" == *"git pull failed on attempt 1; retrying once."* ]]
[ "$(cat "$git_log")" = "pull attempt 2" ]
}
@test "base_git_check_script_up_to_date reports success for an up-to-date tracked script" {
local repo="$TEST_TMPDIR/repo"
local remote="$TEST_TMPDIR/remote.git"
local script_path="$repo/scripts/tool.sh"
create_tracked_repo_with_upstream "$repo" "$remote" "scripts/tool.sh" "#!/usr/bin/env bash"
bats_run base_git_check_script_up_to_date "$script_path"
[ "$status" -eq 0 ]
[[ "$output" == *"Repository is up to date with origin/main."* ]]
}
@test "base_git_check_script_up_to_date uses local remote-tracking refs by default" {
local other="$TEST_TMPDIR/other"
local repo="$TEST_TMPDIR/repo"
local remote="$TEST_TMPDIR/remote.git"
local script_path="$repo/scripts/tool.sh"
create_tracked_repo_with_upstream "$repo" "$remote" "scripts/tool.sh" "#!/usr/bin/env bash"
git clone "$remote" "$other" >/dev/null 2>&1
git -C "$other" config user.name "Bats Test"
git -C "$other" config user.email "bats@example.com"
printf 'echo remote\n' >> "$other/scripts/tool.sh"
git -C "$other" add scripts/tool.sh
git -C "$other" commit -m "Update remote script" >/dev/null 2>&1
git -C "$other" push origin main >/dev/null 2>&1
bats_run base_git_check_script_up_to_date "$script_path"
[ "$status" -eq 0 ]
[[ "$output" != *"Using local remote-tracking refs"* ]]
[[ "$output" == *"Repository is up to date with origin/main."* ]]
}
@test "base_git_check_script_up_to_date reports local remote-tracking refs at debug level" {
local repo="$TEST_TMPDIR/repo"
local remote="$TEST_TMPDIR/remote.git"
local script_path="$repo/scripts/tool.sh"
create_tracked_repo_with_upstream "$repo" "$remote" "scripts/tool.sh" "#!/usr/bin/env bash"
base_std_set_log_level DEBUG
base_std_set_log_category_level -l base_bash_libs.git DEBUG
bats_run base_git_check_script_up_to_date "$script_path"