Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "no tests" a failure for executing tests of a repository, i.e. if no tests are generated it is a failure #5

Merged
merged 4 commits into from
Mar 30, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
chmod +x $PWD/bin/symflower
echo "PATH=$PWD/bin:$PATH" >> $GITHUB_ENV

- name: Install testing tools
run: make install-tools-testing

- name: Build
run: make install

Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ install: # [<Go package] - # Build and install everything, or only the specified
go install -v $(PACKAGE)
.PHONY: install

install-all: install-tools-testing install # Install everything for and of this repository.
.PHONY: install-all

install-tools-testing: # Install tools that are used for testing.
go install -v gotest.tools/gotestsum@v1.11.0
.PHONY: install-tools-testing

test: # [<Go package] - # Test everything, or only the specified package.
go test -race -test.timeout $(UNIT_TEST_TIMEOUT)s -v $(PACKAGE)
gotestsum --format standard-verbose --hide-summary skipped -- -race -test.timeout $(UNIT_TEST_TIMEOUT)s -v $(PACKAGE)
.PHONY: test
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")
)
28 changes: 24 additions & 4 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,13 +52,19 @@ 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",
"-v", // Output with the maximum information for easier debugging.
"./...", // Always execute all tests of the repository in case multiple test files have been generated.
"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.
},

Directory: repositoryPath,
Expand All @@ -64,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
}
8 changes: 6 additions & 2 deletions language/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func TestLanguageGolangExecute(t *testing.T) {
actualError := tc.LanguageGolang.Execute(repositoryPath)

if tc.ExpectedError != nil {
assert.ErrorIs(t, tc.ExpectedError, actualError)
} else if actualError != nil || tc.ExpectedErrorText != "" {
assert.ErrorIs(t, actualError, tc.ExpectedError)
} else if actualError != nil && tc.ExpectedErrorText != "" {
assert.ErrorContains(t, actualError, tc.ExpectedErrorText)
} else {
assert.NoError(t, actualError)
}
})
}
Expand All @@ -86,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
Loading