diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-10 17:42:56 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-10 17:42:56 -0400 |
| commit | 69809346b7c5c710eca080e5390244be53dc2732 (patch) | |
| tree | 4fdb6a63437ddd9d27eef5ddfb2cb8c2613dcb89 | |
| parent | Install shadow-uidmap instead of shadow (diff) | |
| download | cli-69809346b7c5c710eca080e5390244be53dc2732.zip | |
Replace tools with scripts and use them in .build.yml
| -rw-r--r-- | .build.yml | 31 | ||||
| -rw-r--r-- | .env.mysql | 4 | ||||
| -rw-r--r-- | .shellcheckrc | 2 | ||||
| -rwxr-xr-x | scripts/combine-coverage.sh | 38 | ||||
| -rw-r--r-- | scripts/lib/docker.sh | 53 | ||||
| -rw-r--r-- | scripts/lib/env.sh | 20 | ||||
| -rw-r--r-- | scripts/lib/log.sh | 47 | ||||
| -rw-r--r-- | scripts/lib/path.sh | 5 | ||||
| -rw-r--r-- | scripts/lib/test.sh | 177 | ||||
| -rwxr-xr-x | scripts/test-mssql.sh | 22 | ||||
| -rwxr-xr-x | scripts/test-mysql.sh | 18 | ||||
| -rwxr-xr-x | scripts/test-postgresql.sh | 18 | ||||
| -rwxr-xr-x | scripts/test-sqlite3.sh | 6 | ||||
| -rwxr-xr-x | scripts/test.sh | 54 | ||||
| -rwxr-xr-x | scripts/upload-coverage.sh | 14 | ||||
| -rwxr-xr-x | tools/db.sh | 47 | ||||
| -rw-r--r-- | tools/lib.sh | 16 | ||||
| -rwxr-xr-x | tools/test.sh | 25 |
18 files changed, 391 insertions, 206 deletions
@@ -3,37 +3,10 @@ packages: - go - docker - docker-cli-compose - - shadow-uidmap sources: - https://git.sr.ht/~eugenetriguba/bolt tasks: - - setup: | - curl -fsSL https://get.docker.com/rootless | sh - cat >> ~/.docker_env << 'EOF' - export PATH=$HOME/bin:$PATH - export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock - EOF - - - test-sqlite3: | - cd bolt - source ~/.docker_env - ./scripts/test-sqlite3.sh - - - test-postgresql: | - cd bolt - source ~/.docker_env - ./scripts/test-postgresql.sh - - - test-mysql: | - cd bolt - source ~/.docker_env - ./scripts/test-mysql.sh - - - test-mssql: | - cd bolt - source ~/.docker_env - ./scripts/test-mssql.sh - - - combine-coverage: | + - test: | cd bolt + ./scripts/test.sh ./scripts/combine-coverage.sh @@ -1,3 +1,3 @@ -BOLT_DB_DSN=test_user:testpassword@tcp(localhost:4002)/test_db?parseTime=true +BOLT_DB_DSN='test_user:testpassword@tcp(localhost:4002)/test_db?parseTime=true' BOLT_DB_DRIVER=mysql -BOLT_DB_MIGRATIONS_TABLE=bolt_migrations
\ No newline at end of file +BOLT_DB_MIGRATIONS_TABLE=bolt_migrations diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..0bf59b0 --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,2 @@ +source-path=scripts/lib +source-path=scripts diff --git a/scripts/combine-coverage.sh b/scripts/combine-coverage.sh index 64a792f..1ce3773 100755 --- a/scripts/combine-coverage.sh +++ b/scripts/combine-coverage.sh @@ -1,12 +1,32 @@ -#!/bin/bash -set -e +#!/usr/bin/env bash +set -eou pipefail -echo "Combining coverage reports..." -go run github.com/wadey/gocovmerge@b5bfa59ec0adc420475f97f89b58045c721d761c coverage-*.out > coverage.combined.out +source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/test.sh" -echo "Combined coverage report:" -go tool cover -func=coverage.combined.out +usage() { + echo "Usage: $0 [OUTPUT_FILE]" + echo "" + echo "Combine coverage reports from multiple test suites" + echo "" + echo "Arguments:" + echo " OUTPUT_FILE Output file for combined coverage (default: coverage.combined.out)" + echo "" + echo "Examples:" + echo " $0 # Creates coverage.combined.out" + echo " $0 coverage.total.out # Creates coverage.total.out" +} -echo "" -echo "To generate HTML coverage report locally, run:" -echo " go tool cover -html=coverage.combined.out -o coverage.html" +main() { + local output_file="${1:-coverage.combined.out}" + if [[ "$output_file" == "-h" || "$output_file" == "--help" ]]; then + usage + exit 0 + fi + + log_header "Combining Coverage Reports" + test_combine_coverage "$output_file" + test_show_coverage_summary "$output_file" +} + +main "$@" diff --git a/scripts/lib/docker.sh b/scripts/lib/docker.sh new file mode 100644 index 0000000..8f841bc --- /dev/null +++ b/scripts/lib/docker.sh @@ -0,0 +1,53 @@ +# shellcheck shell=bash +[[ -n "${BOLT_DOCKER_LOADED:-}" ]] && return +readonly BOLT_DOCKER_LOADED=1 + +source "$(dirname "${BASH_SOURCE[0]}")/log.sh" + +docker_compose_up() { + local compose_file="$1" + log_info "Starting services from '$compose_file'..." + docker compose --file "$compose_file" up --detach +} + +docker_compose_down() { + local compose_file="$1" + log_info "Stopping services from '$compose_file'..." + docker compose --file "$compose_file" down +} + +docker_wait_for_healthy_container() { + local container_name="$1" + local timeout="${2:-60}" + local elapsed=0 + + log_info "Waiting for container '$container_name' to be healthy..." + while [[ $elapsed -lt $timeout ]]; do + local status + status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found") + + case "$status" in + "healthy") + log_success "Container '$container_name' is healthy!" + return 0 + ;; + "not_found") + log_error "Error: Container '$container_name' not found" + return 1 + ;; + "unhealthy") + log_error "Error: Container '$container_name' is unhealthy" + return 1 + ;; + *) + log_progress + sleep 1 + ((elapsed++)) + ;; + esac + done + + log_progress_end + log_error "Timeout: Container '$container_name' did not become healthy within ${timeout} seconds" + return 1 +} diff --git a/scripts/lib/env.sh b/scripts/lib/env.sh new file mode 100644 index 0000000..e598584 --- /dev/null +++ b/scripts/lib/env.sh @@ -0,0 +1,20 @@ +# shellcheck shell=bash +[[ -n "${BOLT_ENV_LOADED:-}" ]] && return +readonly BOLT_ENV_LOADED=1 + +source "$(dirname "${BASH_SOURCE[0]}")/log.sh" + +load_env_file() { + local -r env_file="$1" + if [[ ! -f "$env_file" ]]; then + log_error "Environment file not found: $env_file" + return 1 + fi + + set -a + # shellcheck disable=SC1090 + source "$env_file" + set +a + + log_info "Loaded environment from $env_file" +} diff --git a/scripts/lib/log.sh b/scripts/lib/log.sh new file mode 100644 index 0000000..f1a3e8e --- /dev/null +++ b/scripts/lib/log.sh @@ -0,0 +1,47 @@ +# shellcheck shell=bash +[[ -n "${BOLT_LOG_LOADED:-}" ]] && return +readonly BOLT_LOG_LOADED=1 + +log_info() { + echo "[INFO] $*" >&2 +} + +log_error() { + echo "[ERROR] $*" >&2 +} + +log_warn() { + echo "[WARN] $*" >&2 +} + +log_success() { + echo "[SUCCESS] $*" >&2 +} + +log_debug() { + [[ "${DEBUG:-}" == "true" ]] && echo "[DEBUG] $*" >&2 +} + +log_newline() { + echo "" >&2 +} + +log_header() { + echo "=== $* ===" >&2 +} + +log_step() { + echo "→ $*" >&2 +} + +log_substep() { + echo " • $*" >&2 +} + +log_progress() { + printf "." >&2 +} + +log_progress_end() { + echo "" >&2 +} diff --git a/scripts/lib/path.sh b/scripts/lib/path.sh new file mode 100644 index 0000000..379c9fa --- /dev/null +++ b/scripts/lib/path.sh @@ -0,0 +1,5 @@ +# shellcheck shell=bash +[[ -n "${BOLT_PATH_LOADED:-}" ]] && return +readonly BOLT_PATH_LOADED=1 + +readonly PATH_PROJECT_DIR="$(dirname "${BASH_SOURCE[0]}")/../.." diff --git a/scripts/lib/test.sh b/scripts/lib/test.sh new file mode 100644 index 0000000..a90eb2c --- /dev/null +++ b/scripts/lib/test.sh @@ -0,0 +1,177 @@ +# 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" +source "$(dirname "${BASH_SOURCE[0]}")/env.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 environment variable loading. +# +# ARGUMENTS: +# $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "sqlite3", or "mysql") +# +# FILES REQUIRED: +# .env.{test_name} - Environment file with database configuration +# docker-compose.{test_name}.yml - Docker Compose configuration (unless $1 is sqlite3) +# +# 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 env_file="${PATH_PROJECT_DIR}/.env.${db_test_suite_name}" + local compose_file="${PATH_PROJECT_DIR}/docker-compose.${db_test_suite_name}.yml" + 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..." + docker_compose_up "$compose_file" + + if ! docker_wait_for_healthy_container "$container_name"; then + log_error "Failed to start $container_name container" + docker_compose_down "$compose_file" + return 1 + fi + fi + + log_step "Running $db_test_suite_name tests..." + load_env_file "$env_file" + 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_compose_down "$compose_file" || log_warn "Unable to clean up docker compose services" + 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 tags and coverage +# +# DESCRIPTION: +# Executes Go tests with optional build tags and coverage reporting. +# Tests are run with parallelism disabled (-p 1). +# +# ARGUMENTS: +# $1 - test_tags (optional): Build tags to include (e.g., "mssql", "postgres") +# $2 - coverage_file (optional): Output file for coverage profile +# +# ENVIRONMENT VARIABLES (required for database tests): +# BOLT_DB_DSN - Database connection string +# BOLT_DB_DRIVER - Database driver name (e.g., "mssql", "postgres") +# BOLT_DB_MIGRATIONS_TABLE - Table name for migration tracking +# +# 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 test_tags="$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 "$test_tags" ]]; then + cmd="$cmd -tags $test_tags" + log_step "Running tests with tags: $test_tags" + else + log_step "Running tests" + fi + + if [[ -n "$coverage_file" ]]; then + cmd="$cmd -covermode=count -coverprofile=$coverage_file" + fi + + cmd="$cmd ./..." + + 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" +} diff --git a/scripts/test-mssql.sh b/scripts/test-mssql.sh deleted file mode 100755 index 5d54f74..0000000 --- a/scripts/test-mssql.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -set -e - -echo "Running MSSQL tests..." -env $(cat .env.mssql | xargs) docker compose -f docker-compose.mssql.yml up -d - -echo "Waiting for MSSQL to be ready..." -sleep 20 - -# Read password from .env.mssql if available -MSSQL_PASSWORD="${MSSQL_SA_PASSWORD:-YourStrong@Passw0rd}" - -until docker exec bolt_mssql_db /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$MSSQL_PASSWORD" -Q "SELECT 1" &>/dev/null; do - echo "Waiting for MSSQL..." - sleep 5 -done -echo "MSSQL is ready." - -env $(cat .env.mssql | xargs) go test -p 1 -tags "mssql" -covermode=count -coverprofile=coverage-mssql.out ./... - -docker compose -f docker-compose.mssql.yml down -echo "MSSQL tests completed." diff --git a/scripts/test-mysql.sh b/scripts/test-mysql.sh deleted file mode 100755 index e0131b7..0000000 --- a/scripts/test-mysql.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -set -e - -echo "Running MySQL tests..." -env $(cat .env.mysql | xargs) docker compose -f docker-compose.mysql.yml up -d - -echo "Waiting for MySQL to be ready..." -sleep 15 -until docker exec bolt_mysql_db mysqladmin ping -h localhost --silent 2>/dev/null; do - echo "Waiting for MySQL..." - sleep 5 -done -echo "MySQL is ready." - -env $(cat .env.mysql | xargs) go test -p 1 -tags "mysql" -covermode=count -coverprofile=coverage-mysql.out ./... - -docker compose -f docker-compose.mysql.yml down -echo "MySQL tests completed." diff --git a/scripts/test-postgresql.sh b/scripts/test-postgresql.sh deleted file mode 100755 index 6046f40..0000000 --- a/scripts/test-postgresql.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -set -e - -echo "Running PostgreSQL tests..." -env "$(cat .env.postgresql | xargs)" docker compose -f docker-compose.postgresql.yml up -d - -echo "Waiting for PostgreSQL to be ready..." -sleep 10 -until docker exec bolt_postgresql_db pg_isready -U postgres 2>/dev/null; do - echo "Waiting for PostgreSQL..." - sleep 5 -done -echo "PostgreSQL is ready." - -env "$(cat .env.postgresql | xargs)" go test -p 1 -tags "postgresql" -covermode=count -coverprofile=coverage-postgresql.out ./... - -docker compose -f docker-compose.postgresql.yml down -echo "PostgreSQL tests completed." diff --git a/scripts/test-sqlite3.sh b/scripts/test-sqlite3.sh deleted file mode 100755 index 60c3a4c..0000000 --- a/scripts/test-sqlite3.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -e - -echo "Running SQLite3 tests..." -env $(cat .env.sqlite3 | xargs) go test -p 1 -tags "sqlite3" -covermode=count -coverprofile=coverage-sqlite3.out ./... -echo "SQLite3 tests completed." diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..7937359 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +set -eou pipefail + +source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/test.sh" + +usage() { + echo "Usage: $0 [TEST_SUITE]" + echo "" + echo "Run database test suites" + echo "" + echo "Arguments:" + echo " TEST_SUITE Test suite to run (mssql, sqlite, postgres, mysql)" + echo " If not specified, runs all test suites" + echo "" + echo "Examples:" + echo " $0 mssql # Run only MSSQL tests" + echo " $0 # Run all test suites" +} + +main() { + local suite="${1:-}" + if [[ $suite == "-h" || $suite == "--help" ]]; then + usage + exit 0 + fi + + if [[ -n "$suite" ]]; then + if ! test_is_valid_suite "$suite"; then + log_error "Invalid test suite: $suite" + log_newline + usage + exit 1 + fi + test_run_suite "$suite" + else + log_header "Running all test suites" + local failed_suites=() + for suite in "${BOLT_TEST_SUITES[@]}"; do + if ! test_run_suite "$suite"; then + failed_suites+=("$suite") + fi + done + + if [[ ${#failed_suites[@]} -eq 0 ]]; then + log_success "All test suites completed successfully!" + else + log_error "Failed test suites: ${failed_suites[*]}" + exit 1 + fi + fi +} + +main "$@" diff --git a/scripts/upload-coverage.sh b/scripts/upload-coverage.sh deleted file mode 100755 index ed57729..0000000 --- a/scripts/upload-coverage.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -set -e - -if [ -z "$CODECOV_TOKEN" ]; then - echo "Warning: CODECOV_TOKEN not set, skipping upload" - exit 0 -fi - -echo "Uploading coverage to Codecov..." -curl -Os https://uploader.codecov.io/latest/linux/codecov -chmod +x codecov -./codecov -t "$CODECOV_TOKEN" -f coverage.combined.out -F unittests -n bolt-coverage - -echo "Coverage uploaded successfully." diff --git a/tools/db.sh b/tools/db.sh deleted file mode 100755 index 8e6f2b9..0000000 --- a/tools/db.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash -# -# Start/Stop test databases. -# -# Subcommands: -# start <database-name>: Start a particular database. -# stop <database-name>: Stop a particular database. -# -# Usage: -# $ ./tools/db.sh start <database-name> -# $ ./tools/db.sh stop <database-name> -set +e - -SCRIPT_DIR=$(dirname "$0") -. "$SCRIPT_DIR/lib.sh" - -BASE_DIR=$(dirname "$0")/.. -ALLOWED_DBS=("postgresql" "mysql" "mssql") -ALLOWED_COMMANDS=("start" "stop") - -function main() { - local COMMAND="$1" - if ! elementIn "$COMMAND" "${ALLOWED_COMMANDS[@]}"; then - echo "Error: Invalid command. Allowed subcommands are: ${ALLOWED_COMMANDS[*]}" >&2 - exit 1 - fi - local DB_NAME="$2" - if ! elementIn "$DB_NAME" "${ALLOWED_DBS[@]}"; then - echo "Error: Invalid database type. Allowed types are: ${ALLOWED_DBS[*]}" >&2 - exit 1 - fi - - case $COMMAND in - start) - env $(cat "$BASE_DIR/.env.$DB_NAME" | xargs) docker compose -f "$BASE_DIR/docker-compose.$DB_NAME.yml" up -d - ;; - stop) - env $(cat "$BASE_DIR/.env.$DB_NAME" | xargs) docker compose -f "$BASE_DIR/docker-compose.$DB_NAME.yml" down - ;; - *) - echo "Error: Invalid command. Use 'start' or 'stop'." - exit 1 - ;; - esac -} - -main "$@" diff --git a/tools/lib.sh b/tools/lib.sh deleted file mode 100644 index e2c2c78..0000000 --- a/tools/lib.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -# elementIn checks whether a element is in an array. -# -# Args: -# $1: The element -# $2: The array -# -# Returns: -# 0 if the element is in the array; 1 otherwise. -function elementIn() { - local e match="$1" - shift - for e; do [[ "$e" == "$match" ]] && return 0; done - return 1 -} diff --git a/tools/test.sh b/tools/test.sh deleted file mode 100755 index c03678c..0000000 --- a/tools/test.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -# -# Run tests against a particular database. -# -# Usage: -# $ ./tools/test.sh <database-name> -set +e - -SCRIPT_DIR=$(dirname "$0") -. "$SCRIPT_DIR/lib.sh" - -BASE_DIR=$(dirname "$0")/.. -ALLOWED_DBS=("postgresql" "mysql" "mssql" "sqlite3") - -function main() { - local DB_NAME="$1" - if ! elementIn "$DB_NAME" "${ALLOWED_DBS[@]}"; then - echo "Error: Invalid database type. Allowed types are: ${ALLOWED_DBS[*]}" >&2 - exit 1 - fi - - env $(cat "$BASE_DIR/.env.$DB_NAME" | xargs) go test -p 1 -tags "$DB_NAME" -cover -coverprofile cover.out ./... -} - -main "$@" |