Skip to content

Commit c88f2a7

Browse files
committed
Add support for codecov
1 parent edeaec8 commit c88f2a7

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

.github/workflows/ci-build.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ jobs:
6868
run: mkdir -p ~/go/src/github.com/gitpod-io
6969
- name: Checkout code
7070
uses: actions/checkout@v2
71+
with:
72+
fetch-depth: 10
7173
- name: Create symlink in GOPATH
7274
run: ln -s $(pwd) ~/go/src/github.com/gitpod-io/leeway
7375
- name: Setup Golang
@@ -100,6 +102,8 @@ jobs:
100102
run: mkdir -p ~/go/src/github.com/gitpod-io
101103
- name: Checkout code
102104
uses: actions/checkout@v2
105+
with:
106+
fetch-depth: 10
103107
- name: Create symlink in GOPATH
104108
run: ln -s $(pwd) ~/go/src/github.com/gitpod-io/leeway
105109
- name: Setup Golang
@@ -126,7 +130,7 @@ jobs:
126130
- name: Checkout code
127131
uses: actions/checkout@v2
128132
with:
129-
fetch-depth: 0
133+
fetch-depth: 10
130134
- name: Create test-results directory
131135
run: |
132136
mkdir -p test-results

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ config:
139139
# [DEPRECATED: use buildCommand instead] A list of flags passed to `go build`. Useful for passing `ldflags`.
140140
buildFlags: []
141141
# Command that's executed to lint the code
142-
lintCommand: ["golangci-lint", "run]
142+
lintCommand: ["golangci-lint", "run"]
143143
```
144144
145145
### Yarn packages
@@ -213,7 +213,7 @@ This however can lead to subtle failure modes where a package built in one envir
213213

214214
To prevent such issues, leeway computes an _environment manifest_ which contains the versions of the tools used, as well as some platform information.
215215
The entries in that manifest depend on the package types used by that workspace, e.g. if only `Go` packages exist in the workspace, only `go version`, [GOOS and GOARCH](https://golang.org/pkg/runtime/#pkg-constants) will be part of the manifest.
216-
You can inspect a workspace's environment manifest using `leeway describe environment-manifest`.
216+
You can inspect a workspace's environment manifest using `leeway describe environment-manifest`.
217217

218218
You can add your own entries to a workspace's environment manifest in the `WORKSPACE.yaml` like so:
219219
```YAML

cmd/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func addBuildFlags(cmd *cobra.Command) {
169169
cmd.Flags().Bool("werft", false, "Produce werft CI compatible output")
170170
cmd.Flags().Bool("dont-test", false, "Disable all package-level tests (defaults to false)")
171171
cmd.Flags().UintP("max-concurrent-tasks", "j", uint(runtime.NumCPU()), "Limit the number of max concurrent build tasks - set to 0 to disable the limit")
172+
cmd.Flags().String("coverage-output-path", "", "Output path where test coverage file will be copied after running tests")
172173
}
173174

174175
func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemCache) {
@@ -263,6 +264,11 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
263264
log.Fatal(err)
264265
}
265266

267+
coverageOutputPath, _ := cmd.Flags().GetString("coverage-output-path")
268+
if coverageOutputPath != "" {
269+
_ = os.MkdirAll(coverageOutputPath, 0644)
270+
}
271+
266272
return []leeway.BuildOption{
267273
leeway.WithLocalCache(localCache),
268274
leeway.WithRemoteCache(remoteCache),
@@ -272,6 +278,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
272278
leeway.WithReporter(reporter),
273279
leeway.WithDontTest(dontTest),
274280
leeway.WithMaxConcurrentTasks(int64(maxConcurrentTasks)),
281+
leeway.WithCoverageOutputPath(coverageOutputPath),
275282
}, localCache
276283
}
277284

pkg/leeway/build.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"regexp"
1213
"strings"
1314
"sync"
1415

@@ -204,6 +205,7 @@ type buildOptions struct {
204205
BuildPlan io.Writer
205206
DontTest bool
206207
MaxConcurrentTasks int64
208+
CoverageOutputPath string
207209

208210
context *buildContext
209211
}
@@ -279,6 +281,14 @@ func WithMaxConcurrentTasks(n int64) BuildOption {
279281
}
280282
}
281283

