tr: fix various issues with case conversion
[coreutils/ericb.git] / tests / cp / same-file
blobbcb2a9d0d1b83b37281beb243eb8af53d794329f
1 #!/bin/sh
2 # Test some of cp's options and how cp handles situations in
3 # which a naive implementation might overwrite the source file.
5 # Copyright (C) 1998-2000, 2002-2004, 2006-2010 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if test "$VERBOSE" = yes; then
21 set -x
22 cp --version
25 . $srcdir/test-lib.sh
27 # Unset CDPATH. Otherwise, output from the `cd dir' command
28 # can make this test fail.
29 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
31 VERSION_CONTROL=numbered; export VERSION_CONTROL
33 # Determine whether a hard link to a symlink points to the symlink
34 # itself or to its referent. For example, the link from FreeBSD6.1
35 # does dereference a symlink, but the one from Linux does not.
36 ln -s no-such dangling-slink
37 ln dangling-slink hard-link > /dev/null 2>&1 \
38 && hard_link_to_symlink_does_the_deref=no \
39 || hard_link_to_symlink_does_the_deref=yes
40 rm -f no-such dangling-slink hard-link
42 test $hard_link_to_symlink_does_the_deref = yes \
43 && remove_these_sed='/^0 -[bf]*l .*sl1 ->/d' \
44 || remove_these_sed='/^ELIDE NO TEST OUTPUT/d'
46 actual=actual-$$
47 expected=expected-$$
49 exec 1> $actual
51 # FIXME: This should be bigger: like more than 8k
52 contents=XYZ
54 for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do
55 for options in '' -d -f -df --rem -b -bd -bf -bdf \
56 -l -dl -fl -dfl -bl -bdl -bfl -bdfl; do
57 case $args$options in
58 # These tests are not portable.
59 # They all involve making a hard link to a symbolic link.
60 # In the past, we've skipped the tests that are not portable,
61 # by doing "continue" here and eliminating the corresponding
62 # expected output lines below. Don't do that anymore.
63 'symlink foo'-dfl)
64 continue;;
65 'symlink foo'-bdl)
66 continue;;
67 'symlink foo'-bdfl)
68 continue;;
69 'sl1 sl2'-dfl)
70 continue;;
71 'sl1 sl2'-bd*l)
72 continue;;
73 'sl1 sl2'-dl)
74 continue;;
75 esac
77 # cont'd Instead, skip them only on systems for which link does
78 # dereference a symlink. Detect and skip such tests here.
79 case $hard_link_to_symlink_does_the_deref:$args:$options in
80 'yes:sl1 sl2:-fl')
81 continue ;;
82 'yes:sl1 sl2:-bl')
83 continue ;;
84 'yes:sl1 sl2:-bfl')
85 continue ;;
86 esac
88 rm -rf dir
89 mkdir dir
90 cd dir
91 echo $contents > foo
92 case "$args" in *symlink*) ln -s foo symlink ;; esac
93 case "$args" in *hardlink*) ln foo hardlink ;; esac
94 case "$args" in *sl1*) ln -s foo sl1;; esac
95 case "$args" in *sl2*) ln -s foo sl2;; esac
98 # echo 1>&2 cp $options $args
99 cp $options $args 2>_err
100 echo $? $options
102 # Normalize the program name and diagnostics in the error output,
103 # and put brackets around the output.
104 if test -s _err; then
105 sed '
106 s/^[^:]*:\([^:]*\).*/cp:\1/
107 1s/^/[/
108 $s/$/]/
109 ' _err
111 # Strip off all but the file names.
112 ls=`ls -gG --ignore=_err . \
113 | sed \
114 -e '/^total /d' \
115 -e 's/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *//'`
116 echo "($ls)"
117 # Make sure the original is unchanged and that
118 # the destination is a copy.
119 for f in $args; do
120 if test -f $f; then
121 case "`cat $f`" in
122 "$contents") ;;
123 *) echo cp FAILED;;
124 esac
125 else
126 echo symlink-loop
128 done
129 ) | tr '\n' ' '
130 echo
131 ) | sed 's/ *$//'
132 cd ..
133 done
134 echo
135 done
137 cat <<\EOF | sed "$remove_these_sed" > $expected
138 1 [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
139 1 -d [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
140 1 -f [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
141 1 -df [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
142 0 --rem (foo symlink)
143 0 -b (foo symlink symlink.~1~ -> foo)
144 0 -bd (foo symlink symlink.~1~ -> foo)
145 0 -bf (foo symlink symlink.~1~ -> foo)
146 0 -bdf (foo symlink symlink.~1~ -> foo)
147 1 -l [cp: cannot create link `symlink'] (foo symlink -> foo)
148 0 -dl (foo symlink -> foo)
149 0 -fl (foo symlink)
150 0 -dfl (foo symlink)
151 0 -bl (foo symlink symlink.~1~ -> foo)
152 0 -bdl (foo symlink symlink.~1~ -> foo)
153 0 -bfl (foo symlink symlink.~1~ -> foo)
154 0 -bdfl (foo symlink symlink.~1~ -> foo)
156 1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
157 1 -d [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
158 1 -f [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
159 1 -df [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
160 1 --rem [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
161 1 -b [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
162 0 -bd (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
163 1 -bf [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
164 0 -bdf (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
165 0 -l (foo symlink -> foo)
166 0 -dl (foo symlink -> foo)
167 0 -fl (foo symlink -> foo)
168 0 -bl (foo symlink -> foo)
169 0 -bfl (foo symlink -> foo)
171 1 [cp: `foo' and `foo' are the same file] (foo)
172 1 -d [cp: `foo' and `foo' are the same file] (foo)
173 1 -f [cp: `foo' and `foo' are the same file] (foo)
174 1 -df [cp: `foo' and `foo' are the same file] (foo)
175 1 --rem [cp: `foo' and `foo' are the same file] (foo)
176 1 -b [cp: `foo' and `foo' are the same file] (foo)
177 1 -bd [cp: `foo' and `foo' are the same file] (foo)
178 0 -bf (foo foo.~1~)
179 0 -bdf (foo foo.~1~)
180 0 -l (foo)
181 0 -dl (foo)
182 0 -fl (foo)
183 0 -dfl (foo)
184 0 -bl (foo)
185 0 -bdl (foo)
186 0 -bfl (foo foo.~1~)
187 0 -bdfl (foo foo.~1~)
189 1 [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
190 0 -d (foo sl1 -> foo sl2 -> foo)
191 1 -f [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
192 0 -df (foo sl1 -> foo sl2 -> foo)
193 0 --rem (foo sl1 -> foo sl2)
194 0 -b (foo sl1 -> foo sl2 sl2.~1~ -> foo)
195 0 -bd (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
196 0 -bf (foo sl1 -> foo sl2 sl2.~1~ -> foo)
197 0 -bdf (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
198 1 -l [cp: cannot create link `sl2'] (foo sl1 -> foo sl2 -> foo)
199 0 -fl (foo sl1 -> foo sl2 -> foo)
200 0 -bl (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
201 0 -bfl (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
203 1 [cp: `foo' and `hardlink' are the same file] (foo hardlink)
204 1 -d [cp: `foo' and `hardlink' are the same file] (foo hardlink)
205 1 -f [cp: `foo' and `hardlink' are the same file] (foo hardlink)
206 1 -df [cp: `foo' and `hardlink' are the same file] (foo hardlink)
207 0 --rem (foo hardlink)
208 0 -b (foo hardlink hardlink.~1~)
209 0 -bd (foo hardlink hardlink.~1~)
210 0 -bf (foo hardlink hardlink.~1~)
211 0 -bdf (foo hardlink hardlink.~1~)
212 0 -l (foo hardlink)
213 0 -dl (foo hardlink)
214 0 -fl (foo hardlink)
215 0 -dfl (foo hardlink)
216 0 -bl (foo hardlink)
217 0 -bdl (foo hardlink)
218 0 -bfl (foo hardlink)
219 0 -bdfl (foo hardlink)
224 compare $expected $actual 1>&2 || fail=1
226 Exit $fail