Skip to content

Commit 4227fe8

Browse files
authored
Merge pull request #549 from TypedDevs/feat/546-login-shell
Add new option for login shell
2 parents d6c11ad + 2fc3894 commit 4227fe8

File tree

10 files changed

+121
-15
lines changed

10 files changed

+121
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66
- Add bootstrap argument passing support via `--env "file.sh arg1 arg2"` or `BASHUNIT_BOOTSTRAP_ARGS` (fixes #546)
77
- 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)
89
- Add `--strict` flag to enable strict shell mode (`set -euo pipefail`) for tests (fixes #540)
910
- Add `BASHUNIT_STRICT_MODE` configuration option (default: `false`)
1011
- Add `-R, --run-all` flag to run all assertions even when one fails (fixes #536)
@@ -283,7 +284,7 @@
283284
- Enable display execution time on macOS with `SHOW_EXECUTION_TIME`
284285
- Support for displaying the clock without `perl` (for non-macOS)
285286
- Enable strict mode
286-
- Add `-l|--log-junit <log.xml>` option
287+
- Add `--log-junit <log.xml>` option
287288
- Add `-r|--report-html <report.html>` option
288289
- Add `--debug` option
289290
- Add `dump` and `dd` functions for local debugging

bashunit

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

41-
# Early scan for --preserve-env before loading env.sh
41+
# Early scan for flags that must be set before loading env.sh
4242
for arg in "$@"; do
43-
if [[ "$arg" == "--preserve-env" ]]; then
44-
export BASHUNIT_PRESERVE_ENV=true
45-
break
46-
fi
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
4751
done
4852

4953
source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh"

docs/command-line.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bashunit test tests/ --parallel --simple
4848
| `-a, --assert <fn> <args>` | Run a standalone assert function |
4949
| `-e, --env, --boot <file>` | Load custom env/bootstrap file |
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 |
@@ -63,6 +63,7 @@ bashunit test tests/ --parallel --simple
6363
| `--no-output` | Suppress all output |
6464
| `--strict` | Enable strict shell mode |
6565
| `--preserve-env` | Skip `.env` loading, use shell environment only |
66+
| `-l, --login` | Run tests in login shell context |
6667

6768
### Standalone Assert
6869

@@ -216,6 +217,29 @@ BASHUNIT_SIMPLE_OUTPUT=true ./bashunit test tests/ --preserve-env
216217
```
217218
:::
218219

220+
### Login Shell
221+
222+
> `bashunit test -l|--login`
223+
224+
Run tests in a login shell context by sourcing profile files.
225+
226+
When enabled, bashunit sources the following files (if they exist) before each test:
227+
- `/etc/profile`
228+
- `~/.bash_profile`
229+
- `~/.bash_login`
230+
- `~/.profile`
231+
232+
Use this when your tests depend on environment setup from login shell profiles, such as:
233+
- PATH modifications
234+
- Shell functions defined in `.bash_profile`
235+
- Environment variables set during login
236+
237+
::: code-group
238+
```bash [Example]
239+
bashunit test tests/ --login
240+
```
241+
:::
242+
219243
## bench
220244

221245
> `bashunit bench [path] [options]`
@@ -244,6 +268,8 @@ bashunit bench --filter "parse"
244268
| `-s, --simple` | Simple output |
245269
| `--detailed` | Detailed output (default) |
246270
| `-vvv, --verbose` | Show execution details |
271+
| `--preserve-env` | Skip `.env` loading, use shell environment only |
272+
| `-l, --login` | Run in login shell context |
247273

248274
## doc
249275

docs/configuration.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,28 @@ BASHUNIT_PRESERVE_ENV=true ./bashunit tests/
328328
```
329329
:::
330330

331+
## Login shell
332+
333+
> `BASHUNIT_LOGIN_SHELL=true|false`
334+
335+
Run tests in a login shell context by sourcing profile files. `false` by default.
336+
337+
When enabled, bashunit sources the following files (if they exist) before each test:
338+
- `/etc/profile`
339+
- `~/.bash_profile`
340+
- `~/.bash_login`
341+
- `~/.profile`
342+
343+
Use this when your tests depend on environment setup from login shell profiles.
344+
345+
Similar as using `-l|--login` option on the [command line](/command-line#login-shell).
346+
347+
::: code-group
348+
```bash [Example]
349+
BASHUNIT_LOGIN_SHELL=true
350+
```
351+
:::
352+
331353
<script setup>
332354
import pkg from '../package.json'
333355
</script>

src/console_header.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Options:
103103
-a, --assert <fn> <args> Run a standalone assert function
104104
-e, --env, --boot <file> Load a custom env/bootstrap file (supports args)
105105
-f, --filter <name> Only run tests matching the name
106-
-l, --log-junit <file> Write JUnit XML report
106+
--log-junit <file> Write JUnit XML report
107107
-p, --parallel Run tests in parallel (default)
108108
--no-parallel Run tests sequentially
109109
-r, --report-html <file> Write HTML report
@@ -116,6 +116,7 @@ Options:
116116
--no-output Suppress all output
117117
--strict Enable strict shell mode (set -euo pipefail)
118118
--preserve-env Skip .env loading, use shell environment only
119+
-l, --login Run tests in login shell context
119120
-h, --help Show this help message
120121
121122
Examples:
@@ -142,6 +143,7 @@ Options:
142143
--detailed Detailed output (default)
143144
-vvv, --verbose Show execution details
144145
--preserve-env Skip .env loading, use shell environment only
146+
-l, --login Run in login shell context
145147
-h, --help Show this help message
146148
147149
Examples:

src/env.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ _BASHUNIT_DEFAULT_SHOW_INCOMPLETE="false"
3939
_BASHUNIT_DEFAULT_STRICT_MODE="false"
4040
_BASHUNIT_DEFAULT_STOP_ON_ASSERTION_FAILURE="true"
4141
_BASHUNIT_DEFAULT_PRESERVE_ENV="false"
42+
_BASHUNIT_DEFAULT_LOGIN_SHELL="false"
4243

4344
: "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_BASHUNIT_DEFAULT_PARALLEL_RUN}}"
4445
: "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_BASHUNIT_DEFAULT_SHOW_HEADER}}"
@@ -55,6 +56,7 @@ _BASHUNIT_DEFAULT_PRESERVE_ENV="false"
5556
: "${BASHUNIT_STRICT_MODE:=${STRICT_MODE:=$_BASHUNIT_DEFAULT_STRICT_MODE}}"
5657
: "${BASHUNIT_STOP_ON_ASSERTION_FAILURE:=${STOP_ON_ASSERTION_FAILURE:=$_BASHUNIT_DEFAULT_STOP_ON_ASSERTION_FAILURE}}"
5758
: "${BASHUNIT_PRESERVE_ENV:=${PRESERVE_ENV:=$_BASHUNIT_DEFAULT_PRESERVE_ENV}}"
59+
: "${BASHUNIT_LOGIN_SHELL:=${LOGIN_SHELL:=$_BASHUNIT_DEFAULT_LOGIN_SHELL}}"
5860

5961
function bashunit::env::is_parallel_run_enabled() {
6062
[[ "$BASHUNIT_PARALLEL_RUN" == "true" ]]
@@ -120,6 +122,10 @@ function bashunit::env::is_preserve_env_enabled() {
120122
[[ "$BASHUNIT_PRESERVE_ENV" == "true" ]]
121123
}
122124

125+
function bashunit::env::is_login_shell_enabled() {
126+
[[ "$BASHUNIT_LOGIN_SHELL" == "true" ]]
127+
}
128+
123129
function bashunit::env::active_internet_connection() {
124130
if [[ "${BASHUNIT_NO_NETWORK:-}" == "true" ]]; then
125131
return 1
@@ -172,6 +178,7 @@ function bashunit::env::print_verbose() {
172178
"BASHUNIT_STRICT_MODE"
173179
"BASHUNIT_STOP_ON_ASSERTION_FAILURE"
174180
"BASHUNIT_PRESERVE_ENV"
181+
"BASHUNIT_LOGIN_SHELL"
175182
)
176183

177184
local max_length=0

src/main.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function bashunit::main::cmd_test() {
5454
source "$boot_file" ${BASHUNIT_BOOTSTRAP_ARGS:-}
5555
shift
5656
;;
57-
-l|--log-junit)
57+
--log-junit)
5858
export BASHUNIT_LOG_JUNIT="$2"
5959
shift
6060
;;
@@ -87,6 +87,9 @@ function bashunit::main::cmd_test() {
8787
--preserve-env)
8888
export BASHUNIT_PRESERVE_ENV=true
8989
;;
90+
-l|--login)
91+
export BASHUNIT_LOGIN_SHELL=true
92+
;;
9093
*)
9194
raw_args+=("$1")
9295
;;
@@ -208,6 +211,9 @@ function bashunit::main::cmd_bench() {
208211
--preserve-env)
209212
export BASHUNIT_PRESERVE_ENV=true
210213
;;
214+
-l|--login)
215+
export BASHUNIT_LOGIN_SHELL=true
216+
;;
211217
-h|--help)
212218
bashunit::console_header::print_bench_help
213219
exit 0

