|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) 2025 Michele Tavella <meeghele@proton.me> |
| 3 | +# Licensed under the MIT License. See LICENSE file for details. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)" |
| 8 | +BIN_DEBUG="$ROOT/bin/mini-ccstatus-debug" |
| 9 | +FIXTURES_DIR="$ROOT/fixtures" |
| 10 | + |
| 11 | +PASS=0 |
| 12 | +FAIL=0 |
| 13 | + |
| 14 | +# Colors for test output |
| 15 | +RED='\033[0;31m' |
| 16 | +GREEN='\033[0;32m' |
| 17 | +YELLOW='\033[0;33m' |
| 18 | +BLUE='\033[0;34m' |
| 19 | +NC='\033[0m' # No Color |
| 20 | + |
| 21 | +# Check if valgrind is installed |
| 22 | +if ! command -v valgrind &> /dev/null; then |
| 23 | + echo -e "${RED}error: valgrind is not installed${NC}" |
| 24 | + echo "Install with: sudo apt-get install valgrind (Debian/Ubuntu)" |
| 25 | + echo " or: sudo dnf install valgrind (Fedora)" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# Build debug version if needed |
| 30 | +if [[ ! -x "$BIN_DEBUG" ]]; then |
| 31 | + echo -e "${BLUE}Building debug version...${NC}" |
| 32 | + make -C "$ROOT" debug || { |
| 33 | + echo -e "${RED}error: failed to build debug version${NC}" |
| 34 | + exit 1 |
| 35 | + } |
| 36 | +fi |
| 37 | + |
| 38 | +test_passed() { |
| 39 | + echo -e "${GREEN}✓${NC} $1" |
| 40 | + PASS=$((PASS + 1)) |
| 41 | +} |
| 42 | + |
| 43 | +test_failed() { |
| 44 | + echo -e "${RED}✗${NC} $1" |
| 45 | + FAIL=$((FAIL + 1)) |
| 46 | +} |
| 47 | + |
| 48 | +# Valgrind options for strict leak checking |
| 49 | +VALGRIND_OPTS=( |
| 50 | + --leak-check=full |
| 51 | + --show-leak-kinds=all |
| 52 | + --track-origins=yes |
| 53 | + --error-exitcode=99 |
| 54 | + --errors-for-leak-kinds=all |
| 55 | + --quiet |
| 56 | +) |
| 57 | + |
| 58 | +run_valgrind_test() { |
| 59 | + local test_name="$1" |
| 60 | + local input_source="$2" |
| 61 | + local prog_exit=0 |
| 62 | + local output |
| 63 | + |
| 64 | + # Run valgrind and capture output. |
| 65 | + # Valgrind wraps the program, so we need to distinguish between: |
| 66 | + # - Program failure (exit code from mini-ccstatus) - OK, not our concern |
| 67 | + # - Memory errors (valgrind exits with 99) - FAIL |
| 68 | + # Temporarily disable set -e so program errors don't exit the script |
| 69 | + set +e |
| 70 | + output=$(eval "$input_source" | NO_COLOR=1 valgrind "${VALGRIND_OPTS[@]}" "$BIN_DEBUG" 2>&1) |
| 71 | + prog_exit=$? |
| 72 | + set -e |
| 73 | + |
| 74 | + # If exit code is 99, valgrind found memory errors |
| 75 | + # Any other exit code means either success (0) or program error (not valgrind error) |
| 76 | + if [[ "$prog_exit" -eq 99 ]]; then |
| 77 | + # Valgrind detected memory issues |
| 78 | + test_failed "$test_name" |
| 79 | + echo -e "${YELLOW} valgrind detected memory issues:${NC}" |
| 80 | + echo "$output" | sed 's/^/ /' |
| 81 | + return 1 |
| 82 | + else |
| 83 | + # No valgrind errors (program may have failed, but that's OK) |
| 84 | + test_passed "$test_name" |
| 85 | + return 0 |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +echo "Running valgrind memory leak tests..." |
| 90 | +echo "========================================" |
| 91 | +echo "" |
| 92 | + |
| 93 | +# Test all JSON fixtures |
| 94 | +if [[ -d "$FIXTURES_DIR" ]]; then |
| 95 | + for fixture in "$FIXTURES_DIR"/*.json; do |
| 96 | + if [[ -f "$fixture" ]]; then |
| 97 | + fixture_name=$(basename "$fixture") |
| 98 | + run_valgrind_test "Fixture: $fixture_name" "cat '$fixture'" |
| 99 | + fi |
| 100 | + done |
| 101 | +else |
| 102 | + echo -e "${YELLOW}warning: fixtures directory not found${NC}" |
| 103 | +fi |
| 104 | + |
| 105 | +# Test empty input (EOF immediately) |
| 106 | +run_valgrind_test "Empty input (immediate EOF)" "echo -n ''" |
| 107 | + |
| 108 | +# Test minimal valid JSON |
| 109 | +run_valgrind_test "Minimal valid JSON" "echo '{}'" |
| 110 | + |
| 111 | +# Test invalid JSON (should still not leak) |
| 112 | +run_valgrind_test "Invalid JSON (no leaks on error path)" "echo 'not valid json'" |
| 113 | + |
| 114 | +# Test large valid JSON (stress test) |
| 115 | +run_valgrind_test "Large valid JSON object" "echo '{\"model\":{\"display_name\":\"test\",\"id\":\"test-id\"},\"cwd\":\"/test\",\"workspace\":{\"project_dir\":\"/test\"},\"version\":\"1.0.0\",\"cost\":{\"total_cost_usd\":0.5,\"total_duration_ms\":1000,\"total_api_duration_ms\":500,\"total_lines_added\":100,\"total_lines_removed\":50},\"exceeds_200k_tokens\":false}'" |
| 116 | + |
| 117 | +# Summary |
| 118 | +echo "" |
| 119 | +echo "========================================" |
| 120 | +TOTAL=$((PASS + FAIL)) |
| 121 | +echo "Results: $PASS/$TOTAL passed" |
| 122 | + |
| 123 | +if [[ "$FAIL" -gt 0 ]]; then |
| 124 | + echo -e "${RED}FAILED${NC}: $FAIL test(s) failed" |
| 125 | + echo "" |
| 126 | + echo "Valgrind detected memory issues. Please fix memory leaks before committing." |
| 127 | + exit 1 |
| 128 | +else |
| 129 | + echo -e "${GREEN}SUCCESS${NC}: All tests passed - no memory leaks detected!" |
| 130 | + exit 0 |
| 131 | +fi |
0 commit comments