Skip to content

Commit 37a6aac

Browse files
authored
Merge pull request #450 from TypedDevs/feat/improve-dev-log
Improve dev log internal messages
2 parents f8ff1cc + 05b07e3 commit 37a6aac

12 files changed

+95
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Fix prevents writing in src dir during tests
66
- Fix negative widths with rpad
77
- Fix internal assert_line_count and call_test_functions
8+
- Include calling function name in dev log output for easier debugging
9+
- Add more internal dev log messages and prefix them with [INTERNAL]
10+
- Toggle internal log messages with `BASHUNIT_INTERNAL_LOG=true|false`
811

912
## [0.22.0](https://github.com/TypedDevs/bashunit/compare/0.21.0...0.22.0) - 2025-07-20
1013

docs/configuration.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,16 @@ log "error" "an" "error" "message"
227227
log "warning" "different log level messages!"
228228
```
229229
```bash [Output: out.log]
230-
2024-10-03 21:27:23 [INFO]: I am tracing something...
231-
2024-10-03 21:27:23 [ERROR]: an error message
232-
2024-10-03 21:27:23 [WARNING]: different log level messages!
230+
2024-10-03 21:27:23 [INFO]: I am tracing something... #tests/sample.sh:11
231+
2024-10-03 21:27:23 [ERROR]: an error message #tests/sample.sh:27
232+
2024-10-03 21:27:24 [WARNING]: different log level messages! #tests/sample.sh:21
233233
```
234234
:::
235+
When enabled, the selected log file path is printed in the header so you can
236+
quickly `tail -f` it while the tests run.
237+
238+
> All internal messages emitted by bashunit are prefixed with `[INTERNAL]`.
239+
> You can toggle internal messages with `BASHUNIT_INTERNAL_LOG=true|false`.
235240
236241
## Verbose
237242

docs/globals.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ You can use this list of global functions that exists to primarily to improve yo
55

66
## log
77

8-
Write into the `BASHUNIT_DEV_LOG` a log message.
8+
Write into the `BASHUNIT_DEV_LOG` a log message. The log line records the source file and line number for easier debugging.
99

1010
> See: [Dev log](/configuration#dev-log)
1111
@@ -17,6 +17,7 @@ log "warning" "hello" "world"
1717
log "critical" "hello" "world"
1818
log "error" "hello" "world"
1919
```
20+
Internal messages from bashunit include the `[INTERNAL]` prefix so you can easily spot them. You can enable them with `BASHUNIT_INTERNAL_LOG=true|false`.
2021

2122
## current_dir
2223

src/console_header.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function console_header::print_version_with_env() {
99
fi
1010

1111
console_header::print_version "$filter" "${files[@]}"
12+
13+
if env::is_dev_mode_enabled; then
14+
printf "%sDev log:%s %s\n" "${_COLOR_INCOMPLETE}" "${_COLOR_DEFAULT}" "$BASHUNIT_DEV_LOG"
15+
fi
1216
}
1317

