aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-10 17:42:56 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-10-10 17:42:56 -0400
commit69809346b7c5c710eca080e5390244be53dc2732 (patch)
tree4fdb6a63437ddd9d27eef5ddfb2cb8c2613dcb89 /scripts/lib
parentInstall shadow-uidmap instead of shadow (diff)
downloadcli-69809346b7c5c710eca080e5390244be53dc2732.zip
Replace tools with scripts and use them in .build.yml
Diffstat (limited to 'scripts/lib')
-rw-r--r--scripts/lib/docker.sh53
-rw-r--r--scripts/lib/env.sh20
-rw-r--r--scripts/lib/log.sh47
-rw-r--r--scripts/lib/path.sh5
-rw-r--r--scripts/lib/test.sh177
5 files changed, 302 insertions, 0 deletions
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"
+}