Merge branch 'testsuite-saner-shell' into maint
[platform/upstream/automake.git] / t / self-check-cleanup.tap
1 #! /bin/sh
2 # Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # Sanity check for the automake testsuite.
18 # Check creation/removal of temporary test working directory by './defs'.
19
20 . ./defs || Exit 1
21
22 plan_ 43
23
24 # We still need a little hack to make ./defs work outside automake's
25 # tree 'tests' subdirectory.  Not a big deal.
26 sed "s|^am_top_builddir=.*|am_top_builddir='`pwd`'|" \
27   "$am_top_builddir"/defs-static >defs-static
28 diff "$am_top_builddir"/defs-static defs-static \
29   && fatal_ "failed to edit defs-static"
30 cp "$am_top_builddir"/defs .
31
32 AM_TESTS_REEXEC=no; export AM_TESTS_REEXEC
33 keep_testdirs=; unset keep_testdirs
34
35 if ln -s defs foo && test -h foo; then
36   have_symlinks=yes
37 else
38   have_symlinks=no
39 fi
40 export have_symlinks # Is used also by spawned shells.
41
42 # Don't let a failure poison all subsequent tests.
43 do_clean ()
44 {
45   # Don't try to be smart and use find here, that has caused issues
46   # and extra ERROR results in the past.  Be dumb and safe.
47   for d in t t/* t/*/* t/*/*/*; do
48     test ! -d $d || chmod u+rwx $d || :
49   done
50   rm -rf t
51 }
52
53 # Exporting 'keep_testdirs' to "yes" in the environment should cause
54 # the cleanup code not to be run, so that the temporary directories
55 # are left on disk.
56 command_ok_ '"keep_testdirs=yes" causes testdir to be kept around' eval '
57      env keep_testdirs=yes \
58        $AM_TEST_RUNNER_SHELL -c ". ./defs && echo okok >foo" t/dummy.sh \
59      && test -f t/dummy.dir/foo \
60      && test okok = `cat t/dummy.dir/foo`'
61
62 do_clean
63
64 # Check that pre-test cleanup works also with directories with
65 # "null" permissions, and containing broken symlinks.
66 mkdir t t/dummy.dir t/dummy.dir/sub
67 (
68   cd t/dummy.dir
69   touch file sub/file
70   if test $have_symlinks = yes; then
71     ln -s file symlink
72     ln -s none brokenlink
73   fi
74 )
75 chmod 000 t/dummy.dir/sub/* t/dummy.dir/file
76 test $have_symlinks = yes && chmod 000 t/dummy.dir/symlink
77 chmod 500 t/dummy.dir/sub t/dummy.dir
78 command_ok_ "pre-cleanup can deal with low-perms testdir" \
79             $AM_TEST_RUNNER_SHELL -c  '. ./defs' t/dummy.sh
80 command_ok_ "pre-cleanup removed low-perms testdir" \
81             eval 'test ! -f t/dummy.dir \
82                && test ! -d t/dummy.dir \
83                && test ! -r t/dummy.dir'
84
85 do_clean
86
87 # Check that post-test cleanup works also with directories with
88 # "null" permissions, and containing broken symlinks.
89 command_ok_ "post-cleanup can deal with low-perms testdir" \
90             $AM_TEST_RUNNER_SHELL -c  '
91   stderr_fileno_=2
92   . ./defs || Exit 1
93   set -e
94   mkdir dir dir/sub
95   cd dir
96   touch file sub/file
97   if test $have_symlinks = yes; then
98     ln -s file symlink
99     ln -s none brokenlink
100   fi
101   cd ..
102   chmod 000 dir/sub/* dir/file
103   test $have_symlinks = yes && chmod 000 dir/symlink
104   chmod 500 dir/sub dir
105   :
106 ' t/dummy.sh
107 command_ok_ "post-cleanup removed null-perms testdir" \
108             eval 'test ! -f t/dummy.dir \
109                && test ! -d t/dummy.dir \
110                && test ! -r t/dummy.dir'
111
112 do_clean
113
114 # Check that pre-test cleanup does not unduly change the permissions of
115 # files to which symlinks in the temporary test directory point to.
116 if test $have_symlinks = yes; then
117
118   mkdir dir
119   chmod 000 dir
120   : > file
121   chmod 000 file
122
123   mkdir t t/dummy.dir
124   (cd t/dummy.dir && ln -s ../../dir ../../file .)
125
126   command_ok_ "pre-cleanup with testdir with zero-perms symlinks" \
127                $AM_TEST_RUNNER_SHELL -c '. ./defs' t/dummy.sh
128   ls -l # For debugging.
129   command_ok_ "pre-cleanup chmod doesn't follow symlinks to files" \
130                eval 'ls -l file | grep "^----------.*file"'
131   command_ok_ "pre-cleanup chmod doesn't follow symlinks to dirs" \
132               eval 'ls -ld dir | grep "^d---------.*dir"'
133
134   command_ok_ "post-cleanup with testdir with zero-perms symlinks" \
135               $AM_TEST_RUNNER_SHELL -c '
136     ocwd=`pwd` || exit 1
137     stderr_fileno_=2
138     . ./defs || Exit 1
139     ln -s "$ocwd/dir" "$ocwd/file" .
140   ' t/dummy.sh
141   ls -l # For debugging.
142   command_ok_ "post-cleanup chmod doesn't follow symlinks to files" \
143                eval 'ls -l file | grep "^----------.*file"'
144   command_ok_ "post-cleanup chmod doesn't follow symlinks to dirs" \
145               eval 'ls -ld dir | grep "^d---------.*dir"'
146
147   chmod u+rwx dir file
148   rmdir dir
149   rm -f file
150
151 else # $have_symlinks = no
152   skip_row_ 6 "symlinks not supported"
153 fi
154
155 do_clean
156
157 # Check that the cleanup trap does not remove the temporary
158 # test directory in case of test failure, skip, hard-error,
159 # or when receiving a signal.
160
161 for st in 1 2 3 10 77 99 126 127 130 255; do
162   command_ok_ "exit trap doesn't clobber exit status $st" \
163               not $AM_TEST_RUNNER_SHELL -c "
164     stderr_fileno_=2
165     . ./defs
166     : > foo
167     Exit $st
168   " t/dummy.sh
169   command_ok_ "testdir not removed if exiting with status $st" \
170               test -f t/dummy.dir/foo
171   do_clean
172 done
173
174 for sig in 1 2 3 9 13 15; do
175   if is_blocked_signal $sig; then
176     skip_row_ 2 -r "signal $sig seems blocked"
177     continue
178   fi
179   command_ok_ "exit trap doesn't clobber signal $sig" \
180               not $AM_TEST_RUNNER_SHELL -c "
181     stderr_fileno_=2
182     . ./defs
183     : > foo
184     kill -$sig \$\$
185   " t/dummy.sh
186   command_ok_ "testdir not removed if getting signal $sig" \
187               test -f t/dummy.dir/foo
188   do_clean
189 done
190
191 :