Skip to content

Commit efd2600

Browse files
peffgitster
authored andcommitted
t0000: run prereq tests inside sub-test
We test the behavior of prerequisites in t0000 by setting up fake ones in the main test script, trying to run some tests, and then seeing if those tests impacted the environment correctly. If they didn't, then we write a message and manually call exit. Instead, let's push these down into a sub-test, like many of the other tests covering the framework itself. This has a few advantages: - it does not pollute the test output with mention of skipped tests (that we know are uninteresting -- the point of the test was to see that these are skipped). - when running in a TAP harness, we get a useful test failure message (whereas when the script exits early, a tool like "prove" simply says "Dubious, test returned 1"). - we do not have to worry about different test environments, such as when GIT_TEST_FAIL_PREREQS_INTERNAL is set. Our sub-test helpers already give us a known environment. - the tests themselves are a bit easier to read, as we can just check the test-framework output to see what happened (and get the usual test_cmp diff if it failed) A few notes on the implementation: - we could do one sub-test per each individual test_expect_success. I broke it up here into a few logical groups, as I think this makes it more readable - the original tests modified environment variables inside the test bodies. Instead, I've used "true" as the body of a test we expect to run and "false" otherwise. Technically this does not confirm that the body of the "true" test actually ran. We are trusting the framework output to believe that it truly ran, which is sufficient for these tests. And I think the end result is much simpler to follow. - the nested_prereq test uses a few bare "test -f" calls; I converted these to our usual test_path_is_* helpers while moving the code around. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 03efadb commit efd2600

File tree

1 file changed

+69
-80
lines changed

1 file changed

+69
-80
lines changed

t/t0000-basic.sh

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -759,96 +759,85 @@ test_expect_success '--run invalid range end' "
759759
EOF_ERR
760760
"
761761

762+
test_expect_success 'tests respect prerequisites' '
763+
run_sub_test_lib_test prereqs "tests respect prereqs" <<-\EOF &&
762764
763-
test_set_prereq HAVEIT
764-
haveit=no
765-
test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
766-
test_have_prereq HAVEIT &&
767-
haveit=yes
768-
'
769-
donthaveit=yes
770-
test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
771-
donthaveit=no
772-
'
773-
if test -z "$GIT_TEST_FAIL_PREREQS_INTERNAL" -a $haveit$donthaveit != yesyes
774-
then
775-
say "bug in test framework: prerequisite tags do not work reliably"
776-
exit 1
777-
fi
765+
test_set_prereq HAVEIT
766+
test_expect_success HAVEIT "prereq is satisfied" "true"
767+
test_expect_success "have_prereq works" "
768+
test_have_prereq HAVEIT
769+
"
770+
test_expect_success DONTHAVEIT "prereq not satisfied" "false"
778771
779-
test_set_prereq HAVETHIS
780-
haveit=no
781-
test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
782-
test_have_prereq HAVEIT &&
783-
test_have_prereq HAVETHIS &&
784-
haveit=yes
785-
'
786-
donthaveit=yes
787-
test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
788-
donthaveit=no
789-
'
790-
donthaveiteither=yes
791-
test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
792-
donthaveiteither=no
793-
'
794-
if test -z "$GIT_TEST_FAIL_PREREQS_INTERNAL" -a $haveit$donthaveit$donthaveiteither != yesyesyes
795-
then
796-
say "bug in test framework: multiple prerequisite tags do not work reliably"
797-
exit 1
798-
fi
772+
test_set_prereq HAVETHIS
773+
test_expect_success HAVETHIS,HAVEIT "multiple prereqs" "true"
774+
test_expect_success HAVEIT,DONTHAVEIT "mixed prereqs (yes,no)" "false"
775+
test_expect_success DONTHAVEIT,HAVEIT "mixed prereqs (no,yes)" "false"
799776
800-
test_lazy_prereq LAZY_TRUE true
801-
havetrue=no
802-
test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
803-
havetrue=yes
804-
'
805-
donthavetrue=yes
806-
test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
807-
donthavetrue=no
777+
test_done
778+
EOF
779+
780+
check_sub_test_lib_test prereqs <<-\EOF
781+
ok 1 - prereq is satisfied
782+
ok 2 - have_prereq works
783+
ok 3 # skip prereq not satisfied (missing DONTHAVEIT)
784+
ok 4 - multiple prereqs
785+
ok 5 # skip mixed prereqs (yes,no) (missing DONTHAVEIT of HAVEIT,DONTHAVEIT)
786+
ok 6 # skip mixed prereqs (no,yes) (missing DONTHAVEIT of DONTHAVEIT,HAVEIT)
787+
# passed all 6 test(s)
788+
1..6
789+
EOF
808790
'
809791

