Skip to content

Commit 7732279

Browse files
committed
FIX #18: bash_unit changes current working directory to current test file
1 parent 525d301 commit 7732279

File tree

7 files changed

+31
-34
lines changed

7 files changed

+31
-34
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Running test_assert_status_code_fails... SUCCESS
6161
Running test_assert_status_code_shows_stdout_stderr_on_failure... SUCCESS
6262
Running test_assert_status_code_succeeds... SUCCESS
6363
Running test_assert_succeeds... SUCCESS
64+
Running test_bash_unit_changes_cwd_to_current_test_file_directory... SUCCESS
6465
Running test_bash_unit_runs_teardown_even_in_case_of_failure... SUCCESS
6566
Running test_bash_unit_succeed_when_no_failure_even_if_no_teardown... SUCCESS
6667
Running test_display_usage_when_test_file_does_not_exist... SUCCESS
@@ -119,6 +120,8 @@ You may write a *teardown* function that will be exectuted after each test is ru
119120

120121
If you need to set someting up only once for all tests, simply write your code outside any test function, this is a bash script.
121122

123+
bash_unit changes the current working directory to the one of the running test file. If you need to access files from your test code, for instance the script under test, use path relative to the test file.
124+
122125
You may need to change the behavior of some commands to create conditions for your code under test to behave as expected. The *fake* function may help you to do that, see bellow.
123126

124127
# Test functions

bash_unit

+5-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ failure=0
202202
for test_file in "$@"
203203
do
204204
echo "Running tests in $test_file"
205-
(source "$test_file"; run_test_suite)
205+
(
206+
source "$test_file"
207+
cd $(dirname "$test_file")
208+
run_test_suite
209+
)
206210
failure=$(( $? || $failure))
207211
done
208212
exit $failure
+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
test_should_print_bonjour_monde() {
2-
assert_equals "Bonjour monde !" "$(./bonjour_monde)"
2+
assert_equals "Bonjour monde !" "$(../bonjour_monde)"
33
}
4-
5-
# Change to directory where bash_unit so
6-
# that we can use our scripts under test
7-
# more easily
8-
9-
cd $(dirname $0)
+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
test_should_print_hello_world() {
2-
assert_equals "Hello world!" "$(./hello_world)"
2+
assert_equals "Hello world!" "$(../hello_world)"
33
}
4-
5-
# Change to directory where bash_unit so
6-
# that we can use our scripts under test
7-
# more easily
8-
9-
cd $(dirname 0)
+2-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
test_no_ablo_espanol() {
2-
assert_fail ./hola_mundo
2+
assert_fail ../hola_mundo
33
}
44

55
test_should_tell_no_ablo_espanol() {
6-
assert_equals "No ablo espanol" "$(./hola_mundo 2>&1)"
6+
assert_equals "No ablo espanol" "$(../hola_mundo 2>&1)"
77
}
8-
9-
# Change to directory where bash_unit so
10-
# that we can use our scripts under test
11-
# more easily
12-
13-
cd $(dirname $0)

tests/test_bash_unit.sh

+16-9
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ EOF
148148
}
149149

