|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +set -x |
| 4 | + |
| 5 | +# Parse command line arguments |
| 6 | +OUTPUT_DIR="coverage-report" |
| 7 | +while [[ $# -gt 0 ]]; do |
| 8 | + case $1 in |
| 9 | + --output-dir) |
| 10 | + OUTPUT_DIR="$2" |
| 11 | + shift 2 |
| 12 | + ;; |
| 13 | + *) |
| 14 | + echo "Unknown option: $1" |
| 15 | + echo "Usage: $0 [--output-dir OUTPUT_DIRECTORY]" |
| 16 | + exit 1 |
| 17 | + ;; |
| 18 | + esac |
| 19 | +done |
| 20 | + |
| 21 | +# Check if we're in the root directory, if so change to fuzz |
| 22 | +if [ -d "fuzz" ]; then |
| 23 | + cd fuzz |
| 24 | +elif [ ! -f "Cargo.toml" ] || ! grep -q "fuzz" Cargo.toml 2>/dev/null; then |
| 25 | + echo "Error: Please run this script from the rust-lightning root directory or fuzz directory" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# Check if test_cases directory exists and has content |
| 30 | +show_corpus_message=false |
| 31 | +if [ ! -d "test_cases" ]; then |
| 32 | + show_corpus_message=true |
| 33 | +elif [ -z "$(find test_cases -name '*' -type f 2>/dev/null | head -1)" ]; then |
| 34 | + show_corpus_message=true |
| 35 | +fi |
| 36 | + |
| 37 | +if [ "$show_corpus_message" = true ]; then |
| 38 | + echo "Warning: No corpus found in test_cases directory." |
| 39 | + echo "Generating coverage report without fuzzing corpus." |
| 40 | + echo "" |
| 41 | + echo "To include fuzzing corpus coverage, create test_cases directories with your corpus:" |
| 42 | + echo " mkdir -p test_cases/{target_name}" |
| 43 | + echo " cp your_corpus_directory/* test_cases/{target_name}/" |
| 44 | + echo "" |
| 45 | + echo "Example:" |
| 46 | + echo " mkdir -p test_cases/base32" |
| 47 | + echo " cp /path/to/your/base32_corpus/* test_cases/base32/" |
| 48 | + echo "" |
| 49 | +fi |
| 50 | + |
| 51 | +# Create output directory if it doesn't exist |
| 52 | +mkdir -p "$OUTPUT_DIR" |
| 53 | + |
| 54 | +export RUSTFLAGS="--cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz" |
| 55 | + |
| 56 | +# dont run this command when running in CI |
| 57 | +if [ "$CI" != "true" ] && [ "$GITHUB_ACTIONS" != "true" ]; then |
| 58 | + cargo llvm-cov --html --ignore-filename-regex "fuzz/" --output-dir "$OUTPUT_DIR" |
| 59 | + |
| 60 | + # Check if coverage report was generated successfully |
| 61 | + # The report is generated in $OUTPUT_DIR/html/index.html when using --html --output-dir |
| 62 | + if [ ! -f "$OUTPUT_DIR/html/index.html" ]; then |
| 63 | + echo "Error: Failed to generate coverage report at $OUTPUT_DIR/html/index.html" |
| 64 | + echo "Contents of $OUTPUT_DIR:" |
| 65 | + ls -la "$OUTPUT_DIR" || echo "Directory $OUTPUT_DIR does not exist" |
| 66 | + if [ -d "$OUTPUT_DIR/html" ]; then |
| 67 | + echo "Contents of $OUTPUT_DIR/html:" |
| 68 | + ls -la "$OUTPUT_DIR/html" |
| 69 | + fi |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + echo "Coverage report generated in $OUTPUT_DIR/html/index.html" |
| 73 | +fi |
| 74 | + |
| 75 | +# Generate codecov JSON format if running in CI environment |
| 76 | +if [ "$CI" = "true" ] || [ "$GITHUB_ACTIONS" = "true" ]; then |
| 77 | + echo "CI environment detected, generating codecov JSON format..." |
| 78 | + cargo llvm-cov --codecov --ignore-filename-regex "fuzz/" --output-path "$OUTPUT_DIR/fuzz-codecov.json" |
| 79 | + |
| 80 | + if [ -f "$OUTPUT_DIR/fuzz-codecov.json" ] && [[ "$OUTPUT_DIR" == *"target/"* ]]; then |
| 81 | + TARGET_DIR="../target" |
| 82 | + cp "$OUTPUT_DIR/fuzz-codecov.json" "$TARGET_DIR/fuzz-codecov.json" |
| 83 | + echo "Fuzz codecov report copied to $TARGET_DIR/fuzz-codecov.json" |
| 84 | + fi |
| 85 | +fi |
| 86 | + |
| 87 | + |
| 88 | + |
0 commit comments