~dricottone/my-utils

573855ab6e5eeaee1e0c935bb6fcf78bf02e13f1 — Dominic Ricottone 2 years ago c361ee9
Rewrote test suite for bats
8 files changed, 364 insertions(+), 187 deletions(-)

M archives/Makefile
A archives/test/mktar-batch.bats
A archives/test/mktar.bats
A archives/test/tarcat.bats
A archives/test/untar.bats
D archives/test_init.sh
D archives/test_mktar_tarcat.sh
D archives/test_untar.sh
M archives/Makefile => archives/Makefile +2 -6
@@ 37,11 37,7 @@ uninstall:
	rm $(COMP_DIR)/myutils_archives.bash

test: clean
	#shellcheck *.bash *.sh $(LIB_DIR)/mylib.bash $(LIB_DIR)/myparse.bash $(LIB_DIR)/myminiparse.sh
	sh test_init.sh
	sh test_mktar_tarcat.sh
	sh test_untar.sh
	bats test/

clean:
	rm -rf test/
	rm -rf test/static/ test/output/


A archives/test/mktar-batch.bats => archives/test/mktar-batch.bats +132 -0
@@ 0,0 1,132 @@
#!/usr/bin/env bats
bats_require_minimum_version 1.5.0

setup() {
  mkdir -p test/static test/output
  printf "[1]\n[2]\n[3]\n\n" > test/static/compression_target.txt
}

teardown() {
  rm -rf test/static test/output
}

@test "mktar-batch none" {
  run --separate-stderr mktar-batch --compress=none --name test/output/archive.tar test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch none gpg" {
  run --separate-stderr mktar-batch --compress=none --encrypt=gpg --passphrase test123 --name test/output/archive.tar.gpg test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.gpg | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch none age" {
  run --separate-stderr mktar-batch --compress=none --encrypt=age --passphrase test123 --name test/output/archive.tar.age test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.age | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch gzip" {
  run --separate-stderr mktar-batch --compress=gzip --name test/output/archive.tar.gz test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.gz | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch gzip gpg" {
  run --separate-stderr mktar-batch --compress=gzip --encrypt=gpg --passphrase test123 --name test/output/archive.tar.gz.gpg test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.gz.gpg | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch gzip age" {
  run --separate-stderr mktar-batch --compress=gzip --encrypt=age --passphrase test123 --name test/output/archive.tar.gz.age test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.gz.age | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch xz" {
  run --separate-stderr mktar-batch --compress=xz --name test/output/archive.tar.xz test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.xz | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch xz gpg" {
  run --separate-stderr mktar-batch --compress=xz --encrypt=gpg --passphrase test123 --name test/output/archive.tar.xz.gpg test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.xz.gpg | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch xz age" {
  run --separate-stderr mktar-batch --compress=xz --encrypt=age --passphrase test123 --name test/output/archive.tar.xz.age test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.xz.age | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch zstd" {
  run --separate-stderr mktar-batch --compress=zstd --name test/output/archive.tar.zst test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.zst | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch zstd gpg" {
  run --separate-stderr mktar-batch --compress=zstd --encrypt=gpg --passphrase test123 --name test/output/archive.tar.zst.gpg test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.zst.gpg | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch zstd age" {
  run --separate-stderr mktar-batch --compress=zstd --encrypt=age --passphrase test123 --name test/output/archive.tar.zst.age test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.zst.age | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch bzip2" {
  run --separate-stderr mktar-batch --compress=bzip2 --name test/output/archive.tar.bz2 test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.bz2 | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch bzip2 gpg" {
  run --separate-stderr mktar-batch --compress=bzip2 --encrypt=gpg --passphrase test123 --name test/output/archive.tar.bz2.gpg test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.bz2.gpg | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar-batch bzip2 age" {
  run --separate-stderr mktar-batch --compress=bzip2 --encrypt=age --passphrase test123 --name test/output/archive.tar.bz2.age test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat --passphrase test123 test/output/archive.tar.bz2.age | cmp test/static/compression_target.txt >/dev/null 2>&1
}


A archives/test/mktar.bats => archives/test/mktar.bats +100 -0
@@ 0,0 1,100 @@
#!/usr/bin/env bats
bats_require_minimum_version 1.5.0

setup() {
  mkdir -p test/static test/output
  printf "[1]\n[2]\n[3]\n\n" > test/static/compression_target.txt
}

teardown() {
  rm -rf test/static test/output
}

