Skip to content

Commit

Permalink
Make "no tests" a failure for executing tests of a repository, i.e. i…
Browse files Browse the repository at this point in the history
…f no tests are generated it is a failure
  • Loading branch information
zimmski committed Mar 30, 2024
1 parent 6ec1e7d commit bb7ceb2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 11 additions & 0 deletions language/errors.go
Original file line number Diff line number Diff line change
@@ -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")
)
23 changes: 21 additions & 2 deletions language/golang.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package language

import (
"errors"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

pkgerrors "github.com/pkg/errors"
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions language/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bb7ceb2

Please sign in to comment.