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
}
// calCoverage calculates coverage percent of code blocks recorded in targetProfile
// by given coverage profiles.
func calCoverage(targetProfile string, fileNames []string) (float64, error) {
type key struct {
// calCoverage calculates coverage percent for given coverage profile.
func calCoverage(fileNames []string) (float64, error) {
type block struct {
fileName string
startLine, startCol int
endLine, endCol int
numStmt int
}
newKey := func(p *cover.Profile, b cover.ProfileBlock) key {
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
}
}
counts := map[block]int{}
for _, f := range fileNames {
profiles, err := cover.ParseProfiles(f)
@ -120,19 +101,21 @@ func calCoverage(targetProfile string, fileNames []string) (float64, error) {
for _, p := range profiles {
for _, b := range p.Blocks {
k := newKey(p, b)
if _, ok := executed[k]; ok && b.Count > 0 {
executed[k] = true
}
counts[block{
p.FileName,
b.StartLine, b.StartCol,
b.EndLine, b.EndCol,
b.NumStmt,
}] += b.Count
}
}
}
var total, covered int
for k, e := range executed {
total += k.numStmt
if e {
covered += k.numStmt
for b, count := range counts {
total += b.numStmt
if count > 0 {
covered += b.numStmt
}
}

View file

@ -255,12 +255,8 @@ func runTests(testDir, privateRepo, problem string) error {
}
coverageReq := getCoverageRequirements(path.Join(privateRepo, problem))
coveragePackages := []string{}
if coverageReq.Enabled {
log.Printf("required coverage: %.2f%%", coverageReq.Percent)
for _, pkg := range coverageReq.Packages {
coveragePackages = append(coveragePackages, path.Join(moduleImportPath, problem, pkg))
}
}
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}
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 {
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 {
log.Printf("checking coverage is at least %.2f%%...", coverageReq.Percent)
// For some reason, this command will record all coverage blocks in coverpkg,
// 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)
percent, err := calCoverage(coverProfiles)
if err != nil {
return err
}