From 573855ab6e5eeaee1e0c935bb6fcf78bf02e13f1 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Fri, 16 Sep 2022 22:56:35 -0500 Subject: [PATCH] Rewrote test suite for bats --- archives/Makefile | 8 +- archives/test/mktar-batch.bats | 132 +++++++++++++++++++++++++++++++++ archives/test/mktar.bats | 100 +++++++++++++++++++++++++ archives/test/tarcat.bats | 61 +++++++++++++++ archives/test/untar.bats | 69 +++++++++++++++++ archives/test_init.sh | 18 ----- archives/test_mktar_tarcat.sh | 115 ---------------------------- archives/test_untar.sh | 48 ------------ 8 files changed, 364 insertions(+), 187 deletions(-) create mode 100644 archives/test/mktar-batch.bats create mode 100644 archives/test/mktar.bats create mode 100644 archives/test/tarcat.bats create mode 100644 archives/test/untar.bats delete mode 100755 archives/test_init.sh delete mode 100755 archives/test_mktar_tarcat.sh delete mode 100755 archives/test_untar.sh diff --git a/archives/Makefile b/archives/Makefile index 54a3f6e..17ae5dc 100644 --- a/archives/Makefile +++ b/archives/Makefile @@ -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/ diff --git a/archives/test/mktar-batch.bats b/archives/test/mktar-batch.bats new file mode 100644 index 0000000..f1e25ba --- /dev/null +++ b/archives/test/mktar-batch.bats @@ -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 +} + diff --git a/archives/test/mktar.bats b/archives/test/mktar.bats new file mode 100644 index 0000000..5396452 --- /dev/null +++ b/archives/test/mktar.bats @@ -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 +} + diff --git a/archives/test/tarcat.bats b/archives/test/tarcat.bats new file mode 100644 index 0000000..175ba62 --- /dev/null +++ b/archives/test/tarcat.bats @@ -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" = "" ] +} + diff --git a/archives/test/untar.bats b/archives/test/untar.bats new file mode 100644 index 0000000..028badf --- /dev/null +++ b/archives/test/untar.bats @@ -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 +} + diff --git a/archives/test_init.sh b/archives/test_init.sh deleted file mode 100755 index 89d821d..0000000 --- a/archives/test_init.sh +++ /dev/null @@ -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 - diff --git a/archives/test_mktar_tarcat.sh b/archives/test_mktar_tarcat.sh deleted file mode 100755 index 76a4f07..0000000 --- a/archives/test_mktar_tarcat.sh +++ /dev/null @@ -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 - diff --git a/archives/test_untar.sh b/archives/test_untar.sh deleted file mode 100755 index b6135cf..0000000 --- a/archives/test_untar.sh +++ /dev/null @@ -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 - -- 2.45.2