Skip to content

Commit

Permalink
Add build tags for coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola committed May 5, 2023
1 parent 280b7d4 commit dae5f7b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
32 changes: 32 additions & 0 deletions pkg/server/cover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build cover

package server

import (
"context"
"os"
"runtime/coverage"
"time"

"github.com/sirupsen/logrus"
)

// writeCoverage checks if GOCOVERDIR is set on startup and writes coverage files to that directory
// every 20 seconds. This is done to ensure that the coverage files are written even if the process is killed.
func writeCoverage(ctx context.Context) {
if k, ok := os.LookupEnv("GOCOVERDIR"); ok {
for {
select {
case <-ctx.Done():
if err := coverage.WriteCountersDir(k); err != nil {
logrus.Warn(err)
}
return
case <-time.After(20 * time.Second):
if err := coverage.WriteCountersDir(k); err != nil {
logrus.Warn(err)
}
}
}
}
}
7 changes: 7 additions & 0 deletions pkg/server/no_cover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !cover

package server

import "context"

func writeCoverage(ctx context.Context) {}
21 changes: 0 additions & 21 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path"
"path/filepath"
"runtime/coverage"
"runtime/debug"
"strconv"
"strings"
Expand Down Expand Up @@ -99,26 +98,6 @@ func startOnAPIServerReady(ctx context.Context, config *Config) {
}
}

// writeCoverage checks if GOCOVERDIR is set on startup and writes coverage files to that directory
// every 20 seconds. This is done to ensure that the coverage files are written even if the process is killed.
func writeCoverage(ctx context.Context) {
if k, ok := os.LookupEnv("GOCOVERDIR"); ok {
for {
select {
case <-ctx.Done():
if err := coverage.WriteCountersDir(k); err != nil {
logrus.Warn(err)
}
return
case <-time.After(20 * time.Second):
if err := coverage.WriteCountersDir(k); err != nil {
logrus.Warn(err)
}
}
}
}
}

func runControllers(ctx context.Context, config *Config) error {
controlConfig := &config.ControlConfig

Expand Down
8 changes: 5 additions & 3 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ VERSIONFLAGS="
-X ${PKG_ETCD}/api/version.GitSHA=HEAD
"
if [ -n "${GOCOVER}" ]; then
BLDFLAGS="-cover"
fi
if [ -n "${DEBUG}" ]; then
GCFLAGS="-N -l"
else
Expand All @@ -83,6 +80,11 @@ else
TAGS="static_build libsqlite3 $TAGS"
fi

if [ -n "${GOCOVER}" ]; then
BLDFLAGS="-cover"
TAGS="cover $TAGS"
fi

mkdir -p bin

if [ ${ARCH} = armv7l ] || [ ${ARCH} = arm ]; then
Expand Down

0 comments on commit dae5f7b

Please sign in to comment.