810-
if test -z "$GIT_TEST_FAIL_PREREQS_INTERNAL" -a "$havetrue$donthavetrue" != yesyes
811-
then
812-
say 'bug in test framework: lazy prerequisites do not work'
813-
exit 1
814-
fi
792+
test_expect_success 'tests respect lazy prerequisites' '
793+
run_sub_test_lib_test lazy-prereqs "respect lazy prereqs" <<-\EOF &&
815794
816-
test_lazy_prereq LAZY_FALSE false
817-
nothavefalse=no
818-
test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
819-
nothavefalse=yes
820-
'
821-
havefalse=yes
822-
test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
823-
havefalse=no
824-
'
795+
test_lazy_prereq LAZY_TRUE true
796+
test_expect_success LAZY_TRUE "lazy prereq is satisifed" "true"
797+
test_expect_success !LAZY_TRUE "negative lazy prereq" "false"
825798
826-
if test -z "$GIT_TEST_FAIL_PREREQS_INTERNAL" -a "$nothavefalse$havefalse" != yesyes
827-
then
828-
say 'bug in test framework: negative lazy prerequisites do not work'
829-
exit 1
830-
fi
799+
test_lazy_prereq LAZY_FALSE false
800+
test_expect_success LAZY_FALSE "lazy prereq not satisfied" "false"
801+
test_expect_success !LAZY_FALSE "negative false prereq" "true"
831802
832-
test_lazy_prereq NESTED_INNER '
833-
>inner &&
834-
rm -f outer
835-
'
836-
test_lazy_prereq NESTED_PREREQ '
837-
>outer &&
838-
test_have_prereq NESTED_INNER &&
839-
echo "can create new file in cwd" >file &&
840-
test -f outer &&
841-
test ! -f inner
842-
'
843-
test_expect_success NESTED_PREREQ 'evaluating nested lazy prereqs dont interfere with each other' '
844-
nestedworks=yes
803+
test_done
804+
EOF
805+
806+
check_sub_test_lib_test lazy-prereqs <<-\EOF
807+
ok 1 - lazy prereq is satisifed
808+
ok 2 # skip negative lazy prereq (missing !LAZY_TRUE)
809+
ok 3 # skip lazy prereq not satisfied (missing LAZY_FALSE)
810+
ok 4 - negative false prereq
811+
# passed all 4 test(s)
812+
1..4
813+
EOF
845814
'
846815

847-
if test -z "$GIT_TEST_FAIL_PREREQS_INTERNAL" && test "$nestedworks" != yes
848-
then
849-
say 'bug in test framework: nested lazy prerequisites do not work'
850-
exit 1
851-
fi
816+
test_expect_success 'nested lazy prerequisites' '
817+
run_sub_test_lib_test nested-lazy "nested lazy prereqs" <<-\EOF &&
818+
819+
test_lazy_prereq NESTED_INNER "
820+
>inner &&
821+
rm -f outer
822+
"
823+
test_lazy_prereq NESTED_PREREQ "
824+
>outer &&
825+
test_have_prereq NESTED_INNER &&
826+
echo can create new file in cwd >file &&
827+
test_path_is_file outer &&
828+
test_path_is_missing inner
829+
"
830+
test_expect_success NESTED_PREREQ "evaluate nested prereq" "true"
831+
832+
test_done
833+
EOF
834+
835+
check_sub_test_lib_test nested-lazy <<-\EOF
836+
ok 1 - evaluate nested prereq
837+
# passed all 1 test(s)
838+
1..1
839+
EOF
840+
'
852841

853842
test_expect_success 'lazy prereqs do not turn off tracing' "
854843
run_sub_test_lib_test lazy-prereq-and-tracing \

0 commit comments

Comments
 (0)