@test "mktar none name" {
  run --separate-stderr mktar --name test/output/archive.tar test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar none name short" {
  run --separate-stderr mktar -n test/output/archive.tar test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar gzip name" {
  run --separate-stderr mktar --name test/output/archive.tar.gz test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.gz | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar gzip name short" {
  run --separate-stderr mktar -n test/output/archive.tar.gz test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.gz | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar xz name" {
  run --separate-stderr mktar --name test/output/archive.tar.xz test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.xz | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar xz name short" {
  run --separate-stderr mktar -n test/output/archive.tar.xz test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.xz | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar zstd name" {
  run --separate-stderr mktar --name test/output/archive.tar.zst test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.zst | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar zstd name alt" {
  run --separate-stderr mktar --name test/output/archive.tar.zstd test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.zstd | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar zstd name short" {
  run --separate-stderr mktar -n test/output/archive.tar.zst test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.zst | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar bzip2 name" {
  run --separate-stderr mktar --name test/output/archive.tar.bz2 test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.bz2 | cmp test/static/compression_target.txt >/dev/null 2>&1
}

@test "mktar bzip2 name short" {
  run --separate-stderr mktar -n test/output/archive.tar.bz2 test/static/compression_target.txt
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  tarcat test/output/archive.tar.bz2 | cmp test/static/compression_target.txt >/dev/null 2>&1
}


A archives/test/tarcat.bats => archives/test/tarcat.bats +61 -0
@@ 0,0 1,61 @@
#!/usr/bin/env bats
bats_require_minimum_version 1.5.0

setup() {
  mkdir -p test/static test/output
  printf "(a)\n(b)\n(c)\n\n" > test/static/decompression_result.txt
  tar -cf test/static/decompression_target.tar test/static/decompression_result.txt
  tar -czf test/static/decompression_target.tar.gz test/static/decompression_result.txt
  tar -cJf test/static/decompression_target.tar.xz test/static/decompression_result.txt
  tar --zstd -cf test/static/decompression_target.tar.zst test/static/decompression_result.txt
  cp test/static/decompression_target.tar.zst test/static/decompression_target.tar.zstd
  tar -cjf test/static/decompression_target.tar.bz2 test/static/decompression_result.txt
  mv test/static/decompression_result.txt test/static/decompression_target.txt
}

teardown() {
  rm -rf test/static test/output
}

@test "tarcat none" {
  run --separate-stderr sh -c 'tarcat test/static/decompression_target.tar | cmp test/static/decompression_target.txt >/dev/null 2>&1'
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
}

@test "tarcat gzip" {
  run --separate-stderr sh -c 'tarcat test/static/decompression_target.tar.gz | cmp test/static/decompression_target.txt >/dev/null 2>&1'
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
}

@test "tarcat xz" {
  run --separate-stderr sh -c 'tarcat test/static/decompression_target.tar.xz | cmp test/static/decompression_target.txt >/dev/null 2>&1'
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
}

@test "tarcat zstd" {
  run --separate-stderr sh -c 'tarcat test/static/decompression_target.tar.zst | cmp test/static/decompression_target.txt >/dev/null 2>&1'
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
}

@test "tarcat zstd alt" {
  run --separate-stderr sh -c 'tarcat test/static/decompression_target.tar.zstd | cmp test/static/decompression_target.txt >/dev/null 2>&1'
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
}

@test "tarcat bzip2" {
  run --separate-stderr sh -c 'tarcat test/static/decompression_target.tar.bz2 | cmp test/static/decompression_target.txt >/dev/null 2>&1'
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
}


A archives/test/untar.bats => archives/test/untar.bats +69 -0
@@ 0,0 1,69 @@
#!/usr/bin/env bats
bats_require_minimum_version 1.5.0

setup() {
  mkdir -p test/static test/output
  printf "(a)\n(b)\n(c)\n\n" > test/static/decompression_result.txt
  cd test/static
  tar -cf decompression_target.tar decompression_result.txt
  tar -czf decompression_target.tar.gz decompression_result.txt
  tar -cJf decompression_target.tar.xz decompression_result.txt
  tar --zstd -cf decompression_target.tar.zst decompression_result.txt
  cp decompression_target.tar.zst decompression_target.tar.zstd
  tar -cjf decompression_target.tar.bz2 decompression_result.txt
  mv decompression_result.txt decompression_target.txt
  cd ../..
}

teardown() {
  rm -rf test/static test/output
}

@test "untar none" {
  run --separate-stderr untar --to-directory test/output/ test/static/decompression_target.tar
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  cmp test/output/decompression_result.txt test/static/decompression_target.txt
}

@test "untar gzip" {
  run --separate-stderr untar --to-directory test/output/ test/static/decompression_target.tar.gz
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  cmp test/output/decompression_result.txt test/static/decompression_target.txt
}

@test "untar xz" {
  run --separate-stderr untar --to-directory test/output/ test/static/decompression_target.tar.xz
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  cmp test/output/decompression_result.txt test/static/decompression_target.txt
}

@test "untar zstd" {
  run --separate-stderr untar --to-directory test/output/ test/static/decompression_target.tar.zst
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  cmp test/output/decompression_result.txt test/static/decompression_target.txt
}

@test "untar zstd alt" {
  run --separate-stderr untar --to-directory test/output/ test/static/decompression_target.tar.zstd
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  cmp test/output/decompression_result.txt test/static/decompression_target.txt
}

