Repository navigation
Expand file tree
/
Copy pathlib_gh.bats
More file actions
2020 lines (1711 loc) · 71.7 KB
/
Copy pathlib_gh.bats
File metadata and controls
2020 lines (1711 loc) · 71.7 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
export TEST_TMPDIR
mkdir -p "$TEST_TMPDIR/bin"
PATH="$TEST_TMPDIR/bin:$BASE_TEST_ORIG_PATH"
source "$BASE_BASH_DIR/std/lib_std.sh"
declare -a setup_args=()
base_init setup_args --source "$BASE_BASH_DIR/gh/tests/lib_gh.bats" --
source "$BASE_BASH_DIR/gh/lib_gh.sh"
}
create_fake_gh() {
local script="$TEST_TMPDIR/bin/gh"
cat > "$script"
chmod +x "$script"
}
create_fake_git() {
local script="$TEST_TMPDIR/bin/git"
cat > "$script"
chmod +x "$script"
}
# Deterministic retry seams. Individual tests configure the fixture globals or
# replace one seam for a narrower assertion. No retry test needs wall-clock
# sleeps or a live GitHub process.
install_gh_api_retry_fixture() {
TEST_GH_API_CLOCK=0
TEST_GH_API_EPOCH=2000000000
TEST_GH_API_ATTEMPT_DURATION=0
TEST_GH_API_SUCCESS_AFTER=2
TEST_GH_API_FAILURE_STATUS=1
TEST_GH_API_FAILURE_STDOUT=""
TEST_GH_API_FAILURE_STDERR=$'Get "https://api.github.test/repos/owner/repo": read tcp 127.0.0.1:443->127.0.0.2:1234: read: connection reset by peer\n'
TEST_GH_API_SUCCESS_STDOUT=$'ok\n'
TEST_GH_API_SUCCESS_STDERR=""
TEST_GH_API_OBSERVATION_DIR="$TEST_TMPDIR/gh-api-observations"
mkdir -p "$TEST_GH_API_OBSERVATION_DIR"
TEST_GH_API_ATTEMPT_COUNT_FILE="$TEST_GH_API_OBSERVATION_DIR/attempt-count"
TEST_GH_API_SLEEP_CALLS_FILE="$TEST_GH_API_OBSERVATION_DIR/sleep-calls"
TEST_GH_API_TIMEOUT_CALLS_FILE="$TEST_GH_API_OBSERVATION_DIR/timeout-calls"
TEST_GH_API_TIMEOUT_PATHS_FILE="$TEST_GH_API_OBSERVATION_DIR/timeout-paths"
TEST_GH_API_JITTER_CALLS_FILE="$TEST_GH_API_OBSERVATION_DIR/jitter-calls"
TEST_GH_API_CAPTURE_MODE_FILE="$TEST_GH_API_OBSERVATION_DIR/capture-mode"
TEST_GH_API_FIRST_ARG_FILE="$TEST_GH_API_OBSERVATION_DIR/first-api-arg"
printf '0' > "$TEST_GH_API_ATTEMPT_COUNT_FILE"
: > "$TEST_GH_API_SLEEP_CALLS_FILE"
: > "$TEST_GH_API_TIMEOUT_CALLS_FILE"
: > "$TEST_GH_API_TIMEOUT_PATHS_FILE"
: > "$TEST_GH_API_JITTER_CALLS_FILE"
: > "$TEST_GH_API_CAPTURE_MODE_FILE"
: > "$TEST_GH_API_FIRST_ARG_FILE"
gh() { return 0; }
__base_bash_libs_gh_api_monotonic_seconds__() {
printf -v "$1" '%s' "$TEST_GH_API_CLOCK"
}
__base_bash_libs_gh_api_epoch_seconds__() {
printf -v "$1" '%s' "$TEST_GH_API_EPOCH"
}
__base_bash_libs_gh_api_jitter_seconds__() {
local result_name="$1" cap="$2" delay=0
gh_api_append_observation "$TEST_GH_API_JITTER_CALLS_FILE" "$cap"
((cap == 0)) || delay=$(((cap + 1) / 2))
printf -v "$result_name" '%s' "$delay"
}
__base_bash_libs_gh_api_sleep__() {
gh_api_append_observation "$TEST_GH_API_SLEEP_CALLS_FILE" "$1"
TEST_GH_API_CLOCK=$((TEST_GH_API_CLOCK + $1))
}
__base_bash_libs_gh_api_attempt__() {
local attempt_timeout="$1" attempt_timeout_path="$2"
local stdout_file="$4" stderr_file="$5" attempt_count=0
local fixture_arg fixture_include=0 fixture_stdout fixture_stderr
for fixture_arg in "${@:6}"; do
case "$fixture_arg" in
--include | -i) fixture_include=1 ;;
esac
done
IFS= read -r attempt_count < "$TEST_GH_API_ATTEMPT_COUNT_FILE"
attempt_count=$((attempt_count + 1))
printf '%s' "$attempt_count" > "$TEST_GH_API_ATTEMPT_COUNT_FILE"
gh_api_append_observation "$TEST_GH_API_TIMEOUT_CALLS_FILE" "$attempt_timeout"
gh_api_append_observation "$TEST_GH_API_TIMEOUT_PATHS_FILE" \
"${attempt_timeout_path:-empty}"
printf '%s' "${6-}" > "$TEST_GH_API_FIRST_ARG_FILE"
TEST_GH_API_CLOCK=$((TEST_GH_API_CLOCK + TEST_GH_API_ATTEMPT_DURATION))
: > "$stdout_file"
: > "$stderr_file"
if ((attempt_count >= TEST_GH_API_SUCCESS_AFTER)); then
fixture_stdout="$TEST_GH_API_SUCCESS_STDOUT"
fixture_stderr="$TEST_GH_API_SUCCESS_STDERR"
else
fixture_stdout="$TEST_GH_API_FAILURE_STDOUT"
fixture_stderr="$TEST_GH_API_FAILURE_STDERR"
fi
if ((fixture_include)) && [[ -n "$fixture_stdout" && "$fixture_stdout" != HTTP/* ]]; then
printf 'HTTP/2.0 200 OK\r\n\r\n' > "$stdout_file"
fi
printf '%s' "$fixture_stdout" >> "$stdout_file"
printf '%s' "$fixture_stderr" > "$stderr_file"
((attempt_count >= TEST_GH_API_SUCCESS_AFTER)) && return 0
return "$TEST_GH_API_FAILURE_STATUS"
}
}
gh_api_append_observation() {
local observation_file="$1" observation_value="$2"
[[ ! -s "$observation_file" ]] || printf ',' >> "$observation_file"
printf '%s' "$observation_value" >> "$observation_file"
}
gh_api_retry_observed() {
local observation_file
case "$1" in
attempts) observation_file="$TEST_GH_API_ATTEMPT_COUNT_FILE" ;;
capture-mode) observation_file="$TEST_GH_API_CAPTURE_MODE_FILE" ;;
first-arg) observation_file="$TEST_GH_API_FIRST_ARG_FILE" ;;
jitter) observation_file="$TEST_GH_API_JITTER_CALLS_FILE" ;;
sleeps) observation_file="$TEST_GH_API_SLEEP_CALLS_FILE" ;;
timeout-paths) observation_file="$TEST_GH_API_TIMEOUT_PATHS_FILE" ;;
timeouts) observation_file="$TEST_GH_API_TIMEOUT_CALLS_FILE" ;;
*) return 1 ;;
esac
command cat -- "$observation_file"
}
@test "lib_gh can be sourced more than once" {
source "$BASE_BASH_DIR/gh/lib_gh.sh"
[ "$(type -t base_gh_run)" = "function" ]
}
@test "lib_gh 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/gh/lib_gh.sh"
[ "$status" -eq 1 ]
[[ "$output" == *"lib_gh.sh requires lib_std.sh to be sourced first"* ]]
[[ "$output" == *"source-rc=1"* ]]
[[ "$output" != *"command not found"* ]]
}
@test "GitHub 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_gh_report_command_failure \
base_gh_repo_from_remote_url \
base_gh_infer_repo_from_origin \
base_gh_repo_default_branch; 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"
"$4"
rc=$?
exit "$rc"
' bash "$mode" "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/gh/lib_gh.sh" "$function_name"
[ "$status" -eq 2 ]
[[ "$output" == *"Usage:"* ]]
[[ "$output" != *"unbound variable"* ]]
done
done
}
@test "GitHub optional forms reject excess arguments and invalid values" {
capture_command base_gh_require_cli one two
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_require_cli [install_hint]"* ]]
capture_command base_gh_auth_status_diagnostics one two
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_auth_status_diagnostics [login_hint]"* ]]
capture_command base_gh_infer_repo_from_origin repo result --required
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_infer_repo_from_origin <repo_dir> <result_variable_name> [--optional]"* ]]
capture_command base_gh_report_command_failure invalid issue list
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_report_command_failure <status> [gh args...]"* ]]
capture_command base_gh_report_command_failure 0 issue list
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_report_command_failure <status> [gh args...]"* ]]
capture_command base_gh_report_command_failure 256 issue list
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_report_command_failure <status> [gh args...]"* ]]
}
@test "GitHub sensitive controls fail closed before executing or echoing malformed arguments" {
local secret="parser-canary with spaces"
local unicode_label=$'unicode-label-canary\xe2\x80\xa8\xe2\x80\xae'
local invocation_file="$TEST_TMPDIR/parser-invoked"
create_fake_gh <<'EOF'
#!/usr/bin/env bash
printf 'invoked\n' > "${TEST_TMPDIR:?}/parser-invoked"
exit 0
EOF
capture_command base_gh_run --sensitive "--opaque=$secret" -- issue list
[ "$status" -eq 2 ]
[[ "$output" == *"protected diagnostic controls must end with --"* ]]
[[ "$output" != *"$secret"* ]]
capture_command base_gh_api_with_retry --safe-display "safe API operation" -- repos/owner/repo
[ "$status" -eq 2 ]
[[ "$output" == *"--safe-display is valid only with --sensitive"* ]]
capture_command base_gh_run --sensitive --safe-display "--token=$secret" -- issue list
[ "$status" -eq 2 ]
[[ "$output" == *"one non-empty printable ASCII label"* ]]
[[ "$output" != *"$secret"* ]]
capture_command base_gh_run --sensitive --safe-display "-H$secret" -- issue list
[ "$status" -eq 2 ]
[[ "$output" == *"one non-empty printable ASCII label"* ]]
[[ "$output" != *"$secret"* ]]
capture_command base_gh_api_with_retry --sensitive --safe-display "-fsecret=$secret" -- repos/owner/repo
[ "$status" -eq 2 ]
[[ "$output" == *"one non-empty printable ASCII label"* ]]
[[ "$output" != *"$secret"* ]]
capture_command base_gh_run --sensitive --safe-display "$unicode_label" -- issue list
[ "$status" -eq 2 ]
[[ "$output" == *"one non-empty printable ASCII label"* ]]
[[ "$output" != *"unicode-label-canary"* ]]
capture_command base_gh_report_command_failure --sensitive 77 issue list
[ "$status" -eq 2 ]
[[ "$output" == *"protected diagnostic controls must end with --"* ]]
capture_command base_gh_report_command_failure --sensitive -- "status=$secret" issue list
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: base_gh_report_command_failure"* ]]
[[ "$output" != *"$secret"* ]]
capture_command base_gh_run --sensitive --safe-display $'unsafe\nparser-secret' -- issue list
[ "$status" -eq 2 ]
[[ "$output" == *"one non-empty printable ASCII label"* ]]
[[ "$output" != *"parser-secret"* ]]
[ ! -e "$invocation_file" ]
}
@test "GitHub diagnostics are independent of and preserve caller IFS" {
local output_file="$TEST_TMPDIR/auth-diagnostics.out"
local rc
create_fake_gh <<'EOF'
#!/usr/bin/env bash
printf 'first diagnostic\nsecond diagnostic\n' >&2
exit 4
EOF
IFS=:
if base_gh_auth_status_diagnostics >"$output_file" 2>&1; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 1 ]
[ "$IFS" = ":" ]
[[ "$(cat "$output_file")" == *"gh auth status: first diagnostic"* ]]
[[ "$(cat "$output_file")" == *"gh auth status: second diagnostic"* ]]
}
@test "base_gh_run preserves command status under every caller option combination" {
local mode
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "${1:-}" == "auth" && "${2:-}" == "status" ]]; then
printf 'not logged in\n' >&2
exit 1
fi
printf 'command failed\n' >&2
exit 7
EOF
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"
PATH="$4:$PATH"
base_gh_run issue list
rc=$?
exit "$rc"
' bash "$mode" "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/gh/lib_gh.sh" "$TEST_TMPDIR/bin"
[ "$status" -eq 7 ]
[[ "$output" == *"GitHub command failed: gh issue list"* ]]
[[ "$output" != *"unbound variable"* ]]
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"
PATH="$4:$PATH"
base_gh_run --sensitive --safe-display "strict protected operation" -- issue list
rc=$?
exit "$rc"
' bash "$mode" "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/gh/lib_gh.sh" "$TEST_TMPDIR/bin"
[ "$status" -eq 7 ]
[[ "$output" == *"strict protected operation [sensitive GitHub operation; arguments hidden]"* ]]
[[ "$output" == *"(exit 7)"* ]]
[[ "$output" != *"gh auth status: not logged in"* ]]
[[ "$output" != *"unbound variable"* ]]
done
}
@test "base_gh_require_cli succeeds when gh is on PATH" {
create_fake_gh <<'EOF'
#!/usr/bin/env bash
exit 0
EOF
capture_command base_gh_require_cli
[ "$status" -eq 0 ]
[ "$output" = "" ]
}
@test "GitHub pass-by-name helpers reject readonly result variables" {
local repo="sentinel"
local stderr_file="$TEST_TMPDIR/gh-readonly-output.err"
local rc
readonly repo
if base_gh_repo_from_remote_url "https://github.com/owner/project.git" repo 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 2 ]
[ "$repo" = "sentinel" ]
[[ "$(cat "$stderr_file")" == *"result variable 'repo' is readonly"* ]]
}
@test "GitHub result parser rejects coercing scalar output before publishing" {
local stderr_file="$TEST_TMPDIR/gh-typed-output.err"
local rc
local -i repo=42
if base_gh_repo_from_remote_url "https://github.com/basefoundry/base-bash-libs.git" repo 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 2 ]
[ "$repo" -eq 42 ]
[[ "$(<"$stderr_file")" == *"attributes incompatible with the scalar output contract"* ]]
}
@test "GitHub result helpers reject exact internal holder names before locals or mutation" {
local -r __base_bash_libs_gh_result_name=parsed
local -r __base_bash_libs_gh_infer_result_name=inferred
local -r __base_bash_libs_gh_repo_result_name=defaulted
local parsed="keep-parsed" inferred="keep-inferred" defaulted="keep-defaulted"
local stderr_file="$TEST_TMPDIR/gh-internal-holder.err"
local rc
if base_gh_repo_from_remote_url "https://github.com/owner/project.git" __base_bash_libs_gh_result_name 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 2 ]
[ "$parsed" = "keep-parsed" ]
if base_gh_infer_repo_from_origin . __base_bash_libs_gh_infer_result_name --optional 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 2 ]
[ "$inferred" = "keep-inferred" ]
if base_gh_repo_default_branch owner/project __base_bash_libs_gh_repo_result_name 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 2 ]
[ "$defaulted" = "keep-defaulted" ]
[[ "$(cat "$stderr_file")" == *"uses the reserved '__' internal namespace"* ]]
[[ "$(cat "$stderr_file")" != *"readonly variable"* ]]
}
@test "base_gh_require_cli reports missing gh with caller hint" {
mkdir -p "$TEST_TMPDIR/no-gh-bin"
bats_run "$BASH" -c '
source "$1"
declare -a app_args=()
base_init app_args --
source "$2"
PATH="$3"
base_gh_require_cli "$4"
' bash "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/gh/lib_gh.sh" "$TEST_TMPDIR/no-gh-bin" "Install GitHub CLI and retry."
[ "$status" -eq 1 ]
[[ "$output" == *"Required command 'gh' was not found on PATH."* ]]
[[ "$output" == *"Install GitHub CLI and retry."* ]]
}
@test "base_gh_auth_status_diagnostics reports bounded auth output and hint" {
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "auth" && "$2" == "status" ]]; then
printf 'auth failed\n' >&2
printf 'run login\n' >&2
exit 4
fi
exit 0
EOF
capture_command base_gh_auth_status_diagnostics "Run a custom login command."
[ "$status" -eq 1 ]
[[ "$output" == *"gh auth status: auth failed"* ]]
[[ "$output" == *"gh auth status: run login"* ]]
[[ "$output" == *"Run a custom login command."* ]]
}
@test "base_gh_run passes through successful gh output" {
create_fake_gh <<'EOF'
#!/usr/bin/env bash
printf 'gh args:'
printf ' <%s>' "$@"
printf '\n'
EOF
capture_command base_gh_run issue list --repo owner/repo
[ "$status" -eq 0 ]
[[ "$output" == *"gh args: <issue> <list> <--repo> <owner/repo>"* ]]
}
@test "base_gh_run reports command failure and auth diagnostics" {
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "auth" && "$2" == "status" ]]; then
printf 'not logged in\n' >&2
exit 1
fi
printf 'command failed\n' >&2
exit 7
EOF
capture_command base_gh_run issue create --title Example
[ "$status" -eq 7 ]
[[ "$output" == *"command failed"* ]]
[[ "$output" == *"GitHub command failed: gh issue create --title Example"* ]]
[[ "$output" == *"gh auth status: not logged in"* ]]
[[ "$output" == *"Run 'gh auth login -h github.com' and retry."* ]]
}
@test "base_gh_run sensitive diagnostics hide every argv form and nested auth output from all log sinks" {
local secret="gh-run-canary with spaces"
local primary_log="$TEST_TMPDIR/gh-run-sensitive.log"
local received_args="$TEST_TMPDIR/gh-run-sensitive.args"
local primary_content
export GH_TEST_SECRET="$secret"
BASE_BASH_LIBS_PRIMARY_LOG="$primary_log"
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "${1-}" == "auth" && "${2-}" == "status" ]]; then
printf 'auth diagnostic exposed %s\n' "${GH_TEST_SECRET:?}" >&2
exit 4
fi
printf '<%s>\n' "$@" > "${TEST_TMPDIR:?}/gh-run-sensitive.args"
exit 73
EOF
capture_command base_gh_run --sensitive --safe-display "create protected issue" -- \
api graphql \
"spaced value $secret" \
"--option=$secret" \
--header "Authorization: Bearer $secret" \
"https://user:$secret@github.example.test/resource" \
--field "token=$secret"
[ "$status" -eq 73 ]
[[ "$output" == *"create protected issue [sensitive GitHub operation; arguments hidden]"* ]]
[[ "$output" == *"GitHub command failed:"* ]]
[[ "$output" == *"(exit 73)"* ]]
[[ "$output" == *"raw auth diagnostics hidden"* ]]
[[ "$output" == *"Run 'gh auth login -h github.com' and retry."* ]]
[[ "$output" != *"$secret"* ]]
[[ "$(cat "$received_args")" == *"$secret"* ]]
primary_content="$(cat "$primary_log")"
[[ "$primary_content" == *"create protected issue"* ]]
[[ "$primary_content" == *"(exit 73)"* ]]
[[ "$primary_content" == *"raw auth diagnostics hidden"* ]]
[[ "$primary_content" != *"$secret"* ]]
}
@test "base_gh_run locks sensitive diagnostics against a dynamically scoped gh function" {
local secret="dynamic-scope-gh-canary"
gh() {
if [[ "${1-}" == "auth" && "${2-}" == "status" ]]; then
printf 'auth diagnostic exposed %s\n' "$secret" >&2
return 4
fi
__base_bash_libs_gh_run_sensitive=0
__base_bash_libs_gh_run_safe_display=""
return 67
}
capture_command base_gh_run --sensitive --safe-display "protected function call" -- \
api graphql --header "Authorization: Bearer $secret"
unset -f gh
[ "$status" -eq 67 ]
[[ "$output" == *"protected function call [sensitive GitHub operation; arguments hidden]"* ]]
[[ "$output" == *"(exit 67)"* ]]
[[ "$output" == *"raw auth diagnostics hidden"* ]]
[[ "$output" != *"$secret"* ]]
}
@test "base_gh_report_command_failure accepts control-first sensitive reporting through status 255" {
local secret="public-reporter-canary"
export GH_TEST_SECRET="$secret"
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "${1-}" == "auth" && "${2-}" == "status" ]]; then
printf 'auth diagnostic exposed %s\n' "${GH_TEST_SECRET:?}" >&2
exit 4
fi
exit 99
EOF
capture_command base_gh_report_command_failure \
--sensitive --safe-display "publish protected release" -- \
255 api repos/owner/repo --header "Authorization: Bearer $secret"
[ "$status" -eq 255 ]
[[ "$output" == *"publish protected release [sensitive GitHub operation; arguments hidden]"* ]]
[[ "$output" == *"(exit 255)"* ]]
[[ "$output" == *"raw auth diagnostics hidden"* ]]
[[ "$output" != *"$secret"* ]]
[[ "$output" != *"--header"* ]]
}
@test "base_gh_run reports command failure under set -e" {
local script="$TEST_TMPDIR/gh-run-set-e.sh"
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "auth" && "$2" == "status" ]]; then
printf 'not logged in\n' >&2
exit 1
fi
printf 'command failed\n' >&2
exit 7
EOF
cat > "$script" <<EOF
#!/usr/bin/env bash
set -euo pipefail
source "$BASE_BASH_DIR/std/lib_std.sh"
declare -a app_args=()
base_init app_args --source "\${BASH_SOURCE[0]}" -- "\$@"
source "$BASE_BASH_DIR/gh/lib_gh.sh"
PATH="$TEST_TMPDIR/bin:$BASE_TEST_ORIG_PATH"
base_gh_run issue create --title Example
printf 'after\n'
EOF
chmod +x "$script"
bats_run bash "$script"
[ "$status" -eq 7 ]
[[ "$output" == *"command failed"* ]]
[[ "$output" == *"GitHub command failed: gh issue create --title Example"* ]]
[[ "$output" == *"gh auth status: not logged in"* ]]
[[ "$output" != *"after"* ]]
}
@test "base_gh_run quotes arguments when reporting command failure" {
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "auth" && "$2" == "status" ]]; then
exit 0
fi
exit 7
EOF
bats_run base_gh_run issue create --title "Example Title" --body "body value"
[ "$status" -eq 7 ]
[[ "$output" == *"GitHub command failed: gh issue create --title Example\\ Title --body body\\ value"* ]]
[[ "$output" == *"(exit 7)"* ]]
[[ "$output" != *"GitHub command failed: gh issue create --title Example Title --body body value"* ]]
}
@test "base_gh_run returns 1 with an error when gh is not on PATH" {
mkdir -p "$TEST_TMPDIR/no-gh-bin"
bats_run "$BASH" -c '
source "$1"
declare -a app_args=()
base_init app_args --
source "$2"
PATH="$3"
base_gh_run issue list
' bash "$BASE_BASH_DIR/std/lib_std.sh" "$BASE_BASH_DIR/gh/lib_gh.sh" "$TEST_TMPDIR/no-gh-bin"
[ "$status" -eq 1 ]
[[ "$output" == *"Required command 'gh' was not found on PATH."* ]]
[[ "$output" != *"GitHub command failed"* ]]
[[ "$output" != *"gh auth status"* ]]
}
@test "base_gh_repo_from_remote_url parses supported GitHub remotes" {
local repo
base_gh_repo_from_remote_url "git@github.com:owner/repo.git" repo
[ "$repo" = "owner/repo" ]
base_gh_repo_from_remote_url "git@github.com:owner/repo" repo
[ "$repo" = "owner/repo" ]
base_gh_repo_from_remote_url "ssh://git@github.com/owner/repo.git" repo
[ "$repo" = "owner/repo" ]
base_gh_repo_from_remote_url "https://github.com/owner/repo.git" repo
[ "$repo" = "owner/repo" ]
base_gh_repo_from_remote_url "https://github.com/owner/repo" repo
[ "$repo" = "owner/repo" ]
}
@test "base_gh_repo_from_remote_url supports shadowing-prone output variable names" {
local result_var=""
local parsed_repo=""
base_gh_repo_from_remote_url "https://github.com/owner/repo.git" result_var
base_gh_repo_from_remote_url "git@github.com:other/project.git" parsed_repo
[ "$result_var" = "owner/repo" ]
[ "$parsed_repo" = "other/project" ]
}
@test "base_gh_repo_from_remote_url rejects non-GitHub and malformed remotes" {
local repo="sentinel"
bats_run base_gh_repo_from_remote_url "https://example.com/owner/repo.git" repo
[ "$status" -eq 2 ]
[ "$repo" = "sentinel" ]
bats_run base_gh_repo_from_remote_url "https://github.com/owner" repo
[ "$status" -eq 2 ]
[ "$repo" = "sentinel" ]
bats_run base_gh_repo_from_remote_url "ssh://git@github.com//repo.git" repo
[ "$status" -eq 2 ]
[ "$repo" = "sentinel" ]
bats_run base_gh_repo_from_remote_url "https://github.com/owner/repo?query=1" repo
[ "$status" -eq 2 ]
[ "$repo" = "sentinel" ]
}
@test "base_gh_infer_repo_from_origin reads origin through git -C" {
local repo_dir="$TEST_TMPDIR/repo"
local repo=""
init_git_repo "$repo_dir"
git -C "$repo_dir" remote add origin "git@github.com:owner/repo.git"
base_gh_infer_repo_from_origin "$repo_dir" repo
[ "$repo" = "owner/repo" ]
}
@test "base_gh_infer_repo_from_origin supports inferred_repo as the result variable name" {
local repo_dir="$TEST_TMPDIR/repo"
local inferred_repo=""
init_git_repo "$repo_dir"
git -C "$repo_dir" remote add origin "git@github.com:owner/repo.git"
base_gh_infer_repo_from_origin "$repo_dir" inferred_repo
[ "$inferred_repo" = "owner/repo" ]
}
@test "base_gh_infer_repo_from_origin supports remote_url as the result variable name" {
local repo_dir="$TEST_TMPDIR/repo"
local remote_url=""
init_git_repo "$repo_dir"
git -C "$repo_dir" remote add origin "git@github.com:owner/repo.git"
base_gh_infer_repo_from_origin "$repo_dir" remote_url
[ "$remote_url" = "owner/repo" ]
}
@test "base_gh_infer_repo_from_origin supports its former internal parsed name as the result variable" {
local repo_dir="$TEST_TMPDIR/repo"
local gh_infer_parsed_repo=""
init_git_repo "$repo_dir"
git -C "$repo_dir" remote add origin "git@github.com:owner/repo.git"
base_gh_infer_repo_from_origin "$repo_dir" gh_infer_parsed_repo
[ "$gh_infer_parsed_repo" = "owner/repo" ]
}
@test "base_gh_infer_repo_from_origin returns empty success for non-GitHub remotes when optional" {
local repo_dir="$TEST_TMPDIR/repo"
local repo="sentinel"
init_git_repo "$repo_dir"
git -C "$repo_dir" remote add origin "https://example.com/owner/repo.git"
base_gh_infer_repo_from_origin "$repo_dir" repo --optional
[ "$repo" = "" ]
}
@test "base_gh_infer_repo_from_origin logs non-optional inference failures" {
local repo_dir="$TEST_TMPDIR/repo"
local repo="sentinel"
init_git_repo "$repo_dir"
git -C "$repo_dir" remote add origin "https://example.com/owner/repo.git"
bats_run base_gh_infer_repo_from_origin "$repo_dir" repo
[ "$status" -eq 1 ]
[ "$repo" = "sentinel" ]
[[ "$output" == *"Could not infer GitHub repository from '$repo_dir' origin remote."* ]]
}
@test "base_gh_repo_default_branch reads GitHub repository default branch" {
local branch=""
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "repo" && "$2" == "view" ]]; then
printf 'develop\n'
exit 0
fi
exit 99
EOF
base_gh_repo_default_branch "owner/repo" branch
[ "$branch" = "develop" ]
}
@test "base_gh_repo_default_branch supports default_branch as the result variable name" {
local default_branch=""
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "repo" && "$2" == "view" ]]; then
printf 'develop\n'
exit 0
fi
exit 99
EOF
base_gh_repo_default_branch "owner/repo" default_branch
[ "$default_branch" = "develop" ]
}
@test "base_gh_repo_default_branch supports remote_default_branch as the result variable name" {
local remote_default_branch=""
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "repo" && "$2" == "view" ]]; then
printf 'develop\n'
exit 0
fi
exit 99
EOF
base_gh_repo_default_branch "owner/repo" remote_default_branch
[ "$remote_default_branch" = "develop" ]
}
@test "base_gh_repo_default_branch supports status as the result variable name" {
local status=""
create_fake_gh <<'EOF'
#!/usr/bin/env bash
if [[ "$1" == "repo" && "$2" == "view" ]]; then
printf 'develop\n'
exit 0
fi
exit 99
EOF
base_gh_repo_default_branch "owner/repo" status
[ "$status" = "develop" ]
}
@test "base_gh_api_with_retry rejects malformed controls before execution without echoing values" {
local secret="retry-control-canary"
local invocation_file="$TEST_TMPDIR/gh-api-control-invoked"
gh() {
printf 'invoked\n' > "$invocation_file"
}
capture_command base_gh_api_with_retry --retry-policy "$secret" -- repos/owner/repo
[ "$status" -eq 2 ]
[[ "$output" != *"$secret"* ]]
capture_command base_gh_api_with_retry --max-attempts 0 -- repos/owner/repo
[ "$status" -eq 2 ]
capture_command base_gh_api_with_retry --max-attempts 2 --max-attempts 3 -- repos/owner/repo
[ "$status" -eq 2 ]
capture_command base_gh_api_with_retry --base-delay-seconds 5 --max-delay-seconds 4 -- repos/owner/repo
[ "$status" -eq 2 ]
capture_command base_gh_api_with_retry --attempt-timeout-seconds 601 -- repos/owner/repo
[ "$status" -eq 2 ]
capture_command base_gh_api_with_retry --max-elapsed-seconds 3601 -- repos/owner/repo
[ "$status" -eq 2 ]
capture_command base_gh_api_with_retry --retry-policy read-only repos/owner/repo
[ "$status" -eq 2 ]
capture_command base_gh_api_with_retry --safe-display "safe operation" -- repos/owner/repo
[ "$status" -eq 2 ]
[ ! -e "$invocation_file" ]
}
@test "base_gh_api_with_retry ignores retired retry environment variables" {
install_gh_api_retry_fixture
export BASE_GH_API_MAX_ATTEMPTS=1
export BASE_GH_API_RETRY_DELAY_SECONDS=59
capture_command base_gh_api_with_retry repos/owner/repo
[ "$status" -eq 0 ]
[ "$(gh_api_retry_observed attempts)" -eq 2 ]
[ "$(gh_api_retry_observed first-arg)" = "--include" ]
[ "$(gh_api_retry_observed sleeps)" = "1" ]
[[ "$output" == *"ok"* ]]
}
@test "base_gh_api_with_retry does not collide with unrelated readonly caller names" {
local -r gh_api_capture_workspace="caller-workspace"
local -r gh_api_hook_result="caller-hook"
local -r gh_api_metadata_epoch="caller-epoch"
install_gh_api_retry_fixture
TEST_GH_API_FAILURE_STDOUT=$'HTTP/2.0 429 Too Many Requests\r\nX-RateLimit-Remaining: 0\r\nX-RateLimit-Reset: 2000000001\r\n\r\nbody\n'
TEST_GH_API_FAILURE_STDERR=""
capture_command base_gh_api_with_retry --max-attempts 2 \
--max-elapsed-seconds 30 --max-delay-seconds 10 -- repos/owner/repo
[ "$status" -eq 0 ]
[ "$(gh_api_retry_observed attempts)" -eq 2 ]
[ "$gh_api_capture_workspace" = "caller-workspace" ]
[ "$gh_api_hook_result" = "caller-hook" ]
[ "$gh_api_metadata_epoch" = "caller-epoch" ]
}
@test "GitHub API argv classification matches gh pflag value and cluster forms" {
local method include stdin_backed file_backed graphql ambiguous
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
repos/owner/repo -iXGET
[ "$method" = "GET" ]
[ "$include" -eq 1 ]
[ "$ambiguous" -eq 0 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
repos/owner/repo --allow-escape-sequences
[ "$method" = "GET" ]
[ "$ambiguous" -eq 0 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
repos/owner/repo --include=TRUE --paginate=0
[ "$include" -eq 1 ]
[ "$ambiguous" -eq 0 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
-XiGET repos/owner/repo
[ "$method" = "IGET" ]
[ "$include" -eq 0 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
repos/owner/repo -ifkey=value
[ "$method" = "POST" ]
[ "$include" -eq 1 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
-fi repos/owner/repo
[ "$method" = "POST" ]
[ "$include" -eq 0 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
--method --include repos/owner/repo
[ "$method" = "--INCLUDE" ]
[ "$include" -eq 0 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
--input --include repos/owner/repo
[ "$method" = "POST" ]
[ "$include" -eq 0 ]
[ "$file_backed" -eq 1 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
repos/owner/repo --method GET -XGET
[ "$method" = "UNKNOWN" ]
[ "$ambiguous" -eq 1 ]
__base_bash_libs_gh_api_classify_argv__ method include stdin_backed file_backed graphql ambiguous \
graphql --raw-field query=@-
[ "$graphql" -eq 1 ]
[ "$stdin_backed" -eq 0 ]
}
@test "base_gh_api_with_retry defaults to reads and requires replay-safe attestation for mutations and files" {
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry repos/owner/repo
[ "$status" -eq 0 ]
[ "$(gh_api_retry_observed attempts)" -eq 2 ]
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry repos/owner/repo --method POST
[ "$status" -eq 1 ]
[ "$(gh_api_retry_observed attempts)" -eq 1 ]
[[ "$output" == *"outcome may be unknown"* ]]
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry repos/owner/repo -f key=value
[ "$status" -eq 1 ]
[ "$(gh_api_retry_observed attempts)" -eq 1 ]
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry graphql
[ "$status" -eq 1 ]
[ "$(gh_api_retry_observed attempts)" -eq 1 ]
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry --retry-policy never -- repos/owner/repo
[ "$status" -eq 1 ]
[ "$(gh_api_retry_observed attempts)" -eq 1 ]
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry repos/owner/repo --future-gh-flag
[ "$status" -eq 1 ]
[ "$(gh_api_retry_observed attempts)" -eq 1 ]
install_gh_api_retry_fixture
capture_command base_gh_api_with_retry repos/owner/repo --input "$TEST_TMPDIR/stable.json"