summaryrefslogtreecommitdiff
path: root/t/lib-subtest.sh
blob: cdbc2d933206bb966add3d8db6b217861ae6bda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
write_sub_test_lib_test () {
	name="$1" # stdin is the body of the test code
	mkdir "$name" &&
	write_script "$name/$name.sh" "$TEST_SHELL_PATH" <<-EOF &&
	test_description='A test of test-lib.sh itself'

	# Point to the t/test-lib.sh, which isn't in ../ as usual
	. "\$TEST_DIRECTORY"/test-lib.sh
	EOF
	cat >>"$name/$name.sh"
}

_run_sub_test_lib_test_common () {
	neg="$1" name="$2" # stdin is the body of the test code
	shift 2

	# intercept pseudo-options at the front of the argument list that we
	# will not pass to child script
	skip=
	while test $# -gt 0
	do
		case "$1" in
		--skip=*)
			skip=${1#--*=}
			shift
			;;
		*)
			break
			;;
		esac
	done

	(
		cd "$name" &&

		# Pretend we're not running under a test harness, whether we
		# are or not. The test-lib output depends on the setting of
		# this variable, so we need a stable setting under which to run
		# the sub-test.
		sane_unset HARNESS_ACTIVE &&

		export TEST_DIRECTORY &&
		# The child test re-sources GIT-BUILD-OPTIONS and may thus
		# override the test output directory. We thus pass it as an
		# explicit override to the child.
		TEST_OUTPUT_DIRECTORY_OVERRIDE=$(pwd) &&
		export TEST_OUTPUT_DIRECTORY_OVERRIDE &&
		GIT_SKIP_TESTS=$skip &&
		export GIT_SKIP_TESTS &&
		sane_unset GIT_TEST_FAIL_PREREQS &&
		if test -z "$neg"
		then
			./"$name.sh" "$@" >out 2>err
		else
			! ./"$name.sh" "$@" >out 2>err
		fi
	)
}

write_and_run_sub_test_lib_test () {
	name="$1" descr="$2" # stdin is the body of the test code
	write_sub_test_lib_test "$@" || return 1
	_run_sub_test_lib_test_common '' "$@"
}

write_and_run_sub_test_lib_test_err () {
	name="$1" descr="$2" # stdin is the body of the test code
	write_sub_test_lib_test "$@" || return 1
	_run_sub_test_lib_test_common '!' "$@"
}

run_sub_test_lib_test () {
	_run_sub_test_lib_test_common '' "$@"
}

run_sub_test_lib_test_err () {
	_run_sub_test_lib_test_common '!' "$@"
}

check_sub_test_lib_test () {
	name="$1" # stdin is the expected output from the test
	(
		cd "$name" &&
		test_must_be_empty err &&
		sed -e 's/^> //' -e 's/Z$//' >expect &&
		test_cmp expect out
	)
}

check_sub_test_lib_test_err () {
	name="$1" # stdin is the expected output from the test
	# expected error output is in descriptor 3
	(
		cd "$name" &&
		sed -e 's/^> //' -e 's/Z$//' >expect.out &&
		test_cmp expect.out out &&
		sed -e 's/^> //' -e 's/Z$//' <&3 >expect.err &&
		test_cmp expect.err err
	)
}