diff --git a/language/errors.go b/language/errors.go new file mode 100644 index 00000000..57c500d5 --- /dev/null +++ b/language/errors.go @@ -0,0 +1,11 @@ +package language + +import ( + "errors" +) + +// Common errors over all languages. +var ( + // ErrNoTestFound indicates that no tests could be found. + ErrNoTestFound = errors.New("no tests could be found") +) diff --git a/language/golang.go b/language/golang.go index f7792458..d5c54655 100644 --- a/language/golang.go +++ b/language/golang.go @@ -1,8 +1,11 @@ package language import ( + "errors" "os" "path/filepath" + "regexp" + "strconv" "strings" pkgerrors "github.com/pkg/errors" @@ -49,11 +52,16 @@ func (language *LanguageGolang) Files(repositoryPath string) (filePaths []string return filePaths, nil } +var languageGoNoTestsMatch = regexp.MustCompile(`(?m)^DONE (\d+) tests.*in (.+?)$`) + // Execute invokes the language specific testing on the given repository. func (language *LanguageGolang) Execute(repositoryPath string) (err error) { - _, _, err = util.CommandWithResult(&util.Command{ + stdout, _, err := util.CommandWithResult(&util.Command{ Command: []string{ - "go", "test", + "gotestsum", + "--format", "standard-verbose", // Keep formatting consistent. + "--hide-summary", "skipped", // We are not interested in skipped tests, because they are the same as no tests at all. + "--", // Let the real Go "test" tool options begin. "-v", // Output with the maximum information for easier debugging. "-vet=off", // Disable all linter checks, because those should be part of a different task. "./...", // Always execute all tests of the repository in case multiple test files have been generated. @@ -65,5 +73,16 @@ func (language *LanguageGolang) Execute(repositoryPath string) (err error) { return pkgerrors.WithStack(err) } + ms := languageGoNoTestsMatch.FindStringSubmatch(stdout) + if ms == nil { + return pkgerrors.WithStack(errors.New("could not find Go test summary")) + } + testCount, err := strconv.ParseUint(ms[1], 10, 64) + if err != nil { + return pkgerrors.WithStack(err) + } else if testCount == 0 { + return pkgerrors.WithStack(ErrNoTestFound) + } + return nil } diff --git a/language/golang_test.go b/language/golang_test.go index 0f99d827..c1d3fbf7 100644 --- a/language/golang_test.go +++ b/language/golang_test.go @@ -88,6 +88,8 @@ func TestLanguageGolangExecute(t *testing.T) { Name: "No test files", RepositoryPath: "../testdata/golang/plain/", + + ExpectedError: ErrNoTestFound, }) t.Run("With test file", func(t *testing.T) {