Home | History | Annotate | Download | only in tests
      1 #!/bin/bash
      2 
      3 [ -f testing.sh ] && . testing.sh
      4 
      5 #testing "name" "command" "result" "infile" "stdin"
      6 
      7 # Compress files.
      8 # On success, the input files are removed and replaced by new
      9 # files with the .gz suffix.
     10 echo -n "foo " > f1
     11 echo "bar" > f2
     12 testing "with input files" "gzip f1 f2 && 
     13     test -f f1.gz && test -f f2.gz && 
     14     ! test -f f1 && ! test -f f2 && 
     15     zcat f1.gz f2.gz" "foo bar\n" "" ""
     16 rm -f f1 f2 f1.gz f2.gz
     17 
     18 # With no files, compresses stdin to stdout.
     19 testing "no files (stdin to stdout)" "echo hello world | gzip > f.gz && 
     20     test -f f.gz && zcat f.gz" "hello world\n" "" ""
     21 rm -f f.gz
     22 
     23 # -c	Output to stdout
     24 echo -n "foo " > f1
     25 echo "bar" > f2
     26 testing "with input files and -c" "gzip -c f1 f2 > out.gz && 
     27     ! test -f f1.gz && ! test -f f2.gz && 
     28     test -f f1 && test -f f2 && 
     29     zcat out.gz" "foo bar\n" "" ""
     30 rm -f f1 f2 out.gz
     31 
     32 # -d	Decompress (act as gunzip)
     33 echo "hello world" | gzip > f.gz
     34 testing "-d (act as gunzip)" "gzip -d f.gz && 
     35     test -f f && ! test -f f.gz && cat f" "hello world\n" "" ""
     36 rm -f f.gz f
     37 
     38 echo "hello world" | gzip > f.gz
     39 testing "-dc (act as zcat)" "gzip -dc f.gz && 
     40     ! test -f f && test -f f.gz" "hello world\n" "" ""
     41 rm -f f.gz f
     42 
     43 # -f	Force: allow overwrite of output file
     44 echo "hello world" > f1
     45 echo "precious data" > f1.gz
     46 testing "no overwrite without -f" \
     47     "gzip f1 2>/dev/null || echo refused && cat f1 f1.gz" \
     48     "refused\nhello world\nprecious data\n" "" ""
     49 testing "overwrite with -f" \
     50     "gzip -f f1 && echo allowed && ! test -f f1 && zcat f1.gz" \
     51     "allowed\nhello world\n" "" ""
     52 rm -f f1 f1.gz
     53 
     54 # -k	Keep input files (don't remove)
     55 echo "hello world" > f1
     56 testing "-k" "gzip -k f1 && cat f1 && zcat f1.gz" \
     57     "hello world\nhello world\n" "" ""
     58 rm -f f1 f1.gz
     59 
     60 # Test that -9 compresses better than -1.
     61 for i in $(seq 1 1000) ; do echo "hello world" >> x ; done
     62 gzip -c1 x > x1.gz
     63 gzip -c9 x > x9.gz
     64 testing "-1 vs -9" \
     65     "test $(stat -c '%s' x1.gz) -gt $(stat -c '%s' x9.gz) && echo okay" \
     66     "okay\n" "" ""
     67 rm -f x x1.gz x9.gz
     68 
     69 # Test that gzip preserves permissions and times.
     70 export TZ=UTC
     71 echo "hello world" > f1
     72 chmod 0411 f1
     73 touch -a -t 197801020304 f1
     74 touch -m -t 198704030201 f1
     75 testing "permissions/times preservation" \
     76     "gzip -k f1 && TZ=UTC stat -c '%a %Y' f1 && stat -c '%a %X %Y' f1.gz" \
     77     "411 544413660\n411 252558240 544413660\n" "" ""
     78 rm -f f1 f1.gz
     79