# shellcheck shell=bash [[ -n "${BOLT_TEST_LOADED:-}" ]] && return readonly BOLT_TEST_LOADED=1 readonly BOLT_TEST_SUITES=("mssql" "sqlite3" "postgresql" "mysql") source "$(dirname "${BASH_SOURCE[0]}")/log.sh" source "$(dirname "${BASH_SOURCE[0]}")/docker.sh" source "$(dirname "${BASH_SOURCE[0]}")/path.sh" # Run a full test suite with database setup and teardown # # DESCRIPTION: # Starts database, waits for health checks, runs tests with # environment variables, and cleans up afterward. Automatically handles # database lifecycle and generates connection strings from Docker configuration. # # ARGUMENTS: # $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "sqlite3", or "mysql") # # RETURN: # 0 - Tests passed successfully # 1 - Tests failed or setup/teardown failed. # # EXAMPLES: # test_run_suite "mssql" # test_run_suite "postgresql" # test_run_suite() { local db_test_suite_name="$1" local container_name="bolt_${db_test_suite_name}_db" local coverage_file="${PATH_PROJECT_DIR}/coverage-${db_test_suite_name}.out" local test_result=0 log_header "$db_test_suite_name Tests" if [[ $db_test_suite_name != "sqlite3" ]]; then log_step "Starting $container_name..." case "$db_test_suite_name" in "postgresql") docker_start_postgresql "$container_name" ;; "mysql") docker_start_mysql "$container_name" ;; "mssql") docker_start_mssql "$container_name" ;; *) log_error "Unknown database test suite: $db_test_suite_name" return 1 ;; esac if ! docker_wait_for_healthy_container "$container_name"; then log_error "Failed to start $container_name container" docker_stop_container "$container_name" return 1 fi fi log_step "Running $db_test_suite_name tests..." if ! test_run_go_tests "$db_test_suite_name" "$coverage_file"; then local test_result=1 fi if [[ $db_test_suite_name != "sqlite3" ]]; then log_step "Cleaning up..." docker_stop_container "$container_name" || log_warn "Unable to clean up container" fi return $test_result } # Check if a test suite name is valid # # ARGUMENTS: # $1: Test suite name to validate # # RETURN: # 0 - Valid test suite name # 1 - Invalid test suite name test_is_valid_suite() { local suite="$1" for valid in "${BOLT_TEST_SUITES[@]}"; do [[ "$suite" == "$valid" ]] && return 0 done return 1 } # Run Go tests with optional database type and coverage # # DESCRIPTION: # Executes Go tests with optional build tags and coverage reporting. # Tests are run with parallelism disabled (-p 1). For database tests, # automatically generates connection strings from Docker configuration. # # ARGUMENTS: # $1 - database_type: Database type (e.g., "mssql", "postgresql", "mysql", "sqlite3"). Defaults to empty string. # $2 - coverage_file: Output file for coverage profile. Defaults to empty string. # # RETURN: # 0 - Tests passed successfully # 1 - Tests failed # # EXAMPLES: # test_run_go_tests # test_run_go_tests "mssql" "coverage-mssql.out" # test_run_go_tests() { local database_type="${1:-}" local coverage_file="${2:-}" # NOTE: -p 1 is used because we want to run tests sequentially. # This is because our tests depend on shared resources e.g. a database. local cmd="go test -p 1" if [[ -n "$database_type" ]]; then cmd="$cmd -tags $database_type" log_step "Running tests with tags: $database_type" else log_step "Running tests" fi if [[ -n "$coverage_file" ]]; then cmd="$cmd -covermode=count -coverprofile=$coverage_file" fi cmd="$cmd ./..." # For database tests, generate DSN and set environment variables if [[ -n "$database_type" ]]; then local dsn dsn=$(docker_generate_dsn "$database_type") cmd="BOLT_DB_DSN='$dsn' BOLT_DB_DRIVER='$database_type' BOLT_DB_MIGRATIONS_TABLE='bolt_migrations' $cmd" fi if eval "$cmd"; then log_success "Tests completed successfully!" else log_error "Tests failed!" return 1 fi } # Combine coverage reports from multiple test suites # # DESCRIPTION: # Merges coverage files matching pattern coverage-*.out into a single report # # ARGUMENTS: # $1 - output_file: Output file for combined coverage # # RETURN: # 0 - Coverage combined successfully # 1 - No coverage files found or merge failed # test_combine_coverage() { local output_file="$1" log_step "Checking for coverage files..." if ! ls coverage-*.out >/dev/null 2>&1; then log_error "No coverage files found (coverage-*.out)" log_error "Run tests first to generate coverage reports" return 1 fi log_step "Combining reports into $output_file..." if ! go run github.com/wadey/gocovmerge@b5bfa59ec0adc420475f97f89b58045c721d761c coverage-*.out >"$output_file"; then log_error "Failed to combine coverage reports" return 1 fi log_success "Combined coverage report created: $output_file" } # Display coverage summary and HTML generation instructions # # ARGUMENTS: # $1 - coverage_file: Coverage file to summarize # test_show_coverage_summary() { local coverage_file="$1" log_step "Generating coverage summary..." go tool cover -func="$coverage_file" }