summaryrefslogtreecommitdiff
path: root/tests/missing-tar.test
blob: 72c7f078719029894663ed8ef4976b80c6419ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /bin/sh
# Copyright (C) 2011 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test how the `missing' script wraps the`tar' program.

. ./defs || Exit 1

get_shell_script "missing"

old_PATH=$PATH; export old_PATH
new_PATH=`pwd`/bin$PATH_SEPARATOR$PATH
mkdir bin

cat > data.txt <<'END'
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
...
END

cat > nonesuch <<'END'
#!/bin/sh
exit 127
END
chmod a+x nonesuch

# Simple runs.
for nullify_gnu in yes no; do
  case $nullify_gnu in
    yes)
      cp nonesuch bin/gtar
      cp nonesuch bin/gnutar
      PATH=$new_PATH; export PATH;
      mkdir simple-nognu
      cd simple-nognu
      ;;
    no)
      mkdir simple-dflt
      cd simple-dflt
      ;;
    *)
      Exit 99 # Can't happen.
      ;;
  esac
  cp ../data.txt foobar.txt
  ../missing --run tar cvf mu.tar foobar.txt
  rm -f foobar.txt
  test -f mu.tar
  ../missing --run tar tf mu.tar >output 2>&1 || { cat output; Exit 1; }
  cat output
  $FGREP 'foobar.txt' output
  test ! -f foobar.txt
  ../missing --run tar xvf mu.tar
  diff ../data.txt foobar.txt
  PATH=$old_PATH; export PATH;
  cd ..
done

rm -f bin/*

# Helper scripts and functions for next tests.

cat > fake-gnu-tar <<'END'
#!/bin/sh
case " $* " in *\ --version\ *) echo fake GNU tar; exit $?;; esac
PATH=$old_PATH; export PATH
exec tar "$@"
END
chmod a+x fake-gnu-tar

cat > failing-tar <<'END'
#!/bin/sh
echo "Error message from tar passed through" >&2
exit 1
END
chmod a+x failing-tar

grep_tar_failed ()
{
  grep "WARNING:.* can't.* run \`tar' with .*given arguments" $*
}

# The `tar' program does not work with the given options, but we have
# gtar or gnutar.
cp failing-tar bin/tar
for pfx in g gnu; do
  case $pfx in g) othpfx=gnu;; gnu) othpfx=g;; *) Exit 99;; esac
  cp nonesuch bin/${othpfx}tar
  if ${pfx}tar --version | grep GNU; then :; else
    cp fake-gnu-tar bin/${pfx}tar
  fi
  tarball=foo-$pfx.tar
  PATH=$new_PATH; export PATH
  ./missing --run tar cvf $tarball ./data.txt 2>stderr \
    || { cat stderr >&2; Exit 1; }
  cat stderr >&2
  PATH=$old_PATH; export PATH
  test -f $tarball
  grep_tar_failed stderr
  grep "Error message from tar passed through" stderr
  grep "[Tt]rying to use GNU tar.*${pfx}tar" stderr
  grep "${othpfx}tar" stderr && Exit 1
  : # For shells with broken 'set -e'
done

rm -f bin/*

# The `tar' program does not work with the given options, and we don't
# have neither gtar nor gnutar.
cp failing-tar bin/tar
cp nonesuch bin/gtar
cp nonesuch bin/gnutar
PATH=$new_PATH; export PATH
./missing --run tar cvf foo.tar ./data.txt 2>stderr \
  && { cat stderr >&2; Exit 1; }
cat stderr >&2
PATH=$old_PATH; export PATH
test ! -f foo.tar
grep_tar_failed stderr
grep "Error message from tar passed through" stderr
grep "[iI]nstall GNU tar or Free [pP]axutils" stderr
$EGREP "(g|gnu)tar" stderr && Exit 1

rm -f bin/*

# We try to use an option that causes any `tar' program (GNU or non-GNU) to.
./missing --run tar --bad-unknonw-option cvf foo.tar ./data.txt 2>stderr \
  && { cat stderr >&2; Exit 1; }
cat stderr >&2
test ! -f foo.tar
grep_tar_failed stderr
grep "bad-unknonw-option" stderr
for p in gtar gnutar; do
  if $p --version; then
    cnt=1
  else
    cnt=0
  fi
  test `$EGREP -c "[Tt]rying to use GNU tar.*$p" stderr` -eq $cnt
done

: