1 #!/bin/sh 2 # Ugly, but works ? 3 4 set -e 5 6 fullpath() { 7 # Because portability is a pita. 8 realpath "$0" 2>/dev/null && return 0 9 readlink -f "$0" 2>/dev/null && return 0 10 python -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$0" 11 } 12 13 SCRIPT="$(fullpath "$@")" 14 TESTDIR="$(dirname "${SCRIPT}")" 15 TOP_SRCDIR="$(dirname "${TESTDIR}")" 16 17 if [ -z "${COMPRESS}" ]; then 18 COMPRESS="${TOP_SRCDIR}/compress" 19 fi 20 compress() { "${COMPRESS}" -v "$@"; } 21 uncompress() { "${COMPRESS}" -v -d "$@"; } 22 23 echo "using compress: ${COMPRESS}" 24 25 echo "Setting up test env" 26 27 TMPDIR="" 28 cleanup() { 29 # See if we're exiting due to an error. 30 local ret=$1 31 # Don't allow failures in this hook to prevent final cleanup. 32 set +e 33 if [ ${ret} -ne 0 ]; then 34 ls -Ral "${TMPDIR}" 35 fi 36 rm -rf "${TMPDIR}" 37 } 38 trap 'cleanup $?' EXIT 39 TMPDIR="$(mktemp -d)" 40 41 cd "${TMPDIR}" 42 43 cp "${TOP_SRCDIR}/README.md" input 44 cp input i 45 mkdir subdir 46 cp input i subdir/ 47 mkdir emptydir 48 touch emptyfile 49 50 set -x 51 52 : "### Check basic CLI exit status" 53 compress -V 54 compress -h >/dev/null 55 56 out=$(compress -h) 57 [ -n "${out}" ] 58 ( 59 # The shell trace output can write to stderr which we check here. 60 set +x 61 out=$(compress -h 2>&1 >/dev/null) 62 [ -z "${out}" ] 63 ) 64 65 if compress -X 2>/dev/null; then false; fi 66 67 : "### Check unknown flags spit to stderr only" 68 out=$(compress -X 2>/dev/null) || : 69 [ -z "${out}" ] 70 71 out=$(compress -X 2>&1) || : 72 [ -n "${out}" ] 73 74 : "### Check -- handling" 75 cp input ./-X 76 compress -- -X 77 [ -e ./-X.Z ] 78 uncompress -- -X 79 [ -e ./-X ] 80 rm ./-X 81 82 : "### Check compression" 83 compress input 84 [ ! -e input ] 85 [ -e input.Z ] 86 87 : "### Check compressing .Z file fails" 88 if compress input.Z; then false; fi 89 90 : "### Check decompression w/explicit .Z" 91 uncompress input.Z 92 [ -e input ] 93 [ ! -e input.Z ] 94 95 : "### Check decompression w/implicit .Z" 96 compress input 97 uncompress input.Z 98 [ -e input ] 99 [ ! -e input.Z ] 100 101 : "### Check uncompressing non-.Z file fails" 102 if uncompress input; then false; fi 103 104 : "### Check empty directory compression" 105 if compress emptydir; then false; fi 106 if compress -r emptydir; then false; fi 107 108 : "### Check directory compression" 109 compress -r subdir 110 111 : "### Check directory compression resume" 112 uncompress subdir/i 113 compress -r subdir 114 115 : "### Check empty directory decompression" 116 if uncompress -r emptydir; then false; fi 117 118 : "### Check directory decompression" 119 uncompress -r subdir 120 121 : "### Check uncompressed directory decompression" 122 if uncompress -r subdir; then false; fi 123 124 : "### Check directory decompression resume" 125 compress subdir/i 126 uncompress -r subdir 127 128 : "### Check various error edge cases" 129 if compress missing; then false; fi 130 if uncompress missing; then false; fi 131 132 : "### Check forced compression" 133 if compress emptyfile; then false; fi 134 if compress <emptyfile >/dev/null; then false; fi 135 compress -f emptyfile 136 uncompress emptyfile 137 138 : "### Check stdin/stdout handling" 139 compress -c input >input.Z 140 [ -e input ] 141 compress <input >input.Z 142 uncompress -c input.Z >input.new 143 cmp input input.new 144 uncompress <input.Z >input.new 145 cmp input input.new 146 147 : "### Check existing files" 148 if compress input </dev/null; then false; fi 149 compress -f input 150 mv input.new input 151 if uncompress input.Z </dev/null; then false; fi 152 uncompress -f input.Z 153 154 : "### Check empty file names" 155 if compress ""; then false; fi 156 if uncompress ""; then false; fi 157 158 : "### Check .Z dir edge case" 159 mkdir .Z 160 if uncompress ""; then false; fi 161 rmdir .Z 162 163 : "### Check short filenames" 164 compress i 165 uncompress i 166 compress i 167 uncompress i.Z 168 169 : "### Check keep option" 170 compress -k i 171 [ -e i -a -e i.Z ] 172 rm i 173 uncompress -k i 174 [ -e i -a -e i.Z ] 175 rm i.Z 176 177 : "### All passed!"