Skip to content

Commit

Permalink
Enhance archive/unarchive capabilities
Browse files Browse the repository at this point in the history
- Complement unarchive function
- Add implementation for GNU Zip
  • Loading branch information
JonatanAntoni authored Feb 1, 2023
1 parent d02d5b5 commit 27feb09
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gen-pack
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

GEN_PACK_LIB_VERSION="0.6.2"
GEN_PACK_LIB_VERSION="0.7.0"
GEN_PACK_LIB_SOURCE="$(realpath $(dirname ${BASH_SOURCE}))"

shopt -s expand_aliases
Expand Down
39 changes: 35 additions & 4 deletions lib/utilities
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function find_zip {
elif type -a zip 1>/dev/null 2>/dev/null; then
UTILITY_ZIP_TYPE="zip"
UTILITY_ZIP="$(which zip)"
UTILITY_UNZIP="$(which unzip)"
report_utility "Zip" "$UTILITY_ZIP"
return 0
fi
Expand Down Expand Up @@ -221,23 +222,53 @@ function find_doxygen {
}

function archive_7zip {
rm -f "$2"
echo_v "pushd \"$1\"; \"${UTILITY_ZIP}\" a -tzip \"$2\" ."
pushd "$1" 2>&1 >/dev/null
"${UTILITY_ZIP}" a -tzip "$2" .
popd 2>&1 >/dev/null
}

function archive_zip {
echo_err "zip not implemented!"
exit 1
"${UTILITY_ZIP}"
echo_v "pushd \"$1\"; \"${UTILITY_ZIP}\" -r \"$2\" ."
pushd "$1" 2>&1 >/dev/null
"${UTILITY_ZIP}" -r "$2" .
popd 2>&1 >/dev/null
}

#
# Usage: archive <folder> <archive>
# <folder> The source folder to be archived recusrively.
# <archive> The target file name for the archive.
# If the file already exists it is overwritten!
function archive {
echo_v "rm -rf \"$2\""
rm -f "$2"
archive_$UTILITY_ZIP_TYPE "$1" "$2"
}

function unarchive_7zip {
echo_v "\"${UTILITY_ZIP}\" x \"$1\" -o\"$2\" -y"
"${UTILITY_ZIP}" x "$1" -o"$2" -y
}

function unarchive_zip {
echo_v "\"${UTILITY_UNZIP}\" \"$1\" -d \"$2\""
"${UTILITY_UNZIP}" "$1" -d "$2"
}

#
# Usage: unarchive <archive> <folder>
# <archive> The source file name of the archive to extract.
# <folder> The target folder to extract the archive to.
# If the folder already exists it is purged!
function unarchive {
echo_v "rm -rf \"$2\""
rm -rf "$2"
echo_v "mkdir -p \"$2\""
mkdir -p "$2"
unarchive_$UTILITY_ZIP_TYPE "$1" "$2"
}

#
# Usage: check_links [--timeout <sec>] <index> <src>
# <index> Index.html file to start link scanning at."
Expand Down
103 changes: 103 additions & 0 deletions test/tests_utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ EOF
test_find_zip_gnuzip_env() {
remove_from_path "7z"
remove_from_path "zip"
remove_from_path "unzip"
PROGRAMFILES=""

cat > "zip" <<EOF
Expand All @@ -202,9 +203,16 @@ echo "zip \$*"
EOF
chmod +x "zip"

cat > "unzip" <<EOF
#!/bin/sh
echo "unzip \$*"
EOF
chmod +x "unzip"

find_zip

assertEquals "$(pwd)/zip" "${UTILITY_ZIP}"
assertEquals "$(pwd)/unzip" "${UTILITY_UNZIP}"
assertEquals "zip" "${UTILITY_ZIP_TYPE}"
}

Expand All @@ -226,6 +234,101 @@ EOF
assertContains "$output" "7z a -tzip $(pwd)/output/test.zip"
}

test_unarchive_7zip() {
UTILITY_ZIP_TYPE="7zip"
UTILITY_ZIP="$(pwd)/7z"

cat > "7z" <<EOF
#!/bin/sh
echo "7z \$*"
EOF
chmod +x "7z"

output=$(unarchive "$(pwd)/input/test.zip" "$(pwd)/output" 2>&1)

assertContains "$output" "$(pwd)/output"
assertContains "$output" "7z x $(pwd)/input/test.zip"
}

test_archive_gnuzip() {
UTILITY_ZIP_TYPE="zip"
UTILITY_ZIP="$(pwd)/zip"

cat > "zip" <<EOF
#!/bin/sh
echo "zip \$*"
EOF
chmod +x "zip"

mkdir "input"

output=$(archive "$(pwd)/input" "$(pwd)/output/test.zip" 2>&1)

assertContains "$output" "$(pwd)/input"
assertContains "$output" "zip -r $(pwd)/output/test.zip"
}

test_unarchive_gnuzip() {
UTILITY_ZIP_TYPE="zip"
UTILITY_UNZIP="$(pwd)/unzip"

cat > "unzip" <<EOF
#!/bin/sh
echo "unzip \$*"
EOF
chmod +x "unzip"

output=$(unarchive "$(pwd)/input/test.zip" "$(pwd)/output" 2>&1)

assertContains "$output" "$(pwd)/output"
assertContains "$output" "unzip $(pwd)/input/test.zip"
}

test_integ_archive_7zip() {
remove_from_path "zip"

if $(find_zip 2>/dev/null); then
find_zip

mkdir -p input
cat > "input/file.txt" <<EOF
Some test content for archive.
EOF

archive "$(pwd)/input" "$(pwd)/archive.zip"
assertTrue "test -f archive.zip"

unarchive "$(pwd)/archive.zip" "$(pwd)/output"
assertTrue "test -f output/file.txt"
assertTrue "diff input/file.txt output/file.txt"
else
echo "7zip not available, skip integration test."
fi
}

test_integ_archive_gnuzip() {
PROGRAMFILES=""
remove_from_path "7z"

if $(find_zip 2>/dev/null); then
find_zip

mkdir -p input
cat > "input/file.txt" <<EOF
Some test content for archive.
EOF

archive "$(pwd)/input" "$(pwd)/archive.zip"
assertTrue "test -f archive.zip"

unarchive "$(pwd)/archive.zip" "$(pwd)/output"
assertTrue "test -f output/file.txt"
assertTrue "diff input/file.txt output/file.txt"
else
echo "zip not available, skip integration test."
fi
}

linkchecker_mock() {
LINKCHECKER_MOCK_ARGS=($*)
}
Expand Down

0 comments on commit 27feb09

Please sign in to comment.