Explicitly wait for intermediate process
[abduco.git] / testsuite.sh
blobd6c73be4e2696d80d27d2a85d9cd0b7255fd14c2
1 #!/bin/sh
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 TESTS_OK=0
11 TESTS_RUN=0
13 detach() {
14 sleep 1
15 printf "\x1c"
18 dvtm_cmd() {
19 printf "\a$1\n"
20 sleep 1
23 dvtm_session() {
24 sleep 1
25 dvtm_cmd 'c'
26 dvtm_cmd 'c'
27 dvtm_cmd 'c'
28 sleep 1
29 dvtm_cmd ' '
30 dvtm_cmd ' '
31 dvtm_cmd ' '
32 sleep 1
33 dvtm_cmd 'qq'
36 expected_abduco_prolog() {
37 printf "\e[?1049h\e[H"
40 # $1 => session-name, $2 => exit status
41 expected_abduco_epilog() {
42 echo "\e[?25h\e[?1049labduco: $1: session terminated with exit status $2"
45 # $1 => session-name, $2 => cmd to run
46 expected_abduco_attached_output() {
47 expected_abduco_prolog
49 expected_abduco_epilog "$1" $?
52 # $1 => session-name, $2 => cmd to run
53 expected_abduco_detached_output() {
54 expected_abduco_prolog
55 $2 >/dev/null 2>&1
56 expected_abduco_epilog "$1" $?
59 check_environment() {
60 [ "`$ABDUCO | wc -l`" -gt 1 ] && echo Abduco session exists && exit 1;
61 pgrep abduco && echo Abduco process exists && exit 1;
62 return 0;
65 test_non_existing_command() {
66 check_environment || return 1;
67 $ABDUCO -c test ./non-existing-command >/dev/null 2>&1
68 check_environment || return 1;
71 # $1 => session-name, $2 => command to execute
72 run_test_attached() {
73 check_environment || return 1;
75 local name="$1"
76 local cmd="$2"
77 local output="$name.out"
78 local output_expected="$name.expected"
80 TESTS_RUN=$((TESTS_RUN + 1))
81 echo -n "Running test attached: $name "
82 expected_abduco_attached_output "$name" "$cmd" > "$output_expected" 2>&1
84 if $ABDUCO -c "$name" $cmd 2>&1 | sed 's/.$//' > "$output" && sleep 1 &&
85 diff -u "$output_expected" "$output" && check_environment; then
86 rm "$output" "$output_expected"
87 TESTS_OK=$((TESTS_OK + 1))
88 echo "OK"
89 return 0
90 else
91 echo "FAIL"
92 return 1
96 # $1 => session-name, $2 => command to execute
97 run_test_detached() {
98 check_environment || return 1;
100 local name="$1"
101 local cmd="$2"
102 local output="$name.out"
103 local output_expected="$name.expected"
105 TESTS_RUN=$((TESTS_RUN + 1))
106 echo -n "Running test detached: $name "
107 expected_abduco_detached_output "$name" "$cmd" > "$output_expected" 2>&1
109 if $ABDUCO -n "$name" $cmd >/dev/null 2>&1 && sleep 1 &&
110 $ABDUCO -a "$name" 2>&1 | sed 's/.$//' > "$output" &&
111 diff -u "$output_expected" "$output" && check_environment; then
112 rm "$output" "$output_expected"
113 TESTS_OK=$((TESTS_OK + 1))
114 echo "OK"
115 return 0
116 else
117 echo "FAIL"
118 return 1
122 # $1 => session-name, $2 => command to execute
123 run_test_attached_detached() {
124 check_environment || return 1;
126 local name="$1"
127 local cmd="$2"
128 local output="$name.out"
129 local output_expected="$name.expected"
131 TESTS_RUN=$((TESTS_RUN + 1))
132 echo -n "Running test: $name "
133 $cmd >/dev/null 2>&1
134 expected_abduco_epilog "$name" $? > "$output_expected" 2>&1
136 if detach | $ABDUCO $ABDUCO_OPTS -c "$name" $cmd >/dev/null 2>&1 && sleep 3 &&
137 $ABDUCO -a "$name" 2>&1 | tail -1 | sed 's/.$//' > "$output" &&
138 diff -u "$output_expected" "$output" && check_environment; then
139 rm "$output" "$output_expected"
140 TESTS_OK=$((TESTS_OK + 1))
141 echo "OK"
142 return 0
143 else
144 echo "FAIL"
145 return 1
149 run_test_dvtm() {
150 echo -n "Running dvtm test: "
151 if ! which dvtm >/dev/null 2>&1; then
152 echo "SKIPPED"
153 return 0;
156 TESTS_RUN=$((TESTS_RUN + 1))
157 local name="dvtm"
158 local output="$name.out"
159 local output_expected="$name.expected"
161 : > "$output_expected"
162 if dvtm_session | $ABDUCO -c "$name" > "$output" 2>&1 &&
163 diff -u "$output_expected" "$output" && check_environment; then
164 rm "$output" "$output_expected"
165 TESTS_OK=$((TESTS_OK + 1))
166 echo "OK"
167 return 0
168 else
169 echo "FAIL"
170 return 1
174 test_non_existing_command || echo "Execution of non existing command FAILED"
176 run_test_attached "awk" "awk 'BEGIN {for(i=1;i<=1000;i++) print i}'"
177 run_test_detached "awk" "awk 'BEGIN {for(i=1;i<=1000;i++) print i}'"
179 run_test_attached "false" "false"
180 run_test_detached "false" "false"
182 run_test_attached "true" "true"
183 run_test_detached "true" "true"
185 cat > exit-status.sh <<-EOT
186 #!/bin/sh
187 exit 42
189 chmod +x exit-status.sh
191 run_test_attached "exit-status" "./exit-status.sh"
192 run_test_detached "exit-status" "./exit-status.sh"
194 rm ./exit-status.sh
196 cat > long-running.sh <<-EOT
197 #!/bin/sh
198 echo Start
199 date
200 sleep 3
201 echo Hello World
202 sleep 3
203 echo End
204 date
205 exit 1
207 chmod +x long-running.sh
209 run_test_attached_detached "attach-detach" "./long-running.sh"
211 rm ./long-running.sh
213 run_test_dvtm
215 [ $TESTS_OK -eq $TESTS_RUN ]