284+
// WithCoverageOutputPath configures coverage output directory
285+
func WithCoverageOutputPath(output string) BuildOption {
286+
return func(opts *buildOptions) error {
287+
opts.CoverageOutputPath = output
288+
return nil
289+
}
290+
}
291+
282292
func withBuildContext(ctx *buildContext) BuildOption {
283293
return func(opts *buildOptions) error {
284294
opts.context = ctx
@@ -894,7 +904,7 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (err error)
894904
if cfg.Generate {
895905
commands = append(commands, []string{goCommand, "generate", "-v", "./..."})
896906
}
897-
commands = append(commands, []string{goCommand, "get", "-v", "./..."})
907+
898908
if !cfg.DontCheckGoFmt {
899909
commands = append(commands, []string{"sh", "-c", `if [ ! $(go fmt ./... | wc -l) -eq 0 ]; then echo; echo; echo please gofmt your code; echo; echo; exit 1; fi`})
900910
}
@@ -906,10 +916,17 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (err error)
906916
}
907917
}
908918
if !cfg.DontTest && !buildctx.DontTest {
919+
testArgs := []string{goCommand, "test", "-v"}
920+
if buildctx.buildOptions.CoverageOutputPath != "" {
921+
testArgs = append(testArgs, fmt.Sprintf("-coverprofile=%v", codecovComponentName(p.FullName())))
922+
}
923+
924+
testArgs = append(testArgs, "./...")
925+
909926
commands = append(commands, [][]string{
910927
// we build the test binaries in addition to running the tests regularly, so that downstream packages can run the tests in different environments
911928
{"sh", "-c", "mkdir _tests; for i in $(" + goCommand + " list ./...); do " + goCommand + " test -c $i; [ -e $(basename $i).test ] && mv $(basename $i).test _tests; true; done"},
912-
{goCommand, "test", "-v", "./..."},
929+
testArgs,
913930
}...)
914931
}
915932

@@ -929,6 +946,12 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (err error)
929946
{"tar", "cfz", result, "."},
930947
}...)
931948

949+
if !cfg.DontTest && !buildctx.DontTest {
950+
commands = append(commands, [][]string{
951+
{"sh", "-c", fmt.Sprintf(`[ -f "%v" ] && cp -f %v %v`, codecovComponentName(p.FullName()), codecovComponentName(p.FullName()), buildctx.buildOptions.CoverageOutputPath)},
952+
}...)
953+
}
954+
932955
return executeCommandsForPackage(buildctx, p, wd, commands)
933956
}
934957

@@ -1156,3 +1179,9 @@ func (s *reporterStream) Write(buf []byte) (n int, err error) {
11561179
s.R.PackageBuildLog(s.P, s.IsErr, buf)
11571180
return len(buf), nil
11581181
}
1182+
1183+
func codecovComponentName(name string) string {
1184+
reg, _ := regexp.Compile("[^a-zA-Z0-9]+")
1185+
component := reg.ReplaceAllString(name, "-")
1186+
return strings.ToLower(component + "-coverage.out")
1187+
}

pkg/leeway/build_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leeway
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCodecovComponentName(t *testing.T) {
8+
tests := []struct {
9+
Test string
10+
Package string
11+
Expected string
12+
}{
13+
{"valid package format", "components/ee/ws-scheduler", "components-ee-ws-scheduler-coverage.out"},
14+
{"lower case", "COMPONENTS/gitpod-cli:app", "components-gitpod-cli-app-coverage.out"},
15+
{"special character", "components/~ü:app", "components-app-coverage.out"},
16+
{"with numbers", "components/1icens0r:app", "components-1icens0r-app-coverage.out"},
17+
}
18+
19+
for _, test := range tests {
20+
name := codecovComponentName(test.Package)
21+
if name != test.Expected {
22+
t.Errorf("%s: expected: %v, actual: %v", test.Test, test.Expected, name)
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)