tests: refactor mechanics of testing in a sub test-lib
[gitweb.git] / t / t0000-basic.sh
index c6b42deef9ce9f381d51971376a294192cafd888..d0f46e8b25aa676415bdb1638eae68d1fa8b8f65 100755 (executable)
@@ -55,39 +55,53 @@ test_expect_failure 'pretend we have a known breakage' '
        false
 '
 
-test_expect_success 'pretend we have fixed a known breakage (run in sub test-lib)' "
-       mkdir passing-todo &&
-       (cd passing-todo &&
-       cat >passing-todo.sh <<-EOF &&
-       #!$SHELL_PATH
-
-       test_description='A passing TODO test
-
-       This is run in a sub test-lib so that we do not get incorrect
-       passing metrics
-       '
-
-       # Point to the t/test-lib.sh, which isn't in ../ as usual
-       TEST_DIRECTORY=\"$TEST_DIRECTORY\"
-       . \"\$TEST_DIRECTORY\"/test-lib.sh
+run_sub_test_lib_test () {
+       name="$1" descr="$2" # stdin is the body of the test code
+       mkdir "$name" &&
+       (
+               cd "$name" &&
+               cat >"$name.sh" <<-EOF &&
+               #!$SHELL_PATH
+
+               test_description='$descr (run in sub test-lib)
+
+               This is run in a sub test-lib so that we do not get incorrect
+               passing metrics
+               '
+
+               # Point to the t/test-lib.sh, which isn't in ../ as usual
+               . "\$TEST_DIRECTORY"/test-lib.sh
+               EOF
+               cat >>"$name.sh" &&
+               chmod +x "$name.sh" &&
+               export TEST_DIRECTORY &&
+               ./"$name.sh" >out 2>err
+       )
+}
 
-       test_expect_failure 'pretend we have fixed a known breakage' '
-               :
-       '
+check_sub_test_lib_test () {
+       name="$1" # stdin is the expected output from the test
+       (
+               cd "$name" &&
+               ! test -s err &&
+               sed -e 's/^> //' -e 's/Z$//' >expect &&
+               test_cmp expect out
+       )
+}
 
+test_expect_success 'pretend we have fixed a known breakage' "
+       run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
+       test_expect_failure 'pretend we have fixed a known breakage' 'true'
        test_done
        EOF
-       chmod +x passing-todo.sh &&
-       ./passing-todo.sh >out 2>err &&
-       ! test -s err &&
-       sed -e 's/^> //' >expect <<-\\EOF &&
+       check_sub_test_lib_test passing-todo <<-\\EOF
        > ok 1 - pretend we have fixed a known breakage # TODO known breakage
        > # fixed 1 known breakage(s)
        > # passed all 1 test(s)
        > 1..1
        EOF
-       test_cmp expect out)
 "
+
 test_set_prereq HAVEIT
 haveit=no
 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
@@ -137,19 +151,8 @@ then
 fi
 
 test_expect_success 'tests clean up even on failures' "
-       mkdir failing-cleanup &&
-       (
-       cd failing-cleanup &&
-
-       cat >failing-cleanup.sh <<-EOF &&
-       #!$SHELL_PATH
-
-       test_description='Failing tests with cleanup commands'
-
-       # Point to the t/test-lib.sh, which isn't in ../ as usual
-       TEST_DIRECTORY=\"$TEST_DIRECTORY\"
-       . \"\$TEST_DIRECTORY\"/test-lib.sh
-
+       test_must_fail run_sub_test_lib_test \
+               failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
        test_expect_success 'tests clean up even after a failure' '
                touch clean-after-failure &&
                test_when_finished rm clean-after-failure &&
@@ -159,14 +162,8 @@ test_expect_success 'tests clean up even on failures' "
                test_when_finished \"(exit 2)\"
        '
        test_done
-
        EOF
-
-       chmod +x failing-cleanup.sh &&
-       test_must_fail ./failing-cleanup.sh >out 2>err &&
-       ! test -s err &&
-       ! test -f \"trash directory.failing-cleanup/clean-after-failure\" &&
-       sed -e 's/Z$//' -e 's/^> //' >expect <<-\\EOF &&
+       check_sub_test_lib_test failing-cleanup <<-\\EOF
        > not ok 1 - tests clean up even after a failure
        > #     Z
        > #     touch clean-after-failure &&
@@ -180,8 +177,6 @@ test_expect_success 'tests clean up even on failures' "
        > # failed 2 among 2 test(s)
        > 1..2
        EOF
-       test_cmp expect out
-       )
 "
 
 ################################################################