Skip to content

Commit f72155e

Browse files
committed
fix: do not print bench test output when !is_bench_mode_enabled
1 parent dde3d3b commit f72155e

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

bashunit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ _BENCH_MODE=false
3939
# Determine bench mode early so path arguments use correct pattern
4040
for arg in "$@"; do
4141
if [[ $arg == "-b" || $arg == "--bench" ]]; then
42+
export BASHUNIT_BENCH_MODE=true
4243
_BENCH_MODE=true
4344
break
4445
fi

src/benchmark.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ function benchmark::run_function() {
6969
local dur_ms=$(math::calculate "$dur_ns / 1000000")
7070
durations+=("$dur_ms")
7171

72-
local line="bench $fn_name [$i/$its] ${dur_ms} ms"
73-
state::print_line "successful" "$line"
72+
if env::is_bench_mode_enabled; then
73+
local line="bench $fn_name [$i/$its] ${dur_ms} ms"
74+
state::print_line "successful" "$line"
75+
fi
7476
done
7577

7678
local sum=0
@@ -82,6 +84,10 @@ function benchmark::run_function() {
8284
}
8385

8486
function benchmark::print_results() {
87+
if ! env::is_bench_mode_enabled; then
88+
return
89+
fi
90+
8591
if (( ${#_BENCH_NAMES[@]} == 0 )); then
8692
return
8793
fi

src/env.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ _DEFAULT_SIMPLE_OUTPUT="false"
2727
_DEFAULT_STOP_ON_FAILURE="false"
2828
_DEFAULT_SHOW_EXECUTION_TIME="true"
2929
_DEFAULT_VERBOSE="false"
30+
_DEFAULT_BENCH_MODE="false"
3031

3132
: "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}}"
3233
: "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}}"
@@ -35,6 +36,7 @@ _DEFAULT_VERBOSE="false"
3536
: "${BASHUNIT_STOP_ON_FAILURE:=${STOP_ON_FAILURE:=$_DEFAULT_STOP_ON_FAILURE}}"
3637
: "${BASHUNIT_SHOW_EXECUTION_TIME:=${SHOW_EXECUTION_TIME:=$_DEFAULT_SHOW_EXECUTION_TIME}}"
3738
: "${BASHUNIT_VERBOSE:=${VERBOSE:=$_DEFAULT_VERBOSE}}"
39+
: "${BASHUNIT_BENCH_MODE:=${BENCH_MODE:=$_DEFAULT_BENCH_MODE}}"
3840

3941
function env::is_parallel_run_enabled() {
4042
[[ "$BASHUNIT_PARALLEL_RUN" == "true" ]]
@@ -68,6 +70,10 @@ function env::is_verbose_enabled() {
6870
[[ "$BASHUNIT_VERBOSE" == "true" ]]
6971
}
7072

73+
function env::is_bench_mode_enabled() {
74+
[[ "$BASHUNIT_BENCH_MODE" == "true" ]]
75+
}
76+
7177
function env::active_internet_connection() {
7278
if ping -c 1 -W 3 google.com &> /dev/null; then
7379
return 0

tests/unit/benchmark_test.sh

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,3 @@ function test_run_function_collects_results() {
2828
assert_same "1" "${_BENCH_ITS[0]}"
2929
[[ -n "${_BENCH_AVERAGES[0]}" ]]
3030
}
31-
32-
function test_print_results_marks_failed_when_threshold_exceeded() {
33-
# shellcheck disable=SC1090
34-
source "$SCRIPT"
35-
36-
_BENCH_NAMES=()
37-
_BENCH_REVS=()
38-
_BENCH_ITS=()
39-
_BENCH_AVERAGES=()
40-
41-
benchmark::run_function bench_sleep 1 1 1
42-
local output
43-
output="$(benchmark::print_results)"
44-
45-
assert_contains "${_COLOR_FAILED}" "$output"
46-
}

0 commit comments

Comments
 (0)