/ maint / coverage
coverage
  1  #!/usr/bin/env bash
  2  
  3  function usage()
  4  {
  5      cat <<EOF
  6  $(basename "$0"): Generate coverage using grcov.
  7  
  8  Usage:
  9    coverage [opts] [suites...] : Run the provided test suites.
 10  
 11  Options:
 12    -h: Print this message.
 13  
 14  Suites:
 15    "unit": equivalent to cargo test --all-features
 16    "integration": a simple integration test with a chutney network.
 17    "all": enables all suites.
 18  
 19  Notes:
 20    You need to have grcov and llvm-tools installed.
 21    You also need the python packages bs4 and lxml for generating the HTML.
 22    For integration tests, you'll need chutney and tor.
 23  EOF
 24  }
 25  
 26  set -euo pipefail
 27  
 28  TOPDIR=$(dirname "$0")/..
 29  cd "$TOPDIR"
 30  
 31  UNIT=no
 32  INTEGRATION=no
 33  
 34  while getopts "h" opt ; do
 35      case "$opt" in
 36  	h) usage
 37  	   exit 0
 38  	   ;;
 39  	*) echo "Unknown option. (Run '$0 -h' for help.)"
 40  	   exit 1
 41  	   ;;
 42      esac
 43  done
 44  
 45  # Remove the parsed flags.
 46  shift $((OPTIND-1))
 47  
 48  for suite in "$@"; do
 49      case "$suite" in
 50  	unit) UNIT=yes
 51  	      ;;
 52  	integration) INTEGRATION=yes
 53  		     ;;
 54  	all) UNIT=yes
 55  	     INTEGRATION=yes
 56  	     ;;
 57  	*) echo "Unrecognized test suite '$suite'. (Run '$0 -h' for help.)"
 58  	   exit 1
 59  	   ;;
 60      esac
 61  done
 62  
 63  if [ "$UNIT" = no ] && [ "$INTEGRATION" = no ]; then
 64      echo "No test suites listed; nothing will be done. (Run '$0 -h' for help.)"
 65      exit 1
 66  fi
 67  
 68  # Clear the old coverage report and profiling data.
 69  rm -rf coverage coverage_meta_{unit,integration}
 70  mkdir coverage
 71  echo '<a href="all">all</a><br/>' > coverage/index.html
 72  
 73  if [ "$UNIT" = yes ] ; then
 74      # Run the unit tests, with coverage.
 75      ./maint/with_coverage -o coverage/unit cargo test --all-features
 76      mv coverage_meta coverage_meta_unit
 77      echo '<a href="unit">unit</a><br/>' >> coverage/index.html
 78  fi
 79  
 80  if [ "$INTEGRATION" = yes ] ; then
 81      # Run the integration tests, with coverage.
 82      #
 83      # (This is just a basic test that uses curl over Arti over a
 84      # chutney network. It's taken from the gitlab-ci tests.)
 85  
 86      # TODO: we might want, at some point, to have a the following stuff
 87      # go into a basic extensible integration-testing script that gets
 88      # run both from here and from the .gitlab-ci.yml file.
 89      trap ./tests/chutney/teardown 0
 90      ./maint/with_coverage -o coverage/integration -s ./tests/chutney/setup proxy
 91      curl http://example.com -vs --socks5-hostname 127.0.0.1:9150 -o /dev/null
 92      trap - 0
 93      ./tests/chutney/teardown
 94      # Report is generated after teardown because chutney/setup returns before any
 95      # test was done, so the report would be generated based on incomplete data.
 96      ./maint/with_coverage -o coverage/integration -c true
 97      mv coverage_meta coverage_meta_integration
 98      echo '<a href="integration">integration</a><br/>' >> coverage/index.html
 99  fi
100  
101  # Generate merged coverage report.
102  mkdir coverage_meta
103  cat coverage_meta_*/commands > coverage_meta/commands
104  mv coverage_meta_* coverage_meta
105  
106  ./maint/with_coverage -o coverage/all -c true
107