Skip to content

Commit 89c8b50

Browse files
committed
Merge branch 'main' into add-assert-subcommand
2 parents 79b9fbc + 30b6424 commit 89c8b50

15 files changed

+404
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
## Unreleased
44

55
### Added
6+
- Add bootstrap argument passing support via `--env "file.sh arg1 arg2"` or `BASHUNIT_BOOTSTRAP_ARGS` (fixes #546)
7+
- Add `--preserve-env` flag to skip `.env` loading and use shell environment only (fixes #546)
8+
- Add `-l, --login` flag to run tests in login shell context (fixes #546)
69
- Add `--strict` flag to enable strict shell mode (`set -euo pipefail`) for tests (fixes #540)
710
- Add `BASHUNIT_STRICT_MODE` configuration option (default: `false`)
811
- Add `-R, --run-all` flag to run all assertions even when one fails (fixes #536)
@@ -281,7 +284,7 @@
281284
- Enable display execution time on macOS with `SHOW_EXECUTION_TIME`
282285
- Support for displaying the clock without `perl` (for non-macOS)
283286
- Enable strict mode
284-
- Add `-l|--log-junit <log.xml>` option
287+
- Add `--log-junit <log.xml>` option
285288
- Add `-r|--report-html <report.html>` option
286289
- Add `--debug` option
287290
- Add `dump` and `dd` functions for local debugging

bashunit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export BASHUNIT_ROOT_DIR
3838
declare -r BASHUNIT_WORKING_DIR="$PWD"
3939
export BASHUNIT_WORKING_DIR
4040

41+
# Early scan for flags that must be set before loading env.sh
42+
for arg in "$@"; do
43+
case "$arg" in
44+
--preserve-env)
45+
export BASHUNIT_PRESERVE_ENV=true
46+
;;
47+
-l|--login)
48+
export BASHUNIT_LOGIN_SHELL=true
49+
;;
50+
esac
51+
done
52+
4153
source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh"
4254
source "$BASHUNIT_ROOT_DIR/src/check_os.sh"
4355
source "$BASHUNIT_ROOT_DIR/src/str.sh"

docs/command-line.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ bashunit test tests/ --parallel --simple
4646
| Option | Description |
4747
|--------|-------------|
4848
| `-a, --assert <fn> <args>` | Run a standalone assert function |
49-
| `-e, --env, --boot <file>` | Load custom env/bootstrap file |
49+
| `-e, --env, --boot <file>` | Load custom env/bootstrap file (supports args) |
5050
| `-f, --filter <name>` | Only run tests matching name |
51-
| `-l, --log-junit <file>` | Write JUnit XML report |
51+
| `--log-junit <file>` | Write JUnit XML report |
5252
| `-p, --parallel` | Run tests in parallel (default) |
5353
| `--no-parallel` | Run tests sequentially |
5454
| `-r, --report-html <file>` | Write HTML report |
@@ -62,6 +62,8 @@ bashunit test tests/ --parallel --simple
6262
| `--debug [file]` | Enable shell debug mode |
6363
| `--no-output` | Suppress all output |
6464
| `--strict` | Enable strict shell mode |
65+
| `--preserve-env` | Skip `.env` loading, use shell environment only |
66+
| `-l, --login` | Run tests in login shell context |
6567

6668
### Standalone Assert
6769

@@ -92,6 +94,42 @@ bashunit test tests/ --filter "user_login"
9294
```
9395
:::
9496

97+
### Environment / Bootstrap
98+
99+
> `bashunit test -e|--env|--boot <file>`
100+
> `bashunit test --env "file arg1 arg2"`
101+
102+
Load a custom environment or bootstrap file before running tests.
103+
104+
::: code-group
105+
```bash [Basic usage]
106+
bashunit test --env tests/bootstrap.sh tests/
107+
```
108+
```bash [With arguments]
109+
# Pass arguments to the bootstrap file
110+
bashunit test --env "tests/bootstrap.sh staging verbose" tests/
111+
```
112+
:::
113+
114+
Arguments are available as positional parameters (`$1`, `$2`, etc.) in your bootstrap script:
115+
116+
```bash
117+
#!/usr/bin/env bash
118+
# tests/bootstrap.sh
119+
ENVIRONMENT="${1:-production}"
120+
VERBOSE="${2:-false}"
121+
122+
export API_URL="https://${ENVIRONMENT}.api.example.com"
123+
```
124+
125+
You can also set arguments via environment variable:
126+
127+
```bash
128+
BASHUNIT_BOOTSTRAP_ARGS="staging verbose" bashunit test tests/
129+
```
130+
131+
See [Configuration: Bootstrap](/configuration#bootstrap) for more details.
132+
95133
### Inline Filter Syntax
96134

97135
You can also specify a filter directly in the file path using `::` or `:line` syntax:
@@ -197,6 +235,47 @@ bashunit test tests/ --strict
197235
```
198236
:::
199237

238+
### Preserve Environment
239+
240+
> `bashunit test --preserve-env`
241+
242+
Skip loading the `.env` file and use the current shell environment only.
243+
244+
By default, bashunit loads variables from `.env` which can override environment
245+
variables set in your shell. Use `--preserve-env` when you want to:
246+
- Run in CI/CD where environment is pre-configured
247+
- Override `.env` values with shell environment variables
248+
- Avoid `.env` interfering with your current settings
249+
250+
::: code-group
251+
```bash [Example]
252+
BASHUNIT_SIMPLE_OUTPUT=true ./bashunit test tests/ --preserve-env
253+
```
254+
:::
255+
256+
### Login Shell
257+
258+
> `bashunit test -l|--login`
259+
260+
Run tests in a login shell context by sourcing profile files.
261+
262+
When enabled, bashunit sources the following files (if they exist) before each test:
263+
- `/etc/profile`
264+
- `~/.bash_profile`
265+
- `~/.bash_login`
266+
- `~/.profile`
267+
268+
Use this when your tests depend on environment setup from login shell profiles, such as:
269+
- PATH modifications
270+
- Shell functions defined in `.bash_profile`
271+
- Environment variables set during login
272+
273+
::: code-group
274+
```bash [Example]
275+
bashunit test tests/ --login
276+
```
277+
:::
278+
200279
## bench
201280

202281
> `bashunit bench [path] [options]`
@@ -220,11 +299,13 @@ bashunit bench --filter "parse"
220299

221300
| Option | Description |
222301
|--------|-------------|
223-
| `-e, --env, --boot <file>` | Load custom env/bootstrap file |
302+
| `-e, --env, --boot <file>` | Load custom env/bootstrap file (supports args) |
224303
| `-f, --filter <name>` | Only run benchmarks matching name |
225304
| `-s, --simple` | Simple output |
226305
| `--detailed` | Detailed output (default) |
227306
| `-vvv, --verbose` | Show execution details |
307+
| `--preserve-env` | Skip `.env` loading, use shell environment only |
308+
| `-l, --login` | Run in login shell context |
228309

229310
## doc
230311

docs/common-patterns.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,52 @@ export DB_NAME=test_db
660660
```
661661
:::
662662

663+
### Bootstrap Files with Arguments
664+
665+
Pass configuration to your bootstrap file for different test scenarios:
666+
667+
::: code-group
668+
```bash [tests/bootstrap.sh]
669+
#!/usr/bin/env bash
670+
671+
# Receive arguments passed to bootstrap
672+
ENVIRONMENT="${1:-production}"
673+
VERBOSE="${2:-false}"
674+
675+
# Configure based on environment
676+
case "$ENVIRONMENT" in
677+
staging)
678+
export API_URL="https://staging.api.example.com"
679+
export DB_NAME="test_staging_db"
680+
;;
681+
ci)
682+
export API_URL="https://ci.api.example.com"
683+
export DB_NAME="test_ci_db"
684+
;;
685+
*)
686+
export API_URL="https://api.example.com"
687+
export DB_NAME="test_db"
688+
;;
689+
esac
690+
691+
[[ "$VERBOSE" == "true" ]] && export LOG_LEVEL=debug
692+
```
693+
694+
```bash [Run with arguments]
695+
# Pass arguments inline with --env
696+
./bashunit --env "tests/bootstrap.sh staging true" tests/
697+
698+
# Or via environment variable
699+
BASHUNIT_BOOTSTRAP_ARGS="staging true" ./bashunit tests/
700+
```
701+
:::
702+
703+
::: tip
704+
Use bootstrap arguments to avoid duplicating bootstrap files for different
705+
environments. A single bootstrap file can configure staging, CI, or local
706+
development based on the arguments passed.
707+
:::
708+
663709
## Testing Private Functions
664710

665711
When you need to test functions that aren't exported:

docs/configuration.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,33 @@ BASHUNIT_BOOTSTRAP="tests/bootstrap.sh"
237237
```
238238
:::
239239

