Skip to content

Commit fa6bec4

Browse files
committed
Moving getting_started folder to a dedicated project.
The getting_started directory formerly stored in the bash_unit repository now gets its own repo: https://github.com/bash-unit/getting_started
1 parent dff8e15 commit fa6bec4

12 files changed

+151
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This getting started project gives you a few example of tests you may write
2+
with [bash_unit](https://github.com/bash-unit/bash_unit/).
3+
4+
You can play with these tests by, first, [installing bash_unit](https://github.com/bash-unit/bash_unit/blob/main/README.adoc#how-to-install-bash_unit) and then git clone this project and run the tests:
5+
6+
```bash
7+
git clone https://github.com/bash-unit/getting_started.git
8+
cd getting_started
9+
bash_unit tests/test_*
10+
```

bonjour_monde

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Bonjour monde !"

hello_i18n

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
main() {
4+
hello "$1"
5+
}
6+
7+
hello() {
8+
case "$1" in
9+
fr) echo Bonjour le monde
10+
;;
11+
*) echo we only speak french for the moment, sorry >&2
12+
;;
13+
esac
14+
}
15+
16+
# do not run main when sourcing the script
17+
if [[ "$0" == "${BASH_SOURCE[0]}" ]]
18+
then
19+
main "$@"
20+
else
21+
true
22+
fi

hello_timed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
declare -F current_hour >/dev/null || current_hour() {
4+
date +%H
5+
}
6+
7+
if (( "$(current_hour)" < 11 ))
8+
then
9+
echo "Good morning World!"
10+
else
11+
if (( "$(current_hour)" > 17 ))
12+
then
13+
echo "Good evening World!"
14+
else
15+
echo "Hello World!"
16+
fi
17+
fi

hello_world

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Hello world!"

hola_mundo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
echo "No ablo espanol" >&2
4+
exit 1

tests/test_bonjour_monde

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_should_print_bonjour_monde() {
2+
assert_equals "Bonjour monde !" "$(../bonjour_monde)"
3+
}

tests/test_hello_i18n

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# In this example, we want to test the specific functions of a script.
2+
# So we source this script in setup_suite.
3+
# However, we don't want the script to be executed when sourcing it,
4+
# we are only interested in having access to the functions to test
5+
# them.
6+
# To do so, we adapt the script under test so that its main code
7+
# is not executed when sourcing it.
8+
# See in ../hello_i18n how we define a main function to encapsulate
9+
# all the main code and the main function is only called when the
10+
# script itself is launched, not when it is sourced.
11+
12+
test_can_say_hi_in_french() {
13+
assert_equals "Bonjour le monde" "$(hello fr)"
14+
}
15+
16+
setup_suite() {
17+
source ../hello_i18n
18+
}

tests/test_hello_timed

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# In this example, we want to test a script which depends on
2+
# the external world (here, the current time). To do so,
3+
# we decide to adapt the script by extracting the time
4+
# querying in a dedicated function named current_hour.
5+
# That way, we can fake the current hour in the test.
6+
# Unfortunately, we also need to adapt the script under
7+
# test so that, when the current_time function is already
8+
# defined, the script does not override this definition.
9+
# Otherwise, the fake created in the test would never be
10+
# used.
11+
# See ../hello_timed
12+
13+
14+
test_can_say_good_morning() {
15+
it_is_o_clock 10
16+
assert_equals "Good morning World!" "$(../hello_timed)"
17+
}
18+
19+
test_can_say_good_evening() {
20+
it_is_o_clock 18
21+
assert_equals "Good evening World!" "$(../hello_timed)"
22+
}
23+
24+
test_can_say_hello() {
25+
it_is_o_clock 12
26+
assert_equals "Hello World!" "$(../hello_timed)"
27+
}
28+
29+
it_is_o_clock() {
30+
local hour="$1"
31+
fake current_hour << EOF
32+
$hour
33+
EOF
34+
}

tests/test_hello_world

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_should_print_hello_world() {
2+
assert_equals "Hello world!" "$(../hello_world)"
3+
}

tests/test_no_ablo_espanol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_no_ablo_espanol() {
2+
assert_fail ../hola_mundo
3+
}
4+
5+
test_should_tell_no_ablo_espanol() {
6+
assert_equals "No ablo espanol" "$(../hola_mundo 2>&1)"
7+
}

tests/test_skipping

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This test demonstrates some test skipping logic that
2+
# one can implement with bash_unit
3+
4+
todo_will_always_be_skipped() {
5+
fail "This test is always skipped"
6+
}
7+
8+
skip_if "uname | grep Darwin" linux
9+
skip_if "uname | grep Linux" darwin
10+
11+
test_linux_proc_exists() {
12+
assert "ls /proc/" "there should exist /proc on Linux"
13+
}
14+
test_another_test_to_run_only_on_linux() {
15+
assert "mkdir /tmp/foo/bar -p"
16+
}
17+
18+
test_that_always_run() {
19+
assert true
20+
}
21+
22+
test_darwin_proc_does_not_exist() {
23+
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
24+
}
25+
test_another_test_to_run_only_on_darwin() {
26+
assert "mkdir -p /tmp/foo/bar"
27+
}

0 commit comments

Comments
 (0)