From 6f5b76b4de097ed7a56cc152a141b9236994e332 Mon Sep 17 00:00:00 2001 From: Dries Schaumont <5946712+DriesSchaumont@users.noreply.github.com> Date: Thu, 2 May 2024 09:00:43 +0200 Subject: [PATCH] BUG: fix untar regex not handling a single nested directory. (#7) --- src/io/untar/script.sh | 2 +- src/io/untar/test.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/io/untar/script.sh b/src/io/untar/script.sh index 37e5012..42da3d1 100644 --- a/src/io/untar/script.sh +++ b/src/io/untar/script.sh @@ -17,7 +17,7 @@ tar -taf "${par_input}" > "$TMPDIR/tar_contents.txt" cat "$TMPDIR/tar_contents.txt" printf "Checking if tarball contains only a single top-level directory: " -if [[ $(grep -o -E "^.*?\\/" "$TMPDIR/tar_contents.txt" | uniq | wc -l) -eq 1 ]]; then +if [[ $(grep -o -E '^[./]*[^/]+/$' "$TMPDIR/tar_contents.txt" | uniq | wc -l) -eq 1 ]]; then echo "It does." echo "Extracting the contents of the top-level directory to the output directory instead of the directory itself." # The directory can be both of the format './' (or ././) or just diff --git a/src/io/untar/test.sh b/src/io/untar/test.sh index 87a843d..c49a576 100644 --- a/src/io/untar/test.sh +++ b/src/io/untar/test.sh @@ -85,4 +85,42 @@ echo "Extracting '$TMPDIR/input_test_4.tar.gz' to '$OUTPUT_DIR_4'". echo ">>> Check whether extracted file exists" [[ ! -f "$OUTPUT_DIR_4/test_file.txt" ]] && echo "Output file could not be found!" && exit 1 +echo ">>> Creating test tarball containing only 1 top-level directory, but it is nested." +mkdir -p "$TMPDIR/input_test_5/nested/" +cp "$INPUT_FILE" "$TMPDIR/input_test_5/nested/" +tar -C "$TMPDIR" -czvf "$TMPDIR/input_test_5.tar.gz" $(basename "$TMPDIR/input_test_5") +TARFILE_5="$TMPDIR/input_test_5.tar.gz" + +echo ">>> Creating temporary output directory for test 5." +OUTPUT_DIR_5="$TMPDIR/output_test_5/" +mkdir "$OUTPUT_DIR_5" + +echo "Extracting '$TARFILE_5' to '$OUTPUT_DIR_5'". +./$meta_functionality_name \ + --input "$TARFILE_5" \ + --output "$OUTPUT_DIR_5" + +echo ">>> Check whether extracted file exists" +[[ ! -f "$OUTPUT_DIR_5/nested/test_file.txt" ]] && echo "Output file could not be found!" && exit 1 + +echo ">>> Creating test tarball containing two top-level directories." +mkdir -p "$TMPDIR/input_test_6/number_1/" +mkdir "$TMPDIR/input_test_6/number_2/" +cp "$INPUT_FILE" "$TMPDIR/input_test_6/number_1/" +tar -C "$TMPDIR" -czvf "$TMPDIR/input_test_6.tar.gz" $(basename "$TMPDIR/input_test_6") +TARFILE_6="$TMPDIR/input_test_6.tar.gz" + +echo ">>> Creating temporary output directory for test 6." +OUTPUT_DIR_6="$TMPDIR/output_test_6/" +mkdir "$OUTPUT_DIR_6" + +echo "Extracting '$TARFILE_6' to '$OUTPUT_DIR_6'". +./$meta_functionality_name \ + --input "$TARFILE_6" \ + --output "$OUTPUT_DIR_6" + +echo ">>> Check whether extracted file exists" +[[ ! -f "$OUTPUT_DIR_6/number_1/test_file.txt" ]] && echo "Output file could not be found!" && exit 1 +[[ ! -d "$OUTPUT_DIR_6/number_2" ]] && echo "Output directory could not be found!" && exit 1 + echo ">>> Test finished successfully"