feat(tools,test): add better output

This commit is contained in:
2026-02-22 21:35:51 +01:00
parent c369e32c93
commit 38c08ff448
3 changed files with 55 additions and 39 deletions
+10 -8
View File
@@ -43,11 +43,12 @@ install: build
@cp $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME) @cp $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME)
@./tools/install.sh $(PROJECT_FILENAME) $(PROJECT_VERSION) @./tools/install.sh $(PROJECT_FILENAME) $(PROJECT_VERSION)
.PHONY: test .PHONY: check
test: build check: install
@echo "Running tests..."
@dropdb morbac_test 2>/dev/null || true @dropdb morbac_test 2>/dev/null || true
@createdb morbac_test @createdb morbac_test
@./tools/test.sh morbac_test $(OUTPUT_DEV_FILENAME) @./tools/test.sh morbac_test
@dropdb morbac_test @dropdb morbac_test
# Uninstall extension from PostgreSQL # Uninstall extension from PostgreSQL
@@ -88,9 +89,10 @@ docker-uninstall:
@./tools/docker_uninstall.sh $(DOCKER_CONTAINER) $(PROJECT_FILENAME) @./tools/docker_uninstall.sh $(DOCKER_CONTAINER) $(PROJECT_FILENAME)
# Build and run tests in Docker container # Build and run tests in Docker container
.PHONY: docker-test .PHONY: docker-check
docker-test: build docker-start docker-check: docker-install
@./tools/docker_test.sh $(DOCKER_CONTAINER) morbac_test $(OUTPUT_DEV_FILENAME) @echo "Running tests in Docker..."
@./tools/docker_test.sh $(DOCKER_CONTAINER) morbac_test postgres
# Show help # Show help
.PHONY: help .PHONY: help
@@ -100,7 +102,7 @@ help:
@echo "Makefile targets:" @echo "Makefile targets:"
@echo " make build - Build $(OUTPUT_DEV_FILENAME) from src/" @echo " make build - Build $(OUTPUT_DEV_FILENAME) from src/"
@echo " make release - Build versioned $(PROJECT_FILENAME)--X.Y.Z.sql 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 install - Build and install $(PROJECT_FILENAME) to PostgreSQL"
@echo " make uninstall - Uninstall $(PROJECT_FILENAME) from PostgreSQL" @echo " make uninstall - Uninstall $(PROJECT_FILENAME) from PostgreSQL"
@echo " make clean - Clean up generated files" @echo " make clean - Clean up generated files"
@@ -112,5 +114,5 @@ help:
@echo " make docker-clean - Remove PostgreSQL Docker container" @echo " make docker-clean - Remove PostgreSQL Docker container"
@echo " make docker-install - Build and install extension in 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-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 "" @echo ""
+16 -13
View File
@@ -1,41 +1,44 @@
#!/bin/bash #!/bin/bash
# docker_test.sh - Run tests in a Docker container # 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 set -e # Exit immediately if a command exits with a non-zero status
CONTAINER="${1:-postgres}" CONTAINER="${1:-postgres}"
DB="${2:-morbac_test}" DB="${2:-morbac_test}"
SQL_FILE="${3:-pg_morbac.sql}" PG_USER="${3:-postgres}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && 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 if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then
echo "Error: Docker container not running: $CONTAINER" >&2 echo "Error: Docker container not running: $CONTAINER" >&2
exit 1 exit 1
fi 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 echo "Error: PostgreSQL not accessible in container: $CONTAINER" >&2
exit 1 exit 1
fi fi
docker exec "$CONTAINER" psql -U postgres -c "DROP DATABASE IF EXISTS $DB" -q if [ ! -d "$PROJECT_DIR/tests" ]; then
docker exec "$CONTAINER" psql -U postgres -c "CREATE DATABASE $DB" -q 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 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 "$PROJECT_DIR/tests" "$CONTAINER:/tmp/pg_morbac_test/"
docker cp "$SCRIPT_DIR/test.sh" "$CONTAINER:/tmp/pg_morbac_test/tools/" 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 docker exec "$CONTAINER" rm -rf /tmp/pg_morbac_test
+29 -18
View File
@@ -1,49 +1,60 @@
#!/bin/bash #!/bin/bash
# test.sh - Run tests against the PostgreSQL extension using psql # 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 set -e # Exit immediately if a command exits with a non-zero status
DB="${1:-morbac_test}" DB="${1:-morbac_test}"
SQL_FILE="${2:-pg_morbac.sql}" PG_USER="${2:-$USER}"
USER="${3:-postgres}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
TEST_DIR="$PROJECT_DIR/tests" 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 if [ ! -d "$TEST_DIR" ]; then
echo "Error: test directory not found: $TEST_DIR" >&2 echo "Error: test directory not found: $TEST_DIR" >&2
exit 1 exit 1
fi 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 "Error: cannot connect to database: $DB" >&2
echo "Ensure the database exists and is accessible" >&2 echo "Ensure the database exists and is accessible" >&2
exit 1 exit 1
fi 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 set +e # Temporarily disable exit on error to capture test failures
psql -U "$USER" -d "$DB" -f "$TEST_DIR/00_setup.sql" -q
fi
for test_file in "$TEST_DIR"/[0-9][0-9]_*.sql; do for test_file in "$TEST_DIR"/[0-9][0-9]_*.sql; do
if [ -f "$test_file" ]; then if [ -f "$test_file" ]; then
test_name=$(basename "$test_file" .sql) test_name=$(basename "$test_file" .sql)
echo "Running: $test_name" echo " - $test_name"
psql -U "$USER" -d "$DB" -f "$test_file" -q ((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 fi
done 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