219 lines
5.6 KiB
Bash
Executable File
219 lines
5.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
show_help() {
|
|
cat <<'EOF'
|
|
sf-test-run — wrapper for focused Apex test execution
|
|
|
|
USAGE:
|
|
sf-test-run -o <ORG_ALIAS> (-c <CLASSES> | -s <SUITES> | -a) [-l <LEVEL>] [-r] [-w <WAIT>] [-h]
|
|
|
|
OPTIONS:
|
|
-o Org alias or username to run tests in (required)
|
|
-c Comma-separated test class names
|
|
-s Comma-separated test suite names
|
|
-a Run all tests in the org
|
|
-l Test level (RunLocalTests, RunAllTestsInOrg, RunSpecifiedTests)
|
|
-r Generate code coverage report
|
|
-w Wait time in minutes (default: 10)
|
|
-h Show this help
|
|
|
|
EXAMPLES:
|
|
1) Run specific test classes:
|
|
sf-test-run -o DEMO-ORG -c "MyTestClass,AnotherTestClass"
|
|
|
|
2) Run test suites:
|
|
sf-test-run -o DEMO-ORG -s "UnitTests,IntegrationTests"
|
|
|
|
3) Run all local tests with coverage:
|
|
sf-test-run -o DEMO-ORG -l RunLocalTests -r
|
|
|
|
4) Run all tests in org (be careful!):
|
|
sf-test-run -o DEMO-ORG -a -w 30
|
|
|
|
5) Quick test with custom wait time:
|
|
sf-test-run -o DEMO-ORG -c "QuickTest" -w 5
|
|
|
|
TEST LEVELS:
|
|
- RunLocalTests: Run all tests in your org except managed package tests
|
|
- RunAllTestsInOrg: Run all tests including managed package tests
|
|
- RunSpecifiedTests: Run only specified test classes/suites
|
|
|
|
Notes:
|
|
- Default wait time is 10 minutes
|
|
- Use -r to generate detailed coverage reports
|
|
- RunAllTestsInOrg can take a very long time
|
|
- Test results will be displayed in formatted output
|
|
EOF
|
|
}
|
|
|
|
# Default values
|
|
ORG=""
|
|
CLASSES=""
|
|
SUITES=""
|
|
ALL_TESTS=false
|
|
TEST_LEVEL=""
|
|
COVERAGE=false
|
|
WAIT_TIME=10
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
show_help
|
|
exit 0
|
|
fi
|
|
|
|
while getopts ":o:c:s:al:rw:h" opt; do
|
|
case "$opt" in
|
|
o) ORG="$OPTARG" ;;
|
|
c) CLASSES="$OPTARG" ;;
|
|
s) SUITES="$OPTARG" ;;
|
|
a) ALL_TESTS=true ;;
|
|
l) TEST_LEVEL="$OPTARG" ;;
|
|
r) COVERAGE=true ;;
|
|
w) WAIT_TIME="$OPTARG" ;;
|
|
h) show_help; exit 0 ;;
|
|
\?) echo "Unknown option: -$OPTARG" >&2; echo; show_help; exit 1 ;;
|
|
:) echo "Option -$OPTARG requires an argument." >&2; echo; show_help; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
# Validate required parameters
|
|
if [[ -z "$ORG" ]]; then
|
|
echo "Error: Org (-o) is required." >&2
|
|
echo
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
# Validate that at least one test method is specified
|
|
if [[ -z "$CLASSES" && -z "$SUITES" && "$ALL_TESTS" != "true" && -z "$TEST_LEVEL" ]]; then
|
|
echo "Error: Must specify -c (classes), -s (suites), -a (all tests), or -l (test level)." >&2
|
|
echo
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
# Validate wait time
|
|
if ! [[ "$WAIT_TIME" =~ ^[0-9]+$ ]] || [[ "$WAIT_TIME" -lt 1 ]]; then
|
|
echo "Error: Wait time must be a positive number." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Validate test level if specified
|
|
if [[ -n "$TEST_LEVEL" ]]; then
|
|
case "$TEST_LEVEL" in
|
|
"RunLocalTests"|"RunAllTestsInOrg"|"RunSpecifiedTests")
|
|
# Valid test levels
|
|
;;
|
|
*)
|
|
echo "Error: Invalid test level. Use RunLocalTests, RunAllTestsInOrg, or RunSpecifiedTests." >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
# Silent environment check
|
|
if ! command -v sf >/dev/null 2>&1; then
|
|
echo "❌ Salesforce CLI (sf) not found!"
|
|
echo
|
|
echo "Running environment check to help you get started..."
|
|
echo
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
if [[ -x "$SCRIPT_DIR/sf-check" ]]; then
|
|
"$SCRIPT_DIR/sf-check"
|
|
elif command -v sf-check >/dev/null 2>&1; then
|
|
sf-check
|
|
else
|
|
echo "sf-check not found. Please install the Salesforce CLI from:"
|
|
echo "https://developer.salesforce.com/tools/sfdxcli"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
# Build the command
|
|
CMD=(sf apex run test)
|
|
CMD+=(--target-org "$ORG")
|
|
CMD+=(--wait "$WAIT_TIME")
|
|
CMD+=(--result-format human)
|
|
|
|
# Determine test execution method
|
|
if [[ -n "$TEST_LEVEL" ]]; then
|
|
CMD+=(--test-level "$TEST_LEVEL")
|
|
elif [[ "$ALL_TESTS" == "true" ]]; then
|
|
CMD+=(--test-level "RunAllTestsInOrg")
|
|
elif [[ -n "$CLASSES" ]]; then
|
|
IFS=',' read -ra CLASSES_ARR <<< "$CLASSES"
|
|
for CLASS in "${CLASSES_ARR[@]}"; do
|
|
CLASS=$(echo "$CLASS" | xargs) # Trim whitespace
|
|
[[ -n "$CLASS" ]] && CMD+=(--tests "$CLASS")
|
|
done
|
|
elif [[ -n "$SUITES" ]]; then
|
|
IFS=',' read -ra SUITES_ARR <<< "$SUITES"
|
|
for SUITE in "${SUITES_ARR[@]}"; do
|
|
SUITE=$(echo "$SUITE" | xargs) # Trim whitespace
|
|
[[ -n "$SUITE" ]] && CMD+=(--suites "$SUITE")
|
|
done
|
|
fi
|
|
|
|
# Add code coverage if requested
|
|
if [[ "$COVERAGE" == "true" ]]; then
|
|
CMD+=(--code-coverage)
|
|
fi
|
|
|
|
# Show what we're running
|
|
echo "🧪 Running Apex tests in org '$ORG'..."
|
|
if [[ -n "$TEST_LEVEL" ]]; then
|
|
echo " Level: $TEST_LEVEL"
|
|
elif [[ "$ALL_TESTS" == "true" ]]; then
|
|
echo " Level: RunAllTestsInOrg (all tests)"
|
|
elif [[ -n "$CLASSES" ]]; then
|
|
echo " Classes: $CLASSES"
|
|
elif [[ -n "$SUITES" ]]; then
|
|
echo " Suites: $SUITES"
|
|
fi
|
|
echo " Wait time: ${WAIT_TIME} minutes"
|
|
[[ "$COVERAGE" == "true" ]] && echo " Code coverage: enabled"
|
|
echo
|
|
|
|
echo ">>> Running: ${CMD[*]}"
|
|
echo
|
|
|
|
# Create a temporary file to capture output
|
|
TEMP_OUTPUT=$(mktemp)
|
|
|
|
if "${CMD[@]}" 2>&1 | tee "$TEMP_OUTPUT"; then
|
|
echo
|
|
echo "✅ Test execution completed!"
|
|
|
|
# Parse and display summary from output
|
|
if grep -q "Test Results" "$TEMP_OUTPUT"; then
|
|
echo
|
|
echo "📊 Test Summary:"
|
|
grep -A 20 "Test Results" "$TEMP_OUTPUT" | head -20
|
|
fi
|
|
|
|
# Show coverage summary if coverage was requested
|
|
if [[ "$COVERAGE" == "true" ]] && grep -q "Coverage" "$TEMP_OUTPUT"; then
|
|
echo
|
|
echo "📈 Coverage Summary:"
|
|
grep -A 10 -B 2 "Coverage" "$TEMP_OUTPUT"
|
|
fi
|
|
|
|
# Check for failures
|
|
if grep -q "FAIL" "$TEMP_OUTPUT" || grep -q "Error" "$TEMP_OUTPUT"; then
|
|
echo
|
|
echo "⚠️ Some tests failed. Check the output above for details."
|
|
rm -f "$TEMP_OUTPUT"
|
|
exit 1
|
|
fi
|
|
|
|
else
|
|
echo
|
|
echo "❌ Test execution failed"
|
|
rm -f "$TEMP_OUTPUT"
|
|
exit 1
|
|
fi
|
|
|
|
# Clean up
|
|
rm -f "$TEMP_OUTPUT"
|