Skip to content

Commit 03ff6e8

Browse files
authored
Merge pull request #547 from TypedDevs/feat/546-bootstrap-args
Add bootstrap argument passing support
2 parents 4ddc898 + a47f6d1 commit 03ff6e8

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
### Added
6+
- Add bootstrap argument passing support via `--env "file.sh arg1 arg2"` or `BASHUNIT_BOOTSTRAP_ARGS` (fixes #546)
67
- Add `--strict` flag to enable strict shell mode (`set -euo pipefail`) for tests (fixes #540)
78
- Add `BASHUNIT_STRICT_MODE` configuration option (default: `false`)
89
- Add `-R, --run-all` flag to run all assertions even when one fails (fixes #536)

src/console_header.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Arguments:
101101
102102
Options:
103103
-a, --assert <fn> <args> Run a standalone assert function
104-
-e, --env, --boot <file> Load a custom env/bootstrap file
104+
-e, --env, --boot <file> Load a custom env/bootstrap file (supports args)
105105
-f, --filter <name> Only run tests matching the name
106106
-l, --log-junit <file> Write JUnit XML report
107107
-p, --parallel Run tests in parallel (default)
@@ -135,7 +135,7 @@ Arguments:
135135
path File or directory containing benchmarks
136136
137137
Options:
138-
-e, --env, --boot <file> Load a custom env/bootstrap file
138+
-e, --env, --boot <file> Load a custom env/bootstrap file (supports args)
139139
-f, --filter <name> Only run benchmarks matching the name
140140
-s, --simple Simple output
141141
--detailed Detailed output (default)

src/env.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ _BASHUNIT_DEFAULT_REPORT_HTML=""
1616
: "${BASHUNIT_DEFAULT_PATH:=${DEFAULT_PATH:=$_BASHUNIT_DEFAULT_DEFAULT_PATH}}"
1717
: "${BASHUNIT_DEV_LOG:=${DEV_LOG:=$_BASHUNIT_DEFAULT_DEV_LOG}}"
1818
: "${BASHUNIT_BOOTSTRAP:=${BOOTSTRAP:=$_BASHUNIT_DEFAULT_BOOTSTRAP}}"
19+
: "${BASHUNIT_BOOTSTRAP_ARGS:=${BOOTSTRAP_ARGS:=}}"
1920
: "${BASHUNIT_LOG_JUNIT:=${LOG_JUNIT:=$_BASHUNIT_DEFAULT_LOG_JUNIT}}"
2021
: "${BASHUNIT_REPORT_HTML:=${REPORT_HTML:=$_BASHUNIT_DEFAULT_REPORT_HTML}}"
2122

@@ -149,6 +150,7 @@ function bashunit::env::print_verbose() {
149150
"BASHUNIT_DEFAULT_PATH"
150151
"BASHUNIT_DEV_LOG"
151152
"BASHUNIT_BOOTSTRAP"
153+
"BASHUNIT_BOOTSTRAP_ARGS"
152154
"BASHUNIT_LOG_JUNIT"
153155
"BASHUNIT_REPORT_HTML"
154156
"BASHUNIT_PARALLEL_RUN"

src/main.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ function bashunit::main::cmd_test() {
4444
export BASHUNIT_PARALLEL_RUN=false
4545
;;
4646
-e|--env|--boot)
47-
# shellcheck disable=SC1090
48-
source "$2"
47+
# Support: --env "bootstrap.sh arg1 arg2"
48+
local boot_file="${2%% *}"
49+
local boot_args="${2#* }"
50+
if [[ "$boot_args" != "$2" ]]; then
51+
export BASHUNIT_BOOTSTRAP_ARGS="$boot_args"
52+
fi
53+
# shellcheck disable=SC1090,SC2086
54+
source "$boot_file" ${BASHUNIT_BOOTSTRAP_ARGS:-}
4955
shift
5056
;;
5157
-l|--log-junit)
@@ -139,8 +145,8 @@ function bashunit::main::cmd_test() {
139145
fi
140146

141147
# Optional bootstrap
142-
# shellcheck disable=SC1090
143-
[[ -f "${BASHUNIT_BOOTSTRAP:-}" ]] && source "$BASHUNIT_BOOTSTRAP"
148+
# shellcheck disable=SC1090,SC2086
149+
[[ -f "${BASHUNIT_BOOTSTRAP:-}" ]] && source "$BASHUNIT_BOOTSTRAP" ${BASHUNIT_BOOTSTRAP_ARGS:-}
144150

145151
if [[ "${BASHUNIT_NO_OUTPUT:-false}" == true ]]; then
146152
exec >/dev/null 2>&1
@@ -183,8 +189,14 @@ function bashunit::main::cmd_bench() {
183189
export BASHUNIT_SIMPLE_OUTPUT=false
184190
;;
185191
-e|--env|--boot)
186-
# shellcheck disable=SC1090
187-
source "$2"
192+
# Support: --env "bootstrap.sh arg1 arg2"
193+
local boot_file="${2%% *}"
194+
local boot_args="${2#* }"
195+
if [[ "$boot_args" != "$2" ]]; then
196+
export BASHUNIT_BOOTSTRAP_ARGS="$boot_args"
197+
fi
198+
# shellcheck disable=SC1090,SC2086
199+
source "$boot_file" ${BASHUNIT_BOOTSTRAP_ARGS:-}
188200
shift
189201
;;
190202
-vvv|--verbose)
@@ -211,8 +223,8 @@ function bashunit::main::cmd_bench() {
211223
fi
212224

213225
# Optional bootstrap
214-
# shellcheck disable=SC1090
215-
[[ -f "${BASHUNIT_BOOTSTRAP:-}" ]] && source "$BASHUNIT_BOOTSTRAP"
226+
# shellcheck disable=SC1090,SC2086
227+
[[ -f "${BASHUNIT_BOOTSTRAP:-}" ]] && source "$BASHUNIT_BOOTSTRAP" ${BASHUNIT_BOOTSTRAP_ARGS:-}
216228

217229
set +euo pipefail
218230

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up_before_script() {
4+
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"
5+
}
6+
7+
function test_bootstrap_receives_arguments_via_env_flag() {
8+
local output
9+
output=$(./bashunit --no-parallel --simple \
10+
--env "tests/acceptance/fixtures/bootstrap_with_args.sh hello world" \
11+
tests/acceptance/fixtures/test_bootstrap_args.sh 2>&1) || true
12+
13+
assert_contains "All tests passed" "$output"
14+
}
15+
16+
function test_bootstrap_without_arguments_still_works() {
17+
local output
18+
output=$(./bashunit --no-parallel --simple --env "$TEST_ENV_FILE" \
19+
tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh 2>&1) || true
20+
21+
assert_contains "All tests passed" "$output"
22+
}
23+
24+
function test_bootstrap_args_via_env_variable() {
25+
local output
26+
# Use --env flag to set the bootstrap file (avoiding .env override),
27+
# but use BASHUNIT_BOOTSTRAP_ARGS from environment
28+
output=$(BASHUNIT_BOOTSTRAP_ARGS="hello world" \
29+
./bashunit --no-parallel --simple \
30+
--env "tests/acceptance/fixtures/bootstrap_with_args.sh" \
31+
tests/acceptance/fixtures/test_bootstrap_args.sh 2>&1) || true
32+
33+
assert_contains "All tests passed" "$output"
34+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
# Bootstrap file that receives arguments and exports them for tests
4+
export BOOTSTRAP_ARG1="${1:-}"
5+
export BOOTSTRAP_ARG2="${2:-}"
6+
export BOOTSTRAP_ALL_ARGS="$*"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
function test_bootstrap_arg1_is_available() {
4+
assert_equals "hello" "$BOOTSTRAP_ARG1"
5+
}
6+
7+
function test_bootstrap_arg2_is_available() {
8+
assert_equals "world" "$BOOTSTRAP_ARG2"
9+
}
10+
11+
function test_all_bootstrap_args_are_available() {
12+
assert_equals "hello world" "$BOOTSTRAP_ALL_ARGS"
13+
}

0 commit comments

Comments
 (0)