#!/bin/bash ################################################################################ # test-unzipfiles.sh # Unit tests for the unzipfiles.sh utility script # # Tests extraction, dry-run, delete, and recursive options for various archive types. # # Usage: ${PROJECT_ROOT}/test-unzipfiles.sh ################################################################################ SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Source the test framework and utilities source "${SCRIPT_DIR}/test-framework.sh" source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" UNZIPFILES_SH="${PROJECT_ROOT}/organizers/unzipfiles.sh" ################################################################################ # TEST: Script exists and is executable ################################################################################ test_unzipfiles_exists() { assert_file_exists "$UNZIPFILES_SH" "unzipfiles.sh should exist" assert_file_executable "$UNZIPFILES_SH" "unzipfiles.sh should be executable" } ################################################################################ # TEST: Dry-run extraction of multiple archive types ################################################################################ test_unzipfiles_dry_run() { local test_dir="$TEST_TEMP_DIR/archives" create_test_dir "$test_dir" # Create test files and archives echo "foo" > "$test_dir/foo.txt" echo "bar" > "$test_dir/bar.txt" (cd "$test_dir" && zip foo.zip foo.txt >/dev/null) (cd "$test_dir" && 7z a bar.7z bar.txt >/dev/null) (cd "$test_dir" && tar -cf baz.tar foo.txt bar.txt) (cd "$test_dir" && gzip -c foo.txt > foo.txt.gz) # Run dry-run local output output=$("$UNZIPFILES_SH" "$test_dir" --dry-run 2>&1) assert_contains "$output" "[EXTRACT] foo.zip" "Dry-run should list foo.zip" assert_contains "$output" "[EXTRACT] bar.7z" "Dry-run should list bar.7z" assert_contains "$output" "[EXTRACT] baz.tar" "Dry-run should list baz.tar" assert_contains "$output" "[EXTRACT] foo.txt.gz" "Dry-run should list foo.txt.gz" assert_contains "$output" "Would extract: foo.zip" "Dry-run should preview extraction" assert_contains "$output" "Summary:" "Dry-run should print summary" } ################################################################################ # TEST: Real extraction and file presence ################################################################################ test_unzipfiles_extract() { local test_dir="$TEST_TEMP_DIR/archives2" create_test_dir "$test_dir" echo "abc" > "$test_dir/a.txt" echo "def" > "$test_dir/b.txt" (cd "$test_dir" && zip ab.zip a.txt >/dev/null) (cd "$test_dir" && tar -cf ab.tar a.txt b.txt) rm "$test_dir/a.txt" "$test_dir/b.txt" "$UNZIPFILES_SH" "$test_dir" >/dev/null assert_file_exists "$test_dir/a.txt" "a.txt should be extracted from ab.zip or ab.tar" assert_file_exists "$test_dir/b.txt" "b.txt should be extracted from ab.tar" } ################################################################################ # TEST: Delete original archive after extraction ################################################################################ test_unzipfiles_delete() { local test_dir="$TEST_TEMP_DIR/archives3" create_test_dir "$test_dir" echo "xyz" > "$test_dir/x.txt" (cd "$test_dir" && zip x.zip x.txt >/dev/null) rm "$test_dir/x.txt" "$UNZIPFILES_SH" "$test_dir" --delete >/dev/null assert_file_exists "$test_dir/x.txt" "x.txt should be extracted from x.zip" if [ -f "$test_dir/x.zip" ]; then test_fail "x.zip should be deleted after extraction with --delete" else test_pass "x.zip deleted after extraction with --delete" fi } ################################################################################ # TEST: Recursive extraction ################################################################################ test_unzipfiles_recursive() { local test_dir="$TEST_TEMP_DIR/rec" local sub_dir="$test_dir/sub" create_test_dir "$sub_dir" echo "subfile" > "$sub_dir/s.txt" (cd "$sub_dir" && zip s.zip s.txt >/dev/null) rm "$sub_dir/s.txt" "$UNZIPFILES_SH" "$test_dir" --recursive >/dev/null assert_file_exists "$sub_dir/s.txt" "s.txt should be extracted from s.zip in subdirectory" } run_tests