• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7# Decompress files.
8# On success, the input files are removed and replaced by new
9# files without the .gz suffix.
10echo -n "foo " | gzip > f1.gz
11echo "bar" | gzip > f2.gz
12testing "with input files" "gunzip f1.gz f2.gz &&
13    ! test -f f1.gz && ! test -f f2.gz &&
14    test -f f1 && test -f f2 &&
15    cat f1 f2" "foo bar\n" "" ""
16rm -f f1 f2 f1.gz f2.gz
17
18# With no files, decompresses stdin to stdout.
19echo "hello world" | gzip > f.gz
20testing "no files (stdin to stdout)" "cat f.gz | gunzip > f &&
21    test -f f.gz && cat f" "hello world\n" "" ""
22rm -f f f.gz
23
24# -c	Output to stdout
25echo -n "foo " | gzip > f1.gz
26echo "bar" | gzip > f2.gz
27testing "with input files and -c" "gunzip -c f1.gz f2.gz > out &&
28    test -f f1.gz && test -f f2.gz &&
29    ! test -f f1 && ! test -f f2 &&
30    cat out" "foo bar\n" "" ""
31rm -f f1.gz f2.gz out
32
33# TODO: how to test "gunzip -f"?
34
35# -k	Keep input files (don't remove)
36echo "hello world" | gzip > f1.gz
37testing "-k" "gunzip -k f1.gz && cat f1 && zcat f1.gz" \
38    "hello world\nhello world\n" "" ""
39rm -f f1 f1.gz
40
41# Test that gunzip preserves permissions and times.
42export TZ=UTC
43echo "hello world" | gzip > f1.gz
44chmod 0411 f1.gz
45touch -a -t 197801020304 f1.gz
46touch -m -t 198704030201 f1.gz
47testing "permissions/times preservation" \
48    "gunzip -k f1.gz && stat -c '%a %X %Y' f1 && stat -c '%a %Y' f1.gz" \
49    "411 252558240 544413660\n411 544413660\n" "" ""
50rm -f f1 f1.gz
51