150150
test_run_all_tests_even_in_case_of_failure() {
151-
bash_unit_output=$($0 <(cat << EOF
151+
bash_unit_output=$($BASH_UNIT <(cat << EOF
152152
function test_succeed() { assert true ; }
153153
function test_fails() { assert false ; }
154154
EOF
@@ -162,15 +162,15 @@ Running test_succeed... SUCCESS" "$bash_unit_output"
162162
}
163163

164164
test_exit_code_not_0_in_case_of_failure() {
165-
assert_fail "$0 <(cat << EOF
165+
assert_fail "$BASH_UNIT <(cat << EOF
166166
function test_succeed() { assert true ; }
167167
function test_fails() { assert false ; }
168168
EOF
169169
)"
170170
}
171171

172172
test_run_all_file_parameters() {
173-
bash_unit_output=$($0 \
173+
bash_unit_output=$($BASH_UNIT \
174174
<(echo "test_one() { echo -n ; }") \
175175
<(echo "test_two() { echo -n ; }") \
176176
| sed -e 's:/dev/fd/[0-9]*:test_file:' \
@@ -184,7 +184,7 @@ Running test_two... SUCCESS" "$bash_unit_output"
184184
}
185185

186186
test_run_only_tests_that_match_pattern() {
187-
bash_unit_output=$($0 -p one \
187+
bash_unit_output=$($BASH_UNIT -p one \
188188
<(echo "test_one() { echo -n ; }") \
189189
<(echo "test_two() { echo -n ; }") \
190190
| sed -e 's:/dev/fd/[0-9]*:test_file:' \
@@ -197,35 +197,42 @@ Running tests in test_file" "$bash_unit_output"
197197
}
198198

199199
test_fails_when_test_file_does_not_exist() {
200-
assert_fail "$0 /not_exist/not_exist"
200+
assert_fail "$BASH_UNIT /not_exist/not_exist"
201201
}
202202

203203
test_display_usage_when_test_file_does_not_exist() {
204-
bash_unit_output=$($0 /not_exist/not_exist 2>&1 >/dev/null | line 1)
204+
bash_unit_output=$($BASH_UNIT /not_exist/not_exist 2>&1 >/dev/null | line 1)
205205

206206
assert_equals "file does not exist: /not_exist/not_exist"\
207207
"$bash_unit_output"
208208
}
209209

210210
test_bash_unit_succeed_when_no_failure_even_if_no_teardown() {
211211
#FIX https://github.com/pgrange/bash_unit/issues/8
212-
assert "$0 <(echo 'test_success() { echo -n ; }')"
212+
assert "$BASH_UNIT <(echo 'test_success() { echo -n ; }')"
213213
}
214214

215215
test_bash_unit_runs_teardown_even_in_case_of_failure() {
216216
#FIX https://github.com/pgrange/bash_unit/issues/10
217217
assert_equals "ran teardown" \
218-
"$($0 <(echo 'test_fail() { fail ; } ; teardown() { echo "ran teardown" >&2 ; }') 2>&1 >/dev/null)"
218+
"$($BASH_UNIT <(echo 'test_fail() { fail ; } ; teardown() { echo "ran teardown" >&2 ; }') 2>&1 >/dev/null)"
219219
}
220220

221221
test_one_test_should_stop_after_first_assertion_failure() {
222222
#FIX https://github.com/pgrange/bash_unit/issues/10
223223
assert_equals "before failure" \
224-
"$($0 <(echo 'test_fail() { echo "before failure" >&2 ; fail ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)"
224+
"$($BASH_UNIT <(echo 'test_fail() { echo "before failure" >&2 ; fail ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)"
225225

226226
}
227227

228+
test_bash_unit_changes_cwd_to_current_test_file_directory() {
229+
assert "ls ../tests/$(basename $BASH_SOURCE)" \
230+
"bash_unit should change current working directory to match the directory of the currenlty running test"
231+
}
232+
228233
line() {
229234
line_nb=$1
230235
tail -n +$line_nb | head -1
231236
}
237+
238+
BASH_UNIT=../bash_unit

tests/test_doc.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TEST_PATTERN='```bash|```test'
44
OUTPUT_PATTERN='```output'
55
LANG=C
66

7-
cd $(dirname $0)
7+
BASH_UNIT=./bash_unit
88

99
prepare_tests() {
1010
mkdir /tmp/$$
@@ -29,7 +29,7 @@ prepare_tests() {
2929
function run_doc_test() {
3030
local remaining="$1"
3131
local swap="$2"
32-
$0 <(
32+
$BASH_UNIT <(
3333
cat "$remaining" | _next_code "$swap"
3434
) | tail -n +2 | sed -e 's:/dev/fd/[0-9]*:doc:g'
3535
cat "$swap" > "$remaining"
@@ -62,4 +62,5 @@ function _next_quote_section() {
6262
'
6363
}
6464

65+
cd $(dirname $0)
6566
prepare_tests

0 commit comments

Comments
 (0)