Skip to content

Commit

Permalink
Merge pull request #457 from codacy/fix-go-parse-inconsistency-COV-207
Browse files Browse the repository at this point in the history
fix: inconsistency on go parse;
  • Loading branch information
rubencodacy authored May 4, 2023
2 parents 69bb96e + fbfc5f1 commit 483be90
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.codacy.parsers.implementation
import com.codacy.parsers.CoverageParser

import java.io.File
import com.codacy.parsers.util.MathUtils

import com.codacy.api.{CoverageFileReport, CoverageReport}

Expand All @@ -12,13 +11,6 @@ import scala.util.{Failure, Success, Try}

case class GoCoverageInfo(filename: String, lineFrom: Int, lineTo: Int, numberOfStatements: Int, countOfStatements: Int)

case class GoCoverageStatementsCount(countStatements: Int, numberStatements: Int, coveredStatements: Int)

case class CoverageFileReportWithStatementsCount(
statementsCount: GoCoverageStatementsCount,
coverageFileReport: CoverageFileReport
)

object GoParser extends CoverageParser {

override val name: String = "Go"
Expand All @@ -45,54 +37,31 @@ object GoParser extends CoverageParser {
coverageInfo.toSet.groupBy((a: GoCoverageInfo) => a.filename)

val coverageFileReports =
coverageInfoGroupedByFilename.foldLeft[Seq[CoverageFileReportWithStatementsCount]](
Seq.empty[CoverageFileReportWithStatementsCount]
)((accum, next) => {
coverageInfoGroupedByFilename.foldLeft[Seq[CoverageFileReport]](Seq.empty[CoverageFileReport])((accum, next) => {
next match {
case (filename, coverageInfosForFile) =>
val statementsCountForFile = coverageInfosForFile.foldLeft(GoCoverageStatementsCount(0, 0, 0)) { (acc, v) =>
val newCountStatements = acc.countStatements + v.countOfStatements
val newNumberStatements = acc.numberStatements + v.numberOfStatements
val newTotalCoveredStatements =
if (v.countOfStatements > 0) acc.coveredStatements + v.numberOfStatements else acc.coveredStatements

GoCoverageStatementsCount(newCountStatements, newNumberStatements, newTotalCoveredStatements)
//calculate hits for a file for given statement reports
val coverage = coverageInfosForFile.foldLeft(Map[Int, Int]()) {
case (hitMapAcc, coverageInfo) =>
//calculate the range of lines the statement has
val lines = Range.inclusive(coverageInfo.lineFrom, coverageInfo.lineTo)

//for each line add the number of hits
hitMapAcc ++ lines.foldLeft(Map[Int, Int]()) {
case (statementHitMapAcc, line) =>
statementHitMapAcc ++
//if the line is already present on the hit map, don't replace the value
Map(line -> (hitMapAcc.getOrElse(line, 0) + coverageInfo.countOfStatements))

}
}

val coverage = coverageInfosForFile.foldLeft(Map[Int, Int]()) { (acc, coverageInfo) =>
acc ++ lineHits(coverageInfo)
}

val totalForFile = calculateTotal(statementsCountForFile)
accum :+ CoverageFileReport(filename, 0, coverage)

accum :+ CoverageFileReportWithStatementsCount(
statementsCountForFile,
CoverageFileReport(filename, totalForFile, coverage)
)
}
})

val (covered, total) = coverageFileReports
.foldLeft[(Int, Int)]((0, 0)) {
case ((covered, total), coverageFileReportWithStatementsCount) =>
(
covered + coverageFileReportWithStatementsCount.statementsCount.coveredStatements,
total + coverageFileReportWithStatementsCount.statementsCount.numberStatements
)
}

val totalCoverage = MathUtils.computePercentage(covered, total)

CoverageReport(totalCoverage, coverageFileReports.map(_.coverageFileReport))
}

private def calculateTotal(coverageFileStatements: GoCoverageStatementsCount): Int = {
MathUtils.computePercentage(coverageFileStatements.coveredStatements, coverageFileStatements.numberStatements)
}

private def lineHits(coverageInfo: GoCoverageInfo): Map[Int, Int] = {
val lines = Range.inclusive(coverageInfo.lineFrom, coverageInfo.lineTo)
lines.foldLeft(Map[Int, Int]())((acc, line) => acc ++ Map(line -> coverageInfo.countOfStatements))
CoverageReport(0, coverageFileReports)
}

private def parseAllCoverageInfo(lines: List[String]): List[GoCoverageInfo] = {
Expand Down
18 changes: 18 additions & 0 deletions coverage-parser/src/test/resources/go/changed_package_name.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:31.101,35.16 3 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:39.2,39.41 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:47.2,47.41 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:56.2,58.71 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:62.2,62.18 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:35.16,38.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:39.41,46.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:47.41,54.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:58.71,60.3 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:70.104,72.9 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:76.2,76.60 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:81.2,82.16 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:92.2,92.15 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:96.2,96.12 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:72.9,74.3 1 0
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:76.60,78.3 1 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:82.16,90.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/apiv1/api.go:92.15,94.3 1 1
18 changes: 18 additions & 0 deletions coverage-parser/src/test/resources/go/original_package.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:31.101,35.16 3 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:39.2,39.41 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:47.2,47.41 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:56.2,58.71 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:62.2,62.18 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:35.16,38.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:39.41,46.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:47.41,54.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:58.71,60.3 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:70.104,72.9 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:76.2,76.60 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:81.2,82.16 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:92.2,92.15 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:96.2,96.12 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:72.9,74.3 1 0
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:76.60,78.3 1 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:82.16,90.3 2 1
github.com/codacy/pulse/modules/entity/src/internal/api/api.go:92.15,94.3 1 1
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ class GoParserTest extends WordSpec with Matchers with EitherValues {
val reader = GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_go.out"))

val testReport = CoverageReport(
75,
0,
List(
CoverageFileReport(
"example.com/m/v2/hello.go",
75,
0,
Map(5 -> 0, 14 -> 1, 6 -> 0, 13 -> 1, 17 -> 1, 12 -> 1, 7 -> 0, 18 -> 1, 11 -> 1, 19 -> 1)
)
)
)

reader.right.value should equal(testReport)
}

"return consistent values" in {
//given two reports where the package names were only changed, should return the same results COV-207
val reader = GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/go/original_package.out"))
val reader1 =
GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/go/changed_package_name.out"))

reader.right.value.fileReports(0).coverage should equal(reader1.right.value.fileReports(0).coverage)
}
}
}

0 comments on commit 483be90

Please sign in to comment.