Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/gocover/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ When run without any flags and arguments, `gocover` will run the unit tests
using `go test` on all packages in the Go module in the workspace, and update
the `README.md` file with a badge showing the coverage percentage.

Coverage is always collected across all packages of the Go module (except for
the `uncovered-packages`), so code exercised only by the tests of other
packages counts as covered, too.

### `gocover` CLI Flags

| Flag | Meaning |
Expand Down
4 changes: 4 additions & 0 deletions src/gocover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ When run without any flags and arguments, `gocover` will run the unit tests
using `go test` on all packages in the Go module in the workspace, and update
the `README.md` file with a badge showing the coverage percentage.

Coverage is always collected across all packages of the Go module (except for
the `uncovered-packages`), so code exercised only by the tests of other
packages counts as covered, too.

### `gocover` CLI Flags

| Flag | Meaning |
Expand Down
2 changes: 1 addition & 1 deletion src/gocover/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Go Coverage with Badge",
"id": "gocover",
"version": "1.0.3",
"version": "1.1.0",
"description": "a gocover command to run Go unit tests with coverages, updating the README.md with a coverage badge.",
"keywords": [
"go",
Expand Down
56 changes: 28 additions & 28 deletions src/gocover/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,45 +154,45 @@ if [[ ${#POSARGS[@]} -eq 0 ]]; then
POSARGS+=("./...")
fi

# Determine packages included in coverage.
COVERPKG=()

if (( ${#IGNORE_PKGS[@]} > 0 )); then

# Discover packages and propagate go list failures.
if ! PKG_OUTPUT="$(go list ./...)"; then
echo "ERROR: Failed to discover Go packages." >&2
exit 1
fi

ALL_PKGS=()
mapfile -t ALL_PKGS <<< "${PKG_OUTPUT}"
# Determine packages included in coverage: always all packages of the module
# (minus the uncovered ones), so that code exercised by the tests of other
# packages counts as covered too.
LIST_ARGS=()
[[ -n "${TAGS}" ]] && LIST_ARGS+=("-tags=${TAGS}")

# Discover packages and propagate go list failures.
if ! PKG_OUTPUT="$(go list "${LIST_ARGS[@]}" ./...)"; then
echo "ERROR: Failed to discover Go packages." >&2
exit 1
fi

COVERPKG_LIST=()
ALL_PKGS=()
mapfile -t ALL_PKGS <<< "${PKG_OUTPUT}"

for pkg in "${ALL_PKGS[@]}"; do
excluded=false
COVERPKG_LIST=()

for ignored in "${IGNORE_PKGS[@]}"; do
if [[ "${pkg}" == "${ignored}" ]]; then
excluded=true
break
fi
done
for pkg in "${ALL_PKGS[@]}"; do
excluded=false

if [[ "${excluded}" == false ]]; then
COVERPKG_LIST+=("${pkg}")
for ignored in "${IGNORE_PKGS[@]}"; do
if [[ "${pkg}" == "${ignored}" ]]; then
excluded=true
break
fi
done

if (( ${#COVERPKG_LIST[@]} == 0 )); then
echo "ERROR: No packages remain for coverage." >&2
exit 1
if [[ "${excluded}" == false ]]; then
COVERPKG_LIST+=("${pkg}")
fi
done

COVERPKG+=("-coverpkg=$(IFS=,; echo "${COVERPKG_LIST[*]}")")
if (( ${#COVERPKG_LIST[@]} == 0 )); then
echo "ERROR: No packages remain for coverage." >&2
exit 1
fi

COVERPKG=("-coverpkg=$(IFS=,; echo "${COVERPKG_LIST[*]}")")

# Create temporary coverage directory.
GOCOVERTMPDIR="$(mktemp -d)"
trap 'rm -rf -- "${GOCOVERTMPDIR}"' EXIT
Expand Down
57 changes: 57 additions & 0 deletions test/gocover/cross-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -e

source dev-container-features-test-lib

go mod init example.com/foo

mkdir -p bar

cat >bar/bar.go <<EOF
package bar

func Barred() {
println("Barred")
}

func Bazzed() {
println("Bazzed")
}
EOF

# bar's own test leaves Barred uncovered, only foo's test covers it.
cat >bar/bar_test.go <<EOF
package bar

import "testing"

func TestBar(t *testing.T) {
Bazzed()
}
EOF

cat >foo.go <<EOF
package foo

import "example.com/foo/bar"

func fooed() {
bar.Barred()
}
EOF

cat >foo_test.go <<EOF
package foo

import "testing"

func TestFoo(t *testing.T) {
fooed()
}
EOF

touch README.md

check "counts coverage of packages exercised only by other packages' tests" bash -c "gocover | tee /dev/tty | grep --color 'badge updated to 100.0%'"

reportResults
7 changes: 7 additions & 0 deletions test/gocover/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@
"uncovered-packages": "example.com/foo/bar example.com/foo/baz"
}
}
},
"cross-package": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"ghcr.io/devcontainers/features/go:1": {},
"gocover": {}
}
}
}
Loading