From 1fa260022e73323d6d0461743d09ea3788b25a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Ma=C5=82ota-W=C3=B3jcik?= <59281144+outofforest@users.noreply.github.com> Date: Fri, 7 Jul 2023 12:16:23 +0200 Subject: [PATCH] Create coverage reports (#59) --- go.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/go.go b/go.go index 3bab8ee..d54911e 100644 --- a/go.go +++ b/go.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "github.com/outofforest/build" "github.com/outofforest/libexec" @@ -54,9 +55,34 @@ func GoLint(ctx context.Context, deps build.DepsFunc) error { func GoTest(ctx context.Context, deps build.DepsFunc) error { deps(EnsureGo) log := logger.Get(ctx) + + rootDir := must.String(filepath.EvalSymlinks(must.String(filepath.Abs("..")))) + repoDir := must.String(filepath.EvalSymlinks(must.String(filepath.Abs(".")))) + coverageDir := filepath.Join(repoDir, "bin", ".coverage") + if err := os.MkdirAll(coverageDir, 0o700); err != nil { + return errors.WithStack(err) + } + return onModule(func(path string) error { + relPath, err := filepath.Rel(rootDir, must.String(filepath.EvalSymlinks(must.String(filepath.Abs(path))))) + if err != nil { + return errors.WithStack(err) + } + coverageName := strings.ReplaceAll(relPath, "/", "-") + coverageFile := filepath.Join(coverageDir, coverageName) + log.Info("Running go tests", zap.String("path", path)) - cmd := exec.Command("go", "test", "-count=1", "-shuffle=on", "-race", "./...") + cmd := exec.Command( + "go", + "test", + "-count=1", + "-shuffle=on", + "-race", + "-cover", "./...", + "-coverpkg", "./...", + "-coverprofile", coverageFile, + "./...", + ) cmd.Dir = path if err := libexec.Exec(ctx, cmd); err != nil { return errors.Wrapf(err, "unit tests failed in module '%s'", path)