tests: prune some weed in a non-POSIX test
[platform/upstream/automake.git] / t / dist-formats.tap
1 #! /bin/sh
2 # Copyright (C) 2012-2013 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 # Check support for different compression formats used by distribution
18 # archives.
19
20 am_create_testdir=empty
21 . test-init.sh
22
23 plan_ 70
24
25 # ---------------------------------------------------- #
26 #  Common and/or auxiliary subroutines and variables.  #
27 # ---------------------------------------------------- #
28
29 ocwd=$(pwd) || fatal_ "getting current working directory"
30
31 unset TAR
32
33 # Create common aclocal.m4 file, for later tests.
34 mkdir setup \
35   && cd setup \
36   && echo 'AC_INIT([x], [0]) AM_INIT_AUTOMAKE' > configure.ac \
37   && $ACLOCAL \
38   && mv aclocal.m4 .. \
39   && cd .. \
40   && rm -rf setup \
41   || fatal_ "creating common aclocal.m4 file"
42
43 # Some make implementations (e.g., HP-UX) don't grok '-j', some require
44 # no space between '-j' and the number of jobs (e.g., older GNU make
45 # versions), and some *do* require a space between '-j' and the number
46 # of jobs (e.g., Solaris dmake).  We need a runtime test to see what
47 # works.
48 for MAKE_j4 in "$MAKE -j4" "$MAKE -j 4" false; do
49   echo all: | $MAKE_j4 -f - && break
50   : For shells with buggy 'set -e'.
51 done
52
53 # Set variables '$compressor' and '$suffix'.
54 setup_vars_for_compression_format ()
55 {
56   suffix=NONE compressor=NONE
57   case $1 in
58     gzip) suffix=tar.gz  compressor=gzip     ;;
59     tarZ) suffix=tar.Z   compressor=compress ;;
60     lzip) suffix=tar.lz  compressor=lzip     ;;
61       xz) suffix=tar.xz  compressor=xz       ;;
62    bzip2) suffix=tar.bz2 compressor=bzip2    ;;
63      zip) suffix=zip     compressor=zip      ;;
64     shar) suffix=shar.gz compressor=shar     ;;
65        *) fatal_ "invalid compression format '$1'";;
66   esac
67 }
68
69 have_compressor ()
70 {
71   test $# -eq 1 || fatal_ "have_compressor(): bad usage"
72   case $1 in
73     # Assume gzip(1) is available on every reasonable portability target.
74     gzip)
75       return 0;;
76     # On Cygwin, as of 9/2/2012, 'compress' is provided by sharutils
77     # and is just a dummy script that is not able to actually compress
78     # (it can only decompress).  So, check that the 'compress' program
79     # is actually able to compress input.
80     # Note that, at least on GNU/Linux, 'compress' does (and is
81     # documented to) exit with status 2 if the output is larger than
82     # the input after (attempted) compression; so we need to pass it
83     # an input that it can actually reduce in size when compressing.
84     compress)
85       for x in 1 2 3 4 5 6 7 8; do
86         echo aaaaaaaaaaaaaaaaaaaaa
87       done | compress -c >/dev/null && return 0
88       return 1
89       ;;
90     *)
91       case $1 in
92         # Do not use --version, or older versions bzip2 would try to
93         # compress stdin.  This would cause binary output in the test
94         # logs, with potential breakage of our testsuite harness.
95         bzip2) o=--help;;
96         # OpenSolaris zip do not support the '--version' option, but
97         # accepts the '-v' one with a similar meaning (if no further
98         # arguments are given).
99         zip) o=-v;;
100         # Assume the other compressors we care about support the
101         # '--version' option.
102         *) o=--version;;
103       esac
104       # Redirect to stderr to avoid polluting the output, in case this
105       # function is used in a command substitution (as it is, below).
106       if $1 $o </dev/null >&2; then
107         return 0
108       else
109         return 1
110       fi
111       ;;
112   esac
113   fatal_ "have_compressor(): dead code reached"
114 }
115
116 all_compression_formats='gzip tarZ lzip xz bzip2 zip shar'
117
118 all_compressors=$(
119   for x in $all_compression_formats; do
120     setup_vars_for_compression_format $x
121     echo $compressor
122   done | tr "$nl" ' ')
123 echo All compressors: $all_compressors
124
125 missing_compressors=$(
126   for c in $all_compressors; do
127     have_compressor $c || echo $c
128   done | tr "$nl" ' ')
129 echo Missing compressors: $missing_compressors
130
131 # Redefine to avoid re-running the already executed checks.
132 have_compressor ()
133 {
134   case " $missing_compressors " in *\ $1\ *) false;; *) : ;; esac
135 }
136
137 have_all_compressors ()
138 {
139   test -z "$missing_compressors"
140 }
141
142 start_subtest ()
143 {
144   name=$1; shift
145   test -n "$name" || fatal_ "start_subtest: no subtest name given"
146   if test $# -gt 0; then
147     eval "$@" || fatal_ "start_subtest: evaluating assignments"
148   fi
149   ac_opts=$(echo $ac_opts | tr ',' ' ')
150   am_opts=$(echo $am_opts | tr ',' ' ')
151   mkdir "$name"
152   cd "$name"
153   unindent > configure.ac <<END
154     AC_INIT([$name], [1.0])
155     AM_INIT_AUTOMAKE([$ac_opts])
156     AC_CONFIG_FILES([Makefile])
157     AC_OUTPUT
158 END
159   echo "AUTOMAKE_OPTIONS = $am_opts" > Makefile.am
160   # It is imperative that aclocal.m4 is copied after configure.ac has
161   # been created, to avoid a spurious trigger of the automatic remake
162   # rules for configure & co.
163   cp "$ocwd"/aclocal.m4 \
164      "$am_scriptdir"/missing \
165      "$am_scriptdir"/install-sh \
166      .
167 }
168
169 end_subtest ()
170 {
171   unset name; unset ac_opts; unset am_opts;
172   cd "$ocwd" || fatal_ "couldn't chdir back to '$ocwd'"
173 }
174
175 command_ok_if_have_compressor ()
176 {
177   if have_compressor "$compressor"; then
178     command_ok_ "$@"
179   else
180     skip_ -r "'$compressor' not available" "$1"
181   fi
182 }
183
184 can_compress ()
185 {
186   test $# -eq 2 || fatal_ "can_compress: bad number of arguments"
187   tarname=$1 format=$2
188   setup_vars_for_compression_format "$format"
189
190   command_ok_ "'dist-$format' target always created" $MAKE -n dist-$format
191
192   command_ok_if_have_compressor "'make dist-$format' work by default" \
193     eval '
194       rm -rf *$tarname* \
195         && $MAKE dist-$format \
196         && test -f $tarname-1.0.$suffix \
197         && ls -l *$tarname* \
198         && test "$(echo *$tarname*)" = $tarname-1.0.$suffix'
199
200   unset suffix compressor format tarname
201 }
202
203 # ---------------------------------------- #
204 #  Defaults layout of the dist-* targets.  #
205 # ---------------------------------------- #
206
207 start_subtest defaults
208
209 command_ok_ "default [automake]"        $AUTOMAKE
210 command_ok_ "default [autoconf]"        $AUTOCONF
211 command_ok_ "default [configure]"       ./configure
212 command_ok_ "default [make distcheck]"  $MAKE distcheck
213
214 command_ok_ "'make dist' only builds *.tar.gz by default" \
215             test "$(ls *defaults*)" = defaults-1.0.tar.gz
216
217 rm -rf *defaults*
218
219 for fmt in $all_compression_formats; do
220   can_compress defaults $fmt
221 done
222 unset fmt
223
224 end_subtest
225
226 # ----------------------------------------------------------- #
227 #  Check diagnostic for no-dist-gzip without another dist-*.  #
228 # ----------------------------------------------------------- #
229
230 nogzip_stderr ()
231 {
232   grep "$1:.*no-dist-gzip" stderr \
233     && grep "$1:.* at least one archive format must be enabled" stderr
234 }
235
236 nogzip_automake_failure ()
237 {
238   AUTOMAKE_fails -d "no-dist-gzip ($1) without other formats is an error"
239   command_ok_ "no-dist-gzip ($1) without other formats gives diagnostic" \
240               nogzip_stderr "$2"
241 }
242
243 start_subtest am-nogz-only am_opts=no-dist-gzip ac_opts=
244 nogzip_automake_failure 'am' 'Makefile\.am:1'
245 end_subtest
246
247 start_subtest ac-nogz-only am_opts= ac_opts=no-dist-gzip
248 nogzip_automake_failure 'ac' 'configure\.ac:2'
249 end_subtest
250
251 # ------------------------------------------------- #
252 #  Check use of no-dist-gzip with a dist-* option.  #
253 # ------------------------------------------------- #
254
255 append_to_opt ()
256 {
257   var=$1_opts val=$2
258   eval "$var=\${$var:+\"\$$var,\"}\$val" || fatal_ "evaluating \${$var}"
259   unset var val
260 }
261
262 nogzip ()
263 {
264   test $#,$1,$3,$5 = 6,in,and,in \
265     && case $2,$6 in ac,ac|ac,am|am,ac|am,am) :;; *) false;; esac \
266     || fatal_ "nogzip: invalid usage"
267   format=$4 where_dist_nogzip=$2 where_dist_format=$6
268   shift 6
269
270   am_opts= ac_opts=
271   append_to_opt $where_dist_format dist-$format
272   append_to_opt $where_dist_nogzip no-dist-gzip
273   setup_vars_for_compression_format "$format"
274   # Do these before the am_opts and ac_opts variable can be munged
275   # by 'start_subtest'.
276   desc=
277   test -n "$am_opts" && desc=${desc:+"$desc "}"am=$am_opts"
278   test -n "$ac_opts" && desc=${desc:+"$desc "}"ac=$ac_opts"
279
280   start_subtest nogzip-$format am_opts=$am_opts ac_opts=$ac_opts
281
282   unindent >> Makefile.am <<END
283     check-ark-name:
284         test \$(DIST_ARCHIVES) = \$(distdir).$suffix
285     check-ark-exists:
286         test -f \$(distdir).$suffix
287     check-no-tar-gz:
288         test ! -f \$(distdir).tar.gz
289 END
290
291   command_ok_ "$desc [automake]" $AUTOMAKE
292   command_ok_ "$desc [autoconf]" $AUTOCONF
293   command_ok_ "$desc [configure]" ./configure
294   command_ok_ "$desc [ark-name]" $MAKE check-ark-name
295   command_ok_if_have_compressor "$desc [distcheck]" $MAKE distcheck
296   command_ok_if_have_compressor "$desc [ark-exists]" $MAKE check-ark-exists
297   command_ok_ "$desc [no .tar.gz]"  $MAKE check-no-tar-gz
298
299   unset desc
300
301   end_subtest
302 }
303
304 #      $1 $2  $3   $4     $5  $6
305 nogzip in am  and  bzip2  in  am
306 nogzip in ac  and  xz     in  am
307 nogzip in am  and  lzip   in  ac
308 nogzip in ac  and  tarZ   in  ac
309
310
311 # ----------------------------------------------------------- #
312 #  The 'dist-gzip' target is created also with no-dist-gzip.  #
313 # ----------------------------------------------------------- #
314
315 start_subtest dist-gzip-persistence am_opts=no-dist-gzip,dist-xz
316 command_ok_ "dist-gzip persistence [automake]"  $AUTOMAKE
317 command_ok_ "dist-gzip persistence [autoconf]"  $AUTOCONF
318 command_ok_ "dist-gzip persistence [configure]" ./configure
319 can_compress dist-gzip-persistence gzip
320 end_subtest
321
322
323 # ----------------------- #
324 #  Parallel compression.  #
325 # ----------------------- #
326
327 # We only use formats requiring 'gzip', 'bzip2' and 'compress' programs,
328 # since there are the most likely to be all found on the great majority
329 # of systems.
330
331 start_subtest parallel-compression ac_opts=dist-bzip2 am_opts=dist-tarZ
332
333 desc=gzip+bzip2+tarZ
334 tarname=parallel-compression-1.0
335
336 check_tarball ()
337 {
338   format=$1
339   setup_vars_for_compression_format $format
340   (
341     tarball=$tarname.$suffix \
342       && test -f $tarball \
343       && mkdir check-$format \
344       && cp $tarball check-$format \
345       && cd check-$format \
346       && $compressor -d $tarball \
347       && tar xvf $tarname.tar \
348       && diff ../Makefile.in $tarname/Makefile.in \
349       && cd .. \
350       && rm -rf check-$format
351    )
352 }
353
354 command_ok_ "$desc [automake]" $AUTOMAKE
355
356 skip_reason=
357 have_compressor compress || skip_reason="'compress' not available"
358 have_compressor bzip2 || skip_reason="'bzip2' not available"
359 if test "$MAKE_j4" = false; then
360   test -z "$skip_reason" || skip_reason="$skip_reason and "
361   skip_reason="${skip_reason}make concurrency unavailable"
362 fi
363
364 if test -n "$skip_reason"; then
365   skip_row_ 6 -r "$skip_reason" "$desc"
366 else
367   command_ok_ "$desc [autoconf]" $AUTOCONF
368   command_ok_ "$desc [configure]" ./configure
369   command_ok_ "$desc [make -j4 dist-all]"  $MAKE_j4 dist
370   ls -l # For debugging.
371   command_ok_ "$desc [check .tar.gz tarball]"  check_tarball gzip
372   command_ok_ "$desc [check .tar.bz2 tarball]" check_tarball bzip2
373   command_ok_ "$desc [check .tar.Z tarball]"   check_tarball tarZ
374 fi
375
376 unset tarname desc skip_reason
377
378 end_subtest
379
380
381 # --------------------------------------------------------- #
382 #  The various 'dist-*' targets can happily work together.  #
383 # --------------------------------------------------------- #
384
385 start_subtest all-together
386
387 desc='all compressors together'
388 tarname=all-together-1.0
389
390 echo 'AM_INIT_AUTOMAKE([' > am-init.m4
391 echo 'AUTOMAKE_OPTIONS =' > Makefile.am
392
393 # Add half 'dist-*' options to AM_INIT_AUTOMAKE, half to AUTOMAKE_OPTIONS.
394 flip=:
395 for fmt in $all_compression_formats; do
396   test $fmt = gzip && continue
397   if $flip; then
398     echo "  dist-$fmt" >> am-init.m4
399     flip=false
400   else
401     echo "AUTOMAKE_OPTIONS += dist-$fmt" >> Makefile.am
402     flip=:
403   fi
404 done
405 unset flip fmt
406
407 echo '])' >> am-init.m4
408
409 sed 's/AM_INIT_AUTOMAKE.*/m4_include([am-init.m4])/' configure.ac > t
410 mv -f t configure.ac
411
412 # For debugging.
413 cat Makefile.am
414 cat configure.ac
415 cat am-init.m4
416
417 command_ok_ "$desc [aclocal]" $ACLOCAL --force
418 command_ok_ "$desc [automake]" $AUTOMAKE
419 command_ok_ "$desc [autoconf]" $AUTOCONF
420 command_ok_ "$desc [configure]" ./configure
421
422 if have_all_compressors; then
423   command_ok_ "$desc [make distcheck, real]" $MAKE distcheck
424 else
425   skip_ -r "not all compressors available" "$desc [make distcheck, real]"
426 fi
427
428 # We fake existence of all the compressors here, so that we don't have
429 # to require any of them to run the further tests.  This is especially
430 # important since it's very unlikely that a non-developer has all the
431 # compression tools installed on his machine at the same time.
432
433 mkdir bin
434 cd bin
435 cat > check-distdir <<END
436 #!/bin/sh
437 { ls -l '$tarname' && diff Makefile.am '$tarname'/Makefile.am; } >&2 \
438   || { echo "== distdir fail =="; exit 1; }
439 END
440 cat > grep-distdir-error <<'END'
441 #!/bin/sh
442 grep 'distdir fail' && exit 1
443 :
444 END
445 chmod a+x check-distdir grep-distdir-error
446 for prog in tar $all_compressors; do
447   case $prog in
448     tar|shar|zip) cp check-distdir $prog;;
449                *) cp grep-distdir-error $prog;;
450   esac
451 done
452 unset prog
453 ls -l # For debugging.
454 cd ..
455
456 oPATH=$PATH
457 PATH=$(pwd)/bin$PATH_SEPARATOR$PATH; export PATH
458
459 command_ok_ \
460   "$desc ['make dist-all', stubbed]" \
461   $MAKE dist-all
462
463 subdesc="$desc ['make dist -j4', stubbed]"
464 if test "$MAKE_j4" = false; then
465   skip_ -r "make concurrency unavailable" "$subdesc"
466 else
467   command_ok_ "$subdesc" $MAKE_j4 dist
468 fi
469 unset subdesc
470
471 PATH=$oPATH; export PATH
472
473 end_subtest
474
475 :