From e2204d007199a60e2adf7fc7ba6e720405e3e711 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Thu, 14 Feb 2019 23:38:47 +0100 Subject: [PATCH] build: Add option to get test coverage (#5539) --- .codecov.yml | 5 +++++ build.go | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 .codecov.yml diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 000000000..fdaa59049 --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,5 @@ +coverage: + range: "40...100" + +ignore: + - "**.pb.go" diff --git a/build.go b/build.go index 304642458..c54d89251 100644 --- a/build.go +++ b/build.go @@ -48,6 +48,7 @@ var ( pkgdir string cc string debugBinary bool + coverage bool timeout = "120s" gogoProtoVersion = "v1.2.0" ) @@ -330,24 +331,27 @@ func parseFlags() { flag.StringVar(&pkgdir, "pkgdir", "", "Set -pkgdir parameter for `go build`") flag.StringVar(&cc, "cc", os.Getenv("CC"), "Set CC environment variable for `go build`") flag.BoolVar(&debugBinary, "debug-binary", debugBinary, "Create unoptimized binary to use with delve, set -gcflags='-N -l' and omit -ldflags") + flag.BoolVar(&coverage, "coverage", coverage, "Write coverage profile of tests to coverage.txt") flag.Parse() } func test(pkgs ...string) { lazyRebuildAssets() - useRace := runtime.GOARCH == "amd64" - switch runtime.GOOS { - case "darwin", "linux", "freebsd": // , "windows": # See https://github.com/golang/go/issues/27089 - default: - useRace = false + args := []string{"test", "-short", "-timeout", timeout, "-tags", "purego"} + + if runtime.GOARCH == "amd64" { + switch runtime.GOOS { + case "darwin", "linux", "freebsd": // , "windows": # See https://github.com/golang/go/issues/27089 + args = append(args, "-race") + } } - if useRace { - runPrint(goCmd, append([]string{"test", "-short", "-race", "-timeout", timeout, "-tags", "purego"}, pkgs...)...) - } else { - runPrint(goCmd, append([]string{"test", "-short", "-timeout", timeout, "-tags", "purego"}, pkgs...)...) + if coverage { + args = append(args, "-covermode", "atomic", "-coverprofile", "coverage.txt") } + + runPrint(goCmd, append(args, pkgs...)...) } func bench(pkgs ...string) {