Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 1 | write_sub_test_lib_test () { |
Ævar Arnfjörð Bjarmason | c3ff7be | 2021-09-22 13:19:49 +0200 | [diff] [blame] | 2 | name="$1" # stdin is the body of the test code |
Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 3 | mkdir "$name" && |
| 4 | write_script "$name/$name.sh" "$TEST_SHELL_PATH" <<-EOF && |
Ævar Arnfjörð Bjarmason | c3ff7be | 2021-09-22 13:19:49 +0200 | [diff] [blame] | 5 | test_description='A test of test-lib.sh itself' |
Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 6 | |
| 7 | # Point to the t/test-lib.sh, which isn't in ../ as usual |
| 8 | . "\$TEST_DIRECTORY"/test-lib.sh |
| 9 | EOF |
| 10 | cat >>"$name/$name.sh" |
| 11 | } |
| 12 | |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 13 | _run_sub_test_lib_test_common () { |
Ævar Arnfjörð Bjarmason | 56722a0 | 2021-09-22 13:19:52 +0200 | [diff] [blame] | 14 | cmp_op="$1" want_code="$2" name="$3" # stdin is the body of the test code |
| 15 | shift 3 |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 16 | |
| 17 | # intercept pseudo-options at the front of the argument list that we |
| 18 | # will not pass to child script |
| 19 | skip= |
| 20 | while test $# -gt 0 |
| 21 | do |
| 22 | case "$1" in |
| 23 | --skip=*) |
| 24 | skip=${1#--*=} |
| 25 | shift |
| 26 | ;; |
| 27 | *) |
| 28 | break |
| 29 | ;; |
| 30 | esac |
| 31 | done |
| 32 | |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 33 | ( |
Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 34 | cd "$name" && |
| 35 | |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 36 | # Pretend we're not running under a test harness, whether we |
| 37 | # are or not. The test-lib output depends on the setting of |
| 38 | # this variable, so we need a stable setting under which to run |
| 39 | # the sub-test. |
| 40 | sane_unset HARNESS_ACTIVE && |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 41 | |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 42 | export TEST_DIRECTORY && |
| 43 | # The child test re-sources GIT-BUILD-OPTIONS and may thus |
| 44 | # override the test output directory. We thus pass it as an |
| 45 | # explicit override to the child. |
| 46 | TEST_OUTPUT_DIRECTORY_OVERRIDE=$(pwd) && |
| 47 | export TEST_OUTPUT_DIRECTORY_OVERRIDE && |
| 48 | GIT_SKIP_TESTS=$skip && |
| 49 | export GIT_SKIP_TESTS && |
| 50 | sane_unset GIT_TEST_FAIL_PREREQS && |
Ævar Arnfjörð Bjarmason | 56722a0 | 2021-09-22 13:19:52 +0200 | [diff] [blame] | 51 | ./"$name.sh" "$@" >out 2>err; |
| 52 | ret=$? && |
| 53 | test "$ret" "$cmp_op" "$want_code" |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 54 | ) |
| 55 | } |
| 56 | |
Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 57 | write_and_run_sub_test_lib_test () { |
| 58 | name="$1" descr="$2" # stdin is the body of the test code |
| 59 | write_sub_test_lib_test "$@" || return 1 |
Ævar Arnfjörð Bjarmason | 56722a0 | 2021-09-22 13:19:52 +0200 | [diff] [blame] | 60 | _run_sub_test_lib_test_common -eq 0 "$@" |
Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | write_and_run_sub_test_lib_test_err () { |
| 64 | name="$1" descr="$2" # stdin is the body of the test code |
| 65 | write_sub_test_lib_test "$@" || return 1 |
Ævar Arnfjörð Bjarmason | 56722a0 | 2021-09-22 13:19:52 +0200 | [diff] [blame] | 66 | _run_sub_test_lib_test_common -eq 1 "$@" |
Ævar Arnfjörð Bjarmason | 9f0a452 | 2021-09-22 13:19:48 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 69 | run_sub_test_lib_test () { |
Ævar Arnfjörð Bjarmason | 56722a0 | 2021-09-22 13:19:52 +0200 | [diff] [blame] | 70 | _run_sub_test_lib_test_common -eq 0 "$@" |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | run_sub_test_lib_test_err () { |
Ævar Arnfjörð Bjarmason | 56722a0 | 2021-09-22 13:19:52 +0200 | [diff] [blame] | 74 | _run_sub_test_lib_test_common -eq 1 "$@" |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Ævar Arnfjörð Bjarmason | e07b817 | 2021-09-22 13:19:51 +0200 | [diff] [blame] | 77 | _check_sub_test_lib_test_common () { |
| 78 | name="$1" && |
| 79 | sed -e 's/^> //' -e 's/Z$//' >"$name"/expect.out && |
| 80 | test_cmp "$name"/expect.out "$name"/out |
| 81 | } |
| 82 | |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 83 | check_sub_test_lib_test () { |
| 84 | name="$1" # stdin is the expected output from the test |
Ævar Arnfjörð Bjarmason | e07b817 | 2021-09-22 13:19:51 +0200 | [diff] [blame] | 85 | _check_sub_test_lib_test_common "$name" && |
| 86 | test_must_be_empty "$name"/err |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | check_sub_test_lib_test_err () { |
| 90 | name="$1" # stdin is the expected output from the test |
Ævar Arnfjörð Bjarmason | e07b817 | 2021-09-22 13:19:51 +0200 | [diff] [blame] | 91 | _check_sub_test_lib_test_common "$name" && |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 92 | # expected error output is in descriptor 3 |
Ævar Arnfjörð Bjarmason | 12fe490 | 2021-09-22 13:19:50 +0200 | [diff] [blame] | 93 | sed -e 's/^> //' -e 's/Z$//' <&3 >"$name"/expect.err && |
| 94 | test_cmp "$name"/expect.err "$name"/err |
Ævar Arnfjörð Bjarmason | 866a301 | 2021-07-22 00:57:40 +0200 | [diff] [blame] | 95 | } |