Skip to content

Commit

Permalink
ipfs-test-lib: fix test_fsh arg quoting
Browse files Browse the repository at this point in the history
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:

```

. ./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 <[email protected]>
  • Loading branch information
chriscool committed Aug 15, 2016
1 parent 685cd28 commit d41ed0a
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions test/ipfs-test-lib.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
# Generic test functions for go-ipfs

# Quote arguments for sh eval
shellquote() {
_space=''
for _arg
do
# On Mac OS, sed adds a newline character.
# With a printf wrapper the extra newline is removed.
printf "$_space'%s'" "$(printf "%s" "$_arg" | sed -e "s/'/'\\\\''/g;")"
_space=' '
done
printf '\n'
}

# Echo the args, run the cmd, and then also fail,
# making sure a test case fails.
test_fsh() {
echo "> $@"
eval "$@"
eval $(shellquote "$@")
echo ""
false
}
Expand All @@ -31,19 +44,6 @@ test_path_cmp() {
test_cmp "$1_std" "$2_std"
}

# Quote arguments for sh eval
shellquote() {
_space=''
for _arg
do
# On Mac OS, sed adds a newline character.
# With a printf wrapper the extra newline is removed.
printf "$_space'%s'" "$(printf "%s" "$_arg" | sed -e "s/'/'\\\\''/g;")"
_space=' '
done
printf '\n'
}

# Docker

# This takes a Dockerfile, and a build context directory
Expand Down

0 comments on commit d41ed0a

Please sign in to comment.