Skip to content

Commit

Permalink
Generate Cobertura report (#441)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* No transformation yet

* Update Jenkinsfile

* Coverage DTD

* Typo

* Write attrs for Cobertura report

* Enable coverage for CI scripts

* Implementation full

* Few bugfixes

* Fix: hell in builder

* Fix: wrong method name

* Supports all test types

* Fix: package context

* Rename Must

* Rename
  • Loading branch information
mtojek authored Jul 22, 2021
1 parent f2bc77f commit a8f6d9e
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 12 deletions.
1 change: 1 addition & 0 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pipeline {
junit(allowEmptyResults: false,
keepLongStdio: true,
testResults: "build/test-results/*.xml")
coverageReport('build/test-coverage')
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func setupTestCommand() *cobraext.Command {
cmd.PersistentFlags().BoolP(cobraext.GenerateTestResultFlagName, "g", false, cobraext.GenerateTestResultFlagDescription)
cmd.PersistentFlags().StringP(cobraext.ReportFormatFlagName, "", string(formats.ReportFormatHuman), cobraext.ReportFormatFlagDescription)
cmd.PersistentFlags().StringP(cobraext.ReportOutputFlagName, "", string(outputs.ReportOutputSTDOUT), cobraext.ReportOutputFlagDescription)
cmd.PersistentFlags().BoolP(cobraext.TestCoverageFlagName, "", false, cobraext.TestCoverageFlagDescription)
cmd.PersistentFlags().DurationP(cobraext.DeferCleanupFlagName, "", 0, cobraext.DeferCleanupFlagDescription)
cmd.PersistentFlags().String(cobraext.VariantFlagName, "", cobraext.VariantFlagDescription)

Expand Down Expand Up @@ -116,6 +117,11 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
return cobraext.FlagParsingError(err, cobraext.ReportOutputFlagName)
}

testCoverage, err := cmd.Flags().GetBool(cobraext.TestCoverageFlagName)
if err != nil {
return cobraext.FlagParsingError(err, cobraext.TestCoverageFlagName)
}

packageRootPath, found, err := packages.FindPackageRoot()
if !found {
return errors.New("package root not found")
Expand Down Expand Up @@ -143,6 +149,10 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
if err != nil {
return cobraext.FlagParsingError(err, cobraext.DataStreamsFlagName)
}

if len(dataStreams) > 0 {
return cobraext.FlagParsingError(errors.New("test coverage can be calculated only if all data streams are selected"), cobraext.DataStreamsFlagName)
}
}

if runner.TestFolderRequired() {
Expand Down Expand Up @@ -217,6 +227,13 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
return errors.Wrap(err, "error writing test report")
}

if testCoverage {
err := testrunner.WriteCoverage(packageRootPath, m.Name, runner.Type(), results)
if err != nil {
return errors.Wrap(err, "error writing test coverage")
}
}

// Check if there is any error or failure reported
for _, r := range results {
if r.ErrorMsg != "" || r.FailureMsg != "" {
Expand Down
36 changes: 27 additions & 9 deletions internal/builder/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,22 @@ import (
"github.com/pkg/errors"
)

// FindBuildDirectory locates the target build directory.
func FindBuildDirectory() (string, bool, error) {
// BuildDirectory function locates the target build directory. If the directory doesn't exist, it will create it.
func BuildDirectory() (string, error) {
buildDir, found, err := findBuildDirectory()
if err != nil {
return "", errors.Wrap(err, "locating build directory failed")
}
if !found {
buildDir, err = createBuildDirectory()
if err != nil {
return "", errors.Wrap(err, "creating new build directory failed")
}
}
return buildDir, nil
}

func findBuildDirectory() (string, bool, error) {
workDir, err := os.Getwd()
if err != nil {
return "", false, errors.Wrap(err, "locating working directory failed")
Expand All @@ -38,15 +52,15 @@ func FindBuildDirectory() (string, bool, error) {
return "", false, nil
}

// MustFindBuildPackagesDirectory function locates the target build directory for packages.
// BuildPackagesDirectory function locates the target build directory for packages.
// If the directories path doesn't exist, it will create it.
func MustFindBuildPackagesDirectory(packageRoot string) (string, error) {
func BuildPackagesDirectory(packageRoot string) (string, error) {
buildDir, found, err := FindBuildPackagesDirectory()
if err != nil {
return "", errors.Wrap(err, "locating build directory failed")
}
if !found {
buildDir, err = createBuildPackagesDirectory()
buildDir, err = createBuildDirectory("integrations") // TODO add support for other package types
if err != nil {
return "", errors.Wrap(err, "creating new build directory failed")
}
Expand All @@ -61,7 +75,7 @@ func MustFindBuildPackagesDirectory(packageRoot string) (string, error) {

// FindBuildPackagesDirectory function locates the target build directory for packages.
func FindBuildPackagesDirectory() (string, bool, error) {
buildDir, found, err := FindBuildDirectory()
buildDir, found, err := findBuildDirectory()
if err != nil {
return "", false, err
}
Expand All @@ -86,7 +100,7 @@ func FindBuildPackagesDirectory() (string, bool, error) {

// BuildPackage function builds the package.
func BuildPackage(packageRoot string) (string, error) {
destinationDir, err := MustFindBuildPackagesDirectory(packageRoot)
destinationDir, err := BuildPackagesDirectory(packageRoot)
if err != nil {
return "", errors.Wrap(err, "locating build directory for package failed")
}
Expand Down Expand Up @@ -118,7 +132,7 @@ func BuildPackage(packageRoot string) (string, error) {
return destinationDir, nil
}

func createBuildPackagesDirectory() (string, error) {
func createBuildDirectory(dirs ...string) (string, error) {
workDir, err := os.Getwd()
if err != nil {
return "", errors.Wrap(err, "locating working directory failed")
Expand All @@ -129,7 +143,11 @@ func createBuildPackagesDirectory() (string, error) {
path := filepath.Join(dir, ".git")
fileInfo, err := os.Stat(path)
if err == nil && fileInfo.IsDir() {
buildDir := filepath.Join(dir, "build", "integrations") // TODO add support for other package types
p := []string{dir, "build"}
if len(dirs) > 0 {
p = append(p, dirs...)
}
buildDir := filepath.Join(p...)
err = os.MkdirAll(buildDir, 0755)
if err != nil {
return "", errors.Wrapf(err, "mkdir failed (path: %s)", buildDir)
Expand Down
3 changes: 3 additions & 0 deletions internal/cobraext/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ const (
StackDumpOutputFlagName = "output"
StackDumpOutputFlagDescription = "output location for the stack dump"

TestCoverageFlagName = "test-coverage"
TestCoverageFlagDescription = "generate Cobertura test coverage reports"

VariantFlagName = "variant"
VariantFlagDescription = "service variant"

Expand Down
2 changes: 1 addition & 1 deletion internal/docs/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func updateReadme(fileName, packageRoot string) (string, error) {
return "", errors.Wrapf(err, "writing %s file failed", fileName)
}

packageBuildRoot, err := builder.MustFindBuildPackagesDirectory(packageRoot)
packageBuildRoot, err := builder.BuildPackagesDirectory(packageRoot)
if err != nil {
return "", errors.Wrap(err, "package build root not found")
}
Expand Down
Loading

0 comments on commit a8f6d9e

Please sign in to comment.