Add coverpkg specification.

Currently, packages in coverme without tests created by student
are not checked for coverage, because no there are no test targets
for them at all!

The issue can be fixed by simply creating empty !change files,
but we are engineers, aren't we? :)

Bonus point: it prevents new packages from giving false coverage credit.
For example, mock file generated by gomock will have lots of covered code
almost instantly. However, the task can be hacked by adding new code to
existing packages.
This commit is contained in:
Alexander Vasilyev 2020-03-22 03:26:50 +03:00 committed by Fedor Korotkiy
parent f81398290b
commit 54ab7b69d1
9 changed files with 37 additions and 18 deletions

View file

@ -2,4 +2,4 @@
package app
// min coverage: 90%
// min coverage: app,client,models,utils 90%

View file

@ -18,8 +18,9 @@ import (
const coverageCommentPrefix = "min coverage: "
type CoverageRequirements struct {
Enabled bool
Percent float64
Enabled bool
Percent float64
Packages []string
}
// getCoverageRequirements searches for comment in test files
@ -58,14 +59,25 @@ func searchCoverageComment(fname string) (*CoverageRequirements, error) {
}
t = strings.TrimPrefix(t, coverageCommentPrefix)
t = strings.TrimSuffix(t, "%\n")
percent, err := strconv.ParseFloat(t, 64)
parts := strings.Split(t, " ")
if len(parts) != 2 {
continue
}
percent, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
continue
}
if percent < 0 || percent > 100.0 {
continue
}
return &CoverageRequirements{Enabled: true, Percent: percent}, nil
return &CoverageRequirements{
Enabled: true,
Percent: percent,
Packages: strings.Split(parts[0], ","),
}, nil
}
return &CoverageRequirements{}, nil

View file

@ -10,4 +10,5 @@ func Test_getCoverageRequirements(t *testing.T) {
r := getCoverageRequirements("../testdata/coverage/sum")
require.True(t, r.Enabled)
require.Equal(t, 90.0, r.Percent)
require.Equal(t, []string{"."}, r.Packages)
}

View file

@ -254,7 +254,11 @@ func runTests(testDir, privateRepo, problem string) error {
testBinaries[testPkg] = binPath
cmd := []string{"test", "-mod", "readonly", "-tags", "private", "-c", "-o", binPath, testPkg}
if coverageReq.Enabled {
cmd = append(cmd, "-cover")
pkgs := make([]string, len(coverageReq.Packages))
for i, pkg := range coverageReq.Packages {
pkgs[i] = path.Join(moduleImportPath, problem, pkg)
}
cmd = append(cmd, "-cover", "-coverpkg", strings.Join(pkgs, ","))
}
if err := runGo(cmd...); err != nil {
return fmt.Errorf("error building test in %s: %w", testPkg, err)

View file

@ -3,20 +3,22 @@ package subpkg
// Incorrect coverage comments:
// min coverage: -1%
// min coverage: . -1%
// min coverage: 100.001%
// min coverage: . 100.001%
// min coverage: 100 %
// min coverage: . 100 %
// min coverage:10%
// min coverage:. 10%
// min coverage: 19%
// Correct coverage comment:
// min coverage: . 19%
// min coverage: 90%
// Correct coverage comment:
// min coverage: . 90%
// Testtool uses first matching comment.
// min coverage: 91%
// min coverage: . 91%

View file

@ -10,7 +10,7 @@ import (
"gitlab.com/slon/shad-go/coverme"
)
// min coverage: 70%
// min coverage: .,subpkg 70%
func TestSum(t *testing.T) {
require.Equal(t, int64(2), coverme.Sum(1, 1))

View file

@ -10,7 +10,7 @@ import (
"gitlab.com/slon/shad-go/coverme"
)
// min coverage: 70%
// min coverage: .,subpkg 70%
func TestSum(t *testing.T) {
require.Equal(t, int64(2), coverme.Sum(1, 1))

View file

@ -9,7 +9,7 @@ import (
"gitlab.com/slon/shad-go/poorcoverage"
)
// min coverage: 100%
// min coverage: . 100%
func TestSum(t *testing.T) {
require.Equal(t, int64(2), poorcoverage.Sum(1, 1))

View file

@ -9,7 +9,7 @@ import (
"gitlab.com/slon/shad-go/poorcoverage"
)
// min coverage: 100%
// min coverage: . 100%
func TestSum(t *testing.T) {
require.Equal(t, int64(2), poorcoverage.Sum(1, 1))