master
sh 126 lines 2.77 KB
Raw
1 # Generic test functions for go-ipfs
2
3 ansi_strip() {
4 sed 's/\x1b\[[0-9;]*m//g'
5 }
6
7 # Quote arguments for sh eval
8 shellquote() {
9 _space=''
10 for _arg
11 do
12 # On macOS, sed adds a newline character.
13 # With a printf wrapper the extra newline is removed.
14 printf "$_space'%s'" "$(printf "%s" "$_arg" | sed -e "s/'/'\\\\''/g;")"
15 _space=' '
16 done
17 printf '\n'
18 }
19
20 # Echo the args, run the cmd, and then also fail,
21 # making sure a test case fails.
22 test_fsh() {
23 echo "> $@"
24 eval $(shellquote "$@")
25 echo ""
26 false
27 }
28
29 # Same as sharness' test_cmp but using test_fsh (to see the output).
30 # We have to do it twice, so the first diff output doesn't show unless it's
31 # broken.
32 test_cmp() {
33 diff -q "$@" >/dev/null || test_fsh diff -u "$@"
34 }
35
36 # Same as test_cmp above, but we sort files before comparing them.
37 test_sort_cmp() {
38 sort "$1" >"$1_sorted" &&
39 sort "$2" >"$2_sorted" &&
40 test_cmp "$1_sorted" "$2_sorted"
41 }
42
43 # Same as test_cmp above, but we standardize directory
44 # separators before comparing the files.
45 test_path_cmp() {
46 sed -e "s/\\\\/\//g" "$1" >"$1_std" &&
47 sed -e "s/\\\\/\//g" "$2" >"$2_std" &&
48 test_cmp "$1_std" "$2_std"
49 }
50
51 # Docker
52
53 # This takes a Dockerfile, a tag name, and a build context directory
54 docker_build() {
55 docker build --rm --tag "$1" --file "$2" "$3" | ansi_strip
56 }
57
58 # This takes an image as argument and writes a docker ID on stdout
59 docker_run() {
60 docker run --detach "$1"
61 }
62
63 # This takes a docker ID and a command as arguments
64 docker_exec() {
65 docker exec --tty "$1" /bin/sh -c "$2"
66 }
67
68 # This takes a docker ID as argument
69 docker_stop() {
70 docker stop "$1"
71 }
72
73 # This takes a docker ID as argument
74 docker_rm() {
75 docker rm --force --volumes "$1" > /dev/null
76 }
77
78 # This takes a docker image name as argument
79 docker_rmi() {
80 docker rmi --force "$1" > /dev/null
81 }
82
83 # Test whether all the expected lines are included in a file. The file
84 # can have extra lines.
85 #
86 # $1 - Path to file with expected lines.
87 # $2 - Path to file with actual output.
88 #
89 # Examples
90 #
91 # test_expect_success 'foo says hello' '
92 # echo hello >expected &&
93 # foo >actual &&
94 # test_cmp expected actual
95 # '
96 #
97 # Returns the exit code of the command set by TEST_CMP.
98 test_includes_lines() {
99 sort "$1" >"$1_sorted" &&
100 sort "$2" >"$2_sorted" &&
101 comm -2 -3 "$1_sorted" "$2_sorted" >"$2_missing" &&
102 [ ! -s "$2_missing" ] || test_fsh comm -2 -3 "$1_sorted" "$2_sorted"
103 }
104
105 # Depending on GNU seq availability is not nice.
106 # Git also has test_seq but it uses Perl.
107 test_seq() {
108 test "$1" -le "$2" || return
109 i="$1"
110 j="$2"
111 while test "$i" -le "$j"
112 do
113 echo "$i"
114 i=$(expr "$i" + 1)
115 done
116 }
117
118 b64decode() {
119 case `uname` in
120 Linux|FreeBSD) base64 -d ;;
121 Darwin) base64 -D ;;
122 *)
123 echo "no compatible base64 command found" >&2
124 return 1
125 esac
126 }