|
| 1 | +# Generic test functions for ipfs cli tests |
| 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 "> $@" |
| 20 | + eval $(shellquote "$@") |
| 21 | + echo "" |
| 22 | + false |
| 23 | +} |
| 24 | + |
| 25 | +# Same as sharness' test_cmp but using test_fsh (to see the output). |
| 26 | +# We have to do it twice, so the first diff output doesn't show unless it's |
| 27 | +# broken. |
| 28 | +test_cmp() { |
| 29 | + diff -q "$@" >/dev/null || test_fsh diff -u "$@" |
| 30 | +} |
| 31 | + |
| 32 | +# Same as test_cmp above, but we sort files before comparing them. |
| 33 | +test_sort_cmp() { |
| 34 | + sort "$1" >"$1_sorted" && |
| 35 | + sort "$2" >"$2_sorted" && |
| 36 | + test_cmp "$1_sorted" "$2_sorted" |
| 37 | +} |
| 38 | + |
| 39 | +# Same as test_cmp above, but we standardize directory |
| 40 | +# separators before comparing the files. |
| 41 | +test_path_cmp() { |
| 42 | + sed -e "s/\\\\/\//g" "$1" >"$1_std" && |
| 43 | + sed -e "s/\\\\/\//g" "$2" >"$2_std" && |
| 44 | + test_cmp "$1_std" "$2_std" |
| 45 | +} |
| 46 | + |
| 47 | +# Docker |
| 48 | + |
| 49 | +# This takes a Dockerfile, and a build context directory |
| 50 | +docker_build() { |
| 51 | + docker build --rm -f "$1" "$2" |
| 52 | +} |
| 53 | + |
| 54 | +# This takes an image as argument and writes a docker ID on stdout |
| 55 | +docker_run() { |
| 56 | + docker run -d "$1" |
| 57 | +} |
| 58 | + |
| 59 | +# This takes a docker ID and a command as arguments |
| 60 | +docker_exec() { |
| 61 | + if test "$CIRCLE" = 1 |
| 62 | + then |
| 63 | + sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' $1)" -- /bin/bash -c "$2" |
| 64 | + else |
| 65 | + docker exec -t "$1" /bin/bash -c "$2" |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +# This takes a docker ID as argument |
| 70 | +docker_stop() { |
| 71 | + docker stop "$1" |
| 72 | +} |
| 73 | + |
| 74 | +# Test whether all the expected lines are included in a file. The file |
| 75 | +# can have extra lines. |
| 76 | +# |
| 77 | +# $1 - Path to file with expected lines. |
| 78 | +# $2 - Path to file with actual output. |
| 79 | +# |
| 80 | +# Examples |
| 81 | +# |
| 82 | +# test_expect_success 'foo says hello' ' |
| 83 | +# echo hello >expected && |
| 84 | +# foo >actual && |
| 85 | +# test_cmp expected actual |
| 86 | +# ' |
| 87 | +# |
| 88 | +# Returns the exit code of the command set by TEST_CMP. |
| 89 | +test_includes_lines() { |
| 90 | + sort "$1" >"$1_sorted" && |
| 91 | + sort "$2" >"$2_sorted" && |
| 92 | + comm -2 -3 "$1_sorted" "$2_sorted" >"$2_missing" && |
| 93 | + [ ! -s "$2_missing" ] || test_fsh comm -2 -3 "$1_sorted" "$2_sorted" |
| 94 | +} |
| 95 | + |
| 96 | +# Depending on GNU seq availability is not nice. |
| 97 | +# Git also has test_seq but it uses Perl. |
| 98 | +test_seq() { |
| 99 | + test "$1" -le "$2" || return |
| 100 | + i="$1" |
| 101 | + j="$2" |
| 102 | + while test "$i" -le "$j" |
| 103 | + do |
| 104 | + echo "$i" |
| 105 | + i=$(expr "$i" + 1) |
| 106 | + done |
| 107 | +} |
0 commit comments