Skip to content

Commit

Permalink
ci: enable coverage (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Apr 28, 2022
1 parent dceaabe commit 752c699
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@ pipeline {
withGithubNotify(context: "Test-${PLATFORM}") {
withMageEnv(){
dir("${BASE_DIR}"){
cmd(label: 'Go unitTest', script: 'mage unitTest')
withEnv(["TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
cmd(label: 'Go unitTest', script: 'mage unitTest')
}
}
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/build/TEST-*.xml")
whenTrue(isCodeCoverageEnabled()) {
coverageReport(baseDir: "**/build", reportFiles: 'TEST-go-unit.html', coverageFiles: 'TEST-go-unit-cov.xml')
}
}
}
}
Expand Down Expand Up @@ -264,6 +269,11 @@ pipeline {
}
}

// As agreed let's report the code coverage for Linux but no ARM only.
def isCodeCoverageEnabled() {
return (isUnix() && !isArm())
}

def withPackageEnv(platform, Closure body) {
if (isUnix()) {
if (platform.contains('macosx')) {
Expand Down
37 changes: 37 additions & 0 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mage

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -292,6 +293,42 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
}
}

// Generate an XML code coverage report.
var codecovReport string
if params.CoverageProfileFile != "" {
fmt.Println(">> go run gocover-cobertura:", params.CoverageProfileFile, "Started")

// execute gocover-cobertura in order to create cobertura report
// install pre-requisites
installCobertura := sh.RunCmd("go", "install", "github.com/boumenot/gocover-cobertura@latest")
if err = installCobertura(); err != nil {
return errors.Wrap(err, "failed to install gocover-cobertura")
}

codecovReport = strings.TrimSuffix(params.CoverageProfileFile,
filepath.Ext(params.CoverageProfileFile)) + "-cov.xml"

coverage, err := ioutil.ReadFile(params.CoverageProfileFile)
if err != nil {
return errors.Wrap(err, "failed to read code coverage report")
}

coberturaFile, err := os.Create(codecovReport)
if err != nil {
return err
}
defer coberturaFile.Close()

coverToXML := exec.Command("gocover-cobertura")
coverToXML.Stdout = coberturaFile
coverToXML.Stderr = os.Stderr
coverToXML.Stdin = bytes.NewReader(coverage)
if err = coverToXML.Run(); err != nil {
return errors.Wrap(err, "failed to write XML code coverage report")
}
fmt.Println(">> go run gocover-cobertura:", params.CoverageProfileFile, "Created")
}

// Return an error indicating that testing failed.
if goTestErr != nil {
fmt.Println(">> go test:", params.TestName, "Test Failed")
Expand Down

0 comments on commit 752c699

Please sign in to comment.