-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbash_lib_test_runner.sh
More file actions
executable file
·823 lines (650 loc) · 26.5 KB
/
bash_lib_test_runner.sh
File metadata and controls
executable file
·823 lines (650 loc) · 26.5 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
#!/usr/bin/env bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# -----------------------------------------------------------------------------
# Copyright (c) 2025 Richard Bloch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# -----------------------------------------------------------------------------
#
# Bash Test Runner for BASH-LIB libraries
# Tests the functionality of the 'args' and 'general' bash-lib libraries
# Version 1.2.0
# --- Configuration -----------------------------------------------------------
#
TEST_TMP_DIR=""
ARGS_LIBS_FILE="bash-lib/args"
GENERAL_LIBS_FILE="bash-lib/general"
DUMMY_CONFIG_CONTENT='
{
"details":
{
"title": "A bash template (BaT) to ease argument parsing and management",
"syntax": "bash_template.sh -a alpha -b bravo [-c charlie] -d delta",
"version": "1.2.0"
},
"arguments":
[
{
"short_form": "-a",
"long_form": "--alpha",
"text_string": "alpha",
"description": "alpha (something descriptive)",
"required": true
},
{
"short_form": "-b",
"long_form": "--bravo",
"text_string": "bravo",
"description": "bravo (something descriptive)",
"required": true
},
{
"short_form": "-c",
"long_form": "--charlie",
"text_string": "charlie",
"description": "charlie (this is optional)",
"required": false
},
{
"short_form": "-d",
"long_form": "--delta",
"text_string": "delta",
"description": "delta (something descriptive)",
"required": true
}
]
}'
# --- Constants and variables -----------------------------------------------
#
TOTAL_TESTS=0
FAILED_TESTS=0
# Declare ARG_VALUE as an associative array
declare -A ARG_VALUE
declare -a REQ_PROGRAMS=('jq')
# -----------------------------------------------------------------------------
# log() echoes a formatted message to stderr
#
log() {
echo "$(date '+%Y/%m/%d %H:%M:%S') $*" >&2
}
# -----------------------------------------------------------------------------
# test_suite_start() starts a test suite
#
test_suite_start() {
log "+---- Starting Test Suite: $1"
log "|"
}
# -----------------------------------------------------------------------------
# test_suite_end() ends a test suite
#
test_suite_end() {
log "|"
log "+---- Finished Test Suite: $1"
log ""
}
# -----------------------------------------------------------------------------
# test_case_start() starts a test case
#
test_case_start() {
TOTAL_TESTS=$((TOTAL_TESTS + 1))
log "| Running Test Case $TOTAL_TESTS: $1:"
}
# -----------------------------------------------------------------------------
# test_case_end() generates output at the end of a test case
#
test_case_end() {
log "|"
}
# -----------------------------------------------------------------------------
# test_case_pass() marks a test case as successful
#
test_case_pass() {
log "| ✅ PASS"
}
# -----------------------------------------------------------------------------
# test_case_fail() marks a test case as failed
#
test_case_fail() {
FAILED_TESTS=$((FAILED_TESTS + 1))
log "| ❌ FAIL"
if [[ -n "$1" ]]; then
log " Reason: $1"
fi
}
# -----------------------------------------------------------------------------
# assert_equal() checks if two values are equal
#
assert_equal() {
local expected="$1"
local actual="$2"
local message="$3"
if [[ "$actual" == "$expected" ]]; then
test_case_pass
else
test_case_fail "Output mismatch. $message
Expected: $expected
Actual: $actual"
fi
}
# -----------------------------------------------------------------------------
# assert_success() checks if a command exits with a success status
#
assert_success() {
local command_str="$1"
local message="$2"
local output=""
local status=0
# Run command in a sub-shell to prevent it from affecting main script flow
output=$(
(eval "$command_str") 2>&1
)
status=$?
if [[ $status -eq 0 ]]; then
test_case_pass
else
test_case_fail "Command failure. $message
Expected: 0
Actual: $status"
fi
}
# -----------------------------------------------------------------------------
# assert_failure() checks if a command exits with a failure status
#
assert_failure() {
local expected_status="$1"
local command_str="$2"
local message="$3"
local output=""
local status=0
# Run command in a sub-shell to prevent it from affecting main script flow
output=$(
(eval "$command_str") 2>&1
)
status=$?
if [[ $status -eq "$expected_status" ]]; then
test_case_pass
else
test_case_fail "Command failure. $message
Expected: $expected_status
Actual: $status"
fi
}
# -----------------------------------------------------------------------------
# assert_output() checks if a command's output matches a given string
#
assert_output() {
local expected_output="$1"
local command_str="$2"
local message="$3"
local actual_output=""
# Run command in a sub-shell and capture stdout/stderr
actual_output=$(
(eval "$command_str") 2>&1
)
expected_output="${expected_output//$'\r'/}"
actual_output="${actual_output//$'\r'/}"
if [[ "$actual_output" == "$expected_output" ]]; then
test_case_pass
else
test_case_fail "Output mismatch. $message
Expected: $expected_output
Actual: $actual_output"
fi
}
# -----------------------------------------------------------------------------
# assert_file_exists() checks if a file exists
#
assert_file_exists() {
local file_path="$1"
local message="$2"
if [[ -f "$file_path" ]]; then
test_case_pass
else
test_case_fail "File '$file_path' does not exist. $message"
fi
}
# -----------------------------------------------------------------------------
# assert_dir_exists() checks if a directory exists
#
assert_dir_exists() {
local dir_path="$1"
local message="$2"
if [[ -d "$dir_path" ]]; then
test_case_pass
else
test_case_fail "Directory '$dir_path' does not exist. $message"
fi
}
# -----------------------------------------------------------------------------
# assert_program_exists() checks if a program exists in the PATH
#
assert_program_exists() {
local program_name="$1"
local message="$2"
if command -v "$program_name" &>/dev/null; then
test_case_pass
else
test_case_fail "Program '$program_name' not found in PATH. $message"
fi
}
# -----------------------------------------------------------------------------
# setup_suite() sets up the test environment
#
setup_suite() {
log ""
log "+---- Setting Up Test Environment"
log "|"
# Create a temporary directory for test files and config
TEST_TMP_DIR=$(mktemp -d -t bash_test_XXXXXX)
log "| ✅ Created temporary directory: $TEST_TMP_DIR"
# Create the data directory required by ARGS
mkdir -p "$TEST_TMP_DIR/data"
log "| ✅ Created directory: $TEST_TMP_DIR/data"
# Create the dummy config.json file
echo "$DUMMY_CONFIG_CONTENT" >"$TEST_TMP_DIR/data/config.json"
log "| ✅ Created dummy config file: $TEST_TMP_DIR/data/config.json"
# Create a dummy bin directory for program dependency tests
mkdir -p "$TEST_TMP_DIR/bin"
log "| ✅ Created directory: $TEST_TMP_DIR/bin"
log "|"
# Create dummy executable programs
touch "$TEST_TMP_DIR/bin/dummy_program_1"
chmod +x "$TEST_TMP_DIR/bin/dummy_program_1"
log "| ✅ Created dummy executable: $TEST_TMP_DIR/bin/dummy_program_1"
touch "$TEST_TMP_DIR/bin/dummy_program_2"
chmod +x "$TEST_TMP_DIR/bin/dummy_program_2"
log "| ✅ Created dummy executable: $TEST_TMP_DIR/bin/dummy_program_2"
# Create dummy files for file dependency tests
touch "$TEST_TMP_DIR/dummy_file_1.txt"
log "| ✅ Created dummy file: $TEST_TMP_DIR/dummy_file_1.txt"
touch "$TEST_TMP_DIR/dummy_file_2.txt"
log "| ✅ Created dummy file: $TEST_TMP_DIR/dummy_file_2.txt"
# Create dummy directories for directory existence tests
mkdir "$TEST_TMP_DIR/dummy_dir_1"
log "| ✅ Created dummy directory: $TEST_TMP_DIR/dummy_dir_1"
mkdir "$TEST_TMP_DIR/dummy_dir_2"
log "| ✅ Created dummy directory: $TEST_TMP_DIR/dummy_dir_2"
log "|"
# Source the scripts used for testing
#
# ARGS uses EXEC_DIR and ARGS_LIBS_FILE, so we need to set EXEC_DIR before sourcing ARGS so it finds
# the dummy config.json
#
# GENERAL defines quit(), which is used by ARGS, so source GENERAL first
#
log "| ✅ Sourcing scripts under test"
export EXEC_DIR="$TEST_TMP_DIR"
log "| ✅ Set EXEC_DIR=$EXEC_DIR"
# Verify scripts exist before sourcing
if [[ ! -f "$GENERAL_LIBS_FILE" ]]; then
log "| ❌ Error: General script '$GENERAL_LIBS_FILE' not found!"
exit 1
fi
if [[ ! -f "$ARGS_LIBS_FILE" ]]; then
log "| ❌ Error: Args script '$ARGS_LIBS_FILE' not found!"
exit 1
fi
# Source GENERAL first because ARGS uses the 'quit' function
#
# shellcheck source=../bash-lib/general
source "$GENERAL_LIBS_FILE"
# shellcheck source=../bash-lib/args
source "$ARGS_LIBS_FILE"
log "| ✅ Scripts sourced"
# Check for program dependencies
log "| ✅ Checking for program dependencies"
# Capture stderr and exit status from check_program_dependencies
local dep_check_output
local dep_check_status
dep_check_output=$(check_program_dependencies "${REQ_PROGRAMS[@]}" 2>&1)
dep_check_status=$?
# An error occurred. Prepend to the message it and exit
if [[ $dep_check_status -ne 0 ]]; then
log "| ❌ $dep_check_output" >&2
exit "$dep_check_status"
fi
# Reset ARG_VALUE before running tests
ARG_VALUE=()
log "|"
log "+---- Setup Complete"
log ""
}
# -----------------------------------------------------------------------------
# teardown_suite() is run on exit to clean up the test environment
#
teardown_suite() {
log ""
log "+---- Cleaning Up Test Environment"
log "|"
if [[ -d "$TEST_TMP_DIR" ]]; then
rm -rf "$TEST_TMP_DIR"
log "| ✅ Removed temporary directory: $TEST_TMP_DIR"
else
log "| ✅ Temporary directory $TEST_TMP_DIR already removed or not created"
fi
log "| ✅ Teardown complete"
}
# Set trap to ensure teardown runs on exit
trap teardown_suite EXIT
# -----------------------------------------------------------------------------
# test_ARGS_jq_functions() tests the jq functions
#
test_ARGS_jq_functions() {
test_suite_start "ARGS jq Functions"
# Test get_config_details
test_case_start "get_config_details (title)"
assert_output "A bash template (BaT) to ease argument parsing and management" "get_config_details title" "Check title details"
test_case_end
test_case_start "get_config_details (version)"
assert_output "1.2.0" "get_config_details version" "Check version details"
test_case_end
test_case_start "get_config_details (syntax)"
assert_output "bash_template.sh -a alpha -b bravo [-c charlie] -d delta" "get_config_details syntax" "Check syntax details"
test_case_end
# Test get_config_arg
test_case_start "get_config_arg (index 0, short_form)"
assert_output "-a" "get_config_arg 0 short_form" "Check first arg short form"
test_case_end
test_case_start "get_config_arg (index 0, long_form)"
assert_output "--alpha" "get_config_arg 0 long_form" "Check first arg long form"
test_case_end
test_case_start "get_config_arg (index 1, text string)"
assert_output "bravo" "get_config_arg 1 text_string" "Check second arg text string"
test_case_end
test_case_start "get_config_arg (index 1, description)"
assert_output "bravo (something descriptive)" "get_config_arg 1 description" "Check second arg description"
test_case_end
test_case_start "get_config_arg (index 2, required)"
assert_output "false" "get_config_arg 2 required" "Check third arg required status"
test_case_end
# Test get_config_args_length
test_case_start "get_config_args_length"
assert_output "4" "get_config_args_length" "Check total number of arguments"
test_suite_end "ARGS jq Functions"
}
# -----------------------------------------------------------------------------
# test_ARGS_scan_for_args() tests the scan_for_args function
#
test_ARGS_scan_for_args() {
test_suite_start "ARGS scan_for_args"
# Test case 1: Scan with long and short forms, some missing
test_case_start "scan_for_args - long and short forms, some missing"
ARG_VALUE=() # Reset global array
scan_for_args "--alpha" "alpha_value" "-c" "charlie_value"
assert_equal "alpha_value" "${ARG_VALUE[alpha]:-}" "Alpha arg value should be set"
assert_equal "" "${ARG_VALUE[bravo]:-}" "bravo arg value should be empty (missing)"
assert_equal "charlie_value" "${ARG_VALUE[charlie]:-}" "Charlie arg value should be set"
test_case_end
# Test case 2: Scan with different order
test_case_start "scan_for_args - different order"
ARG_VALUE=() # Reset global array
scan_for_args "-c" "charlie_value_2" "--bravo" "bravo_value_2" "--alpha" "alpha_value_2"
assert_equal "alpha_value_2" "${ARG_VALUE[alpha]:-}" "Alpha arg value should be set"
assert_equal "bravo_value_2" "${ARG_VALUE[bravo]:-}" "bravo arg value should be set"
assert_equal "charlie_value_2" "${ARG_VALUE[charlie]:-}" "Charlie arg value should be set"
test_case_end
# Test case 3: Scan with unknown arguments (should be ignored)
test_case_start "scan_for_args - with unknown arguments"
ARG_VALUE=() # Reset global array
scan_for_args "--alpha" "alpha_value_3" "--unknown" "unknown_value" "-x" "x_value" "-b" "bravo_value_3"
assert_equal "alpha_value_3" "${ARG_VALUE[alpha]:-}" "Alpha arg value should be set"
assert_equal "bravo_value_3" "${ARG_VALUE[bravo]:-}" "bravo arg value should be set"
assert_equal "" "${ARG_VALUE[charlie]:-}" "Charlie arg value should be empty (missing)"
test_case_end
# Test case 4: Scan with no arguments
test_case_start "scan_for_args - with no arguments"
ARG_VALUE=() # Reset global array
scan_for_args
assert_equal "" "${ARG_VALUE[alpha]:-}" "Alpha arg value should be empty"
assert_equal "" "${ARG_VALUE[bravo]:-}" "bravo arg value should be empty"
assert_equal "" "${ARG_VALUE[charlie]:-}" "Charlie arg value should be empty"
test_suite_end "ARGS scan_for_args"
}
# -----------------------------------------------------------------------------
# test_ARGS_get_config_arg_value() tests the get_config_arg_value function
#
test_ARGS_get_config_arg_value() {
test_suite_start "ARGS get_config_arg_value"
# Test case 1: Find an existing value after scan
test_case_start "get_config_arg_value - find existing value after scan"
ARG_VALUE=() # Reset global array
scan_for_args "--alpha" "" "--bravo" "bravo_value_to_find" "-c" "charlie_value_to_find"
# The function prints to stdout, so we capture
assert_output "bravo_value_to_find" "get_config_arg_value $(get_config_arg 1 text_string)" "Should find bravo value"
# Test case 2: Find another existing value
test_case_start "get_config_arg_value - find another existing value"
# ARG_VALUE still contains values from previous test
assert_output "charlie_value_to_find" "get_config_arg_value $(get_config_arg 2 text_string)" "Should find charlie value"
# Test case 3: Try to find a value for a missing argument
test_case_start "get_config_arg_value - find value for missing argument"
# Alpha argument is missing in ARG_VALUE (above)
assert_output "" "get_config_arg_value $(get_config_arg 0 text_string)" "Should output nothing for missing value"
# Test case 4: Try to find value for an unknown text_string
test_case_start "get_config_arg_value - find value for unknown text_string"
assert_output "" "get_config_arg_value unknown_value" "Should output nothing for unknown text_string"
test_suite_end "ARGS get_config_arg_value"
}
# -----------------------------------------------------------------------------
# test_ARGS_check_for_args_completeness() tests the check_for_args_completeness function
#
test_ARGS_check_for_args_completeness() {
test_suite_start "ARGS check_for_args_completeness"
# Test case 1: All required arguments present
test_case_start "check_for_args_completeness - all required present"
ARG_VALUE=() # Reset global array
scan_for_args "--alpha" "a_value" "--bravo" "b_value" "--charlie" "c_value" "--delta" "d_value"
# This should not call quit, so exit status should be 0 from the sub-shell
assert_success "check_for_args_completeness" "Should succeed when all required args are present"
test_case_end
# Test case 2: One required argument missing
test_case_start "check_for_args_completeness - one required missing (--alpha)"
ARG_VALUE=()
scan_for_args "-a" "" "--bravo" "b_value" "--charlie" "c_value" "--delta" "d_value" # Alpha missing
# This should call quit 1 and print an error
expected_output=$'Error: argument \'alpha\' (-a|--alpha) is missing.'
assert_output "$expected_output" "check_for_args_completeness" "Should output error message for missing alpha"
assert_failure 1 "check_for_args_completeness" "Should exit with status 1 for missing required arg"
test_case_end
# Test case 3: Another required argument missing
test_case_start "check_for_args_completeness - one required missing (--bravo)"
ARG_VALUE=()
scan_for_args "--alpha" "a_value" "--charlie" "c_value" "--delta" "d_value" # Bravo missing
expected_output=$'Error: argument \'bravo\' (-b|--bravo) is missing.'
assert_output "$expected_output" "check_for_args_completeness" "Should output error message for missing bravo"
assert_failure 1 "check_for_args_completeness" "Should exit with status 1 for missing required arg"
test_case_end
# Test case 4: Multiple required arguments missing
test_case_start "check_for_args_completeness - multiple required missing (--alpha, --delta)"
ARG_VALUE=()
scan_for_args "--bravo" "b_value" # Alpha and delta missing
# Output multiple errors, order depends on loop, check for both
local output status
output=$(
(check_for_args_completeness) 2>&1
) || status=$?
status=${status:-$?}
# Check status
if [[ $status -eq 1 ]]; then
test_case_pass
else
test_case_fail "Expected status 1; Actual $status for multiple missing args."
fi
# Check output content (order might vary, check for presence of lines)
local line1="Error: argument 'alpha' (-a|--alpha) is missing."
local line2="Error: argument 'delta' (-d|--delta) is missing."
local expected_output="$line1"$'\n'"$line2"
if ! [[ "$output" == *"$line1"* && "$output" == *"$line2"* ]]; then
test_case_fail "Output mismatch. $message
Expected: $expected_output
Actual: $output"
fi
test_suite_end "ARGS check_for_args_completeness"
}
# -----------------------------------------------------------------------------
# test_GENERAL_quit() tests the quit function
#
test_GENERAL_quit() {
test_suite_start "GENERAL quit function"
# Test case 1: quit 0
test_case_start "quit 0"
# Run quit in a sub-shell and check its exit status and output
assert_output $'' "( quit 0 )" "quit 0 should print newline"
assert_success "( quit 0 )" "quit 0 should exit with status 0"
test_case_end
# Test case 2: quit 1
test_case_start "quit 1"
assert_output $'' "( quit 1 )" "quit 1 should print newline"
assert_failure 1 "( quit 1 )" "quit 1 should exit with status 1"
test_case_end
# Test case 3: quit with non-zero status
test_case_start "quit 42"
assert_output $'' "( quit 42 )" "quit 42 should print newline"
assert_failure 42 "( quit 42 )" "quit 42 should exit with status 42"
test_suite_end "GENERAL quit function"
}
# -----------------------------------------------------------------------------
# test_GENERAL_check_program_dependencies() tests the check_program_dependencies function
#
test_GENERAL_check_program_dependencies() {
test_suite_start "GENERAL check_program_dependencies"
# Temporarily add the dummy bin directory to PATH
local old_path="$PATH"
export PATH="$TEST_TMP_DIR/bin:$PATH"
log "| Temporarily updated PATH for dependency checks: $PATH"
# Test case 1: All dependencies exist (dummy programs)
test_case_start "check_program_dependencies - all exist"
assert_success "check_program_dependencies dummy_program_1 dummy_program_2 jq" "Should succeed when all programs exist"
test_case_end
# Test case 2: One dependency missing
test_case_start "check_program_dependencies - one missing"
local missing_program="non_existent_program"
local expected_output=$"Error: program '$missing_program' not installed."
assert_output "$expected_output" "check_program_dependencies dummy_program_1 $missing_program dummy_program_2" "Should output error for missing program"
assert_failure 1 "check_program_dependencies dummy_program_1 $missing_program dummy_program_2" "Should exit with status 1 for missing program"
test_case_end
# Test case 3: No dependencies specified
test_case_start "check_program_dependencies - no dependencies"
assert_success "check_program_dependencies" "Should succeed when no dependencies are specified"
test_case_end
# Restore original PATH
export PATH="$old_path"
log "| Restored original PATH"
test_suite_end "GENERAL check_program_dependencies"
}
# -----------------------------------------------------------------------------
# test_GENERAL_check_file_dependencies() tests the check_file_dependencies function
#
test_GENERAL_check_file_dependencies() {
test_suite_start "GENERAL check_file_dependencies"
# Paths relative to TEST_TMP_DIR
local file1="$TEST_TMP_DIR/dummy_file_1.txt"
local file2="$TEST_TMP_DIR/dummy_file_2.txt"
local non_existent_file="$TEST_TMP_DIR/non_existent_file.txt"
# Test case 1: All dependencies exist
test_case_start "check_file_dependencies - all exist"
assert_success "check_file_dependencies $file1 $file2" "Should succeed when all files exist"
test_case_end
# Test case 2: One dependency missing
test_case_start "check_file_dependencies - one missing"
local expected_output=$"Error: file '$non_existent_file' not found."
assert_output "$expected_output" "check_file_dependencies $file1 $non_existent_file $file2" "Should output error for missing file"
assert_failure 1 "check_file_dependencies $file1 $non_existent_file $file2" "Should exit with status 1 for missing file"
test_case_end
# Test case 3: No dependencies specified
test_case_start "check_file_dependencies - no dependencies"
assert_success "check_file_dependencies" "Should succeed when no dependencies are specified"
test_suite_end "GENERAL check_file_dependencies"
}
# -----------------------------------------------------------------------------
# test_GENERAL_exist_directory() tests the exist_directory function
#
test_GENERAL_exist_directory() {
test_suite_start "GENERAL exist_directory"
# Paths relative to TEST_TMP_DIR
local dir1="$TEST_TMP_DIR/dummy_dir_1"
local non_existent_dir="$TEST_TMP_DIR/non_existent_dir"
# Test case 1: Directory exists
test_case_start "exist_directory - exists"
assert_success "exist_directory $dir1" "Should succeed when directory exists"
test_case_end
# Test case 2: Directory does not exist
test_case_start "exist_directory - does not exist"
local expected_output=$"Error: directory '$non_existent_dir' not found."
assert_output "$expected_output" "exist_directory $non_existent_dir" "Should output error for missing directory"
assert_failure 1 "exist_directory $non_existent_dir" "Should exit with status 1 for missing directory"
test_suite_end "GENERAL exist_directory"
}
# -----------------------------------------------------------------------------
# test_GENERAL_display_banner() tests the display_banner function
#
test_GENERAL_display_banner() {
test_suite_start "GENERAL display_banner"
# This test relies on the dummy config.json setup in setup_suite
test_case_start "display_banner - default output"
# Construct the expected output based on the DUMMY_CONFIG_CONTENT
# This requires careful formatting matching the script's printf
local expected_banner_output
expected_banner_output=$'\n'
expected_banner_output+=" |"$'\n'
expected_banner_output+=" | A bash template (BaT) to ease argument parsing and management"$'\n'
expected_banner_output+=" | 1.2.0"$'\n'
expected_banner_output+=" |"$'\n'
expected_banner_output+=" | Usage:"$'\n'
expected_banner_output+=" | bash_template.sh -a alpha -b bravo [-c charlie] -d delta"$'\n'
expected_banner_output+=" |"$'\n'
# The argument lines require calculating the max width
# Short/long forms in config: "-a", "--alpha" ; "-b", "--bravo" ; "-c", "--charlie" ; "-d", "--delta"
# Combined lengths: "-a--alpha" (9), "-b--bravo" (8), "-c--charlie" (11), "-d--delta" (8)
# Max length is 11. Script adds 6 to max_col. So max_col = 11 + 6 = 17.
# printf format is "%-${max_col}s" -> "%-17s"
expected_banner_output+=" | -a, --alpha alpha (something descriptive)"$'\n'
expected_banner_output+=" | -b, --bravo bravo (something descriptive)"$'\n'
expected_banner_output+=" | -c, --charlie charlie (this is optional)"$'\n'
expected_banner_output+=" | -d, --delta delta (something descriptive)"$'\n'
expected_banner_output+=" |"
assert_output "$expected_banner_output" "display_banner" "Check banner format and content"
test_suite_end "GENERAL display_banner"
}
# -----------------------------------------------------------------------------
# run_tests() runs all the tests
#
run_tests() {
setup_suite
# Run tests for the ARGS library
test_ARGS_jq_functions
test_ARGS_scan_for_args
test_ARGS_get_config_arg_value
test_ARGS_check_for_args_completeness
# Run tests for the GENERAL library
test_GENERAL_quit # Test quit first as others depend on its functionality indirectly
test_GENERAL_check_program_dependencies
test_GENERAL_check_file_dependencies
test_GENERAL_exist_directory
test_GENERAL_display_banner # Depends on ARGS functions, which depend on jq program
log "+---- Test Summary"
log "|"
log "| ✅ Successful Tests: $((TOTAL_TESTS - FAILED_TESTS))"
log "| ❌ Failed Tests: $FAILED_TESTS"
if [[ $FAILED_TESTS -eq 0 ]]; then
log "| All $TOTAL_TESTS tests passed!"
log "|"
log "+----"
return 0
else
log "| Some tests failed!"
log "|"
log "+----"
return 1
fi
}
# Run the tests already!
run_tests