Set version to 0.3
[abduco.git] / testsuite.sh
blob9d81c0075abe2518452be1b177b43560bd2ba20b
1 #!/bin/bash
3 ABDUCO="./abduco"
4 # set detach key explicitly in case it was changed in config.h
5 ABDUCO_OPTS="-e ^\\"
7 [ ! -z "$1" ] && ABDUCO="$1"
8 [ ! -x "$ABDUCO" ] && echo "usage: $0 /path/to/abduco" && exit 1
10 detach() {
11 sleep 1
12 printf "\x1c"
15 dvtm_cmd() {
16 printf "\a$1\n"
17 sleep 1
20 dvtm_session() {
21 sleep 1
22 dvtm_cmd 'c'
23 dvtm_cmd 'c'
24 dvtm_cmd 'c'
25 sleep 1
26 dvtm_cmd ' '
27 dvtm_cmd ' '
28 dvtm_cmd ' '
29 sleep 1
30 dvtm_cmd 'q'
33 expected_abduco_prolog() {
34 printf "\e[?1049h\e[H"
37 # $1 => session-name, $2 => exit status
38 expected_abduco_epilog() {
39 echo "\e[?1049labduco: $1: session terminated with exit status $2"
42 # $1 => session-name, $2 => cmd to run
43 expected_abduco_attached_output() {
44 expected_abduco_prolog
46 expected_abduco_epilog "$1" $?
49 # $1 => session-name, $2 => cmd to run
50 expected_abduco_detached_output() {
51 expected_abduco_prolog
52 $2 &> /dev/null
53 expected_abduco_epilog "$1" $?
56 check_environment() {
57 [ "`$ABDUCO | wc -l`" -gt 1 ] && echo Abduco session exists && exit 1;
58 pgrep abduco && echo Abduco process exists && exit 1;
59 return 0;
62 test_non_existing_command() {
63 check_environment || return 1;
64 $ABDUCO -c test ./non-existing-command &> /dev/null
65 check_environment || return 1;
68 # $1 => session-name, $2 => command to execute
69 run_test_attached() {
70 check_environment || return 1;
72 local name="$1"
73 local cmd="$2"
74 local output="$name.out"
75 local output_expected="$name.expected"
77 echo -n "Running test attached: $name "
78 expected_abduco_attached_output "$name" "$cmd" &> "$output_expected"
79 $ABDUCO -c "$name" $cmd 2>&1 | sed 's/.$//' > "$output"
81 if diff -u "$output_expected" "$output" && check_environment; then
82 rm "$output" "$output_expected"
83 echo "OK"
84 return 0
85 else
86 echo "FAIL"
87 return 1
91 # $1 => session-name, $2 => command to execute
92 run_test_detached() {
93 check_environment || return 1;
95 local name="$1"
96 local cmd="$2"
97 local output="$name.out"
98 local output_expected="$name.expected"
100 echo -n "Running test detached: $name "
101 expected_abduco_detached_output "$name" "$cmd" &> "$output_expected"
103 if $ABDUCO -n "$name" $cmd &> /dev/null && sleep 1 &&
104 $ABDUCO -a "$name" 2>&1 | sed 's/.$//' > "$output" &&
105 diff -u "$output_expected" "$output" && check_environment; then
106 rm "$output" "$output_expected"
107 echo "OK"
108 return 0
109 else
110 echo "FAIL"
111 return 1
115 # $1 => session-name, $2 => command to execute
116 run_test_attached_detached() {
117 check_environment || return 1;
119 local name="$1"
120 local cmd="$2"
121 local output="$name.out"
122 local output_expected="$name.expected"
124 echo -n "Running test: $name "
125 $cmd &> /dev/null
126 expected_abduco_epilog "$name" $? &> "$output_expected"
128 if detach | $ABDUCO $ABDUCO_OPTS -c "$name" $cmd &> /dev/null && sleep 3 &&
129 $ABDUCO -a "$name" 2>&1 | tail -1 | sed 's/.$//' > "$output" &&
130 diff -u "$output_expected" "$output" && check_environment; then
131 rm "$output" "$output_expected"
132 echo "OK"
133 return 0
134 else
135 echo "FAIL"
136 return 1
140 run_test_dvtm() {
141 echo -n "Running dvtm test: "
142 if ! which dvtm &> /dev/null; then
143 echo "SKIPPED"
144 return 0;
147 local name="dvtm"
148 local output="$name.out"
149 local output_expected="$name.expected"
151 echo exit | dvtm &> /dev/null
152 expected_abduco_epilog "$name" $? > "$output_expected"
153 local len=`wc -c "$output_expected" | awk '{ print $1 }'`
154 len=$((len+1))
155 if dvtm_session | $ABDUCO -c "$name" 2>&1 | tail -c $len | sed 's/.$//' > "$output" &&
156 diff -u "$output_expected" "$output" && check_environment; then
157 rm "$output" "$output_expected"
158 echo "OK"
159 return 0
160 else
161 echo "FAIL"
162 return 1
166 test_non_existing_command || echo "Execution of non existing command FAILED"
168 run_test_attached "seq" "seq 1 1000"
169 run_test_detached "seq" "seq 1 1000"
171 run_test_attached "false" "false"
172 run_test_detached "false" "false"
174 run_test_attached "true" "true"
175 run_test_detached "true" "true"
177 cat > exit-status.sh <<-EOT
178 #!/bin/sh
179 exit 42
181 chmod +x exit-status.sh
183 run_test_attached "exit-status" "./exit-status.sh"
184 run_test_detached "exit-status" "./exit-status.sh"
186 rm ./exit-status.sh
188 cat > long-running.sh <<-EOT
189 #!/bin/sh
190 echo Start
191 date
192 sleep 3
193 echo Hello World
194 sleep 3
195 echo End
196 date
197 exit 1
199 chmod +x long-running.sh
201 run_test_attached_detached "attach-detach" "./long-running.sh"
203 rm ./long-running.sh
205 run_test_dvtm