File: coverage

package info (click to toggle)
hub 2.14.2~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,376 kB
  • sloc: sh: 1,049; ruby: 857; makefile: 89
file content (75 lines) | stat: -rwxr-xr-x 1,926 bytes parent folder | download | duplicates (2)
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
#!/bin/bash
set -e

source_files() {
  script/build files | grep -vE '^\./(coverage|fixtures|version)/'
}

prepare() {
  local changed_files="$(source_files | xargs git diff --name-only --)"
  if [ -n "$changed_files" ]; then
    echo "Aborted: please commit the following files before continuing" >&2
    cat <<<"$changed_files" >&2
    exit 1
  fi

  local n=0
  for f in $(source_files); do
    go tool cover -mode=set -var="LiveCoverage$((++n))" "$f" > "$f"~
    sed -E '
      /^package /a\
      import "github.com/github/hub/coverage"
      s/(LiveCoverage[0-9]+)\.Count\[([0-9]+)\][^;]+/coverage.Record(\1, \2)/g
    ' < "$f"~ > "$f"
    rm "$f"~
  done

  rm -rf "$HUB_COVERAGE"
  mkdir -p "${HUB_COVERAGE%/*}"
}

generate() {
  source_files | xargs git checkout --

  echo 'mode: count' > "$HUB_COVERAGE"~
  sed -E 's!^.+/(github.com/github/hub/)!\1!' "$HUB_COVERAGE" | awk '
    { a[substr($0, 0, length()-2)] += $(NF) }
    END { for (k in a) print k, a[k] }
  ' >> "$HUB_COVERAGE"~

  go tool cover -func="$HUB_COVERAGE"~ > "${HUB_COVERAGE%.out}.func"
  if [ -z "$CI" ]; then
    go tool cover -html="$HUB_COVERAGE"~ -o "${HUB_COVERAGE%.out}.html"
  fi

  awk '/^total:/ { print $(NF) }' "${HUB_COVERAGE%.out}.func"
}

summarize() {
  local total_coverage
  local min_coverage="${1?}"
  total_coverage="$(generate)"
  echo "Code coverage: $total_coverage"
  local result="$(bc <<<"${total_coverage%\%} < $min_coverage")"
  if [ "$result" -eq 1 ]; then
    echo "Error: coverage dropped below the minimum treshold of ${min_coverage}%!"
    if [ -n "$CI" ]; then
      html_result="${HUB_COVERAGE%.out}.html"
      html_result="${html_result#$PWD/}"
      printf 'Please run `script/test --coverage` locally and open `%s` to analyze the results.\n' "$html_result"
    fi
    return 1
  fi
}

cmd="${1?}"
shift 1

case "$cmd" in
  prepare | generate | summarize )
    "$cmd" "$@"
    ;;
  * )
    exit 1
    ;;
esac