@test "untar bzip2" {
  run --separate-stderr untar --to-directory test/output/ test/static/decompression_target.tar.bz2
  [ "$status" -eq 0 ]
  [ "$output" = "" ]
  [ "$stderr" = "" ]
  cmp test/output/decompression_result.txt test/static/decompression_target.txt
}


D archives/test_init.sh => archives/test_init.sh +0 -18
@@ 1,18 0,0 @@
#!/bin/sh

# create and move into temp directory
mkdir test/
cd test/

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

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


D archives/test_mktar_tarcat.sh => archives/test_mktar_tarcat.sh +0 -115
@@ 1,115 0,0 @@
#!/bin/sh
# tests mktar and tarcat

# move to temp directory
cd test

test_compression() {

  #set internal variables based on selected compression
  case "$1" in
    NONE)
      arg_compression="none"
      fn_compressed="archive.tar"
      fn_compressed_gpg="archive.tar.gpg"
      fn_compressed_age="archive.tar.age"
      ;;
    GZIP)
      arg_compression="gzip"
      fn_compressed="archive.tar.gz"
      fn_compressed_gpg="archive.tar.gz.gpg"
      fn_compressed_age="archive.tar.gz.age"
      ;;
    XZ)
      arg_compression="xz"
      fn_compressed="archive.tar.xz"
      fn_compressed_gpg="archive.tar.xz.gpg"
      fn_compressed_age="archive.tar.xz.age"
      ;;
    ZSTD)
      arg_compression="zstd"
      fn_compressed="archive.tar.zst"
      fn_compressed_gpg="archive.tar.zst.gpg"
      fn_compressed_age="archive.tar.zst.age"
      ;;
    BZIP2)
      arg_compression="bzip2"
      fn_compressed="archive.tar.bz2"
      fn_compressed_gpg="archive.tar.bz2.gpg"
      fn_compressed_age="archive.tar.bz2.age"
      ;;
    *)
      printf "Failure in compression tests: not a valid scheme: '%s'\n" "$1"
      exit 1
      ;;
  esac

  #
  # mktar tests
  #

  test="implicit compression with short name flag using"
  ../mktar -n "$fn_compressed" compression_target.txt
  ../tarcat "$fn_compressed" > compression_result.txt
  if ! cmp compression_result.txt compression_target.txt >/dev/null 2>&1; then
    printf "Failure in compression tests: %s '%s'\n" "$test" "$1"
    exit 1
  fi
  rm -f compression_result.txt "$fn_compressed"

  test="implicit compression with long name flag using"
  ../mktar --name "$fn_compressed" compression_target.txt
  ../tarcat "$fn_compressed" > compression_result.txt
  if ! cmp compression_result.txt compression_target.txt >/dev/null 2>&1; then
    printf "Failure in compression tests: %s '%s'\n" "$test" "$1"
    exit 1
  fi
  rm -f compression_result.txt "$fn_compressed"

  #
  # mktar-batch tests
  #

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

  test="compression with misordered positional arguments using"
  ../mktar-batch compression_target.txt --compress="$arg_compression" --name "$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: %s '%s'\n" "$test" "$1"
    exit 1
  fi
  rm -f compression_result.txt "$fn_compressed"

  test="compression using gpg and"
  ../mktar-batch --compress="$arg_compression" --encrypt="gpg" --passphrase test123 --name "$fn_compressed_gpg" compression_target.txt
  ../tarcat --passphrase test123 "$fn_compressed_gpg" > compression_result.txt
  if ! cmp compression_result.txt compression_target.txt >/dev/null 2>&1; then
    printf "Failure in compression tests: %s '%s'\n" "$test" "$1"
    exit 1
  fi
  rm -f compression_result.txt "$fn_compressed_gpg"

  test="compression using age and"
  ../mktar-batch --compress="$arg_compression" --encrypt="age" --passphrase test123 --name "$fn_compressed_age" compression_target.txt
  ../tarcat --passphrase test123 "$fn_compressed_age" > compression_result.txt
  if ! cmp compression_result.txt compression_target.txt >/dev/null 2>&1; then
    printf "Failure in compression tests: %s '%s'\n" "$test" "$1"
    exit 1
  fi
  rm -f compression_result.txt "$fn_compressed_age"
}

test_compression NONE
test_compression GZIP
test_compression XZ
test_compression ZSTD
test_compression BZIP2


D archives/test_untar.sh => archives/test_untar.sh +0 -48
@@ 1,48 0,0 @@
#!/bin/sh
# tests untar

# move to temp directory
cd test

test_decompression() {

  #set internal variables based on selected compression
  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

  # try decompression
  ../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

  # clean up
  rm -f decompression_result.txt
}

test_decompression NONE
test_decompression GZIP
test_decompression XZ
test_decompression ZSTD
test_decompression BZIP2