@cryptotaxi247 / kubo / commits / 2489dfe8a

ipfs-test-lib: fix test_fsh arg quoting

test_fsh() should quote its arguments before passing them to `eval` otherwise there are problems when the arguments contain spaces. For example when running the following program: ``` #!/bin/sh . ./ipfs-test-lib.sh die () { printf >&2 "%s\n" "$@" exit 1 } DIR1="test dir 1" DIR2="test dir 2" mkdir "$DIR1" "$DIR2" || die "Could not mkdir '$DIR1' '$DIR2'" echo "in dir 1" >"$DIR1/file1" || die "Could not write into '$DIR1/file1'" echo "in dir 2" >"$DIR2/file2" || die "Could not write into '$DIR2/file2'" if test_cmp "$DIR1/file1" "$DIR2/file2" then echo "test_cmp succeeded!" else echo "test_cmp failed!" fi rm -rf "$DIR1" "$DIR2" || die "Could not rm -rf '$DIR1' '$DIR2'" ``` we get: ``` > diff -u test dir 1/file1 test dir 2/file2 diff: extra operand '1/file1' diff: Try 'diff --help' for more information. test_cmp failed! ``` License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>

Christian Couder committed Aug 15, 2016 at 11:49 UTC 2489dfe8a9ddce41658ae5142d5b23829410677d
1 file changed +14 -14
test/ipfs-test-lib.sh
+14 -14
@@ -1,10 +1,23 @@
1 # Generic test functions for go-ipfs
2
3 +# Quote arguments for sh eval
4 +shellquote() {
5 + _space=''
6 + for _arg
7 + do
8 + # On Mac OS, sed adds a newline character.
9 + # With a printf wrapper the extra newline is removed.
10 + printf "$_space'%s'" "$(printf "%s" "$_arg" | sed -e "s/'/'\\\\''/g;")"
11 + _space=' '
12 + done
13 + printf '\n'
14 +}
15 +
16 # Echo the args, run the cmd, and then also fail,
17 # making sure a test case fails.
18 test_fsh() {
19 echo "> $@"
7 - eval "$@"
20 + eval $(shellquote "$@")
21 echo ""
22 false
23 }
@@ -31,19 +44,6 @@ test_path_cmp() {
44 test_cmp "$1_std" "$2_std"
45 }
46
34 -# Quote arguments for sh eval
35 -shellquote() {
36 - _space=''
37 - for _arg
38 - do
39 - # On Mac OS, sed adds a newline character.
40 - # With a printf wrapper the extra newline is removed.
41 - printf "$_space'%s'" "$(printf "%s" "$_arg" | sed -e "s/'/'\\\\''/g;")"
42 - _space=' '
43 - done
44 - printf '\n'
45 -}
46 -
47 # Docker
48
49 # This takes a Dockerfile, and a build context directory