From 38c08ff4489f528773a0f6f45a3f138d224f5fae Mon Sep 17 00:00:00 2001 From: Marc Villain Date: Sun, 22 Feb 2026 21:35:51 +0100 Subject: [PATCH] feat(tools,test): add better output --- Makefile | 18 +++++++++-------- tools/docker_test.sh | 29 +++++++++++++++------------ tools/test.sh | 47 +++++++++++++++++++++++++++----------------- 3 files changed, 55 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index f03d61d..7ddbdb5 100644 --- a/Makefile +++ b/Makefile @@ -43,11 +43,12 @@ install: build @cp $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME) @./tools/install.sh $(PROJECT_FILENAME) $(PROJECT_VERSION) -.PHONY: test -test: build +.PHONY: check +check: install + @echo "Running tests..." @dropdb morbac_test 2>/dev/null || true @createdb morbac_test - @./tools/test.sh morbac_test $(OUTPUT_DEV_FILENAME) + @./tools/test.sh morbac_test @dropdb morbac_test # Uninstall extension from PostgreSQL @@ -88,9 +89,10 @@ docker-uninstall: @./tools/docker_uninstall.sh $(DOCKER_CONTAINER) $(PROJECT_FILENAME) # Build and run tests in Docker container -.PHONY: docker-test -docker-test: build docker-start - @./tools/docker_test.sh $(DOCKER_CONTAINER) morbac_test $(OUTPUT_DEV_FILENAME) +.PHONY: docker-check +docker-check: docker-install + @echo "Running tests in Docker..." + @./tools/docker_test.sh $(DOCKER_CONTAINER) morbac_test postgres # Show help .PHONY: help @@ -100,7 +102,7 @@ help: @echo "Makefile targets:" @echo " make build - Build $(OUTPUT_DEV_FILENAME) from src/" @echo " make release - Build versioned $(PROJECT_FILENAME)--X.Y.Z.sql from src/" - @echo " make test - Build and run test suite" + @echo " make check - Build and run test suite" @echo " make install - Build and install $(PROJECT_FILENAME) to PostgreSQL" @echo " make uninstall - Uninstall $(PROJECT_FILENAME) from PostgreSQL" @echo " make clean - Clean up generated files" @@ -112,5 +114,5 @@ help: @echo " make docker-clean - Remove PostgreSQL Docker container" @echo " make docker-install - Build and install extension in Docker container" @echo " make docker-uninstall - Uninstall extension from Docker container" - @echo " make docker-test - Build and run test suite in Docker container" + @echo " make docker-check - Build and run test suite in Docker container" @echo "" diff --git a/tools/docker_test.sh b/tools/docker_test.sh index dd8dbb8..19c978f 100755 --- a/tools/docker_test.sh +++ b/tools/docker_test.sh @@ -1,41 +1,44 @@ #!/bin/bash # docker_test.sh - Run tests in a Docker container -# Usage: ./docker_test.sh [container_name] [database_name] [sql_file] +# Usage: ./docker_test.sh [container_name] [database_name] [postgres_user] set -e # Exit immediately if a command exits with a non-zero status CONTAINER="${1:-postgres}" DB="${2:-morbac_test}" -SQL_FILE="${3:-pg_morbac.sql}" +PG_USER="${3:-postgres}" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" -if [ ! -f "$PROJECT_DIR/$SQL_FILE" ]; then - echo "Error: SQL file not found: $SQL_FILE" >&2 - exit 1 -fi - if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then echo "Error: Docker container not running: $CONTAINER" >&2 exit 1 fi -if ! docker exec "$CONTAINER" psql -U postgres -c '\q'; then +if ! docker exec "$CONTAINER" psql -U "$PG_USER" -c '\q' 2>/dev/null; then echo "Error: PostgreSQL not accessible in container: $CONTAINER" >&2 exit 1 fi -docker exec "$CONTAINER" psql -U postgres -c "DROP DATABASE IF EXISTS $DB" -q -docker exec "$CONTAINER" psql -U postgres -c "CREATE DATABASE $DB" -q +if [ ! -d "$PROJECT_DIR/tests" ]; then + echo "Error: tests directory not found" >&2 + exit 1 +fi +echo "Creating test database..." +docker exec "$CONTAINER" psql -U "$PG_USER" -c "DROP DATABASE IF EXISTS $DB" -q +docker exec "$CONTAINER" psql -U "$PG_USER" -c "CREATE DATABASE $DB" -q + +echo "Copying test files to container..." docker exec "$CONTAINER" mkdir -p /tmp/pg_morbac_test/tools -docker cp "$PROJECT_DIR/$SQL_FILE" "$CONTAINER:/tmp/pg_morbac_test/" docker cp "$PROJECT_DIR/tests" "$CONTAINER:/tmp/pg_morbac_test/" docker cp "$SCRIPT_DIR/test.sh" "$CONTAINER:/tmp/pg_morbac_test/tools/" -docker exec -u postgres -w /tmp/pg_morbac_test "$CONTAINER" bash tools/test.sh "$DB" "$SQL_FILE" postgres +echo "Running tests..." +docker exec -w /tmp/pg_morbac_test "$CONTAINER" bash tools/test.sh "$DB" "$PG_USER" -docker exec "$CONTAINER" psql -U postgres -c "DROP DATABASE $DB" -q +# Cleanup +docker exec "$CONTAINER" psql -U "$PG_USER" -c "DROP DATABASE $DB" -q docker exec "$CONTAINER" rm -rf /tmp/pg_morbac_test diff --git a/tools/test.sh b/tools/test.sh index ef6078d..343d2d8 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -1,49 +1,60 @@ #!/bin/bash # test.sh - Run tests against the PostgreSQL extension using psql -# Usage: ./test.sh [database_name] [sql_file] [user] +# Usage: ./test.sh [database_name] [postgres_user] +# Note: This script expects the extension to be already installed via 'make install' set -e # Exit immediately if a command exits with a non-zero status DB="${1:-morbac_test}" -SQL_FILE="${2:-pg_morbac.sql}" -USER="${3:-postgres}" +PG_USER="${2:-$USER}" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" TEST_DIR="$PROJECT_DIR/tests" -if [ ! -f "$PROJECT_DIR/$SQL_FILE" ]; then - echo "Error: SQL file not found: $SQL_FILE" >&2 - echo "Run 'make build' to generate the required file" >&2 - exit 1 -fi - if [ ! -d "$TEST_DIR" ]; then echo "Error: test directory not found: $TEST_DIR" >&2 exit 1 fi -if ! psql -U "$USER" -d "$DB" -c '\q'; then +if ! psql -U "$PG_USER" -d "$DB" -c '\q'; then echo "Error: cannot connect to database: $DB" >&2 echo "Ensure the database exists and is accessible" >&2 exit 1 fi -echo "Testing $SQL_FILE on database $DB" +echo "Running tests on database $DB" -psql -U "$USER" -d "$DB" -f "$PROJECT_DIR/$SQL_FILE" -q +total=0 +passed=0 +failed=0 -if [ -f "$TEST_DIR/00_setup.sql" ]; then - psql -U "$USER" -d "$DB" -f "$TEST_DIR/00_setup.sql" -q -fi +set +e # Temporarily disable exit on error to capture test failures for test_file in "$TEST_DIR"/[0-9][0-9]_*.sql; do if [ -f "$test_file" ]; then test_name=$(basename "$test_file" .sql) - echo "Running: $test_name" - psql -U "$USER" -d "$DB" -f "$test_file" -q + echo " - $test_name" + ((total++)) + + output=$(psql -U "$PG_USER" -d "$DB" -f "$test_file" 2>&1) + exit_code=$? + echo "$output" + + if [ $exit_code -eq 0 ] && ! echo "$output" | grep -qE "ERROR|FAILED"; then + ((passed++)) + else + ((failed++)) + echo " FAILED" + fi fi done -echo "All tests completed" +set -e # Re-enable exit on error + +echo "Tests: $total total, $passed passed, $failed failed" + +if [ $failed -gt 0 ]; then + exit 1 +fi