coverage_test.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_platform010.sh
  13. GCOV=$GCC_BASE/bin/gcov
  14. else
  15. GCOV=$(which gcov)
  16. fi
  17. echo -e "Using $GCOV"
  18. COVERAGE_DIR="$PWD/COVERAGE_REPORT"
  19. mkdir -p $COVERAGE_DIR
  20. # Find all gcno files to generate the coverage report
  21. PYTHON=${1:-`which python3`}
  22. echo -e "Using $PYTHON"
  23. GCNO_FILES=`find $ROOT -name "*.gcno"`
  24. $GCOV --preserve-paths --relative-only --no-output $GCNO_FILES 2>/dev/null |
  25. # Parse the raw gcov report to more human readable form.
  26. $PYTHON $ROOT/coverage/parse_gcov_output.py |
  27. # Write the output to both stdout and report file.
  28. tee $COVERAGE_DIR/coverage_report_all.txt &&
  29. echo -e "Generated coverage report for all files: $COVERAGE_DIR/coverage_report_all.txt\n"
  30. # TODO: we also need to get the files of the latest commits.
  31. # Get the most recently committed files.
  32. LATEST_FILES=`
  33. git show --pretty="format:" --name-only HEAD |
  34. grep -v "^$" |
  35. paste -s -d,`
  36. RECENT_REPORT=$COVERAGE_DIR/coverage_report_recent.txt
  37. echo -e "Recently updated files: $LATEST_FILES\n" > $RECENT_REPORT
  38. $GCOV --preserve-paths --relative-only --no-output $GCNO_FILES 2>/dev/null |
  39. $PYTHON $ROOT/coverage/parse_gcov_output.py -interested-files $LATEST_FILES |
  40. tee -a $RECENT_REPORT &&
  41. echo -e "Generated coverage report for recently updated files: $RECENT_REPORT\n"
  42. # Unless otherwise specified, we'll not generate html report by default
  43. if [ -z "$HTML" ]; then
  44. exit 0
  45. fi
  46. # Generate the html report. If we cannot find lcov in this machine, we'll simply
  47. # skip this step.
  48. echo "Generating the html coverage report..."
  49. LCOV=$(which lcov || true 2>/dev/null)
  50. if [ -z $LCOV ]
  51. then
  52. echo "Skip: Cannot find lcov to generate the html report."
  53. exit 0
  54. fi
  55. LCOV_VERSION=$(lcov -v | grep 1.1 || true)
  56. if [ $LCOV_VERSION ]
  57. then
  58. echo "Not supported lcov version. Expect lcov 1.1."
  59. exit 0
  60. fi
  61. (cd $ROOT; lcov --no-external \
  62. --capture \
  63. --directory $PWD \
  64. --gcov-tool $GCOV \
  65. --output-file $COVERAGE_DIR/coverage.info)
  66. genhtml $COVERAGE_DIR/coverage.info -o $COVERAGE_DIR
  67. echo "HTML Coverage report is generated in $COVERAGE_DIR"