src/runner.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ function bashunit::runner::run_test() {
397397
trap 'exit_code=$?; bashunit::runner::cleanup_on_exit "$test_file" "$exit_code"' EXIT
398398
bashunit::state::initialize_assertions_count
399399
400+
# Source login shell profiles if enabled
401+
if bashunit::env::is_login_shell_enabled; then
402+
# shellcheck disable=SC1091
403+
[[ -f /etc/profile ]] && source /etc/profile 2>/dev/null || true
404+
# shellcheck disable=SC1090
405+
[[ -f ~/.bash_profile ]] && source ~/.bash_profile 2>/dev/null || true
406+
# shellcheck disable=SC1090
407+
[[ -f ~/.bash_login ]] && source ~/.bash_login 2>/dev/null || true
408+
# shellcheck disable=SC1090
409+
[[ -f ~/.profile ]] && source ~/.profile 2>/dev/null || true
410+
fi
411+
400412
# Run set_up and capture exit code without || to preserve errexit behavior
401413
local setup_exit_code=0
402414
bashunit::runner::run_set_up "$test_file"

tests/acceptance/bashunit_init_test.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
set -euo pipefail
55

6-
TMP_DIR="tmp/init"
6+
BASHUNIT_PATH="$PWD/bashunit"
7+
78
function set_up() {
8-
rm -rf "$TMP_DIR"
9-
mkdir -p "$TMP_DIR"
9+
TMP_DIR=$(mktemp -d)
1010
}
1111

1212
function tear_down() {
@@ -17,7 +17,7 @@ function test_bashunit_init_creates_structure() {
1717
# switch into a clean temporary directory
1818
pushd "$TMP_DIR" >/dev/null
1919
# generate test scaffolding
20-
../../bashunit init > /tmp/init.log
20+
"$BASHUNIT_PATH" init > /tmp/init.log
2121
# perform the assertions
2222
assert_file_exists "tests/example_test.sh"
2323
assert_file_exists "tests/bootstrap.sh"
@@ -27,7 +27,7 @@ function test_bashunit_init_creates_structure() {
2727

2828
function test_bashunit_init_custom_directory() {
2929
pushd "$TMP_DIR" >/dev/null
30-
../../bashunit init custom > /tmp/init.log
30+
"$BASHUNIT_PATH" init custom > /tmp/init.log
3131
assert_file_exists "custom/example_test.sh"
3232
assert_file_exists "custom/bootstrap.sh"
3333
popd >/dev/null
@@ -38,7 +38,7 @@ function test_bashunit_init_updates_env() {
3838

3939
pushd "$TMP_DIR" >/dev/null
4040
echo "BASHUNIT_BOOTSTRAP=old/bootstrap.sh" > .env
41-
../../bashunit init custom > /tmp/init.log
41+
"$BASHUNIT_PATH" init custom > /tmp/init.log
4242
assert_file_exists "custom/example_test.sh"
4343
assert_file_exists "custom/bootstrap.sh"
4444
assert_file_contains .env "#BASHUNIT_BOOTSTRAP=old/bootstrap.sh"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
function test_login_flag_works_without_errors() {
4+
local output
5+
output=$(./bashunit --no-parallel --simple --login \
6+
tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh 2>&1) || true
7+
8+
assert_contains "All tests passed" "$output"
9+
}
10+
11+
function test_login_flag_short_form_works() {
12+
local output
13+
output=$(./bashunit --no-parallel --simple -l \
14+
tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh 2>&1) || true
15+
16+
assert_contains "All tests passed" "$output"
17+
}
18+
19+
function test_login_via_env_var() {
20+
local output
21+
output=$(BASHUNIT_LOGIN_SHELL=true \
22+
./bashunit --no-parallel --simple \
23+
tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh 2>&1) || true
24+
25+
assert_contains "All tests passed" "$output"
26+
}

0 commit comments

Comments
 (0)