From 482da4b8338daeb1e209fa7a0a12fe73502f5cd9 Mon Sep 17 00:00:00 2001 From: redxef Date: Wed, 7 Jun 2023 13:21:53 +0200 Subject: [PATCH] Add tests. Fix single-file archiving. --- Makefile | 7 +++++-- tarback.sh | 36 ++++++++++++++++++++++++++++++------ test.sh | 22 ++++++++++++++++++++++ tests/common.sh | 10 ++++++++++ tests/test-000-simple.sh | 27 +++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 8 deletions(-) create mode 100755 test.sh create mode 100755 tests/common.sh create mode 100755 tests/test-000-simple.sh diff --git a/Makefile b/Makefile index 60a5369..37cf96d 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,11 @@ SCRIPT := tarback.sh SCRIPT_NAME := $(SCRIPT:.sh=) PREFIX ?= /usr/local -install: $(PLUGINS) +install: $(SCRIPT) $(PLUGINS) install -D -t $(PREFIX)/share/tarback -m 0644 -o root -g root $^ install -D -m 0755 -o root -g root -T $(SCRIPT) $(PREFIX)/bin/$(SCRIPT_NAME) -.PHONY: install +test: $(SCRIPT) $(PLUGINS) + ./test.sh + +.PHONY: install test diff --git a/tarback.sh b/tarback.sh index c101bd8..4caf9e4 100755 --- a/tarback.sh +++ b/tarback.sh @@ -8,9 +8,13 @@ XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share:/usr/share}" TARBACK_REMOTE_DEFAULT= TARBACK_TRANSFORM_REMOTE_COMMAND_DEFAULT= # shellcheck disable=SC2016 # disable variable not expanding hint -TARBACK_TAR_CREATE_COMMAND_DEFAULT='tar c -C "$(dirname "$1")" "$(basename "$1")"' +TARBACK_TAR_CREATE_COMMAND_DEFAULT='tar c -C "$1" .' # shellcheck disable=SC2016 # disable variable not expanding hint -TARBACK_TAR_EXTRACT_COMMAND_DEFAULT='tar x -C "$(dirname "$1")"' +TARBACK_TAR_CREATE_COMMAND_FILE_DEFAULT='tar c -C "$(dirname "$1")" "$(basename "$1")"' +# shellcheck disable=SC2016 # disable variable not expanding hint +TARBACK_TAR_EXTRACT_COMMAND_DEFAULT='tar x -C "$1"' +# shellcheck disable=SC2016 # disable variable not expanding hint +TARBACK_TAR_EXTRACT_COMMAND_FILE_DEFAULT='tar x -C "$(dirname "$1")"' TARBACK_COMPRESSION_DEFAULT='xz' TARBACK_SPLIT_DEFAULT="split -b $((128*1024*1024)) -" TARBACK_MERGE_DEFAULT='cat' @@ -23,7 +27,9 @@ TARBACK_REMOTE="${TARBACK_REMOTE:-$TARBACK_REMOTE_DEFAULT}" # remote command, see _transform_ssh_command for an example TARBACK_TRANSFORM_REMOTE_COMMAND="${TARBACK_TRANSFORM_REMOTE_COMMAND:-$TARBACK_TRANSFORM_REMOTE_COMMAND_DEFAULT}" TARBACK_TAR_CREATE_COMMAND="${TARBACK_TAR_CREATE_COMMAND:-$TARBACK_TAR_CREATE_COMMAND_DEFAULT}" +TARBACK_TAR_CREATE_COMMAND_FILE="${TARBACK_TAR_CREATE_COMMAND_FILE:-$TARBACK_TAR_CREATE_COMMAND_FILE_DEFAULT}" TARBACK_TAR_EXTRACT_COMMAND="${TARBACK_TAR_EXTRACT_COMMAND:-$TARBACK_TAR_EXTRACT_COMMAND_DEFAULT}" +TARBACK_TAR_EXTRACT_COMMAND_FILE="${TARBACK_TAR_EXTRACT_COMMAND_FILE:-$TARBACK_TAR_EXTRACT_COMMAND_FILE_DEFAULT}" TARBACK_COMPRESSION="${TARBACK_COMPRESSION:-$TARBACK_COMPRESSION_DEFAULT}" TARBACK_SPLIT="${TARBACK_SPLIT:-$TARBACK_SPLIT_DEFAULT}" TARBACK_MERGE="${TARBACK_MERGE:-$TARBACK_MERGE_DEFAULT}" @@ -58,26 +64,44 @@ _transform_remote() { echo "$r" } -create() { +_create() { # $1 ... src # $2 ... dst src="$1" dst="$(_transform_split_name "$2")" - tarback_tar_create_command="$(_transform_remote "$TARBACK_TAR_CREATE_COMMAND")" + if [ -d "$src" ]; then + tarback_tar_create_command="$TARBACK_TAR_CREATE_COMMAND" + else + tarback_tar_create_command="$TARBACK_TAR_CREATE_COMMAND_FILE" + fi + tarback_tar_create_command="$(_transform_remote "$tarback_tar_create_command")" $TARBACK_REMOTE sh -c "$tarback_tar_create_command" - "$src" \ | $TARBACK_COMPRESSION \ | $TARBACK_SPLIT "$dst" } -restore() { +create() { + _create "$@" +} + +_restore() { src="$(_transform_split_name "$1")" dst="$2" - tarback_tar_extract_command="$(_transform_remote "$TARBACK_TAR_EXTRACT_COMMAND")" + if [ ! -e "$dst" ] && [ -d "$(dirname "$dst")" ]; then + tarback_tar_extract_command="$TARBACK_TAR_EXTRACT_COMMAND_FILE" + else + tarback_tar_extract_command="$TARBACK_TAR_EXTRACT_COMMAND" + fi + tarback_tar_extract_command="$(_transform_remote "$tarback_tar_extract_command")" $TARBACK_MERGE "$src"* \ | $TARBACK_COMPRESSION --decompress --stdout \ | $TARBACK_REMOTE sh -c "$tarback_tar_extract_command" - "$dst" } +restore() { + _restore "$@" +} + short_options='p:' while getopts "$short_options" arg; do case "$arg" in diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..46d3750 --- /dev/null +++ b/test.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -eu + +failed=0 +for f in ./tests/test-*.sh; do + printf '%s' "Running test $f ... " + if "$f"; then + echo "succeeded" + else + failed=$((failed+1)) + echo "failed" + fi +done + +if [ $failed -ne 0 ]; then + echo "$failed tests failed" + exit 1 +else + echo "all succeeded" + exit 0 +fi diff --git a/tests/common.sh b/tests/common.sh new file mode 100755 index 0000000..a3af51e --- /dev/null +++ b/tests/common.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +create_directories() { + workdir="$(mktemp -d)" + mkdir -p "$workdir/source" + mkdir -p "$workdir/dest" + mkdir -p "$workdir/restore" + echo 'Hello World!' > "$workdir/source/hello_world.txt" + echo "$workdir" +} diff --git a/tests/test-000-simple.sh b/tests/test-000-simple.sh new file mode 100755 index 0000000..b9dafde --- /dev/null +++ b/tests/test-000-simple.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -eu + +. ./tests/common.sh + +# archive whole directory +workdir="$(create_directories)" +./tarback.sh create "$workdir/source" "$workdir/dest/source.tar.xz" +./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore" +diff "$workdir/source" "$workdir/restore" +rm -rf "$workdir" + +# archive single file +workdir="$(create_directories)" +./tarback.sh create "$workdir/source/hello_world.txt" "$workdir/dest/source.tar.xz" +./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/hello_world.txt" +diff "$workdir/source" "$workdir/restore" +rm -rf "$workdir" + +# split archive +workdir="$(create_directories)" +TARBACK_SPLIT="split -b 10 -" ./tarback.sh create "$workdir/source/hello_world.txt" "$workdir/dest/source.tar.xz" +TARBACK_SPLIT="split -b 10 -" ./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/hello_world.txt" +diff "$workdir/source" "$workdir/restore" +[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ] +rm -rf "$workdir"