#!/bin/bash ################################################################################ # test-build-script-naming.sh # Unit tests for build script naming standards and conventions # # Ensures all build scripts follow the standardized naming pattern: # build-SYSTEM-VARIANT-1g1r.sh # # Usage: ${PROJECT_ROOT}/test-build-script-naming.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 no scripts use old dot notation (.1g1r.sh) test_no_dot_notation_scripts() { local dot_scripts dot_scripts=$(find "${PROJECT_ROOT}/builders" -name "build-*.1g1r.sh" 2>/dev/null | wc -l) assert_equals "0" "$dot_scripts" "No build scripts should use dot notation (.1g1r.sh)" } # Test: Verify all build scripts use hyphen notation (-1g1r.sh) test_all_scripts_use_hyphen_notation() { local hyphen_scripts local total_build_scripts hyphen_scripts=$(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" 2>/dev/null | wc -l) total_build_scripts=$(find "${PROJECT_ROOT}/builders" -name "build-*.sh" | grep -E "1g1r\.sh$" | wc -l) assert_equals "$hyphen_scripts" "$total_build_scripts" "All build scripts should use hyphen notation (-1g1r.sh)" assert_greater_than "0" "$hyphen_scripts" "Should have at least some build scripts" } # Test: Verify build script naming pattern consistency test_build_script_naming_pattern() { local invalid_scripts=() local script_count=0 # Find all build scripts and validate naming pattern while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Valid pattern: build-SYSTEM-VARIANT-1g1r.sh # Where VARIANT is: all, eng, usa if [[ ! "$basename_script" =~ ^build-[a-z0-9]+-(all|eng|usa)-1g1r\.sh$ ]]; then invalid_scripts+=("$basename_script") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#invalid_scripts[@]}" "All build scripts should follow naming pattern: build-SYSTEM-VARIANT-1g1r.sh. Invalid scripts: ${invalid_scripts[*]}" assert_greater_than "0" "$script_count" "Should find at least some build scripts to validate" } # Test: Verify internal script name references match filename test_internal_script_references_match_filename() { local mismatched_scripts=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check if internal "# Script:" reference matches filename local internal_name internal_name=$(grep "^# Script:" "$script" | head -1 | sed 's/^# Script: //') if [[ "$internal_name" != "$basename_script" ]]; then mismatched_scripts+=("$basename_script (internal: $internal_name)") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#mismatched_scripts[@]}" "Internal script name references should match filename. Mismatched: ${mismatched_scripts[*]}" } # Test: Verify internal usage examples match filename test_internal_usage_examples_match_filename() { local mismatched_scripts=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check if internal "# Usage:" reference matches filename local usage_name usage_name=$(grep "^# Usage:" "$script" | head -1 | sed 's/^# Usage: \.\/\?//') if [[ "$usage_name" != "$basename_script" ]]; then mismatched_scripts+=("$basename_script (usage: $usage_name)") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#mismatched_scripts[@]}" "Internal usage examples should match filename. Mismatched: ${mismatched_scripts[*]}" } # Test: Verify FILTER variables use correct dot notation test_filter_variables_use_dot_notation() { local invalid_filters=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Extract variant from filename (all, eng, usa) local variant variant=$(echo "$basename_script" | sed -n 's/build-.*-\(all\|eng\|usa\)-1g1r\.sh/\1/p') # Check if FILTER variable matches expected pattern local filter_value filter_value=$(grep 'FILTER=' "$script" | head -1 | sed 's/.*FILTER="\([^"]*\)".*/\1/') local expected_filter="${variant}.1g1r" if [[ "$filter_value" != "$expected_filter" ]]; then invalid_filters+=("$basename_script (filter: $filter_value, expected: $expected_filter)") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#invalid_filters[@]}" "FILTER variables should use dot notation (variant.1g1r). Invalid: ${invalid_filters[*]}" } # Test: Verify required variants exist for each system test_required_variants_exist() { local systems=() local missing_variants=() # Get unique system names from build scripts while IFS= read -r -d '' script; do local basename_script basename_script=$(basename "$script") local system system=$(echo "$basename_script" | sed 's/build-\([^-]*\)-.*/\1/') if [[ ! " ${systems[*]} " =~ " ${system} " ]]; then systems+=("$system") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) # Check each system has required variants: all, eng for system in "${systems[@]}"; do if [[ ! -f "${PROJECT_ROOT}/builders/build-${system}-all-1g1r.sh" ]]; then missing_variants+=("${system}-all") fi if [[ ! -f "${PROJECT_ROOT}/builders/build-${system}-eng-1g1r.sh" ]]; then missing_variants+=("${system}-eng") fi # USA variant is optional, so we don't enforce it done assert_equals "0" "${#missing_variants[@]}" "Each system should have 'all' and 'eng' variants. Missing: ${missing_variants[*]}" } # Test: Verify scripts are executable test_build_scripts_are_executable() { local non_executable=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") if [[ ! -x "$script" ]]; then non_executable+=("$basename_script") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#non_executable[@]}" "All build scripts should be executable. Non-executable: ${non_executable[*]}" assert_greater_than "0" "$script_count" "Should find at least some build scripts to check" } # Test: Verify no legacy directory references test_no_legacy_directory_references() { local scripts_with_legacy_refs=() 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 old rom-organizer references if grep -q "rom-organizer" "$script"; then scripts_with_legacy_refs+=("$basename_script") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#scripts_with_legacy_refs[@]}" "No build scripts should reference legacy 'rom-organizer' directory. Scripts with legacy refs: ${scripts_with_legacy_refs[*]}" } # Test: Verify syntax is valid for all build scripts test_build_scripts_syntax_valid() { local syntax_errors=() local script_count=0 while IFS= read -r -d '' script; do script_count=$((script_count + 1)) local basename_script basename_script=$(basename "$script") # Check bash syntax if ! bash -n "$script" >/dev/null 2>&1; then syntax_errors+=("$basename_script") fi done < <(find "${PROJECT_ROOT}/builders" -name "build-*-1g1r.sh" -print0) assert_equals "0" "${#syntax_errors[@]}" "All build scripts should have valid bash syntax. Syntax errors: ${syntax_errors[*]}" assert_greater_than "0" "$script_count" "Should find at least some build scripts to check" } # Test: Verify SG1000 scripts specifically (regression test) test_sg1000_scripts_specific() { local sg1000_scripts=("build-sg1000-all-1g1r.sh" "build-sg1000-eng-1g1r.sh" "build-sg1000-usa-1g1r.sh") local missing_sg1000=() local invalid_sg1000=() for script in "${sg1000_scripts[@]}"; do local script_path="${PROJECT_ROOT}/builders/${script}" # Check if script exists if [[ ! -f "$script_path" ]]; then missing_sg1000+=("$script") continue fi # Check internal references local internal_name internal_name=$(grep "^# Script:" "$script_path" | head -1 | sed 's/^# Script: //') if [[ "$internal_name" != "$script" ]]; then invalid_sg1000+=("$script (internal name mismatch)") fi # Check FILTER variable local variant variant=$(echo "$script" | sed 's/build-sg1000-\(.*\)-1g1r\.sh/\1/') local expected_filter="${variant}.1g1r" local actual_filter actual_filter=$(grep 'FILTER=' "$script_path" | head -1 | sed 's/.*FILTER="\([^"]*\)".*/\1/') if [[ "$actual_filter" != "$expected_filter" ]]; then invalid_sg1000+=("$script (filter: $actual_filter, expected: $expected_filter)") fi done assert_equals "0" "${#missing_sg1000[@]}" "All SG1000 scripts should exist. Missing: ${missing_sg1000[*]}" assert_equals "0" "${#invalid_sg1000[@]}" "All SG1000 scripts should be properly configured. Invalid: ${invalid_sg1000[*]}" } # Main function to run all tests main() { init_test_framework echo "===============================================" echo " BUILD SCRIPT NAMING STANDARDS TEST SUITE" echo "===============================================" echo # Run all naming tests run_test "test_no_dot_notation_scripts" run_test "test_all_scripts_use_hyphen_notation" run_test "test_build_script_naming_pattern" run_test "test_internal_script_references_match_filename" run_test "test_internal_usage_examples_match_filename" run_test "test_filter_variables_use_dot_notation" run_test "test_required_variants_exist" run_test "test_build_scripts_are_executable" run_test "test_no_legacy_directory_references" run_test "test_build_scripts_syntax_valid" run_test "test_sg1000_scripts_specific" # 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