aboutsummaryrefslogtreecommitdiff
path: root/testsuite/lib/libsup.exp
blob: afedfe6ee0d9ff485dd255047d62f111176c0b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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)"
	}
    }
}