diff --git a/tests/unit/learn_test.sh b/tests/unit/learn_test.sh index 064cd87b..775a490c 100644 --- a/tests/unit/learn_test.sh +++ b/tests/unit/learn_test.sh @@ -14,15 +14,28 @@ function _learn_in_sandbox() { # Resolve the repo root from this test file, not $BASHUNIT_ROOT_DIR: under # `build.sh --verify` the running binary's root dir has no src/ (#834). root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" + # stdin is /dev/null so the `read -p "Press Enter to continue..."` prompts in + # the interactive paths hit EOF and return instead of blocking the suite. + # BASHUNIT_ROOT_DIR is set INSIDE the child (run_lesson_test shells out to the + # entrypoint through it): it is readonly in the caller, so a `VAR=x cmd` prefix + # assignment would abort with "readonly variable". ( cd "$sandbox" && HOME="$sandbox" bash -c ' set -euo pipefail + BASHUNIT_ROOT_DIR="'"$root_dir"'" + export BASHUNIT_ROOT_DIR GREP="$(command -v grep)" + # learn.sh renders coloured output but does not source colors.sh. Stub + # the palette to empty rather than pulling in the real chain + # (str -> globals -> env -> colors), which would couple this unit test to + # source order and make the assertions match escape codes. + _BASHUNIT_COLOR_BOLD="" _BASHUNIT_COLOR_DEFAULT="" _BASHUNIT_COLOR_FAILED="" + _BASHUNIT_COLOR_FAINT="" _BASHUNIT_COLOR_INCOMPLETE="" _BASHUNIT_COLOR_PASSED="" # shellcheck source=/dev/null source "'"$root_dir"'/src/learn.sh" '"$1"' - ' + ' /dev/null || true + if ! bashunit::learn::is_completed "lesson_4"; then echo cleared; fi + ')" + + assert_same "cleared" "$out" +} + +function test_learn_reset_progress_is_safe_without_a_progress_file() { + local out + out="$(_learn_in_sandbox ' + bashunit::learn::reset_progress >/dev/null || true + echo safe + ')" + + assert_same "safe" "$out" +} + +function test_learn_create_example_file_writes_and_makes_it_executable() { + local out + out="$(_learn_in_sandbox ' + bashunit::learn::create_example_file "sample_test.sh" "#!/usr/bin/env bash" >/dev/null || true + [ -f sample_test.sh ] && echo exists + [ -x sample_test.sh ] && echo executable + cat sample_test.sh + ')" + + assert_same $'exists\nexecutable\n#!/usr/bin/env bash' "$out" +} + +function test_learn_run_lesson_test_marks_the_lesson_completed_when_it_passes() { + local out + out="$(_learn_in_sandbox ' + printf "#!/usr/bin/env bash\n\nfunction test_ok() {\n assert_same 1 1\n}\n" >pass_test.sh + bashunit::learn::run_lesson_test "pass_test.sh" "2" >/dev/null 2>&1 || true + if bashunit::learn::is_completed "lesson_2"; then echo completed; fi + ')" + + assert_same "completed" "$out" +} + +function test_learn_run_lesson_test_does_not_mark_the_lesson_when_it_fails() { + local out + out="$(_learn_in_sandbox ' + printf "#!/usr/bin/env bash\n\nfunction test_bad() {\n assert_same 1 2\n}\n" >fail_test.sh + bashunit::learn::run_lesson_test "fail_test.sh" "5" >/dev/null 2>&1 || true + if ! bashunit::learn::is_completed "lesson_5"; then echo not_completed; fi + ')" + + assert_same "not_completed" "$out" +}