~dricottone/my-utils

6e88ef836939f27f3c6d1e830639bda8a895ed58 — Dominic Ricottone 3 years ago aa48802
Refactor tests
4 files changed, 123 insertions(+), 122 deletions(-)

M Makefile
M tests/bom_test.sh
M tests/compression_test.sh
A tests/init.sh
M Makefile => Makefile +5 -23
@@ 110,32 110,14 @@ uninstall:
	rm $(BIN_DIR)/wttr
	rm $(BIN_DIR)/zipls

test: clean static-test-files
	shellcheck src/*.bash src/*.sh $(LIB_DIR)/mylib.bash $(LIB_DIR)/myparse.bash $(LIB_DIR)/myminiparse.sh
	sh tests/compression_test.sh
test: clean
	#shellcheck src/*.bash src/*.sh $(LIB_DIR)/mylib.bash $(LIB_DIR)/myparse.bash $(LIB_DIR)/myminiparse.sh
	sh tests/init.sh
	sh tests/compression_test_new.sh
	sh tests/ctdir_test.sh
	sh tests/which_test.sh
	sh tests/bom_test.sh

static-test-files:
	printf "[1]\n[2]\n[3]\n\n[1]\n[2]\n[3]\n\n[1]\n[2]\n[3]\n\n[1]\n[2]\n[3]\n\n[1]\n[2]\n[3]\n\n" > tests/static/compression_result.txt
	printf "[1]\n[2]\n[3]\n\n" > tests/static/compression_target.txt
	printf "foo bar\n" > tests/static/debom_result.txt
	printf "\xEF\xBB\xBFfoo bar\n" > tests/static/debom_target.txt
	printf "(a)\n(b)\n(c)\n\n" > tests/static/decompression_result.txt
	cp tests/static/decompression_result.txt tests/static/tar.txt
	cp tests/static/decompression_result.txt tests/static/gzip.txt
	cp tests/static/decompression_result.txt tests/static/xz.txt
	cp tests/static/decompression_result.txt tests/static/zstd.txt
	cp tests/static/decompression_result.txt tests/static/bzip2.txt
	cd tests/static/; tar -cf decompression_target.tar tar.txt
	cd tests/static/; tar -czf decompression_target.tar.gz gzip.txt
	cd tests/static/; tar -cJf decompression_target.tar.xz xz.txt
	cd tests/static/; tar --zstd -cf decompression_target.tar.zst zstd.txt
	cd tests/static/; tar -cjf decompression_target.tar.bz2 bzip2.txt
	rm tests/static/tar.txt tests/static/gzip.txt tests/static/xz.txt tests/static/zstd.txt tests/static/bzip2.txt

clean:
	rm -rf tests/temp_compression
	rm -rf tests/temp_bom
	rm -rf tests/static


M tests/bom_test.sh => tests/bom_test.sh +5 -10
@@ 2,8 2,7 @@

# debom

# create temp directory
mkdir -p tests/temp_bom
cd tests/static

# exit codes
if ! debom -V >/dev/null 2>&1; then


@@ 21,13 20,11 @@ elif debom non-existant >/dev/null 2>&1; then
fi

# de-BOM a file
cd tests/temp_bom
debom -d ../static/debom_target.txt > debom.txt
if ! cmp ../static/debom_result.txt debom.txt >/dev/null 2>&1; then
debom -d debom_target.txt > debom_result.txt
if ! cmp rebom_target.txt debom_result.txt >/dev/null 2>&1; then
  printf "Failure in de-BOM test\n"
  exit 1
fi
cd ../..

# rebom



@@ 47,11 44,9 @@ elif rebom non-existant >/dev/null 2>&1; then
fi

# re-BOM a file
cd tests/temp_bom
rebom ../static/debom_result.txt > rebom.txt
if ! cmp ../static/debom_target.txt rebom.txt >/dev/null 2>&1; then
rebom rebom_target.txt > rebom_result.txt
if ! cmp debom_target.txt rebom_result.txt >/dev/null 2>&1; then
  printf "Failure in re-BOM test\n"
  exit 1
fi
cd ../..


M tests/compression_test.sh => tests/compression_test.sh +91 -89
@@ 2,98 2,100 @@

# mktar and tarcat

# create temp directory
mkdir -p tests/temp_compression

# compress file with algorithms set by options
cd tests/static
mktar compression_target.txt --compress=none
mktar compression_target.txt --compress=gzip
mktar compression_target.txt --compress=xz
mktar compression_target.txt --compress=zstd
mktar compression_target.txt --compress=bzip2
mv archive* ../temp_compression/
cd ..

# uncompress and combine compressed files
cd temp_compression
tarcat archive* > basic_concat.txt
cd ..

# test
if ! cmp static/compression_result.txt temp_compression/basic_concat.txt >/dev/null 2>&1; then
  printf "Failure in compression tests: basic compression\n"
  exit 1
fi

# compress file with algorithms implicitly set by output filename
cd static
mktar compression_target.txt -n implicit.tar
mktar compression_target.txt -n implicit.tar.gz
mktar compression_target.txt -n implicit.tar.xz
mktar compression_target.txt -n implicit.tar.zst
mktar compression_target.txt -n implicit.tar.bz2
mv implicit* ../temp_compression/
cd ..

# uncompress and combine compressed files
cd temp_compression
tarcat implicit* > implicit_concat.txt
cd ..

# test
if ! cmp static/compression_result.txt temp_compression/implicit_concat.txt >/dev/null 2>&1; then
  printf "Failure in compression tests: implicit compression\n"
  exit 1
fi

# compress file with algorithms set and filenames set
cd static
mktar compression_target.txt --compress=none  -n explicit.tar
mktar compression_target.txt --compress=gzip  -n explicit.tar.gz
mktar compression_target.txt --compress=xz    -n explicit.tar.xz
mktar compression_target.txt --compress=zstd  -n explicit.tar.zst
mktar compression_target.txt --compress=bzip2 -n explicit.tar.bz2
mv explicit* ../temp_compression/
cd ..

# uncompress and combine compressed files
cd temp_compression
tarcat explicit* > explicit_concat.txt
cd ..

# test
if ! cmp static/compression_result.txt temp_compression/explicit_concat.txt >/dev/null 2>&1; then
  printf "Failure in compression tests: explicit compression\n"
  exit 1
fi
test_compression() {
  case "$1" in
    NONE)
      arg_compression="none"
      fn_compressed="archive.tar"
      ;;
    GZIP)
      arg_compression="gzip"
      fn_compressed="archive.tar.gz"
      ;;
    XZ)
      arg_compression="xz"
      fn_compressed="archive.tar.xz"
      ;;
    ZSTD)
      arg_compression="zstd"
      fn_compressed="archive.tar.zst"
      ;;
    BZIP2)
      arg_compression="bzip2"
      fn_compressed="archive.tar.bz2"
      ;;
    *)
      printf "Failure in compression tests: not a valid scheme: '%s'\n" "$1"
      exit 1
      ;;
  esac

  #basic compression
  mktar compression_target.txt --compress="$arg_compression"
  tarcat "$fn_compressed" > compression_result.txt
  if ! cmp compression_result.txt compression_target.txt >/dev/null 2>&1; then
    printf "Failure in compression tests: basic compression: '%s'\n" "$1"
    exit 1
  fi

  rm -f compression_result.txt "$fn_compressed"

  #implicit compression
  mktar compression_target.txt -n "$fn_compressed"
  tarcat "$fn_compressed" > compression_result.txt
  if ! cmp compression_result.txt compression_target.txt >/dev/null 2>&1; then
    printf "Failure in compression tests: implicit compression: '%s'\n" "$1"
    exit 1
  fi

  rm -f compression_result.txt "$fn_compressed"
}

test_compression NONE
test_compression GZIP
test_compression XZ
test_compression ZSTD
test_compression BZIP2

# untar

# copy archives to temp directory
cp static/decompression_target* temp_compression/

# decompress files
cd temp_compression
untar decompression_target.tar decompression_target.tar.gz decompression_target.tar.xz decompression_target.tar.zst decompression_target.tar.bz2
cd ..

# test
if ! cmp static/decompression_result.txt temp_compression/tar.txt >/dev/null 2>&1; then
  printf "Failure in decompression tests: tar\n"
  exit 1
elif ! cmp static/decompression_result.txt temp_compression/gzip.txt >/dev/null 2>&1; then
  printf "Failure in decompression tests: gzip\n"
  exit 1
elif ! cmp static/decompression_result.txt temp_compression/xz.txt >/dev/null 2>&1; then
  printf "Failure in decompression tests: xz\n"
  exit 1
elif ! cmp static/decompression_result.txt temp_compression/zstd.txt >/dev/null 2>&1; then
  printf "Failure in decompression tests: zstd\n"
  exit 1
elif ! cmp static/decompression_result.txt temp_compression/bzip2.txt >/dev/null 2>&1; then
  printf "Failure in decompression tests: bzip2\n"
  exit 1
fi

test_decompression() {
  case "$1" in
    NONE)
      fn_compressed="decompression_target.tar"
      ;;
    GZIP)
      fn_compressed="decompression_target.tar.gz"
      ;;
    XZ)
      fn_compressed="decompression_target.tar.xz"
      ;;
    ZSTD)
      fn_compressed="decompression_target.tar.zst"
      ;;
    BZIP2)
      fn_compressed="decompression_target.tar.bz2"
      ;;
    *)
      printf "Failure in compression tests: not a valid scheme: '%s'\n" "$1"
      exit 1
      ;;
  esac

  untar "$fn_compressed"
  if ! cmp decompression_result.txt decompression_target.txt >/dev/null 2>&1; then
    printf "Failure in decompression tests: '%s'\n" "$1"
    exit 1
  fi

  rm -f decompression_result.txt
}

test_decompression NONE
test_decompression GZIP
test_decompression XZ
test_decompression ZSTD
test_decompression BZIP2


A tests/init.sh => tests/init.sh +22 -0
@@ 0,0 1,22 @@
#!/bin/sh

mkdir tests/static

# compression target
printf "[1]\n[2]\n[3]\n\n" > tests/static/compression_target.txt

# debom target
printf "\xEF\xBB\xBFfoo bar\n" > tests/static/debom_target.txt

# rebom target
printf "foo bar\n" > tests/static/rebom_target.txt

# decompression targets
printf "(a)\n(b)\n(c)\n\n" > tests/static/decompression_result.txt
tar -C tests/static -cf tests/static/decompression_target.tar decompression_result.txt
tar -C tests/static -czf tests/static/decompression_target.tar.gz decompression_result.txt
tar -C tests/static -cJf tests/static/decompression_target.tar.xz decompression_result.txt
tar -C tests/static --zstd -cf tests/static/decompression_target.tar.zst decompression_result.txt
tar -C tests/static -cjf tests/static/decompression_target.tar.bz2 decompression_result.txt
mv tests/static/decompression_result.txt tests/static/decompression_target.txt