1418
function console_header::print_version() {

src/env.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ _DEFAULT_SHOW_EXECUTION_TIME="true"
2929
_DEFAULT_VERBOSE="false"
3030
_DEFAULT_BENCH_MODE="false"
3131
_DEFAULT_NO_OUTPUT="false"
32+
_DEFAULT_INTERNAL_LOG="false"
3233

3334
: "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}}"
3435
: "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}}"
@@ -39,6 +40,7 @@ _DEFAULT_NO_OUTPUT="false"
3940
: "${BASHUNIT_VERBOSE:=${VERBOSE:=$_DEFAULT_VERBOSE}}"
4041
: "${BASHUNIT_BENCH_MODE:=${BENCH_MODE:=$_DEFAULT_BENCH_MODE}}"
4142
: "${BASHUNIT_NO_OUTPUT:=${NO_OUTPUT:=$_DEFAULT_NO_OUTPUT}}"
43+
: "${BASHUNIT_INTERNAL_LOG:=${INTERNAL_LOG:=$_DEFAULT_INTERNAL_LOG}}"
4244

4345
function env::is_parallel_run_enabled() {
4446
[[ "$BASHUNIT_PARALLEL_RUN" == "true" ]]
@@ -68,6 +70,10 @@ function env::is_dev_mode_enabled() {
6870
[[ -n "$BASHUNIT_DEV_LOG" ]]
6971
}
7072

73+
function env::is_internal_log_enabled() {
74+
[[ "$BASHUNIT_INTERNAL_LOG" == "true" ]]
75+
}
76+
7177
function env::is_verbose_enabled() {
7278
[[ "$BASHUNIT_VERBOSE" == "true" ]]
7379
}
@@ -104,6 +110,7 @@ function env::find_terminal_width() {
104110
}
105111

106112
function env::print_verbose() {
113+
internal_log "Printing verbose environment variables"
107114
local keys=(
108115
"BASHUNIT_DEFAULT_PATH"
109116
"BASHUNIT_DEV_LOG"
@@ -128,6 +135,7 @@ function env::print_verbose() {
128135
done
129136

130137
for key in "${keys[@]}"; do
138+
internal_log "$key=${!key}"
131139
printf "%s:%*s%s\n" "$key" $((max_length - ${#key} + 1)) "" "${!key}"
132140
done
133141
}
@@ -140,3 +148,7 @@ TEMP_FILE_PARALLEL_STOP_ON_FAILURE="$TEMP_DIR_PARALLEL_TEST_SUITE/.stop-on-failu
140148
TERMINAL_WIDTH="$(env::find_terminal_width)"
141149
FAILURES_OUTPUT_PATH=$(mktemp)
142150
CAT="$(which cat)"
151+
152+
if env::is_dev_mode_enabled; then
153+
internal_log "info" "Dev log enabled" "file:$BASHUNIT_DEV_LOG"
154+
fi

src/globals.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function temp_dir() {
5858
}
5959

6060
function cleanup_temp_files() {
61+
internal_log "cleanup_temp_files"
6162
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
6263
rm -rf /tmp/bashunit/tmp/"${BASHUNIT_CURRENT_TEST_ID}"_*
6364
else
@@ -83,9 +84,15 @@ function log() {
8384
*) set -- "$level $@"; level="INFO" ;;
8485
esac
8586

86-
local GRAY='\033[1;30m'
87-
local RESET='\033[0m'
88-
echo -e "$(current_timestamp) [$level]: $@ ${GRAY}#${BASH_SOURCE[1]}:${BASH_LINENO[0]}${RESET}" >> "$BASHUNIT_DEV_LOG"
87+
echo "$(current_timestamp) [$level]: $* #${BASH_SOURCE[1]}:${BASH_LINENO[0]}" >> "$BASHUNIT_DEV_LOG"
88+
}
89+
90+
function internal_log() {
91+
if ! env::is_dev_mode_enabled || ! env::is_internal_log_enabled; then
92+
return
93+
fi
94+
95+
echo "$(current_timestamp) [INTERNAL]: $* #${BASH_SOURCE[1]}:${BASH_LINENO[0]}" >> "$BASHUNIT_DEV_LOG"
8996
}
9097

9198
function print_line() {

src/main.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function main::exec_tests() {
99
test_files+=("$line")
1010
done < <(helper::load_test_files "$filter" "${files[@]}")
1111

12+
internal_log "exec_tests" "filter:$filter" "files:${test_files[*]}"
13+
1214
if [[ ${#test_files[@]} -eq 0 || -z "${test_files[0]}" ]]; then
1315
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
1416
console_header::print_help
@@ -69,6 +71,7 @@ function main::exec_tests() {
6971
fi
7072

7173
cleanup_temp_files
74+
internal_log "Finished tests" "exit_code:$exit_code"
7275
exit $exit_code
7376
}
7477

@@ -81,6 +84,8 @@ function main::exec_benchmarks() {
8184
bench_files+=("$line")
8285
done < <(helper::load_bench_files "$filter" "${files[@]}")
8386

87+
internal_log "exec_benchmarks" "filter:$filter" "files:${bench_files[*]}"
88+
8489
if [[ ${#bench_files[@]} -eq 0 || -z "${bench_files[0]}" ]]; then
8590
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
8691
console_header::print_help
@@ -94,6 +99,7 @@ function main::exec_benchmarks() {
9499
benchmark::print_results
95100

96101
cleanup_temp_files
102+
internal_log "Finished benchmarks"
97103
}
98104

99105
function main::cleanup() {

src/parallel.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
function parallel::aggregate_test_results() {
44
local temp_dir_parallel_test_suite=$1
55

6+
internal_log "aggregate_test_results" "dir:$temp_dir_parallel_test_suite"
7+
68
local total_failed=0
79
local total_passed=0
810
local total_skipped=0
@@ -78,6 +80,13 @@ function parallel::aggregate_test_results() {
7880
export _ASSERTIONS_SKIPPED=$total_skipped
7981
export _ASSERTIONS_INCOMPLETE=$total_incomplete
8082
export _ASSERTIONS_SNAPSHOT=$total_snapshot
83+
84+
internal_log "aggregate_totals" \
85+
"failed:$total_failed" \
86+
"passed:$total_passed" \
87+
"skipped:$total_skipped" \
88+
"incomplete:$total_incomplete" \
89+
"snapshot:$total_snapshot"
8190
}
8291

8392
function parallel::mark_stop_on_failure() {
@@ -96,6 +105,8 @@ function parallel::reset() {
96105
}
97106

98107
function parallel::is_enabled() {
108+
internal_log "parallel::is_enabled" "requested:$BASHUNIT_PARALLEL_RUN" "os:${_OS:-Unknown}"
109+
99110
if env::is_parallel_run_enabled && \
100111
(check_os::is_macos || check_os::is_ubuntu || check_os::is_windows); then
101112
return 0

src/runner.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function runner::load_test_files() {
1010
if [[ ! -f $test_file ]]; then
1111
continue
1212
fi
13+
internal_log "Loading file" "$test_file"
1314
# shellcheck source=/dev/null
1415
source "$test_file"
1516
runner::run_set_up_before_script
@@ -20,6 +21,7 @@ function runner::load_test_files() {
2021
fi
2122
runner::run_tear_down_after_script
2223
runner::clean_set_up_and_tear_down_after_script
24+
internal_log "Finished file" "$test_file"
2325
done
2426

2527
if parallel::is_enabled; then
@@ -161,6 +163,8 @@ function runner::call_bench_functions() {
161163
function runner::render_running_file_header() {
162164
local script="$1"
163165

166+
internal_log "Running file" "$script"
167+
164168
if parallel::is_enabled; then
165169
return
166170
fi
@@ -190,6 +194,7 @@ function runner::run_test() {
190194
# race conditions when running tests in parallel.
191195
local sanitized_fn_name
192196
sanitized_fn_name="$(helper::normalize_variable_name "$fn_name")"
197+
internal_log "Running test" "$fn_name" "$*"
193198
if env::is_parallel_run_enabled; then
194199
export BASHUNIT_CURRENT_TEST_ID="${sanitized_fn_name}_$$_$(random_str 6)"
195200
else
@@ -208,8 +213,11 @@ function runner::run_test() {
208213
exec 3>&1
209214

210215
local test_execution_result=$(
216+
# shellcheck disable=SC2154
211217
trap '
212-
state::set_test_exit_code $?
218+
exit_code=$?
219+
set +e
220+
state::set_test_exit_code "$exit_code"
213221
runner::run_tear_down
214222
runner::clear_mocks
215223
state::export_subshell_context
@@ -288,6 +296,7 @@ function runner::run_test() {
288296
console_results::print_error_test "$fn_name" "$runtime_error"
289297
reports::add_test_failed "$test_file" "$fn_name" "$duration" "$total_assertions"
290298
runner::write_failure_result_output "$test_file" "$runtime_error"
299+
internal_log "Test error" "$fn_name" "$runtime_error"
291300
return
292301
fi
293302

@@ -296,6 +305,8 @@ function runner::run_test() {
296305
reports::add_test_failed "$test_file" "$fn_name" "$duration" "$total_assertions"
297306
runner::write_failure_result_output "$test_file" "$subshell_output"
298307

308+
internal_log "Test failed" "$fn_name"
309+
299310
if env::is_stop_on_failure_enabled; then
300311
if parallel::is_enabled; then
301312
parallel::mark_stop_on_failure
@@ -310,18 +321,21 @@ function runner::run_test() {
310321
state::add_tests_snapshot
311322
console_results::print_snapshot_test "$fn_name"
312323
reports::add_test_snapshot "$test_file" "$fn_name" "$duration" "$total_assertions"
324+
internal_log "Test snapshot" "$fn_name"
313325
return
314326
fi
315327

316328
if [[ "$current_assertions_incomplete" != "$(state::get_assertions_incomplete)" ]]; then
317329
state::add_tests_incomplete
318330
reports::add_test_incomplete "$test_file" "$fn_name" "$duration" "$total_assertions"
331+
internal_log "Test incomplete" "$fn_name"
319332
return
320333
fi
321334

322335
if [[ "$current_assertions_skipped" != "$(state::get_assertions_skipped)" ]]; then
323336
state::add_tests_skipped
324337
reports::add_test_skipped "$test_file" "$fn_name" "$duration" "$total_assertions"
338+
internal_log "Test skipped" "$fn_name"
325339
return
326340
fi
327341

@@ -334,6 +348,7 @@ function runner::run_test() {
334348
fi
335349
state::add_tests_passed
336350
reports::add_test_passed "$test_file" "$fn_name" "$duration" "$total_assertions"
351+
internal_log "Test passed" "$fn_name"
337352
}
338353

339354
function runner::decode_subshell_output() {
@@ -392,7 +407,7 @@ function runner::parse_result_parallel() {
392407
mv "$unique_test_result_file" "${unique_test_result_file}.result"
393408
unique_test_result_file="${unique_test_result_file}.result"
394409

395-
log "debug" "[PARA]" "fn_name:$fn_name" "execution_result:$execution_result"
410+
internal_log "[PARA]" "fn_name:$fn_name" "execution_result:$execution_result"
396411

397412
runner::parse_result_sync "$fn_name" "$execution_result"
398413

@@ -431,14 +446,22 @@ function runner::parse_result_sync() {
431446
test_exit_code="${BASH_REMATCH[6]}"
432447
fi
433448

434-
log "debug" "[SYNC]" "fn_name:$fn_name" "execution_result:$execution_result"
449+
internal_log "[SYNC]" "fn_name:$fn_name" "execution_result:$execution_result"
435450

436451
((_ASSERTIONS_PASSED += assertions_passed)) || true
437452
((_ASSERTIONS_FAILED += assertions_failed)) || true
438453
((_ASSERTIONS_SKIPPED += assertions_skipped)) || true
439454
((_ASSERTIONS_INCOMPLETE += assertions_incomplete)) || true
440455
((_ASSERTIONS_SNAPSHOT += assertions_snapshot)) || true
441456
((_TEST_EXIT_CODE += test_exit_code)) || true
457+
458+
internal_log "result_summary" \
459+
"failed:$assertions_failed" \
460+
"passed:$assertions_passed" \
461+
"skipped:$assertions_skipped" \
462+
"incomplete:$assertions_incomplete" \
463+
"snapshot:$assertions_snapshot" \
464+
"exit_code:$test_exit_code"
442465
}
443466

444467
function runner::write_failure_result_output() {
@@ -454,14 +477,17 @@ function runner::write_failure_result_output() {
454477
}
455478

456479
function runner::run_set_up() {
480+
internal_log "run_set_up"
457481
helper::execute_function_if_exists 'set_up'
458482
}
459483

460484
function runner::run_set_up_before_script() {
485+
internal_log "run_set_up_before_script"
461486
helper::execute_function_if_exists 'set_up_before_script'
462487
}
463488

464489
function runner::run_tear_down() {
490+
internal_log "run_tear_down"
465491
helper::execute_function_if_exists 'tear_down'
466492
}
467493

@@ -472,10 +498,12 @@ function runner::clear_mocks() {
472498
}
473499

474500
function runner::run_tear_down_after_script() {
501+
internal_log "run_tear_down_after_script"
475502
helper::execute_function_if_exists 'tear_down_after_script'
476503
}
477504

478505
function runner::clean_set_up_and_tear_down_after_script() {
506+
internal_log "clean_set_up_and_tear_down_after_script"
479507
helper::unset_if_exists 'set_up'
480508
helper::unset_if_exists 'tear_down'
481509
helper::unset_if_exists 'set_up_before_script'

tests/acceptance/snapshots/bashunit_benchmark_test_sh.test_bashunit_functional_benchmark.snapshot

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)