Skip to content

Commit

Permalink
Merge pull request #520 from codacy/fix-go-reports-paths
Browse files Browse the repository at this point in the history
allow different paths in go parser
  • Loading branch information
DMarinhoCodacy authored Feb 6, 2025
2 parents cbe80ad + f94a52c commit 5568545
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ object GoParser extends CoverageParser {

final val MODE = """mode: ([set|count|atomic]*)""".r

//filename.go:lineFrom.column,lineTo.column numberOfStatements countOfStatements
// filename.go:lineFrom.column,lineTo.column numberOfStatements countOfStatements
final val regexpString = """([a-zA-Z\/\._\-\d]*):(\d+).*?,(\d+).* (\d+) (\d+)""".r

override def parse(rootProject: File, reportFile: File): Either[String, CoverageReport] = {
@@ -40,24 +40,29 @@ object GoParser extends CoverageParser {
coverageInfoGroupedByFilename.foldLeft[Seq[CoverageFileReport]](Seq.empty[CoverageFileReport])((accum, next) => {
next match {
case (filename, coverageInfosForFile) =>
//calculate hits for a file for given statement reports
// Only for Github - Process filename to remove "github.com/orgname/repositoryname/"
val processedFilename = if (filename.startsWith("github.com/")) {
filename.split("/", 4).lastOption.getOrElse(filename) // Get everything after repository name
} else {
filename
}

// 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
// Calculate the range of lines the statement has
val lines = Range.inclusive(coverageInfo.lineFrom, coverageInfo.lineTo)

//for each line add the number of hits
// 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
// If the line is already present on the hit map, don't replace the value
Map(line -> (hitMapAcc.getOrElse(line, 0) + coverageInfo.countOfStatements))

}
}

accum :+ CoverageFileReport(filename, coverage)

accum :+ CoverageFileReport(processedFilename, coverage)
}
})

@@ -70,5 +75,4 @@ object GoParser extends CoverageParser {
GoCoverageInfo(filename, lineFrom.toInt, lineTo.toInt, numberOfStatements.toInt, countOfStatements.toInt)
}
}

}
4 changes: 4 additions & 0 deletions coverage-parser/src/test/resources/test_go_gh.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mode: set
github.com/orgname/reponame/src/hello.go:5.13,7.2 1 0
github.com/orgname/reponame/src/hello.go:11.30,14.2 2 1
github.com/orgname/reponame/src/hello.go:17.35,19.2 1 1
Original file line number Diff line number Diff line change
@@ -27,6 +27,21 @@ class GoParserTest extends AnyWordSpec with Matchers with EitherValues {
}
}

"return a valid report from github" in {
val reader = GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_go_gh.out"))

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

reader.value should equal(testReport)
}

"return a valid report" in {
val reader = GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_go.out"))

0 comments on commit 5568545

Please sign in to comment.