240+
## Bootstrap arguments
241+
242+
> `BASHUNIT_BOOTSTRAP_ARGS=arguments`
243+
244+
Pass arguments to the bootstrap file. Arguments are space-separated and available
245+
as positional parameters (`$1`, `$2`, etc.) in your bootstrap script.
246+
247+
::: code-group
248+
```bash [.env]
249+
BASHUNIT_BOOTSTRAP="tests/bootstrap.sh"
250+
BASHUNIT_BOOTSTRAP_ARGS="staging verbose"
251+
```
252+
```bash [bootstrap.sh]
253+
#!/usr/bin/env bash
254+
ENVIRONMENT="${1:-production}"
255+
VERBOSE="${2:-false}"
256+
257+
export API_URL="https://${ENVIRONMENT}.api.example.com"
258+
```
259+
:::
260+
261+
You can also pass arguments inline via the [--env](/command-line#environment) option:
262+
263+
```bash
264+
bashunit --env "tests/bootstrap.sh staging verbose" tests/
265+
```
266+
240267
## Dev log
241268

242269
> `BASHUNIT_DEV_LOG=file`
@@ -310,6 +337,46 @@ BASHUNIT_STRICT_MODE=true
310337
```
311338
:::
312339

340+
## Preserve environment
341+
342+
> `BASHUNIT_PRESERVE_ENV=true|false`
343+
344+
Skip loading the `.env` file and use the current shell environment only. `false` by default.
345+
346+
By default, bashunit loads variables from `.env` which can override environment
347+
variables set in your shell. Enable this option when running in CI/CD pipelines
348+
or when you want shell environment variables to take precedence.
349+
350+
Similar as using `--preserve-env` option on the [command line](/command-line#preserve-environment).
351+
352+
::: code-group
353+
```bash [Example]
354+
BASHUNIT_PRESERVE_ENV=true ./bashunit tests/
355+
```
356+
:::
357+
358+
## Login shell
359+
360+
> `BASHUNIT_LOGIN_SHELL=true|false`
361+
362+
Run tests in a login shell context by sourcing profile files. `false` by default.
363+
364+
When enabled, bashunit sources the following files (if they exist) before each test:
365+
- `/etc/profile`
366+
- `~/.bash_profile`
367+
- `~/.bash_login`
368+
- `~/.profile`
369+
370+
Use this when your tests depend on environment setup from login shell profiles.
371+
372+
Similar as using `-l|--login` option on the [command line](/command-line#login-shell).
373+
374+
::: code-group
375+
```bash [Example]
376+
BASHUNIT_LOGIN_SHELL=true
377+
```
378+
:::
379+
313380
<script setup>
314381
import pkg from '../package.json'
315382
</script>

src/console_header.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ Arguments:
103103
104104
Options:
105105
-a, --assert <fn> <args> Run a standalone assert function (deprecated: use 'bashunit assert')
106-
-e, --env, --boot <file> Load a custom env/bootstrap file
106+
-e, --env, --boot <file> Load a custom env/bootstrap file (supports args)
107107
-f, --filter <name> Only run tests matching the name
108-
-l, --log-junit <file> Write JUnit XML report
108+
--log-junit <file> Write JUnit XML report
109109
-p, --parallel Run tests in parallel (default)
110110
--no-parallel Run tests sequentially
111111
-r, --report-html <file> Write HTML report
@@ -117,6 +117,8 @@ Options:
117117
--debug [file] Enable shell debug mode
118118
--no-output Suppress all output
119119
--strict Enable strict shell mode (set -euo pipefail)
120+
--preserve-env Skip .env loading, use shell environment only
121+
-l, --login Run tests in login shell context
120122
-h, --help Show this help message
121123
122124
Examples:
@@ -137,11 +139,13 @@ Arguments:
137139
path File or directory containing benchmarks
138140
139141
Options:
140-
-e, --env, --boot <file> Load a custom env/bootstrap file
142+
-e, --env, --boot <file> Load a custom env/bootstrap file (supports args)
141143
-f, --filter <name> Only run benchmarks matching the name
142144
-s, --simple Simple output
143145
--detailed Detailed output (default)
144146
-vvv, --verbose Show execution details
147+
--preserve-env Skip .env loading, use shell environment only
148+
-l, --login Run in login shell context
145149
-h, --help Show this help message
146150
147151
Examples:

0 commit comments

Comments
 (0)