Skip to content

Commit 840c519

Browse files
committed
tests: add write_script helper function
Many of the scripts in the test suite write small helper shell scripts to disk. It's best if these shell scripts start with "#!$SHELL_PATH" rather than "#!/bin/sh", because /bin/sh on some platforms is too buggy to be used. However, it can be cumbersome to expand $SHELL_PATH, because the usual recipe for writing a script is: cat >foo.sh <<-\EOF #!/bin/sh echo my arguments are "$@" EOF To expand $SHELL_PATH, you have to either interpolate the here-doc (which would require quoting "\$@"), or split the creation into two commands (interpolating the $SHELL_PATH line, but not the rest of the script). Let's provide a helper function that makes that less syntactically painful. While we're at it, this helper can also take care of the "chmod +x" that typically comes after the creation of such a script, saving the caller a line. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 828ea97 commit 840c519

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

t/test-lib.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,20 @@ test_config () {
381381
git config "$@"
382382
}
383383

384+
384385
test_config_global () {
385386
test_when_finished "test_unconfig --global '$1'" &&
386387
git config --global "$@"
387388
}
388389

390+
write_script () {
391+
{
392+
echo "#!${2-"$SHELL_PATH"}" &&
393+
cat
394+
} >"$1" &&
395+
chmod +x "$1"
396+
}
397+
389398
# Use test_set_prereq to tell that a particular prerequisite is available.
390399
# The prerequisite can later be checked for in two ways:
391400
#

0 commit comments

Comments
 (0)