fedora-csb-system-manager
  1#!/usr/bin/env bash
  2# run-tests.sh - Run ERT tests for org-manager batch functions
  3# Copyright (C) 2025 Vincent Demeester
  4# Part of Claude Code Org skill
  5
  6set -euo pipefail
  7
  8# Configuration
  9SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 10TOOLS_DIR="$(dirname "$SCRIPT_DIR")"
 11TEST_FILE="$SCRIPT_DIR/batch-functions-test.el"
 12EMACS="${EMACS:-emacs}"
 13
 14# Colors
 15RED='\033[0;31m'
 16GREEN='\033[0;32m'
 17YELLOW='\033[1;33m'
 18NC='\033[0m' # No Color
 19
 20usage() {
 21    cat <<EOF
 22Usage: $0 [options]
 23
 24Run ERT tests for org-manager batch functions.
 25
 26Options:
 27  -v, --verbose     Verbose output (show all test details)
 28  -s, --selector    Run specific test selector (e.g., "test-org-batch-list-todos")
 29  -h, --help        Show this help message
 30
 31Examples:
 32  # Run all tests
 33  $0
 34
 35  # Run specific test
 36  $0 -s test-org-batch-list-todos
 37
 38  # Verbose output
 39  $0 -v
 40
 41Environment:
 42  EMACS            Path to emacs binary (default: emacs)
 43
 44EOF
 45    exit 0
 46}
 47
 48# Parse arguments
 49VERBOSE=0
 50SELECTOR=""
 51
 52while [[ $# -gt 0 ]]; do
 53    case "$1" in
 54        -v|--verbose)
 55            VERBOSE=1
 56            shift
 57            ;;
 58        -s|--selector)
 59            SELECTOR="$2"
 60            shift 2
 61            ;;
 62        -h|--help)
 63            usage
 64            ;;
 65        *)
 66            echo -e "${RED}Unknown option: $1${NC}"
 67            usage
 68            ;;
 69    esac
 70done
 71
 72# Check dependencies
 73if ! command -v "$EMACS" &> /dev/null; then
 74    echo -e "${RED}Error: Emacs not found${NC}"
 75    echo "Set EMACS environment variable or install emacs"
 76    exit 1
 77fi
 78
 79if [[ ! -f "$TEST_FILE" ]]; then
 80    echo -e "${RED}Error: Test file not found: $TEST_FILE${NC}"
 81    exit 1
 82fi
 83
 84echo -e "${YELLOW}Running org-manager tests...${NC}"
 85echo "Test file: $TEST_FILE"
 86echo "Emacs: $EMACS"
 87
 88if [[ -n "$SELECTOR" ]]; then
 89    echo "Selector: $SELECTOR"
 90fi
 91echo ""
 92
 93# Build emacs command
 94EMACS_CMD=(
 95    "$EMACS"
 96    --batch
 97    --no-init-file
 98    -L "$TOOLS_DIR"
 99    -l "$TOOLS_DIR/batch-functions.el"
100    -l "$TEST_FILE"
101)
102
103if [[ -n "$SELECTOR" ]]; then
104    # Run specific test
105    EMACS_CMD+=(--eval "(ert-run-tests-batch-and-exit '$SELECTOR)")
106else
107    # Run all tests
108    EMACS_CMD+=(-f ert-run-tests-batch-and-exit)
109fi
110
111# Run tests
112if [[ "$VERBOSE" == "1" ]]; then
113    "${EMACS_CMD[@]}" 2>&1
114    EXIT_CODE=$?
115else
116    # Capture output and show summary
117    OUTPUT=$("${EMACS_CMD[@]}" 2>&1)
118    EXIT_CODE=$?
119
120    # Show summary
121    echo "$OUTPUT" | tail -20
122fi
123
124echo ""
125if [[ $EXIT_CODE -eq 0 ]]; then
126    echo -e "${GREEN}✓ All tests passed!${NC}"
127else
128    echo -e "${RED}✗ Some tests failed${NC}"
129    if [[ "$VERBOSE" == "0" ]]; then
130        echo ""
131        echo "Run with -v for detailed output"
132    fi
133fi
134
135exit $EXIT_CODE