fixup: distribute t/README
[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 if test x"$sh_errexit_works" != x"yes"; then
23   skip_all_ "$me: no working exit trap with 'set -e'"
24 fi
25
26 plan_ 43
27
28 # We still need a little hack to make ./defs work outside automake's
29 # tree 'tests' subdirectory.  Not a big deal.
30 sed "s|^am_top_builddir=.*|am_top_builddir='`pwd`'|" \
31   "$am_top_builddir"/defs-static >defs-static
32 diff "$am_top_builddir"/defs-static defs-static \
33   && fatal_ "failed to edit defs-static"
34 cp "$am_top_builddir"/defs .
35
36 AM_TESTS_REEXEC=no; export AM_TESTS_REEXEC
37 keep_testdirs=; unset keep_testdirs
38
39 if ln -s defs foo && test -h foo; then
40   have_symlinks=yes
41 else
42   have_symlinks=no
43 fi
44 export have_symlinks # Is used also by spawned shells.
45
46 # Don't let a failure poison all subsequent tests.
47 do_clean ()
48 {
49   # Don't try to be smart and use find here, that has caused issues
50   # and extra ERROR results in the past.  Be dumb and safe.
51   for d in t t/* t/*/* t/*/*/*; do
52     test ! -d $d || chmod u+rwx $d || :
53   done
54   rm -rf t
55 }
56
57 # Exporting 'keep_testdirs' to "yes" in the environment should cause
58 # the cleanup code not to be run, so that the temporary directories
59 # are left on disk.
60 command_ok_ '"keep_testdirs=yes" causes testdir to be kept around' eval '
61      keep_testdirs=yes $SHELL -c ". ./defs && echo okok >foo" t/dummy.sh \
62      && test -f t/dummy.dir/foo \
63      && test okok = `cat t/dummy.dir/foo`'
64
65 do_clean
66
67 # Check that pre-test cleanup works also with directories with
68 # "null" permissions, and containing broken symlinks.
69 mkdir t t/dummy.dir t/dummy.dir/sub
70 (
71   cd t/dummy.dir
72   touch file sub/file
73   if test $have_symlinks = yes; then
74     ln -s file symlink
75     ln -s none brokenlink
76   fi
77 )
78 chmod 000 t/dummy.dir/sub/* t/dummy.dir/file
79 test $have_symlinks = yes && chmod 000 t/dummy.dir/symlink
80 chmod 500 t/dummy.dir/sub t/dummy.dir
81 command_ok_ "pre-cleanup can deal with low-perms testdir" \
82             $SHELL -c  '. ./defs' t/dummy.sh
83 command_ok_ "pre-cleanup removed low-perms testdir" \
84             eval 'test ! -f t/dummy.dir \
85                && test ! -d t/dummy.dir \
86                && test ! -r t/dummy.dir'
87
88 do_clean
89
90 # Check that post-test cleanup works also with directories with
91 # "null" permissions, and containing broken symlinks.
92 command_ok_ "post-cleanup can deal with low-perms testdir" \
93             $SHELL -c  '
94   stderr_fileno_=2
95   . ./defs || Exit 1
96   set -e
97   mkdir dir dir/sub
98   cd dir
99   touch file sub/file
100   if test $have_symlinks = yes; then
101     ln -s file symlink
102     ln -s none brokenlink
103   fi
104   cd ..
105   chmod 000 dir/sub/* dir/file
106   test $have_symlinks = yes && chmod 000 dir/symlink
107   chmod 500 dir/sub dir
108   :
109 ' t/dummy.sh
110 command_ok_ "post-cleanup removed null-perms testdir" \
111             eval 'test ! -f t/dummy.dir \
112                && test ! -d t/dummy.dir \
113                && test ! -r t/dummy.dir'
114
115 do_clean
116
117 # Check that pre-test cleanup does not unduly change the permissions of
118 # files to which symlinks in the temporary test directory point to.
119 if test $have_symlinks = yes; then
120
121   mkdir dir
122   chmod 000 dir
123   : > file
124   chmod 000 file
125
126   mkdir t t/dummy.dir
127   (cd t/dummy.dir && ln -s ../../dir ../../file .)
128
129   command_ok_ "pre-cleanup with testdir with zero-perms symlinks" \
130                $SHELL -c '. ./defs' t/dummy.sh
131   ls -l # For debugging.
132   command_ok_ "pre-cleanup chmod doesn't follow symlinks to files" \
133                eval 'ls -l file | grep "^----------.*file"'
134   command_ok_ "pre-cleanup chmod doesn't follow symlinks to dirs" \
135               eval 'ls -ld dir | grep "^d---------.*dir"'
136
137   command_ok_ "post-cleanup with testdir with zero-perms symlinks" \
138               $SHELL -c '
139     ocwd=`pwd` || exit 1
140     stderr_fileno_=2
141     . ./defs || Exit 1
142     ln -s "$ocwd/dir" "$ocwd/file" .
143   ' t/dummy.sh
144   ls -l # For debugging.
145   command_ok_ "post-cleanup chmod doesn't follow symlinks to files" \
146                eval 'ls -l file | grep "^----------.*file"'
147   command_ok_ "post-cleanup chmod doesn't follow symlinks to dirs" \
148               eval 'ls -ld dir | grep "^d---------.*dir"'
149
150   chmod u+rwx dir file
151   rmdir dir
152   rm -f file
153
154 else # $have_symlinks = no
155   skip_row_ 6 "symlinks not supported"
156 fi
157
158 do_clean
159
160 # Check that the cleanup trap does not remove the temporary
161 # test directory in case of test failure, skip, hard-error,
162 # or when receiving a signal.
163
164 for st in 1 2 3 10 77 99 126 127 130 255; do
165   command_ok_ "exit trap doesn't clobber exit status $st" not $SHELL -c "
166     stderr_fileno_=2
167     . ./defs
168     : > foo
169     Exit $st
170   " t/dummy.sh
171   command_ok_ "testdir not removed if exiting with status $st" \
172               test -f t/dummy.dir/foo
173   do_clean
174 done
175
176 for sig in 1 2 3 9 13 15; do
177   if is_blocked_signal $sig; then
178     skip_row_ 2 -r "signal $sig seems blocked"
179     continue
180   fi
181   command_ok_ "exit trap doesn't clobber signal $sig" not $SHELL -c "
182     stderr_fileno_=2
183     . ./defs
184     : > foo
185     kill -$sig \$\$
186   " t/dummy.sh
187   command_ok_ "testdir not removed if getting signal $sig" \
188               test -f t/dummy.dir/foo
189   do_clean
190 done
191
192 :