aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib/test.sh
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-10 19:39:56 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-10-10 19:39:56 -0400
commitb471f6a1684320a90f65ef5aac7c0f87c69fb79b (patch)
treeef421348260efadeb9f04b56b7e3bfb42c2b28b8 /scripts/lib/test.sh
parentRemove need for docker compose (diff)
downloadcli-b471f6a1684320a90f65ef5aac7c0f87c69fb79b.zip
Remove the need for .env files
Diffstat (limited to 'scripts/lib/test.sh')
-rw-r--r--scripts/lib/test.sh66
1 files changed, 32 insertions, 34 deletions
diff --git a/scripts/lib/test.sh b/scripts/lib/test.sh
index 22fa8eb..dab2241 100644
--- a/scripts/lib/test.sh
+++ b/scripts/lib/test.sh
@@ -7,21 +7,17 @@ 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.
+# 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")
#
-# FILES REQUIRED:
-# .env.{test_name} - Environment file with database configuration
-#
# RETURN:
# 0 - Tests passed successfully
# 1 - Tests failed or setup/teardown failed.
@@ -33,7 +29,6 @@ source "$(dirname "${BASH_SOURCE[0]}")/env.sh"
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 coverage_file="${PATH_PROJECT_DIR}/coverage-${db_test_suite_name}.out"
local test_result=0
@@ -43,19 +38,19 @@ test_run_suite() {
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
- ;;
+ "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
@@ -66,7 +61,7 @@ test_run_suite() {
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
@@ -95,20 +90,16 @@ test_is_valid_suite() {
return 1
}
-# Run Go tests with optional tags and coverage
+# 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).
+# Tests are run with parallelism disabled (-p 1). For database tests,
+# automatically generates connection strings from Docker configuration.
#
# 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
+# $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
@@ -119,16 +110,16 @@ test_is_valid_suite() {
# test_run_go_tests "mssql" "coverage-mssql.out"
#
test_run_go_tests() {
- local test_tags="$1"
- local coverage_file="$2"
+ 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 "$test_tags" ]]; then
- cmd="$cmd -tags $test_tags"
- log_step "Running tests with tags: $test_tags"
+ if [[ -n "$database_type" ]]; then
+ cmd="$cmd -tags $database_type"
+ log_step "Running tests with tags: $database_type"
else
log_step "Running tests"
fi
@@ -139,6 +130,13 @@ test_run_go_tests() {
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