-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_std.bats
More file actions
4870 lines (3983 loc) · 145 KB
/
Copy pathlib_std.bats
File metadata and controls
4870 lines (3983 loc) · 145 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
readonly STDLIB_PATH="$BASE_BASH_DIR/std/lib_std.sh"
create_script() {
local script_path="$1"
local source_line init_lines content
shift
content="$(cat)"
source_line="source \"$STDLIB_PATH\""
if [[ "$content" == *"$source_line"* && "$content" != *"base-bash-libs: passive-source"* ]]; then
init_lines=$'declare -a base_bash_libs_test_args=()\nbase_init base_bash_libs_test_args -- "$@"\nset -- "${base_bash_libs_test_args[@]}"'
content="${content/"$source_line"/"$source_line"$'\n'"$init_lines"}"
fi
printf '%s\n' "$content" > "$script_path"
chmod +x "$script_path"
}
file_mode() {
if stat -c '%a' "$1" >/dev/null 2>&1; then
stat -c '%a' "$1"
else
stat -f '%Lp' "$1"
fi
}
normalize_tty_output() {
local text="$1"
text="${text//$'\r'/}"
text="${text//$'\b'/}"
printf '%s' "$text"
}
run_tty_script() {
local script_path="$1"
local command
shift
command -v script >/dev/null 2>&1 || skip "The 'script' command is required for tty tests."
if script --version >/dev/null 2>&1; then
printf -v command '%q ' "$script_path" "$@"
bats_run script -q -e -c "${command% }" /dev/null
else
bats_run script -q /dev/null "$script_path" "$@"
fi
}
run_pty_command() {
local input="$1"
local driver="$TEST_TMPDIR/pty-driver.py"
shift
cat > "$driver" <<'PY'
import errno
import os
import pty
import select
import signal
import sys
import time
input_bytes = sys.argv[1].encode()
command = sys.argv[2:]
pid, fd = pty.fork()
if pid == 0:
os.execvp(command[0], command)
os.write(fd, input_bytes)
output = bytearray()
status = None
deadline = time.monotonic() + 10
while True:
if time.monotonic() > deadline:
try:
os.kill(pid, signal.SIGKILL)
except ProcessLookupError:
pass
status = 124
break
readable, _, _ = select.select([fd], [], [], 0.05)
if readable:
try:
chunk = os.read(fd, 4096)
except OSError as exc:
if exc.errno == errno.EIO:
chunk = b""
else:
raise
if chunk:
output.extend(chunk)
waited, child_status = os.waitpid(pid, os.WNOHANG)
if waited:
if os.WIFEXITED(child_status):
status = os.WEXITSTATUS(child_status)
elif os.WIFSIGNALED(child_status):
status = 128 + os.WTERMSIG(child_status)
else:
status = 1
while True:
readable, _, _ = select.select([fd], [], [], 0)
if not readable:
break
try:
chunk = os.read(fd, 4096)
except OSError as exc:
if exc.errno == errno.EIO:
break
raise
if not chunk:
break
output.extend(chunk)
break
sys.stdout.buffer.write(output)
sys.exit(status if status is not None else 1)
PY
bats_run python3 "$driver" "$input" "$@"
}
run_pty_signal_command() {
local signal_name="$1" main_pid_file="$2" ready_file="$3"
local driver="$TEST_TMPDIR/pty-signal-driver.py"
shift 3
cat > "$driver" <<'PY'
import errno
import os
import pty
import select
import signal
import sys
import time
signal_name = sys.argv[1]
main_pid_file = sys.argv[2]
ready_file = sys.argv[3]
command = sys.argv[4:]
pid, fd = pty.fork()
if pid == 0:
os.execvp(command[0], command)
deadline = time.monotonic() + 10
while not os.path.exists(ready_file):
if time.monotonic() > deadline:
try:
os.kill(pid, signal.SIGKILL)
except ProcessLookupError:
pass
sys.exit(124)
time.sleep(0.002)
with open(main_pid_file, encoding="ascii") as stream:
main_pid = int(stream.read())
if signal_name == "CTRL_C":
os.write(fd, b"\x03")
else:
os.kill(main_pid, getattr(signal, "SIG" + signal_name))
output = bytearray()
status = None
deadline = time.monotonic() + 10
while status is None:
if time.monotonic() > deadline:
try:
os.kill(pid, signal.SIGKILL)
except ProcessLookupError:
pass
status = 124
break
readable, _, _ = select.select([fd], [], [], 0.05)
if readable:
try:
chunk = os.read(fd, 4096)
except OSError as exc:
if exc.errno == errno.EIO:
chunk = b""
else:
raise
if chunk:
output.extend(chunk)
waited, child_status = os.waitpid(pid, os.WNOHANG)
if waited:
if os.WIFEXITED(child_status):
status = os.WEXITSTATUS(child_status)
elif os.WIFSIGNALED(child_status):
status = 128 + os.WTERMSIG(child_status)
else:
status = 1
while True:
readable, _, _ = select.select([fd], [], [], 0)
if not readable:
break
try:
chunk = os.read(fd, 4096)
except OSError as exc:
if exc.errno == errno.EIO:
break
raise
if not chunk:
break
output.extend(chunk)
os.close(fd)
sys.stdout.buffer.write(output)
sys.exit(status if status is not None else 1)
PY
bats_run python3 "$driver" "$signal_name" "$main_pid_file" \
"$ready_file" "$@"
}
create_timeout_signal_disposition_script() {
local script_path="$1"
create_script "$script_path" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
disposition="\$1"
main_pid_file="\$2"
ready_file="\$3"
marker_file="\$4"
child_pid_file="\$5"
printf '%s\n' "\$BASHPID" >| "\$main_pid_file"
case "\$disposition" in
custom)
trap 'printf "handled\\n" >| "\$marker_file"' TERM
;;
ignored)
trap '' TERM
;;
*)
exit 64
;;
esac
before_trap="\$(trap -p TERM)"
signal_worker() {
if [[ "\$disposition" == custom ]]; then
trap '' TERM
(trap '' TERM; while :; do /bin/sleep 1; done) &
printf '%s\n' "\$!" >| "\$child_pid_file"
: >| "\$ready_file"
wait
else
: >| "\$ready_file"
/bin/sleep 0.2
return 42
fi
}
if __base_bash_libs_std_run_with_timeout_fallback__ 30 signal_worker; then
rc=0
else
rc=\$?
fi
after_trap="\$(trap -p TERM)"
if [[ "\$before_trap" == "\$after_trap" ]]; then
state=preserved
else
state=changed
fi
printf 'rc=%s state=%s\n' "\$rc" "\$state"
EOF
}
setup() {
setup_test_tmpdir
PATH="$BASE_TEST_ORIG_PATH"
unset BASE_BASH_LIBS_DRY_RUN BASE_BASH_LIBS_DRY_RUN BASE_BASH_LIBS_LOG_DEBUG BASE_BASH_LIBS_LOG_UTC NO_COLOR BASE_BASH_LIBS_BOOTSTRAP_SOURCE BASE_BASH_LIBS_PRIMARY_LOG
source "$STDLIB_PATH"
declare -a setup_args=()
base_init setup_args -- "$@"
}
teardown() {
PATH="$BASE_TEST_ORIG_PATH"
}
@test "sourcing stdlib is passive and preserves caller state" {
bats_run bash -c '
set -euo pipefail
stdlib_path="$1"
set -- "argument with spaces" "" "literal-*"
IFS="| "
OPTIND=7
umask 027
shopt -s extglob nullglob nocasematch
trap ":" EXIT HUP INT TERM
before_args=$(printf "%s\\n" "$#" "${1-}" "${2-}" "${3-}")
before_set=$(set +o)
before_shopt=$(shopt -p)
before_traps=$(trap -p)
before_exports=$(export -p)
before_cwd=$(pwd -P)
source "$stdlib_path"
after_args=$(printf "%s\\n" "$#" "${1-}" "${2-}" "${3-}")
[[ "$before_args" == "$after_args" ]]
[[ "$before_set" == "$(set +o)" ]]
[[ "$before_shopt" == "$(shopt -p)" ]]
[[ "$before_traps" == "$(trap -p)" ]]
[[ "$before_exports" == "$(export -p)" ]]
[[ "$before_cwd" == "$(pwd -P)" ]]
[[ ! -v BASE_BASH_LIBS_SCRIPT_ARGS && ! -v BASE_BASH_LIBS_SCRIPT_DIR ]]
[[ ! -v BASE_BASH_LIBS_STD_LOG_LEVELS && ! -v __base_bash_libs_std_cleanup_hooks ]]
printf "passive=yes\\nargs=%s\\n" "$#"
' bash "$STDLIB_PATH"
[ "$status" -eq 0 ]
[[ "$output" == *"passive=yes"* ]]
[[ "$output" == *"args=3"* ]]
[[ "$output" != *"unbound variable"* ]]
}
@test "explicit init filters args, publishes context, and is idempotent" {
local script="$TEST_TMPDIR/explicit-init.sh"
local expected_dir
create_script "$script" <<EOF
#!/usr/bin/env bash
# base-bash-libs: passive-source
source "$STDLIB_PATH"
set -- --debug-wrapper alpha --color -- --debug-wrapper omega
before="\$*"
declare -a filtered=()
base_init filtered --source "\$0" -- "\$@"
[[ "\$*" == "\$before" ]]
printf 'filtered=%s\n' "\${filtered[*]}"
printf 'original=%s\n' "\${BASE_BASH_LIBS_SCRIPT_ARGS[*]}"
printf 'source-dir=%s\n' "\$BASE_BASH_LIBS_SCRIPT_DIR"
if base_init filtered --source "\$0" -- "\$@"; then
printf 'repeat=same\n'
else
exit 41
fi
if base_init filtered --source "\$0" -- changed; then
exit 42
else
printf 'different=diagnosed\n'
fi
EOF
expected_dir="$(cd "$(dirname "$script")" && pwd -P)"
bats_run bash "$script"
[ "$status" -eq 0 ]
[[ "$output" == *"filtered=alpha -- --debug-wrapper omega"* ]]
[[ "$output" == *"original=--debug-wrapper alpha --color -- --debug-wrapper omega"* ]]
[[ "$output" == *"source-dir=$expected_dir"* ]]
[[ "$output" == *"repeat=same"* ]]
[[ "$output" == *"different=diagnosed"* ]]
[[ "$output" != *"unbound variable"* ]]
}
@test "stdlib source guard rejects caller-owned incompatible metadata" {
bats_run bash -c '
BASE_BASH_LIBS_STD_SOURCE_GUARD=1
BASE_BASH_LIBS_STD_SOURCE_VERSION=0.0.0
source "$1"
' bash "$STDLIB_PATH"
[ "$status" -eq 1 ]
[[ "$output" == *"incompatible base-bash-libs stdlib versions"* ]]
}
@test "deprecated --verbose-wrapper preserves original args and enables VERBOSE compatibility" {
local script="$TEST_TMPDIR/check-init.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
printf 'orig=%s\n' "\${BASE_BASH_LIBS_SCRIPT_ARGS[*]}"
printf 'argv=%s\n' "\$*"
printf 'debug=%s\n' "\${BASE_BASH_LIBS_LOG_DEBUG:-}"
printf 'utc=%s\n' "\${BASE_BASH_LIBS_LOG_UTC:-}"
printf 'color=%s\n' "\${BASE_BASH_LIBS_STD_COLOR_RED:-}"
printf 'terminal-level=%s\n' "\${BASE_BASH_LIBS_STD_LOGGER_LEVELS[default]}"
printf 'library-level=%s\n' "\${BASE_BASH_LIBS_STD_LOG_CATEGORY_LEVELS[base_bash_libs]}"
EOF
bats_run bash "$script" --verbose-wrapper --utc-wrapper --color alpha beta
[ "$status" -eq 0 ]
[[ "$output" == *"orig=--verbose-wrapper --utc-wrapper --color alpha beta"* ]]
[[ "$output" == *"argv=alpha beta"* ]]
[[ "$output" == *"debug=1"* ]]
[[ "$output" == *"utc=1"* ]]
[[ "$output" == *"color="* ]]
[[ "$output" == *"terminal-level=5"* ]]
[[ "$output" == *"library-level=5"* ]]
[[ "$output" != *"deprecated"* ]]
[[ "$output" != *"Command line:"* ]]
}
@test "--debug-wrapper enables caller and reusable-library DEBUG" {
local script="$TEST_TMPDIR/check-debug-init.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
printf 'orig=%s\n' "\${BASE_BASH_LIBS_SCRIPT_ARGS[*]}"
printf 'argv=%s\n' "\$*"
printf 'debug=%s\n' "\${BASE_BASH_LIBS_LOG_DEBUG:-}"
printf 'terminal-level=%s\n' "\${BASE_BASH_LIBS_STD_LOGGER_LEVELS[default]}"
printf 'library-level=%s\n' "\${BASE_BASH_LIBS_STD_LOG_CATEGORY_LEVELS[base_bash_libs]}"
EOF
bats_run bash "$script" --debug-wrapper alpha beta
[ "$status" -eq 0 ]
[[ "$output" == *"orig=--debug-wrapper alpha beta"* ]]
[[ "$output" == *"argv=alpha beta"* ]]
[[ "$output" == *"debug=1"* ]]
[[ "$output" == *"terminal-level=4"* ]]
[[ "$output" == *"library-level=4"* ]]
}
@test "stdlib never logs process arguments automatically" {
local script="$TEST_TMPDIR/check-argv-logging.sh"
local primary_log="$TEST_TMPDIR/check-argv-logging.log"
local primary_content secret
local separate_secret="separate-secret-191"
local inline_secret="inline-secret-191"
local positional_secret="positional-secret-191"
local url_secret="url-secret-191"
create_script "$script" <<EOF
#!/usr/bin/env bash
export BASE_BASH_LIBS_PRIMARY_LOG="$primary_log"
source "$STDLIB_PATH"
base_std_log_debug -l base_bash_libs.std "safe explicit diagnostic"
printf 'argv-count=%s\n' "\$#"
EOF
bats_run bash "$script" \
--debug-wrapper \
--api-token "$separate_secret" \
"--token=$inline_secret" \
"$positional_secret" \
"https://user:$url_secret@example.invalid/path"
[ "$status" -eq 0 ]
[[ "$output" == *"safe explicit diagnostic"* ]]
[[ "$output" == *"argv-count=5"* ]]
[[ "$output" != *"Command line:"* ]]
primary_content="$(cat "$primary_log")"
[[ "$primary_content" == *"safe explicit diagnostic"* ]]
[[ "$primary_content" != *"Command line:"* ]]
for secret in \
"$separate_secret" \
"$inline_secret" \
"$positional_secret" \
"$url_secret"; do
[[ "$output" != *"$secret"* ]]
[[ "$primary_content" != *"$secret"* ]]
done
}
@test "sourcing stdlib stops filtering wrapper flags after --" {
local script="$TEST_TMPDIR/check-init-escape.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
printf 'argv=%s\n' "\$*"
EOF
bats_run bash "$script" --color alpha -- --color omega
[ "$status" -eq 0 ]
[[ "$output" == *"argv=alpha -- --color omega"* ]]
}
@test "bootstrap source override controls BASE_BASH_LIBS_SCRIPT_DIR" {
local command_dir="$TEST_TMPDIR/commands/demo"
local script="$TEST_TMPDIR/bootstrap-dir.sh"
local expected_dir
mkdir -p "$command_dir"
create_script "$script" <<EOF
#!/usr/bin/env bash
export BASE_BASH_LIBS_BOOTSTRAP_SOURCE="$command_dir/demo.sh"
source "$STDLIB_PATH"
printf 'script_dir=%s\n' "\$BASE_BASH_LIBS_SCRIPT_DIR"
EOF
expected_dir="$(cd "$command_dir" && pwd -P)"
bats_run bash "$script"
[ "$status" -eq 0 ]
[[ "$output" == *"script_dir=$expected_dir"* ]]
}
@test "stdlib supports a top-level strict shell without an outer BASH_SOURCE frame" {
local expected_dir
expected_dir="$(cd "$TEST_TMPDIR" && pwd -P)"
bats_run bash -c '
set -euo pipefail
cd -- "$2"
source "$1"
declare -a app_args=()
base_init app_args -- "$@"
source_dir=""
base_std_get_my_source_dir source_dir
[[ "$-" == *e* && "$-" == *u* ]]
shopt -qo pipefail
printf "script_dir=%s\nsource_dir=%s\nstrict=preserved\n" "$BASE_BASH_LIBS_SCRIPT_DIR" "$source_dir"
' bash "$STDLIB_PATH" "$TEST_TMPDIR"
[ "$status" -eq 0 ]
[[ "$output" == *"script_dir=$expected_dir"* ]]
[[ "$output" == *"source_dir=$expected_dir"* ]]
[[ "$output" == *"strict=preserved"* ]]
[[ "$output" != *"unbound variable"* ]]
}
@test "stdlib handles empty arrays in a strict child with no positional arguments" {
local script="$TEST_TMPDIR/strict-empty-arrays.sh"
local directory="$TEST_TMPDIR/strict-directory"
create_script "$script" <<EOF
#!/usr/bin/env bash
set -euo pipefail
source "$STDLIB_PATH"
base_std_safe_mkdir "$directory"
temp_path=""
TMPDIR="$TEST_TMPDIR" base_std_make_temp_file temp_path strict-empty
base_std_unregister_cleanup_path "\$temp_path"
rm -f -- "\$temp_path"
printf 'args=%s strict=preserved\n' "\$#"
EOF
bats_run bash "$script"
[ "$status" -eq 0 ]
[ -d "$directory" ]
[[ "$output" == *"args=0 strict=preserved"* ]]
[[ "$output" != *"unbound variable"* ]]
}
@test "stdlib exposes readonly package version from root VERSION" {
local expected_version
IFS= read -r expected_version < "$BASE_REPO_ROOT/VERSION"
[ "${BASE_BASH_LIBS_VERSION:-}" = "$expected_version" ]
readonly -p BASE_BASH_LIBS_VERSION >/dev/null
}
@test "base_require_version accepts the loaded version and older versions" {
local stdout_file="$TEST_TMPDIR/version-check.out"
base_require_version "$BASE_BASH_LIBS_VERSION" >"$stdout_file"
base_require_version "0.1.0" >>"$stdout_file"
[ ! -s "$stdout_file" ]
}
@test "base_require_version returns status 1 when the loaded version is too old" {
local script="$TEST_TMPDIR/version-too-old.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
base_require_version "999.0.0"
EOF
bats_run bash "$script"
[ "$status" -eq 1 ]
[[ "$output" == *"base-bash-libs 999.0.0 or newer is required"* ]]
[[ "$output" == *"loaded version is $BASE_BASH_LIBS_VERSION"* ]]
}
@test "base_require_version returns status 2 for invalid version strings" {
local script="$TEST_TMPDIR/version-invalid.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
base_require_version "1.two.0"
EOF
bats_run bash "$script"
[ "$status" -eq 2 ]
[[ "$output" == *"base_require_version expects dotted numeric versions"* ]]
}
@test "stdlib exposes readonly loaded marker" {
[ "${BASE_BASH_LIBS_STDLIB_LOADED:-}" = "1" ]
readonly -p BASE_BASH_LIBS_STDLIB_LOADED >/dev/null
}
@test "base_std_is_interactive is false in a non-interactive subprocess" {
local script="$TEST_TMPDIR/non-interactive.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
if base_std_is_interactive; then
echo "interactive=yes"
else
echo "interactive=no"
fi
EOF
bats_run bash "$script" </dev/null
[ "$status" -eq 0 ]
[[ "$output" == *"interactive=no"* ]]
}
@test "base_std_is_interactive is true when run through a tty" {
local script="$TEST_TMPDIR/tty-interactive.sh"
local normalized
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
if base_std_is_interactive; then
echo "interactive=yes"
else
echo "interactive=no"
fi
EOF
run_tty_script "$script"
normalized="$(normalize_tty_output "$output")"
[ "$status" -eq 0 ]
[[ "$normalized" == *"interactive=yes"* ]]
}
@test "stdlib exposes passive bash version check helper" {
base_std_check_bash_version
[ "$?" -eq 0 ]
}
@test "stdlib passive bash version check requires Bash 4.2 or newer" {
local script="$TEST_TMPDIR/bash-version.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
BASE_TEST_BASH_VERSION=41 base_std_check_bash_version
EOF
bats_run "$script"
[ "$status" -eq 1 ]
[[ "$output" == *"requires Bash 4.2 or higher"* ]]
}
@test "stdlib passive bash version check rejects Bash 3.10 arithmetically" {
local script="$TEST_TMPDIR/bash-version-3-10.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
BASE_TEST_BASH_VERSION=310 base_std_check_bash_version
EOF
bats_run "$script"
[ "$status" -eq 1 ]
[[ "$output" == *"requires Bash 4.2 or higher"* ]]
}
@test "sourcing stdlib fails cleanly under unsupported /bin/bash" {
[[ -x /bin/bash ]] || skip "/bin/bash is not available."
if ! /bin/bash -c '((BASH_VERSINFO[0] < 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 2)))'; then
skip "/bin/bash is supported on this host."
fi
bats_run /bin/bash -c 'source "$1"; rc=$?; printf "source-rc=%s\n" "$rc"; exit "$rc"' bash "$STDLIB_PATH"
[ "$status" -eq 1 ]
[[ "$output" == *"requires Bash 4.2 or higher"* ]]
[[ "$output" == *"Your version"* ]]
[[ "$output" == *"source-rc=1"* ]]
[[ "$output" != *"syntax error"* ]]
}
@test "color initialization honors tty mode when --color is passed" {
local script="$TEST_TMPDIR/tty-colors.sh"
local normalized
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
if [[ -n "\${BASE_BASH_LIBS_STD_COLOR_RED:-}" ]]; then
echo "colors=enabled"
else
echo "colors=disabled"
fi
EOF
run_tty_script "$script" --color
normalized="$(normalize_tty_output "$output")"
[ "$status" -eq 0 ]
[[ "$normalized" == *"colors=enabled"* ]]
}
@test "color initialization uses stderr terminal for log colors" {
local script="$TEST_TMPDIR/stderr-colors.sh"
local stdout_file="$TEST_TMPDIR/stdout.txt"
local normalized
create_script "$script" <<EOF
#!/usr/bin/env bash
exec >"\$1"
source "$STDLIB_PATH"
if [[ -n "\${BASE_BASH_LIBS_STD_COLOR_RED:-}" ]]; then
printf 'colors=enabled\n' >&2
else
printf 'colors=disabled\n' >&2
fi
EOF
run_tty_script "$script" "$stdout_file" --color
normalized="$(normalize_tty_output "$output")"
[ "$status" -eq 0 ]
[[ "$normalized" == *"colors=enabled"* ]]
[ ! -s "$stdout_file" ]
}
@test "NO_COLOR disables explicit tty color output" {
local script="$TEST_TMPDIR/no-color.sh"
local normalized
create_script "$script" <<EOF
#!/usr/bin/env bash
export NO_COLOR=1
source "$STDLIB_PATH"
if [[ -n "\${BASE_BASH_LIBS_STD_COLOR_RED:-}" ]]; then
echo "colors=enabled"
else
echo "colors=disabled"
fi
EOF
run_tty_script "$script" --color
normalized="$(normalize_tty_output "$output")"
[ "$status" -eq 0 ]
[[ "$normalized" == *"colors=disabled"* ]]
}
@test "base_std_import loads package-relative libraries independent of cwd and is idempotent" {
local script="$TEST_TMPDIR/base_std_import-driver.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
base_std_import str/lib_str.sh
base_std_import str/lib_str.sh
value=' package-relative '
base_str_trim value
printf 'value=<%s> state=%s\n' "\$value" "\${__base_bash_libs_std_import_state[*]}"
EOF
bats_run bash -c "cd \"$TEST_TMPDIR\" && \"$script\""
[ "$status" -eq 0 ]
[[ "$output" == *"value=<package-relative>"* ]]
[[ "$output" == *"loaded"* ]]
}
@test "base_std_import returns a recoverable failure when a library is missing" {
local script="$TEST_TMPDIR/base_std_import-missing.sh"
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$STDLIB_PATH"
base_std_import missing.sh
printf 'after=%s\n' "\$?"
EOF
bats_run bash "$script"
[ "$status" -eq 0 ]
[[ "$output" == *"module 'missing.sh' does not exist"* ]]
[[ "$output" == *"after=1"* ]]
}
@test "base_std_import failure does not leave relative base_std_import directory on the stack" {
local package_dir="$TEST_TMPDIR/package"
local script_dir="$TEST_TMPDIR/driver"
local helper_dir="$package_dir/lib/bash/helpers"
local run_dir="$TEST_TMPDIR/run"
local script="$script_dir/base_std_import-failing-helper.sh"
local cwd_file="$TEST_TMPDIR/base_std_import-exit-pwd.txt"
local dirs_file="$TEST_TMPDIR/base_std_import-exit-dirs.txt"
mkdir -p "$helper_dir" "$run_dir" "$script_dir"
cp -R "$BASE_REPO_ROOT/lib/bash" "$package_dir/lib/"
cp "$BASE_REPO_ROOT/VERSION" "$package_dir/VERSION"
cat > "$helper_dir/failing.sh" <<EOF
trap 'pwd > "$cwd_file"; dirs -p > "$dirs_file"' EXIT
base_std_exit_if_error 7 "helper failed during base_std_import"
EOF
create_script "$script" <<EOF
#!/usr/bin/env bash
source "$package_dir/lib/bash/std/lib_std.sh"
base_std_import helpers/failing.sh
EOF
bats_run bash -c "cd \"$run_dir\" && \"$script\""
[ "$status" -eq 7 ]
[ "$(cat "$cwd_file")" = "$run_dir" ]
[ "$(head -n 1 "$dirs_file")" = "$run_dir" ]
[[ "$(cat "$dirs_file")" != *"$script_dir"* ]]
}
@test "base_std_import rejects absolute, traversal, and package-escaping symlink paths" {
local package_dir="$TEST_TMPDIR/safety package"
local outside="$TEST_TMPDIR/outside.sh"
local escaped="$package_dir/lib/bash/escape/lib_escape.sh"
mkdir -p "$package_dir/lib"
cp -R "$BASE_REPO_ROOT/lib/bash" "$package_dir/lib/"
cp "$BASE_REPO_ROOT/VERSION" "$package_dir/VERSION"
printf 'printf escaped\n' > "$outside"
mkdir -p "$(dirname "$escaped")"
ln -s "$outside" "$escaped"
run bash -c '
source "$1"
base_std_import "$2"; printf "absolute=%s\n" "$?"
base_std_import ../outside.sh; printf "traversal=%s\n" "$?"
base_std_import escape/lib_escape.sh; printf "escape=%s\n" "$?"
' bash "$package_dir/lib/bash/std/lib_std.sh" "$outside"
[ "$status" -eq 0 ]
[[ "$output" == *"absolute=2"* ]]
[[ "$output" == *"traversal=2"* ]]
[[ "$output" == *"escape=2"* ]]
}
@test "base_std_import promotes top-level module declarations to globals" {
local package_dir="$TEST_TMPDIR/scope package"
local module_dir="$package_dir/lib/bash/test"
local module="$module_dir/lib_scope.sh"
mkdir -p "$package_dir/lib" "$module_dir"
cp -R "$BASE_REPO_ROOT/lib/bash" "$package_dir/lib/"
cp "$BASE_REPO_ROOT/VERSION" "$package_dir/VERSION"
cat > "$module" <<'EOF'
declare -A BASE_BASH_LIBS_SCOPE_TEST=([state]=global)
EOF
run bash -c '
source "$1/lib/bash/std/lib_std.sh"
base_std_import test/lib_scope.sh
declare -p BASE_BASH_LIBS_SCOPE_TEST
' bash "$package_dir"
[ "$status" -eq 0 ]
[[ "$output" == *"declare -A BASE_BASH_LIBS_SCOPE_TEST"* ]]
}
@test "copy-only library artifacts report embedded release identity without VERSION" {
local artifact_dir="$TEST_TMPDIR/copy artifact"
local output
mkdir -p "$artifact_dir/lib"
cp -R "$BASE_REPO_ROOT/lib/bash" "$artifact_dir/lib/"
rm -f "$artifact_dir/VERSION"
output="$(cd "$TEST_TMPDIR" && bash -c '
source "$1/lib/bash/std/lib_std.sh"
base_std_import str/lib_str.sh
printf "version=%s provenance=%s commit=%s dirty=%s\n" \
"$BASE_BASH_LIBS_VERSION" "$BASE_BASH_LIBS_PROVENANCE" \
"$BASE_BASH_LIBS_COMMIT" "$BASE_BASH_LIBS_DIRTY_STATE"
' bash "$artifact_dir")"
[[ "$output" == *"version=2.0.0"* ]]
[[ "$output" == *"provenance=release-artifact"* ]]
[[ "$output" == *"commit=unknown"* ]]
[[ "$output" == *"dirty=unknown"* ]]
}
@test "symlinked package roots and spaced paths keep one physical package identity" {
local link_path="$TEST_TMPDIR/spaced package"
local output
ln -s "$BASE_REPO_ROOT" "$link_path"
output="$(cd "$TEST_TMPDIR" && bash -c '
source "$1/lib/bash/std/lib_std.sh"
base_std_import str/lib_str.sh
printf "root=%s version=%s\n" "$BASE_BASH_LIBS_STD_ROOT" "$BASE_BASH_LIBS_VERSION"
' bash "$link_path")"
[[ "$output" == *"root=$BASE_REPO_ROOT"* ]]
[[ "$output" == *"version=2.0.0"* ]]
}
@test "mixed-major stdlib inputs fail with migration guidance" {
local other_root="$TEST_TMPDIR/v2-input"
local output
mkdir -p "$other_root/lib"
cp -R "$BASE_REPO_ROOT/lib/bash" "$other_root/lib/"
printf '1.4.0\n' > "$other_root/VERSION"
sed 's/^version=.*/version=1.4.0/' \
"$BASE_REPO_ROOT/lib/bash/base-bash-libs.release" \
> "$other_root/lib/bash/base-bash-libs.release"
output="$(bash -c '
source "$1/lib/bash/std/lib_std.sh"
source "$2/lib/bash/std/lib_std.sh"
' bash "$BASE_REPO_ROOT" "$other_root" 2>&1 || true)"
[[ "$output" == *"mixed-major base-bash-libs module graph refused"* ]]
[[ "$output" == *"v1 inputs are not fallback-loaded by v2"* ]]
}
@test "base_std_add_to_path appends an existing directory only once" {
mkdir -p "$TEST_TMPDIR/bin"
PATH="/usr/bin:/bin"
base_std_add_to_path "$TEST_TMPDIR/bin"
base_std_add_to_path "$TEST_TMPDIR/bin"
[ "$PATH" = "/usr/bin:/bin:$TEST_TMPDIR/bin" ]
}
@test "base_std_add_to_path prepends when requested" {
mkdir -p "$TEST_TMPDIR/bin"
PATH="/usr/bin:/bin"
base_std_add_to_path -p "$TEST_TMPDIR/bin"
[ "$PATH" = "$TEST_TMPDIR/bin:/usr/bin:/bin" ]
}
@test "base_std_add_to_path skips missing directories unless -n is used" {
PATH="/usr/bin:/bin"
base_std_add_to_path "$TEST_TMPDIR/missing"
[ "$PATH" = "/usr/bin:/bin" ]
base_std_add_to_path -n "$TEST_TMPDIR/missing"
[ "$PATH" = "/usr/bin:/bin:$TEST_TMPDIR/missing" ]
}
@test "base_std_add_to_path rejects invalid options" {
local stderr_file="$TEST_TMPDIR/add-to-path.err"
local rc
if base_std_add_to_path -z 2>"$stderr_file"; then
rc=0
else
rc=$?
fi
[ "$rc" -eq 1 ]
[[ "$(cat "$stderr_file")" == *"base_std_add_to_path: invalid option"* ]]
}
@test "base_std_add_to_path preserves caller OPTIND and batch prepend order" {
local first="$TEST_TMPDIR/first"
local second="$TEST_TMPDIR/second"
local OPTIND=9
mkdir -p "$first" "$second"
PATH="/base"
base_std_add_to_path -p "$first" "$second"
[ "$OPTIND" -eq 9 ]
[ "$PATH" = "$first:$second:/base" ]
}
@test "base_std_dedupe_path removes duplicates and empty entries" {
PATH="/one:/two:/one::/three:/two"
base_std_dedupe_path