Revert "Calculate coverage of private code."

This reverts commit a56f50ac46.
This commit is contained in:
Fedor Korotkiy 2023-02-22 17:02:44 +04:00
parent 545540cea9
commit a0b7eb9c6d
2 changed files with 20 additions and 56 deletions

View file

@ -83,34 +83,15 @@ func searchCoverageComment(fname string) (*CoverageRequirements, error) {
return &CoverageRequirements{}, nil return &CoverageRequirements{}, nil
} }
// calCoverage calculates coverage percent of code blocks recorded in targetProfile // calCoverage calculates coverage percent for given coverage profile.
// by given coverage profiles. func calCoverage(fileNames []string) (float64, error) {
func calCoverage(targetProfile string, fileNames []string) (float64, error) { type block struct {
type key struct {
fileName string fileName string
startLine, startCol int startLine, startCol int
endLine, endCol int endLine, endCol int
numStmt int numStmt int
} }
newKey := func(p *cover.Profile, b cover.ProfileBlock) key { counts := map[block]int{}
return key{
p.FileName,
b.StartLine, b.StartCol,
b.EndLine, b.EndCol,
b.NumStmt,
}
}
executed := map[key]bool{}
targetProfiles, err := cover.ParseProfiles(targetProfile)
if err != nil {
return 0.0, fmt.Errorf("cannot parse target profile %s: %w", targetProfile, err)
}
for _, p := range targetProfiles {
for _, b := range p.Blocks {
executed[newKey(p, b)] = false
}
}
for _, f := range fileNames { for _, f := range fileNames {
profiles, err := cover.ParseProfiles(f) profiles, err := cover.ParseProfiles(f)
@ -120,19 +101,21 @@ func calCoverage(targetProfile string, fileNames []string) (float64, error) {
for _, p := range profiles { for _, p := range profiles {
for _, b := range p.Blocks { for _, b := range p.Blocks {
k := newKey(p, b) counts[block{
if _, ok := executed[k]; ok && b.Count > 0 { p.FileName,
executed[k] = true b.StartLine, b.StartCol,
} b.EndLine, b.EndCol,
b.NumStmt,
}] += b.Count
} }
} }
} }
var total, covered int var total, covered int
for k, e := range executed { for b, count := range counts {
total += k.numStmt total += b.numStmt
if e { if count > 0 {
covered += k.numStmt covered += b.numStmt
} }
} }

View file

@ -255,12 +255,8 @@ func runTests(testDir, privateRepo, problem string) error {
} }
coverageReq := getCoverageRequirements(path.Join(privateRepo, problem)) coverageReq := getCoverageRequirements(path.Join(privateRepo, problem))
coveragePackages := []string{}
if coverageReq.Enabled { if coverageReq.Enabled {
log.Printf("required coverage: %.2f%%", coverageReq.Percent) log.Printf("required coverage: %.2f%%", coverageReq.Percent)
for _, pkg := range coverageReq.Packages {
coveragePackages = append(coveragePackages, path.Join(moduleImportPath, problem, pkg))
}
} }
binariesJSON, _ := json.Marshal(binaries) binariesJSON, _ := json.Marshal(binaries)
@ -271,7 +267,11 @@ func runTests(testDir, privateRepo, problem string) error {
cmd := []string{"test", "-mod", "readonly", "-tags", "private", "-c", "-o", testPath, testPkg} cmd := []string{"test", "-mod", "readonly", "-tags", "private", "-c", "-o", testPath, testPkg}
if coverageReq.Enabled { if coverageReq.Enabled {
cmd = append(cmd, "-cover", "-coverpkg", strings.Join(coveragePackages, ",")) 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 { if err := runGo(cmd...); err != nil {
return fmt.Errorf("error building test in %s: %w", testPkg, err) return fmt.Errorf("error building test in %s: %w", testPkg, err)
@ -378,26 +378,7 @@ func runTests(testDir, privateRepo, problem string) error {
if coverageReq.Enabled { if coverageReq.Enabled {
log.Printf("checking coverage is at least %.2f%%...", coverageReq.Percent) log.Printf("checking coverage is at least %.2f%%...", coverageReq.Percent)
// For some reason, this command will record all coverage blocks in coverpkg, percent, err := calCoverage(coverProfiles)
// even if no test binaries depend on given package.
// Hacky way to record all the code present in problem definition.
targetProfile := path.Join(os.TempDir(), randomName())
coverCmd := exec.Command("go",
"test",
"-coverpkg", strings.Join(coveragePackages, ","),
"-coverprofile", targetProfile,
"-run", "^$",
"./...",
)
coverCmd.Env = append(os.Environ(), "GOFLAGS=")
coverCmd.Dir = path.Join(privateRepo, problem)
coverCmd.Stderr = os.Stderr
log.Printf("> %s", strings.Join(coverCmd.Args, " "))
if err := coverCmd.Run(); err != nil {
return fmt.Errorf("error getting target coverage profile: %w", err)
}
percent, err := calCoverage(targetProfile, coverProfiles)
if err != nil { if err != nil {
return err return err
} }