| #!/usr/bin/env bash |
| # Copyright 2022 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| #### CATEGORY=Test |
| ### Runs test and exports coverage data. |
| |
| ## usage: fx coverage [--lcov-output-path <path>] [--html-output-dir <directory>] [-h|--help] [testName ...] |
| ## |
| ## Runs test and exports coverage data. |
| ## |
| ## --html-output-dir: directory to output test coverage HTML to |
| ## --lcov-output-path: path to write output lcov to |
| ## --test-output-dir: directory to use as test output, skipping running fx test |
| ## -v|--verbose: print all executed commands for debugging |
| ## -h|--help: prints help message |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/vars.sh || exit $? |
| fx-config-read |
| |
| # Turn on checking after reading config. Otherwise unbound variables in the config read |
| # can cause the script to fail. |
| set -euo pipefail |
| |
| function main() { |
| local lcov_output_path='' |
| local html_output_dir='' |
| local verbose=false |
| local test_output_dir='' |
| |
| while [[ $# -ge 1 ]]; do |
| case "$1" in |
| --lcov-output-path) |
| shift |
| # Get absolute path because `ffx coverage` is executed in a different |
| # dir. |
| lcov_output_path="$(realpath ${1})" |
| ;; |
| --html-output-dir) |
| shift |
| # Get absolute path because `ffx coverage` is executed in a different |
| # dir. |
| html_output_dir="$(realpath ${1})" |
| ;; |
| --test-output-dir) |
| shift |
| test_output_dir="$(realpath ${1})" |
| ;; |
| -v|--verbose) |
| verbose=true |
| ;; |
| -h|--help) |
| fx-command-help |
| exit 0 |
| ;; |
| *) |
| break |
| ;; |
| esac |
| shift |
| done |
| |
| if [[ -z "${test_output_dir}" ]]; then |
| test_output_dir="$(mktemp -d)" |
| trap "rm -rf ${test_output_dir}" EXIT |
| |
| echo "Running tests to collect coverage data ..." |
| |
| # Use `fxtest` to run test, which knows to how to fuzzy match test names, plus |
| # rebuild targets when necessary. |
| fx-command-run test --device --no-timestamp-artifacts \ |
| --ffx-output-directory "${test_output_dir}" "${@}" |
| else |
| echo "Using existing test output directory: ${test_output_dir}" |
| fi |
| |
| |
| echo "Exporting coverage data ..." |
| |
| local ffx_cov_args= |
| if [[ -n "${lcov_output_path}" ]]; then |
| ffx_cov_args+=" --export-lcov ${lcov_output_path}" |
| fi |
| if [[ -n "${html_output_dir}" ]]; then |
| ffx_cov_args+=" --export-html ${html_output_dir}" |
| fi |
| if [[ ${verbose} == true ]]; then |
| ffx_cov_args+=" --verbose" |
| fi |
| |
| # TODO(https://fxbug.dev/42054992): realpath is not available on Mac, use a |
| # platform-neutral implementation instead. |
| readonly _REL_BUILD_DIR="$(realpath -s --relative-to="${FUCHSIA_BUILD_DIR}" "${FUCHSIA_DIR}")" |
| |
| readonly start_time=$SECONDS |
| fx-command-run host-tool ffx \ |
| --config 'coverage=true' \ |
| coverage \ |
| --clang-dir "${PREBUILT_CLANG_DIR}" \ |
| --test-output-dir "${test_output_dir}" ${ffx_cov_args[@]} \ |
| --path-remappings "${_REL_BUILD_DIR},${FUCHSIA_DIR}" \ |
| --compilation-dir "${FUCHSIA_BUILD_DIR}" |
| readonly duration=$((SECONDS - start_time)) |
| echo "Coverage exported in ${duration} seconds." |
| |
| if [[ -n "${lcov_output_path}" ]]; then |
| echo "Coverage LCOV exported to ${lcov_output_path}" |
| fi |
| if [[ -n "${html_output_dir}" ]]; then |
| echo "Coverage HTML exported to ${html_output_dir}" |
| fi |
| } |
| |
| main $@ |