Skip to content

Commit c16b95b

Browse files
committed
Change working directory before loading test file
bash_unit changes the current working directory to the one of the code under test just before running the tests. But it was not changing the current working directory while loading the test file. Hence, if one had static code in its test file, this code was run in an unknown working directory. Know bash_unit changes the current working directory before loading the test file.
1 parent 5977cc4 commit c16b95b

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

README.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ Running test_assert_shows_stdout_on_failure... SUCCESS
8686
Running test_assert_status_code_fails... SUCCESS
8787
Running test_assert_status_code_succeeds... SUCCESS
8888
Running test_assert_succeeds... SUCCESS
89-
Running test_bash_unit_changes_cwd_to_current_test_file_directory... SUCCESS
9089
Running test_fail_fails... SUCCESS
9190
Running test_fail_prints_failure_message... SUCCESS
9291
Running test_fail_prints_where_is_error... SUCCESS
@@ -143,7 +142,6 @@ ok - test_assert_shows_stdout_on_failure
143142
ok - test_assert_status_code_fails
144143
ok - test_assert_status_code_succeeds
145144
ok - test_assert_succeeds
146-
ok - test_bash_unit_changes_cwd_to_current_test_file_directory
147145
ok - test_fail_fails
148146
ok - test_fail_prints_failure_message
149147
ok - test_fail_prints_where_is_error

bash_unit

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,13 @@ for test_file in "$@"
348348
do
349349
notify_suite_starting "$test_file"
350350
(
351-
source "$test_file"
352-
cd "$(dirname "$test_file")"
351+
if [[ "${STICK_TO_CWD}" != true ]]
352+
then
353+
cd "$(dirname "$test_file")"
354+
source "$(basename "$test_file")"
355+
else
356+
source "$test_file"
357+
fi
353358
run_test_suite
354359
)
355360
failure=$(( $? || failure))

tests/test_cli.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#!/bin/bash
22

33
test_run_all_tests_even_in_case_of_failure() {
4-
bash_unit_output=$($BASH_UNIT <(cat << EOF
5-
function test_succeed() { assert true ; }
6-
function test_fails() { assert false ; }
7-
EOF
8-
) | sed -e 's:/dev/fd/[0-9]*:test_file:')
9-
10-
assert_equals "\
11-
Running tests in test_file
4+
assert_equals \
5+
"\
6+
Running tests in code
127
Running test_fails... FAILURE
13-
test_file:2:test_fails()
14-
Running test_succeed... SUCCESS" "$bash_unit_output"
8+
code:2:test_fails()
9+
Running test_succeed... SUCCESS\
10+
" \
11+
"$(bash_unit_out_for_code << EOF
12+
function test_succeed() { assert true ; }
13+
function test_fails() { assert false ; }
14+
EOF
15+
)"
1516
}
1617

1718
test_exit_code_not_0_in_case_of_failure() {
@@ -29,11 +30,14 @@ test_run_all_file_parameters() {
2930
| sed -e 's:/dev/fd/[0-9]*:test_file:' \
3031
)
3132

32-
assert_equals "\
33+
assert_equals \
34+
"\
3335
Running tests in test_file
3436
Running test_one... SUCCESS
3537
Running tests in test_file
36-
Running test_two... SUCCESS" "$bash_unit_output"
38+
Running test_two... SUCCESS\
39+
" \
40+
"$bash_unit_output"
3741
}
3842

3943
test_run_only_tests_that_match_pattern() {
@@ -109,4 +113,8 @@ line() {
109113
tail -n +$line_nb | head -1
110114
}
111115

116+
bash_unit_out_for_code() {
117+
$BASH_UNIT <(cat) | sed -e 's:/dev/fd/[0-9]*:code:' -e 's/[0-9]*:/code:/'
118+
}
119+
112120
BASH_UNIT="eval FORCE_COLOR=false ../bash_unit"

tests/test_core.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,19 @@ EOF
138138
assert_equals 2 $(ps | grep pts | wc -l)
139139
}
140140

141-
test_bash_unit_changes_cwd_to_current_test_file_directory() {
141+
if [[ "${STICK_TO_CWD}" != true ]]
142+
then
143+
# do not test for cwd if STICK_TO_CWD is true
144+
test_bash_unit_changes_cwd_to_current_test_file_directory() {
145+
assert "ls ../tests/$(basename "$BASH_SOURCE")" \
146+
"bash_unit should change current working directory to match the directory of the currenlty running test"
147+
}
148+
149+
#the following assertion is out of any test on purpose
142150
assert "ls ../tests/$(basename "$BASH_SOURCE")" \
143-
"bash_unit should change current working directory to match the directory of the currenlty running test"
144-
}
151+
"bash_unit should change current working directory to match the directory of the currenlty running test before sourcing test file"
152+
fi
153+
145154

146155
line() {
147156
line_nb=$1

tests/test_doc.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ TEST_PATTERN='```bash|```test'
44
OUTPUT_PATTERN='```output'
55
LANG=C.UTF-8
66

7-
BASH_UNIT="eval FORCE_COLOR=false ./bash_unit"
7+
export FORCE_COLOR=false
8+
export STICK_TO_CWD=true
9+
BASH_UNIT="eval ./bash_unit"
10+
#BASH_UNIT="eval FORCE_COLOR=false ./bash_unit"
811

912
prepare_tests() {
1013
mkdir /tmp/$$
@@ -62,5 +65,7 @@ function _next_quote_section() {
6265
'
6366
}
6467

65-
cd "$(dirname "$0")"
68+
# change to bash_unit source directory since we should be in
69+
# test subdirectory
70+
cd ..
6671
prepare_tests

tests/test_tap_format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ EOF
102102
}
103103

104104
bash_unit_out_for_code() {
105-
$BASH_UNIT -f tap <(cat) | sed -e 's:/dev/fd/[0-9]*:code:g'
105+
$BASH_UNIT -f tap <(cat) | sed -e 's:/dev/fd/[0-9]*:code:' -e 's/[0-9]*:/code:/'
106106
}
107107

108108
BASH_UNIT="eval FORCE_COLOR=false ../bash_unit"

0 commit comments

Comments
 (0)