#!/bin/bash ################################################################################ # test-specific-fixes.sh # Unit tests for specific issues that need to be fixed # # This file contains targeted tests for individual fixes, allowing us to # verify each fix works correctly using test-driven development. # # Usage: ${PROJECT_ROOT}/test-specific-fixes.sh [fix_name] ################################################################################ # 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" ################################################################################ # TEST: Fix NES internal script references ################################################################################ test_fix_nes_internal_references() { local nes_all_script="${PROJECT_ROOT}/builders/build-nes-all-1g1r.sh" # Test that internal script name matches filename if [[ -f "$nes_all_script" ]]; then local internal_name=$(grep -oP 'SCRIPT_NAME="\K[^"]+' "$nes_all_script" 2>/dev/null || echo "") assert_equals "build-nes-all-1g1r.sh" "$internal_name" "NES all script internal name should match filename" # Test that usage example matches filename local usage_example=$(grep -oP 'Usage: \./\K[^[:space:]]+' "$nes_all_script" 2>/dev/null || echo "") assert_equals "build-nes-all-1g1r.sh" "$usage_example" "NES all script usage example should match filename" else test_fail "NES all script should exist at $nes_all_script" fi } ################################################################################ # TEST: Fix FILTER variable mismatches ################################################################################ test_fix_filter_variables() { # Test specific scripts that were identified as having wrong FILTER values local -A expected_filters=( ["${PROJECT_ROOT}/builders/build-snes-eng-1g1r.sh"]="eng.1g1r" ["${PROJECT_ROOT}/builders/build-snes-all-1g1r.sh"]="all.1g1r" ["${PROJECT_ROOT}/builders/build-atari7800-all-1g1r.sh"]="all.1g1r" ["${PROJECT_ROOT}/builders/build-atari5200-all-1g1r.sh"]="all.1g1r" ["${PROJECT_ROOT}/builders/build-gamegear-all-1g1r.sh"]="all.1g1r" ["${PROJECT_ROOT}/builders/build-gamegear-eng-1g1r.sh"]="eng.1g1r" ["${PROJECT_ROOT}/builders/build-sega32x-all-1g1r.sh"]="all.1g1r" ["${PROJECT_ROOT}/builders/build-sega32x-eng-1g1r.sh"]="eng.1g1r" ["${PROJECT_ROOT}/builders/build-nds-all-1g1r.sh"]="all.1g1r" ["${PROJECT_ROOT}/builders/build-nds-eng-1g1r.sh"]="eng.1g1r" ) for script_path in "${!expected_filters[@]}"; do if [[ -f "$script_path" ]]; then local actual_filter=$(grep -oP 'FILTER="\K[^"]+' "$script_path" 2>/dev/null || echo "") local expected_filter="${expected_filters[$script_path]}" local script_name=$(basename "$script_path") assert_equals "$expected_filter" "$actual_filter" "Script $script_name should have FILTER=$expected_filter" else test_fail "Script should exist: $script_path" fi done } ################################################################################ # TEST: Create missing NES-ENG variant ################################################################################ test_create_nes_eng_variant() { local nes_eng_script="${PROJECT_ROOT}/builders/build-nes-eng-1g1r.sh" # Test that the script exists assert_file_exists "$nes_eng_script" "NES-ENG variant script should exist" if [[ -f "$nes_eng_script" ]]; then # Test that it's executable assert_file_executable "$nes_eng_script" "NES-ENG script should be executable" # Test that it has correct FILTER value local filter_value=$(grep -oP 'FILTER="\K[^"]+' "$nes_eng_script" 2>/dev/null || echo "") assert_equals "eng.1g1r" "$filter_value" "NES-ENG script should have FILTER=eng.1g1r" # Test that internal script name is correct local internal_name=$(grep -oP 'SCRIPT_NAME="\K[^"]+' "$nes_eng_script" 2>/dev/null || echo "") assert_equals "build-nes-eng-1g1r.sh" "$internal_name" "NES-ENG script internal name should be correct" # Test that usage example is correct local usage_example=$(grep -oP 'Usage: \./\K[^[:space:]]+' "$nes_eng_script" 2>/dev/null || echo "") assert_equals "build-nes-eng-1g1r.sh" "$usage_example" "NES-ENG script usage example should be correct" # Test that it has valid bash syntax bash -n "$nes_eng_script" assert_equals 0 $? "NES-ENG script should have valid bash syntax" fi } ################################################################################ # TEST: Comprehensive post-fix validation ################################################################################ test_all_fixes_applied() { # Re-run the original tests that were failing local build_scripts=($(find "${ROMGROOMERDIR}" -name "build-*-1g1r.sh" -type f | sort)) # Test 1: No internal reference mismatches local mismatched_refs=0 for script in "${build_scripts[@]}"; do local filename=$(basename "$script") local internal_name=$(grep -oP 'SCRIPT_NAME="\K[^"]+' "$script" 2>/dev/null || echo "") if [[ -n "$internal_name" && "$internal_name" != "$filename" ]]; then ((mismatched_refs++)) fi done assert_equals 0 $mismatched_refs "No scripts should have mismatched internal references" # Test 2: No usage example mismatches local mismatched_usage=0 for script in "${build_scripts[@]}"; do local filename=$(basename "$script") local usage_example=$(grep -oP 'Usage: \./\K[^[:space:]]+' "$script" 2>/dev/null || echo "") if [[ -n "$usage_example" && "$usage_example" != "$filename" ]]; then ((mismatched_usage++)) fi done assert_equals 0 $mismatched_usage "No scripts should have mismatched usage examples" # Test 3: All FILTER variables should match expected pattern local invalid_filters=0 for script in "${build_scripts[@]}"; do local filename=$(basename "$script") local variant=$(echo "$filename" | sed -n 's/build-[^-]*-\([^-]*\)-1g1r\.sh/\1/p') local expected_filter="${variant}.1g1r" local actual_filter=$(grep -oP 'FILTER="\K[^"]+' "$script" 2>/dev/null || echo "") if [[ -n "$actual_filter" && "$actual_filter" != "$expected_filter" ]]; then ((invalid_filters++)) fi done assert_equals 0 $invalid_filters "All FILTER variables should match the expected pattern" # Test 4: NES-ENG variant should exist assert_file_exists "${PROJECT_ROOT}/builders/build-nes-eng-1g1r.sh" "NES-ENG variant should exist after fix" } ################################################################################ # Main test execution ################################################################################ main() { if [[ $# -eq 1 ]]; then # Run specific test case "$1" in "nes-refs") init_test_framework run_test "test_fix_nes_internal_references" cleanup_test_framework ;; "filter-vars") init_test_framework run_test "test_fix_filter_variables" cleanup_test_framework ;; "nes-eng") init_test_framework run_test "test_create_nes_eng_variant" cleanup_test_framework ;; "all-fixes") init_test_framework run_test "test_all_fixes_applied" cleanup_test_framework ;; *) echo "Unknown test: $1" echo "Available tests: nes-refs, filter-vars, nes-eng, all-fixes" exit 1 ;; esac else # Run all tests using the framework init_test_framework run_test "test_fix_nes_internal_references" run_test "test_fix_filter_variables" run_test "test_create_nes_eng_variant" run_test "test_all_fixes_applied" cleanup_test_framework fi print_test_summary } # Run main function with all arguments main "$@"