| # Copyright (C) 1992-2016, 2024 Free Software Foundation, Inc. |
| # |
| # This file is part of DejaGnu. |
| # |
| # DejaGnu is free software; you can redistribute it and/or modify it |
| # under the terms of the GNU General Public License as published by |
| # the Free Software Foundation; either version 3 of the License, or |
| # (at your option) any later version. |
| # |
| # DejaGnu is distributed in the hope that it will be useful, but |
| # WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| # General Public License for more details. |
| # |
| # You should have received a copy of the GNU General Public License |
| # along with DejaGnu; if not, write to the Free Software Foundation, |
| # Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| |
| # Setup an environment so we can execute library procs without DejaGnu. |
| |
| # |
| # Start an Expect process |
| # |
| proc start_expect { } { |
| global EXPECT |
| global spawn_id |
| |
| # We need to setup default values and a few default procs so we |
| # can execute library code without DejaGnu |
| |
| # Start expect |
| set stty_init { -onlcr } |
| spawn $EXPECT |
| expect { |
| -re "expect.*> " { |
| verbose "Started the child expect shell" 2 |
| } |
| timeout { |
| perror "Timed out starting the child expect shell." |
| return -1 |
| } |
| } |
| send_defaults |
| } |
| |
| # |
| # Send default variables to a running Expect |
| # |
| proc send_defaults { } { |
| global spawn_id |
| |
| global build_triplet |
| global host_triplet |
| global target_triplet |
| global target_os |
| global target_cpu |
| |
| set vars [subst { |
| tool foobar |
| srcdir {[testsuite file -source -top]} |
| objdir {[testsuite file -object -top]} |
| subdir {[relative_filename\ |
| [testsuite file -source -top]\ |
| [testsuite file -source -test]]} |
| build_triplet $build_triplet |
| host_triplet $host_triplet |
| target_triplet $target_triplet |
| target_os $target_os |
| target_cpu $target_cpu |
| prms_id 0 |
| bug_id 0 |
| exit_status 0 |
| xfail_flag 0 xfail_prms 0 |
| kfail_flag 0 kfail_prms 0 |
| mail_logs 0 |
| multipass_name 0 |
| }] |
| |
| # Load defaults |
| exp_send "array set default_vars {$vars}\n" |
| expect { |
| "expect*> " { |
| verbose "Loaded testing defaults." 2 |
| return 1 |
| } |
| "+> " { |
| # discard continuation prompts generated from sending a |
| # multiline command to Expect |
| exp_continue |
| } |
| timeout { |
| perror "Couldn't load the testing defaults file." |
| return -1 |
| } |
| } |
| } |
| |
| # |
| # Stop the running expect process |
| # |
| proc stop_expect { } { |
| global spawn_id |
| |
| # make expect exit |
| exp_send "exit\n" |
| catch "close -i $spawn_id" |
| catch "wait -i $spawn_id" |
| } |
| |
| # |
| # Load the library to test |
| # |
| proc load_test_lib { lib } { |
| global spawn_id |
| exp_send "source $lib\n" |
| expect { |
| "expect*> " { |
| verbose "Testing $lib" 2 |
| } |
| timeout { |
| perror "Couldn't load the libraries to test" |
| return -1 |
| } |
| } |
| } |
| |
| # |
| # test a library proc that emits patterns |
| # |
| proc exp_test { cmd pattern msg } { |
| global spawn_id |
| |
| exp_send "puts ACK ; $cmd ; puts NAK\r\n" |
| expect { |
| "puts ACK*puts NAK" { |
| verbose "Got command echo" 3 |
| } |
| timeout { |
| warning "Never got command echo" |
| } |
| } |
| |
| expect { |
| "ACK" { |
| exp_continue |
| } |
| -re "\r\n1\r\n" { |
| warning "$msg, 1 was returned" |
| exp_continue |
| } |
| -re "\r\n0\r\n" { |
| warning "$msg, 0 was returned" |
| exp_continue |
| } |
| $pattern { |
| pass $msg |
| } |
| timeout { |
| fail $msg |
| } |
| } |
| } |
| |
| # test a config proc that only returns a code |
| # ex... config_test "isbuild $build_triplet" "pass" "fail" "isbuild, native" |
| # args are: command, true condition, false condition, message to print |
| proc config_test { cmd true false msg } { |
| global spawn_id |
| |
| set timeout 20 |
| exp_send "puts ACK ; puts \[$cmd\] ; puts NAK\r\n" |
| expect { |
| "puts ACK*$cmd*puts NAK" { |
| verbose "Got command echo" 3 |
| } |
| timeout { |
| warning "Never got command echo" |
| } |
| } |
| |
| expect { |
| -re {Checking pattern*with*[\r\n]} { |
| exp_continue |
| } |
| -re "\r\n1\r\n" { |
| $true $msg |
| } |
| -re "\r\n0\r\n" { |
| $false $msg |
| } |
| timeout { |
| perror "$msg (timed out)" |
| } |
| } |
| } |