Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this flag only be allowed if --report-output file is also used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --report-output refers to test results, so I assumed it's something else. I don't see a value in printing the coverage to STDOUT, so I didn't bind these flags together.

Do you think we should introduce additional flag for --coverage-output file (or stdout) or is there low gain?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that it may be weird to have files created even when there is only stdout output by default. But improving this, if needed, sounds like something for the future. For example we might show coverage info in the normal test reports, this could be nice, then --report-output would control the output of both features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, let's keep it for further improvement if there is feature need for this feature.

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