github.com/psrajat/prototool@v1.3.0/etc/bin/cover.sh (about)

     1  #!/bin/bash
     2  
     3  set -euo pipefail
     4  
     5  DIR="$(cd "$(dirname "${0}")/../.." && pwd)"
     6  cd "${DIR}"
     7  
     8  if echo "${GOPATH}" | grep : >/dev/null; then
     9  	echo "error: GOPATH must be one directory, but has multiple directories separated by colons: ${GOPATH}" >&2
    10  	exit 1
    11  fi
    12  
    13  if ! which jq > /dev/null; then
    14    echo "error: jq must be installed to run code coverage" >&2
    15    exit 1
    16  fi
    17  
    18  COVER=cover
    19  ROOT_PKG=github.com/uber/prototool
    20  
    21  if [[ -d "$COVER" ]]; then
    22  	rm -rf "$COVER"
    23  fi
    24  mkdir -p "$COVER"
    25  
    26  ignorePkgs=""
    27  
    28  filterIgnorePkgs() {
    29    if [[ -z "${ignorePkgs}" ]]; then
    30      cat
    31    else
    32      grep -v "${ignorePkgs}"
    33    fi
    34  }
    35  
    36  # If a package directory has a .nocover file, don't count it when calculating
    37  # coverage.
    38  filter=""
    39  for pkg in "$@"; do
    40  	if [[ -f "$GOPATH/src/$pkg/.nocover" ]]; then
    41  		if [[ -n "$filter" ]]; then
    42  			ignorePkgs="$ignorePkgs\|"
    43  			filter="$filter, "
    44  		fi
    45  		ignorePkgs="$ignorePkgs$pkg/"
    46  		filter="$filter\"$pkg\": true"
    47  	fi
    48  done
    49  
    50  i=0
    51  commands_file="$(mktemp)"
    52  echo 'commands:' >> "${commands_file}"
    53  trap 'rm -rf "${commands_file}"' EXIT
    54  for pkg in "$@"; do
    55  	if ! ls "${GOPATH}/src/${pkg}" | grep _test\.go$ >/dev/null; then
    56  		continue
    57  	fi
    58  	i=$((i + 1))
    59  
    60  	extracoverpkg=""
    61  	if [[ -f "$GOPATH/src/$pkg/.extra-coverpkg" ]]; then
    62  		extracoverpkg=$( \
    63  			sed -e "s|^|$pkg/|g" < "$GOPATH/src/$pkg/.extra-coverpkg" \
    64  			| tr '\n' ',')
    65  	fi
    66  
    67  	coverpkg=$(go list -json "$pkg" | jq -r '
    68  		.Deps + .TestImports + .XTestImports
    69  		| . + ["'"$pkg"'"]
    70  		| unique
    71  		| map
    72  			( select(startswith("'"$ROOT_PKG"'"))
    73  			| select(contains("/vendor/") | not)
    74  			| select({'"$filter"'}[.] | not)
    75  			)
    76  		| join(",")
    77  	')
    78  	if [[ -n "$extracoverpkg" ]]; then
    79  		coverpkg="$extracoverpkg$coverpkg"
    80  	fi
    81  
    82  	args=""
    83  	if [[ -n "$coverpkg" ]]; then
    84  		args="-coverprofile $COVER/cover.${i}.out -covermode=count -coverpkg $coverpkg"
    85  	fi
    86  
    87    echo go test ${args} "${pkg}"
    88    go test ${args} "${pkg}" 2>&1 | grep -v 'warning: no packages being tested depend on'
    89  done
    90  
    91  rm -f coverage.txt
    92  
    93  # Merge cross-package coverage and then split the result into main and
    94  # experimental coverages.
    95  #
    96  # We ignore packages in the form "footest" and any mock files.
    97  gocovmerge "$COVER"/*.out \
    98  	| filterIgnorePkgs \
    99  	> coverage.txt