2 # Test-specific TCL procedures required by DejaGNU.
3 # Copyright (C) 2000, 2003, 2004, 2005, 2006, 2010, 2011 Free Software
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Modified by Kevin Dalley <kevind@rahul.net> from the xargs files.
20 # Modified by David MacKenzie <djm@gnu.ai.mit.edu> from the gcc files
21 # written by Rob Savoye <rob@cygnus.com>.
27 verbose "base_dir is $base_dir" 2
29 set env(GNU_FINDUTILS_FD_LEAK_CHECK) "1"
31 # look for OLDFIND and FTSFIND
32 if { ![info exists OLDFIND] || ![info exists FTSFIND] } {
33 verbose "Searching for oldfind"
34 set dir "$base_dir/.."
37 if ![file exists "$dir/$objfile"] then {
38 error "dir is $dir, but I cannot see $objfile in that directory"
40 if ([findfile $dir/oldfind 1 0]) {
41 verbose "found oldfind, so ftsfind must be called find"
42 set OLDFIND [findfile $dir/oldfind $dir/oldfind [transform oldfind]]
43 set FTSFIND [findfile $dir/find $dir/find [transform find ]]
45 verbose "did not find oldfind, so ftsfind must be called ftsfind"
46 set OLDFIND [findfile $dir/find $dir/find [transform find ]]
47 set FTSFIND [findfile $dir/ftsfind $dir/ftsfind [transform ftsfind]]
51 verbose "ftsfind is at $FTSFIND" 2
52 verbose "oldfind is at $OLDFIND" 2
54 if { [ string equal $FTSFIND $OLDFIND ] } {
55 error "OLDFIND and FTSFIND are set to $FTSFIND, which can't be right"
58 if [file exists $FTSFIND] then {
59 if [file exists $OLDFIND] then {
60 verbose "FTSFIND=$FTSFIND and OLDFIND=$OLDFIND both exist." 2
62 error "OLDFIND=$OLDFIND, but that program does not exist"
65 error "FTSFIND=$FTSFIND, but that program does not exist (base_dir is $base_dir)"
70 if ![info exists FINDFLAGS] then {
75 # Extract and print the version number of find.
76 proc find_version {} {
80 if {[which $FTSFIND] != 0} then {
81 set tmp [ eval exec $FTSFIND $FINDFLAGS --version </dev/null | sed 1q ]
84 warning "$FTSFIND, program does not exist"
89 # Called by individual test scripts.
90 proc do_find_start { suffix findprogram flags passfail options infile output } {
93 set scriptname [uplevel {info script}]
94 set testbase [file rootname $scriptname]
97 if { [string match "f*" $passfail] } {
100 if { [string match "p*" $passfail] } {
103 if { [string match "xf*" $passfail] } {
107 if { [string match "xp*" $passfail] } {
112 untested "Badly defined test"
113 error "The first argument to find_start was $passfail but it should begin with p (pass) or f (fail) or xf (should fail but we know it passes) or xp (should pass but we know it fails)"
119 set test [file tail $testbase]
120 set testname "$test.$suffix"
122 # set compareprog "cmp"
123 set compareprog "diff -u"
126 if { $output != "" } {
127 error "The output option is not supported yet"
130 set outfile "$testbase.xo"
131 if {$infile != ""} then {
132 set infile "[file dirname [file dirname $testbase]]/inputs/$infile"
137 set cmd "$findprogram $flags $options < $infile > find.out.uns"
140 send_user "Spawning \"$cmd\"\n"
144 send_log "Hoping for this command to return nonzero\n"
146 send_log "Hoping for this command to return 0\n"
148 set failed [ catch "exec $cmd" result ]
149 send_log "return value is $failed, result is '$result'\n"
151 # The command failed.
153 send_log "As expected, $cmd returned nonzero\n"
155 fail "$testname, $result"
158 # The command returned 0.
160 fail "$testname, $result"
162 send_log "As expected, $cmd returned 0\n"
166 exec sort < find.out.uns > find.out
167 file delete find.out.uns
169 if [file exists $outfile] then {
170 # We use the 'sort' above to sort the output of find to ensure
171 # that the directory entries appear in a predictable order.
172 # Because in the general case the person compiling and running
173 # "make check" will have a different collating order to the
174 # maintainer, we can't guarantee that our "correct" answer
175 # is already sorted in the correct order. To avoid trying
176 # to figure out how to select a POSIX environment on a
177 # random system, we just sort the data again here, using
178 # the local user's environment.
179 exec sort < $outfile > cmp.out
180 set cmp_cmd "$compareprog find.out cmp.out"
182 send_log "$cmp_cmd\n"
183 catch "exec $cmp_cmd" cmpout
184 if {$cmpout != ""} then {
185 fail "$testname, standard output differs from the expected result:\n$cmpout"
189 if {[file size find.out] != 0} then {
190 fail "$testname, output should be empty"
197 proc optimisation_levels_to_test {} {
198 global OPTIMISATION_LEVELS
199 if [info exists OPTIMISATION_LEVELS] {
200 send_log "Running find at optimisation levels $OPTIMISATION_LEVELS\n"
201 return $OPTIMISATION_LEVELS
203 send_log "Running find at default optimisation levels\n"
208 proc find_start { passfail options {infile ""} {output ""} {setup ""}} {
215 if {$infile != ""} then {
216 set msg "Did not expect infile parameter to be set"
221 if {[which $FTSFIND] == 0} then {
222 error "$FTSFIND, program does not exist"
225 if {[which $OLDFIND] == 0} then {
226 error "$OLDFIND, program does not exist"
230 # Now run the test with each binary, once with each optimisation level.
231 foreach optlevel [optimisation_levels_to_test] {
232 set flags "$FINDFLAGS -O$optlevel"
233 if { ![info exists SKIP_OLD] || ! $SKIP_OLD } {
235 do_find_start old-O$optlevel $OLDFIND $flags $passfail $options $infile $output
237 if { ![info exists SKIP_NEW] || !$SKIP_NEW } {
239 do_find_start new-O$optlevel $FTSFIND $flags $passfail $options $infile $output
245 # Clean up (remove temporary files) before runtest exits.
247 catch "exec rm -f find.out cmp.out"
250 proc path_setting_is_unsafe {} {
252 set itemlist [ split $env(PATH) : ]
253 foreach item $itemlist {
254 if { [ string equal $item "" ] } {
257 if { [ string equal $item "." ] } {
260 if { ! [ string match "/*" $item ] } {
261 # not an absolute path element.
269 foreach filename $args {
270 set f [open "$filename" "a"]
275 proc mkdir { dirname } {
276 # Not all versions of Tcl offer 'file mkdir'.
277 set failed [ catch "file mkdir $dirname" result ]
279 # Fall back on the external command.
280 send_log "file mkdir does not work, falling back on exec mkdir\n"
281 exec mkdir "$dirname"
287 if { [ path_setting_is_unsafe ] } {
288 warning { Cannot perform test as your $PATH environment variable includes a reference to the current directory or a directory name which is not absolute }
289 untested { skipping this test because your $PATH variable is wrongly set }
297 proc fs_superuser [ ] {
301 exec chmod 000 $tmpfile
304 if [ file readable $tmpfile ] {
305 # On Cygwin, a user with admin rights can read all files, and
306 # access(foo,R_OK) correctly returns 1 for all files.
307 warning "You have superuser privileges, skipping this test."
308 untested {skipping this test because you have superuser privileges}