Skip to content

Commit a3abd27

Browse files
committed
sharness.d: add support for heredoc test bodies
problem: quoting is annoying, and having to escape quotes in every single test much moreso solution: add *_hd versions of each of the test functions that accept their code body on stdin (captured by cat) such that a heredoc, or even a pipe if that's desirable for some reason, can provide the code. Combined with a single-quoted heredoc and tab stripping (<<-'EOT' for example) quoting woes disappear. Vim syntax highlighting still WIP.
1 parent 87a05da commit a3abd27

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

t/sharness.d/heredoc.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
test_expect_success_hd() {
2+
test "$#" = 2 && { test_prereq=$1; shift; } || test_prereq=
3+
local TEST_CODE
4+
# these extra newlines are intentional, and mimic the ones we get
5+
# naturally in the non-heredoc case
6+
TEST_CODE="\
7+
8+
$(cat)
9+
"
10+
test_expect_success "$test_prereq" "'$1'" "$TEST_CODE"
11+
}
12+
13+
test_expect_failure_hd() {
14+
test "$#" = 2 && { test_prereq=$1; shift; } || test_prereq=
15+
local TEST_CODE
16+
# these extra newlines are intentional, and mimic the ones we get
17+
# naturally in the non-heredoc case
18+
TEST_CODE="\
19+
20+
$(cat)
21+
"
22+
test_expect_failure "$test_prereq" "'$1'" "$TEST_CODE"
23+
}
24+

t/t0001-basic.t

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ path_printenv=$(which printenv)
2424
test_expect_success 'TEST_NAME is set' '
2525
test -n "$TEST_NAME"
2626
'
27+
test_expect_success_hd 'heredoc tests work: success' <<-'EOT'
28+
test -n "$TEST_NAME" &&
29+
# embedded heredoc
30+
cat >tmp.sh <<-EOF &&
31+
true
32+
EOF
33+
chmod +x tmp.sh &&
34+
./tmp.sh
35+
EOT
36+
test_expect_failure_hd 'heredoc tests work: failure' <<'EOT'
37+
test -n ""
38+
EOT
2739
test_expect_success 'run_timeout works' '
2840
test_expect_code 142 run_timeout -s ALRM 0.001 sleep 2
2941
'

0 commit comments

Comments
 (0)