#!/bin/bash #################} # Test create_directory function#################################################### # test-utils.sh # Unit tests for utils.sh utility functions ################################################################################ # Get the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Source the test framework source "${SCRIPT_DIR}/test-framework.sh" # Source the script utilities we need for testing source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" source "${PROJECT_ROOT}/core/utils.sh" # Override setup for our tests setup_test() { # Create test directories and files within the test temp directory # (which is now safely within EMUBASEDIR) TEST_SOURCE_DIR="$TEST_TEMP_DIR/source" TEST_DEST_DIR="$TEST_TEMP_DIR/dest" TEST_FILE="$TEST_TEMP_DIR/testfile.txt" create_test_dir "$TEST_SOURCE_DIR" create_test_file "$TEST_SOURCE_DIR/file1.txt" "content1" create_test_file "$TEST_SOURCE_DIR/file2.txt" "content2" create_test_file "$TEST_FILE" "test content" } # Override teardown for our tests teardown_test() { # Restore original EMUBASEDIR if [ -n "$ORIGINAL_EMUBASEDIR" ]; then EMUBASEDIR="$ORIGINAL_EMUBASEDIR" fi } # Test validate_path_within_emubasedir function test_validate_path_within_emubasedir_valid() { # Test with a path within EMUBASEDIR local test_path="${EMUBASEDIR}/test/valid/path" mkdir -p "$test_path" # This should succeed (return 0) assert_exit_code "0" "validate_path_within_emubasedir '$test_path'" # Cleanup rm -rf "${EMUBASEDIR}/test" } test_validate_path_within_emubasedir_invalid() { # Test with a path outside EMUBASEDIR (use root tmp which is definitely outside) local test_path="/tmp/outside/path" # This should fail (return 1) assert_exit_code "1" "validate_path_within_emubasedir '$test_path'" } # Test create_directory function test_create_directory_new() { local new_dir="$TEST_TEMP_DIR/new_directory" # Directory shouldn't exist initially assert_file_not_exists "$new_dir" "Directory should not exist initially" # Create the directory create_directory "$new_dir" # Add a brief pause to ensure directory creation is complete sleep 0.1 # Directory should now exist assert_dir_exists "$new_dir" "Directory should be created" assert_true "[ -d '$new_dir' ]" "Created path should be a directory" } test_create_directory_existing() { local existing_dir="$TEST_TEMP_DIR/existing_directory" mkdir -p "$existing_dir" # This should not fail even if directory exists assert_exit_code "0" "create_directory '$existing_dir'" assert_dir_exists "$existing_dir" "Existing directory should still exist" } # Test move_files function test_move_files_basic() { local source_file="$TEST_SOURCE_DIR/moveme.txt" local dest_dir="$TEST_TEMP_DIR/move_dest" create_test_file "$source_file" "move content" create_test_dir "$dest_dir" # File should exist in source assert_file_exists "$source_file" "Source file should exist" # Move the file move_files "$source_file" "$dest_dir" # File should now be in destination assert_file_not_exists "$source_file" "Source file should be moved" assert_file_exists "$dest_dir/moveme.txt" "File should exist in destination" } # Test copy_files function (the main one used in workflow) test_copy_files_basic() { # Use the setup source directory and create destination create_test_dir "$TEST_DEST_DIR" # Verify source files exist assert_file_exists "$TEST_SOURCE_DIR/file1.txt" "Source file1 should exist" assert_file_exists "$TEST_SOURCE_DIR/file2.txt" "Source file2 should exist" # Copy files copy_files "$TEST_SOURCE_DIR" "$TEST_DEST_DIR" # Verify destination files exist assert_file_exists "$TEST_DEST_DIR/file1.txt" "Destination file1 should exist" assert_file_exists "$TEST_DEST_DIR/file2.txt" "Destination file2 should exist" # Verify content is preserved local content1=$(cat "$TEST_DEST_DIR/file1.txt") assert_equals "content1" "$content1" "File1 content should be preserved" } test_copy_files_with_delete() { create_test_dir "$TEST_DEST_DIR" create_test_file "$TEST_DEST_DIR/existing.txt" "existing content" # Store original DELETE flag and set to true local original_delete="${DELETE:-false}" DELETE=true # Copy files (this should delete existing files first) copy_files "$TEST_SOURCE_DIR" "$TEST_DEST_DIR" # Verify source files were copied assert_file_exists "$TEST_DEST_DIR/file1.txt" "Source files should be copied" # The copy_files function with DELETE=true removes the entire destination contents # and then copies source files, so existing.txt should be gone assert_file_not_exists "$TEST_DEST_DIR/existing.txt" "Existing file should be deleted when DELETE=true" # Restore original DELETE flag DELETE="$original_delete" } # Test validate_parameters function test_validate_parameters_valid() { # All required parameters provided assert_exit_code "0" "validate_parameters 'nes' 'usa.1g1r' 'myrient.erista.me' 'Nintendo - NES'" } test_validate_parameters_missing() { # Missing parameter should fail assert_exit_code "1" "validate_parameters '' 'usa.1g1r' 'myrient.erista.me' 'Nintendo - NES'" assert_exit_code "1" "validate_parameters 'nes' '' 'myrient.erista.me' 'Nintendo - NES'" } # Test urlencode/urldecode functions (they use different encoding methods) test_urlencode_basic() { local input="hello world" local encoded=$(urlencode "$input") local expected="hello+world" # curl's --data-urlencode uses + for spaces assert_equals "$expected" "$encoded" "URL encoding should replace spaces with +" } test_urldecode_basic() { local input="hello%20world" local decoded=$(urldecode "$input") local expected="hello world" assert_equals "$expected" "$decoded" "URL decoding should replace %20 with spaces" } # Test compatibility - encode with one method, decode with another requires conversion test_url_encode_decode_compatibility() { local original="Hello World" local encoded=$(urlencode "$original") # produces "Hello+World" local percent_encoded="${encoded//+/%20}" # convert + to %20 for urldecode local decoded=$(urldecode "$percent_encoded") assert_equals "$original" "$decoded" "Encoding/decoding should work with format conversion" } # Main test runner main() { echo "Running utils.sh tests..." run_tests } # Run tests if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi