#!/bin/bash ################################################################################ # test-build-script-content.sh # Unit tests for build script content standards and completeness # # Validates that all build scripts have proper content structure: # - Complete headers with all required sections # - Required function implementations # - Proper dependency declarations # - Correct source configurations # # Usage: ${PROJECT_ROOT}/test-build-script-content.sh ################################################################################ # 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: Verify all scripts have complete headers test_build_scripts_have_complete_headers() { local incomplete_headers=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check for required header sections local missing_sections=() if ! grep -q "^# Script:" "$script"; then missing_sections+=("Script") fi if ! grep -q "^# Description:" "$script"; then missing_sections+=("Description") fi if ! grep -q "^# Usage:" "$script"; then missing_sections+=("Usage") fi if ! grep -q "^# Dependencies:" "$script"; then missing_sections+=("Dependencies") fi if ! grep -q "^# Example:" "$script"; then missing_sections+=("Example") fi if [ ${#missing_sections[@]} -gt 0 ]; then incomplete_headers+=("$basename_script (missing: ${missing_sections[*]})") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#incomplete_headers[@]}" "All build scripts should have complete headers. Incomplete: ${incomplete_headers[*]}" assert_greater_than "0" "$script_count" "Should find build scripts to validate" } # Test: Verify all scripts have required functions test_build_scripts_have_required_functions() { local missing_functions=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check for required functions local missing_funcs=() if ! grep -q "^set_environment()" "$script"; then missing_funcs+=("set_environment") fi if ! grep -q "^run_processing()" "$script"; then missing_funcs+=("run_processing") fi if ! grep -q "^display_summary()" "$script"; then missing_funcs+=("display_summary") fi if ! grep -q "^main()" "$script"; then missing_funcs+=("main") fi if [ ${#missing_funcs[@]} -gt 0 ]; then missing_functions+=("$basename_script (missing: ${missing_funcs[*]})") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#missing_functions[@]}" "All build scripts should have required functions. Missing: ${missing_functions[*]}" } # Test: Verify dependency declarations are accurate test_dependency_declarations_accurate() { local inaccurate_deps=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check that all sourced files are declared as dependencies local sourced_files local declared_deps sourced_files=$(grep "^source " "$script" | sed 's/source.*\/\([^/]*\)".*/\1/' | sort) declared_deps=$(grep -A 10 "^# Dependencies:" "$script" | grep "^# -" | sed 's/^# - //' | sort) # Basic check - if we source config.sh, logger.sh, etc., they should be declared local expected_deps=("nointro-processor.sh" "config.sh" "logger.sh") # Check for config-arrays.sh if eng variant if [[ "$basename_script" == *"-eng-"* ]]; then expected_deps+=("config-arrays.sh") fi for dep in "${expected_deps[@]}"; do if ! echo "$declared_deps" | grep -q "$dep"; then inaccurate_deps+=("$basename_script (missing dep: $dep)") fi done done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#inaccurate_deps[@]}" "All sourced dependencies should be declared. Inaccurate: ${inaccurate_deps[*]}" } # Test: Verify source configurations are appropriate test_source_configurations_appropriate() { local inappropriate_sources=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Extract system name local system system=$(echo "$basename_script" | sed 's/build-\([^-]*\)-.*/\1/') # Get SOURCESITE from script local sourcesite sourcesite=$(grep 'SOURCESITE=' "$script" | head -1 | sed 's/.*SOURCESITE="\([^"]*\)".*/\1/') # Check source site appropriateness case "$system" in # Disc-based systems should use Redump "3do"|"dreamcast"|"saturn"|"psx"|"segacd"|"pcenginecd") if [[ "$sourcesite" != *"Redump"* ]]; then inappropriate_sources+=("$basename_script (disc system should use Redump, got: $sourcesite)") fi ;; # Cartridge systems should use No-Intro "atari"*|"nes"|"snes"|"gb"*|"gba"|"nds"|"n64"|"gamegear"|"mastersystem"|"megadrive"|"sg1000"|"colecovision"|"vectrex"|"lynx"|"jaguar"|"ngp"|"ngpc") if [[ "$sourcesite" != *"No-Intro"* ]]; then inappropriate_sources+=("$basename_script (cartridge system should use No-Intro, got: $sourcesite)") fi ;; # Modern systems might use extra "3ds"|"psp"|"pspminis") # These can use various sources, so we're more lenient if [[ "$sourcesite" != *"No-Intro"* && "$sourcesite" != *"Redump"* && "$sourcesite" != *"extra"* ]]; then inappropriate_sources+=("$basename_script (modern system should use known source, got: $sourcesite)") fi ;; esac done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#inappropriate_sources[@]}" "Source configurations should be appropriate for system type. Inappropriate: ${inappropriate_sources[*]}" } # Test: Verify eng variants have language overrides test_eng_variants_have_language_overrides() { local missing_overrides=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Only check eng variants if [[ "$basename_script" == *"-eng-"* ]]; then if ! grep -q "LANGUAGE_ARRAY_OVERRIDE" "$script"; then missing_overrides+=("$basename_script") fi fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-eng-1g1r.sh" -print0) assert_equals "0" "${#missing_overrides[@]}" "All eng variants should have LANGUAGE_ARRAY_OVERRIDE. Missing: ${missing_overrides[*]}" } # Test: Verify proper umask settings test_proper_umask_settings() { local missing_umask=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") if ! grep -q "umask 000" "$script"; then missing_umask+=("$basename_script") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#missing_umask[@]}" "All build scripts should set umask 000. Missing: ${missing_umask[*]}" } # Test: Verify consistent error handling patterns test_consistent_error_handling() { local inconsistent_error_handling=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check for error handling in run_processing function (expand search scope) if ! grep -A 20 "^run_processing()" "$script" | grep -q "exit 1"; then inconsistent_error_handling+=("$basename_script (no exit 1 in run_processing)") fi # Check for proper exit code checking (accept either $? -ne 0 or $exit_code -ne 0 patterns) if ! grep -A 20 "^run_processing()" "$script" | grep -qE '(\$\? -ne 0|\$exit_code -ne 0)'; then inconsistent_error_handling+=("$basename_script (no \$? check in run_processing)") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#inconsistent_error_handling[@]}" "All build scripts should have consistent error handling. Inconsistent: ${inconsistent_error_handling[*]}" } # Main function to run all tests main() { init_test_framework echo "===============================================" echo " BUILD SCRIPT CONTENT VALIDATION TEST SUITE" echo "===============================================" echo # Run all content validation tests run_test "test_build_scripts_have_complete_headers" run_test "test_build_scripts_have_required_functions" run_test "test_dependency_declarations_accurate" run_test "test_source_configurations_appropriate" run_test "test_eng_variants_have_language_overrides" run_test "test_proper_umask_settings" run_test "test_consistent_error_handling" # Print final results print_test_summary # Exit with appropriate code if [ $TESTS_FAILED -gt 0 ]; then exit 1 else exit 0 fi } # Run main function if script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi