coverage_test.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. # Exit on error.
  4. set -e
  5. if [ -n "$USE_CLANG" ]; then
  6. echo "Error: Coverage test is supported only for gcc."
  7. exit 1
  8. fi
  9. ROOT=".."
  10. # Fetch right version of gcov
  11. if [ -d /mnt/gvfs/third-party -a -z "$CXX" ]; then
  12. source $ROOT/build_tools/fbcode_config.sh
  13. GCOV=$GCC_BASE/bin/gcov
  14. else
  15. GCOV=$(which gcov)
  16. fi
  17. COVERAGE_DIR="$PWD/COVERAGE_REPORT"
  18. mkdir -p $COVERAGE_DIR
  19. # Find all gcno files to generate the coverage report
  20. GCNO_FILES=`find $ROOT -name "*.gcno"`
  21. $GCOV --preserve-paths --relative-only --no-output $GCNO_FILES 2>/dev/null |
  22. # Parse the raw gcov report to more human readable form.
  23. python $ROOT/coverage/parse_gcov_output.py |
  24. # Write the output to both stdout and report file.
  25. tee $COVERAGE_DIR/coverage_report_all.txt &&
  26. echo -e "Generated coverage report for all files: $COVERAGE_DIR/coverage_report_all.txt\n"
  27. # TODO: we also need to get the files of the latest commits.
  28. # Get the most recently committed files.
  29. LATEST_FILES=`
  30. git show --pretty="format:" --name-only HEAD |
  31. grep -v "^$" |
  32. paste -s -d,`
  33. RECENT_REPORT=$COVERAGE_DIR/coverage_report_recent.txt
  34. echo -e "Recently updated files: $LATEST_FILES\n" > $RECENT_REPORT
  35. $GCOV --preserve-paths --relative-only --no-output $GCNO_FILES 2>/dev/null |
  36. python $ROOT/coverage/parse_gcov_output.py -interested-files $LATEST_FILES |
  37. tee -a $RECENT_REPORT &&
  38. echo -e "Generated coverage report for recently updated files: $RECENT_REPORT\n"
  39. # Unless otherwise specified, we'll not generate html report by default
  40. if [ -z "$HTML" ]; then
  41. exit 0
  42. fi
  43. # Generate the html report. If we cannot find lcov in this machine, we'll simply
  44. # skip this step.
  45. echo "Generating the html coverage report..."
  46. LCOV=$(which lcov || true 2>/dev/null)
  47. if [ -z $LCOV ]
  48. then
  49. echo "Skip: Cannot find lcov to generate the html report."
  50. exit 0
  51. fi
  52. LCOV_VERSION=$(lcov -v | grep 1.1 || true)
  53. if [ $LCOV_VERSION ]
  54. then
  55. echo "Not supported lcov version. Expect lcov 1.1."
  56. exit 0
  57. fi
  58. (cd $ROOT; lcov --no-external \
  59. --capture \
  60. --directory $PWD \
  61. --gcov-tool $GCOV \
  62. --output-file $COVERAGE_DIR/coverage.info)
  63. genhtml $COVERAGE_DIR/coverage.info -o $COVERAGE_DIR
  64. echo "HTML Coverage report is generated in $COVERAGE_DIR"