about summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/haskell/private/coverage_wrapper.sh.tpl
blob: 8bc12187f4e84b108e3dd03b0f4bf3ab24cc66d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
# A wrapper for Haskell binaries which have been instrumented for hpc code coverage.

# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
set -euo pipefail
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
  if [[ -f "$0.runfiles_manifest" ]]; then
    export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
  elif [[ -f "$0.runfiles/MANIFEST" ]]; then
    export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
  elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
    export RUNFILES_DIR="$0.runfiles"
  fi
fi
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
  exit 1
fi
# --- end runfiles.bash initialization ---

ERRORCOLOR='\033[1;31m'
CLEARCOLOR='\033[0m'
binary_path=$(rlocation {binary_path})
hpc_path=$(rlocation {hpc_path})
tix_file_path={tix_file_path}
coverage_report_format={coverage_report_format}
strict_coverage_analysis={strict_coverage_analysis}
package_path={package_path}

# either of the two expected coverage metrics should be set to -1 if they're meant to be unused
expected_covered_expressions_percentage={expected_covered_expressions_percentage}
expected_uncovered_expression_count={expected_uncovered_expression_count}

# gather the hpc directories
hpc_dir_args=""
mix_file_paths={mix_file_paths}
for m in "${mix_file_paths[@]}"
do
  absolute_mix_file_path=$(rlocation $m)
  hpc_parent_dir=$(dirname $absolute_mix_file_path)
  trimmed_hpc_parent_dir=$(echo "${hpc_parent_dir%%.hpc*}")
  hpc_dir_args="$hpc_dir_args --hpcdir=$trimmed_hpc_parent_dir.hpc"
done

# gather the modules to exclude from the coverage analysis
hpc_exclude_args=""
modules_to_exclude={modules_to_exclude}
for m in "${modules_to_exclude[@]}"
do
  hpc_exclude_args="$hpc_exclude_args --exclude=$m"
done

# run the test binary, and then generate the report
$binary_path "$@" > /dev/null 2>&1
$hpc_path report "$tix_file_path" $hpc_dir_args $hpc_exclude_args \
  --srcdir "." --srcdir "$package_path" > __hpc_coverage_report

# if we want a text report, just output the file generated in the previous step
if [ "$coverage_report_format" == "text" ]
then
  echo "Overall report"
  cat __hpc_coverage_report
fi

# check the covered expression percentage, and if it matches our expectations
if [ "$expected_covered_expressions_percentage" -ne -1 ]
then
  covered_expression_percentage=$(grep "expressions used" __hpc_coverage_report | cut -c 1-3)
  if [ "$covered_expression_percentage" -lt "$expected_covered_expressions_percentage" ]
  then
    echo -e "\n==>$ERRORCOLOR Inadequate expression coverage percentage.$CLEARCOLOR"
    echo -e "==> Expected $expected_covered_expressions_percentage%, but the actual coverage was $ERRORCOLOR$(($covered_expression_percentage))%$CLEARCOLOR.\n"
    exit 1
  elif [ "$strict_coverage_analysis" == "True" ] && [ "$covered_expression_percentage" -gt "$expected_covered_expressions_percentage" ]
  then
    echo -e "\n==>$ERRORCOLOR ** BECAUSE STRICT COVERAGE ANALYSIS IS ENABLED **$CLEARCOLOR"
    echo -e "==> Your coverage percentage is now higher than expected.$CLEARCOLOR"
    echo -e "==> Expected $expected_covered_expressions_percentage% of expressions covered, but the actual value is $ERRORCOLOR$(($covered_expression_percentage))%$CLEARCOLOR."
    echo -e "==> Please increase the expected coverage percentage to match.\n"
    exit 1
  fi
fi

# check how many uncovered expressions there are, and if that number matches our expectations
if [ "$expected_uncovered_expression_count" -ne -1 ]
then
  coverage_numerator=$(grep "expressions used" __hpc_coverage_report | sed s:.*\(::g | cut -f1 -d "/")
  coverage_denominator=$(grep "expressions used" __hpc_coverage_report | sed s:.*/::g | cut -f1 -d ")")
  uncovered_expression_count="$(($coverage_denominator - $coverage_numerator))"
  if [ "$uncovered_expression_count" -gt "$expected_uncovered_expression_count" ]
  then
    echo -e "\n==>$ERRORCOLOR Too many uncovered expressions.$CLEARCOLOR"
    echo -e "==> Expected $expected_uncovered_expression_count uncovered expressions, but the actual count was $ERRORCOLOR$(($uncovered_expression_count))$CLEARCOLOR.\n"
    exit 1
  elif [ "$strict_coverage_analysis" == "True" ] && [ "$uncovered_expression_count" -lt "$expected_uncovered_expression_count" ]
  then
    echo -e "\n==>$ERRORCOLOR ** BECAUSE STRICT COVERAGE ANALYSIS IS ENABLED **$CLEARCOLOR"
    echo -e "==>$ERRORCOLOR Your uncovered expression count is now lower than expected.$CLEARCOLOR"
    echo -e "==> Expected $expected_uncovered_expression_count uncovered expressions, but there is $ERRORCOLOR$(($uncovered_expression_count))$CLEARCOLOR."
    echo -e "==> Please lower the expected uncovered expression count to match.\n"
    exit 1
  fi
fi

# if we want an html report, run the hpc binary again with the "markup" command,
# and feed its generated files into stdout, wrapped in XML tags
if [ "$coverage_report_format" == "html" ]
then
  $hpc_path markup "$tix_file_path" $hpc_dir_args $hpc_exclude_args \
    --srcdir "." --srcdir "$package_path" --destdir=hpc_out > /dev/null 2>&1
  cd hpc_out
  echo "COVERAGE REPORT BELOW"
  echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
  for file in *.html **/*.hs.html; do
    [ -e "$file" ] || continue
    echo "<coverage-report-part name=\"$file\">"
    echo '<![CDATA['
    cat $file
    echo ']]>'
    echo "</coverage-report-part>"
  done
  echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
fi