| 1 |
# Copyright (C) 1992-2016 Free Software Foundation, Inc. |
| 2 |
# |
| 3 |
# This file is part of DejaGnu. |
| 4 |
# |
| 5 |
# DejaGnu is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# DejaGnu is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with DejaGnu; if not, write to the Free Software Foundation, |
| 17 |
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
|
| 19 |
# Setup an environment so we can execute library procs without DejaGnu. |
| 20 |
|
| 21 |
# |
| 22 |
# Start an Expect process |
| 23 |
# |
| 24 |
proc start_expect { } { |
| 25 |
global EXPECT |
| 26 |
global spawn_id |
| 27 |
|
| 28 |
# We need to setup default values and a few default procs so we |
| 29 |
# can execute library code without DejaGnu |
| 30 |
|
| 31 |
# Start expect |
| 32 |
set stty_init { -onlcr -onlret } |
| 33 |
spawn $EXPECT |
| 34 |
expect { |
| 35 |
-re "expect.*> " { |
| 36 |
verbose "Started the child expect shell" 2 |
| 37 |
} |
| 38 |
timeout { |
| 39 |
perror "Timed out starting the child expect shell." |
| 40 |
return -1 |
| 41 |
} |
| 42 |
} |
| 43 |
send_defaults |
| 44 |
} |
| 45 |
|
| 46 |
# |
| 47 |
# Send default variables to a running Expect |
| 48 |
# |
| 49 |
proc send_defaults { } { |
| 50 |
global spawn_id |
| 51 |
|
| 52 |
global build_triplet |
| 53 |
global host_triplet |
| 54 |
global target_triplet |
| 55 |
global target_os |
| 56 |
global target_cpu |
| 57 |
|
| 58 |
set vars [subst { |
| 59 |
tool foobar |
| 60 |
srcdir {[testsuite file -source -top]} |
| 61 |
objdir {[testsuite file -object -top]} |
| 62 |
subdir {[relative_filename\ |
| 63 |
[testsuite file -source -top]\ |
| 64 |
[testsuite file -source -test]]} |
| 65 |
build_triplet $build_triplet |
| 66 |
host_triplet $host_triplet |
| 67 |
target_triplet $target_triplet |
| 68 |
target_os $target_os |
| 69 |
target_cpu $target_cpu |
| 70 |
prms_id 0 |
| 71 |
bug_id 0 |
| 72 |
exit_status 0 |
| 73 |
xfail_flag 0 xfail_prms 0 |
| 74 |
kfail_flag 0 kfail_prms 0 |
| 75 |
mail_logs 0 |
| 76 |
multipass_name 0 |
| 77 |
}] |
| 78 |
|
| 79 |
# Load defaults |
| 80 |
exp_send "array set default_vars {$vars}\n" |
| 81 |
expect { |
| 82 |
"expect*> " { |
| 83 |
verbose "Loaded testing defaults." 2 |
| 84 |
return 1 |
| 85 |
} |
| 86 |
"+> " { |
| 87 |
# discard continuation prompts generated from sending a |
| 88 |
# multiline command to Expect |
| 89 |
exp_continue |
| 90 |
} |
| 91 |
timeout { |
| 92 |
perror "Couldn't load the testing defaults file." |
| 93 |
return -1 |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
# |
| 99 |
# Stop the running expect process |
| 100 |
# |
| 101 |
proc stop_expect { } { |
| 102 |
global spawn_id |
| 103 |
|
| 104 |
# make expect exit |
| 105 |
exp_send "exit\n" |
| 106 |
catch "close -i $spawn_id" |
| 107 |
catch "wait -i $spawn_id" |
| 108 |
} |
| 109 |
|
| 110 |
# |
| 111 |
# Load the library to test |
| 112 |
# |
| 113 |
proc load_test_lib { lib } { |
| 114 |
global spawn_id |
| 115 |
exp_send "source $lib\n" |
| 116 |
expect { |
| 117 |
"expect*> " { |
| 118 |
verbose "Testing $lib" 2 |
| 119 |
} |
| 120 |
timeout { |
| 121 |
perror "Couldn't load the libraries to test" |
| 122 |
return -1 |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
# |
| 128 |
# test a library proc that emits patterns |
| 129 |
# |
| 130 |
proc exp_test { cmd pattern msg } { |
| 131 |
global spawn_id |
| 132 |
|
| 133 |
exp_send "puts ACK ; $cmd ; puts NAK\r\n" |
| 134 |
expect { |
| 135 |
"puts ACK*puts NAK" { |
| 136 |
verbose "Got command echo" 3 |
| 137 |
} |
| 138 |
timeout { |
| 139 |
warning "Never got command echo" |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
expect { |
| 144 |
"ACK" { |
| 145 |
exp_continue |
| 146 |
} |
| 147 |
-re "\r\n1\r\n" { |
| 148 |
warning "$msg, 1 was returned" |
| 149 |
exp_continue |
| 150 |
} |
| 151 |
-re "\r\n0\r\n" { |
| 152 |
warning "$msg, 0 was returned" |
| 153 |
exp_continue |
| 154 |
} |
| 155 |
$pattern { |
| 156 |
pass $msg |
| 157 |
} |
| 158 |
timeout { |
| 159 |
fail $msg |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
# test a config proc that only returns a code |
| 165 |
# ex... config_test "isbuild $build_triplet" "pass" "fail" "isbuild, native" |
| 166 |
# args are: command, true condition, false condition, message to print |
| 167 |
proc config_test { cmd true false msg } { |
| 168 |
global spawn_id |
| 169 |
|
| 170 |
set timeout 20 |
| 171 |
exp_send "puts ACK ; puts \[$cmd\] ; puts NAK\r\n" |
| 172 |
expect { |
| 173 |
"puts ACK*$cmd*puts NAK" { |
| 174 |
verbose "Got command echo" 3 |
| 175 |
} |
| 176 |
timeout { |
| 177 |
warning "Never got command echo" |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
expect { |
| 182 |
-re {Checking pattern*with*[\r\n]} { |
| 183 |
exp_continue |
| 184 |
} |
| 185 |
-re "\r\n1\r\n" { |
| 186 |
$true $msg |
| 187 |
} |
| 188 |
-re "\r\n0\r\n" { |
| 189 |
$false $msg |
| 190 |
} |
| 191 |
timeout { |
| 192 |
perror "$msg (timed out)" |
| 193 |
} |
| 194 |
} |
| 195 |
} |