Skip to content

Commit 5568545

Browse files
Merge pull request #520 from codacy/fix-go-reports-paths
allow different paths in go parser
2 parents cbe80ad + f94a52c commit 5568545

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

coverage-parser/src/main/scala/com/codacy/parsers/implementation/GoParser.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object GoParser extends CoverageParser {
1717

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

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

2323
override def parse(rootProject: File, reportFile: File): Either[String, CoverageReport] = {
@@ -40,24 +40,29 @@ object GoParser extends CoverageParser {
4040
coverageInfoGroupedByFilename.foldLeft[Seq[CoverageFileReport]](Seq.empty[CoverageFileReport])((accum, next) => {
4141
next match {
4242
case (filename, coverageInfosForFile) =>
43-
//calculate hits for a file for given statement reports
43+
// Only for Github - Process filename to remove "github.com/orgname/repositoryname/"
44+
val processedFilename = if (filename.startsWith("github.com/")) {
45+
filename.split("/", 4).lastOption.getOrElse(filename) // Get everything after repository name
46+
} else {
47+
filename
48+
}
49+
50+
// Calculate hits for a file for given statement reports
4451
val coverage = coverageInfosForFile.foldLeft(Map[Int, Int]()) {
4552
case (hitMapAcc, coverageInfo) =>
46-
//calculate the range of lines the statement has
53+
// Calculate the range of lines the statement has
4754
val lines = Range.inclusive(coverageInfo.lineFrom, coverageInfo.lineTo)
4855

49-
//for each line add the number of hits
56+
// For each line, add the number of hits
5057
hitMapAcc ++ lines.foldLeft(Map[Int, Int]()) {
5158
case (statementHitMapAcc, line) =>
5259
statementHitMapAcc ++
53-
//if the line is already present on the hit map, don't replace the value
60+
// If the line is already present on the hit map, don't replace the value
5461
Map(line -> (hitMapAcc.getOrElse(line, 0) + coverageInfo.countOfStatements))
55-
5662
}
5763
}
5864

59-
accum :+ CoverageFileReport(filename, coverage)
60-
65+
accum :+ CoverageFileReport(processedFilename, coverage)
6166
}
6267
})
6368

@@ -70,5 +75,4 @@ object GoParser extends CoverageParser {
7075
GoCoverageInfo(filename, lineFrom.toInt, lineTo.toInt, numberOfStatements.toInt, countOfStatements.toInt)
7176
}
7277
}
73-
7478
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mode: set
2+
github.com/orgname/reponame/src/hello.go:5.13,7.2 1 0
3+
github.com/orgname/reponame/src/hello.go:11.30,14.2 2 1
4+
github.com/orgname/reponame/src/hello.go:17.35,19.2 1 1

coverage-parser/src/test/scala/com/codacy/parsers/GoParserTest.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ class GoParserTest extends AnyWordSpec with Matchers with EitherValues {
2727
}
2828
}
2929

30+
"return a valid report from github" in {
31+
val reader = GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_go_gh.out"))
32+
33+
val testReport = CoverageReport(
34+
List(
35+
CoverageFileReport(
36+
"src/hello.go",
37+
Map(5 -> 0, 14 -> 1, 6 -> 0, 13 -> 1, 17 -> 1, 12 -> 1, 7 -> 0, 18 -> 1, 11 -> 1, 19 -> 1)
38+
)
39+
)
40+
)
41+
42+
reader.value should equal(testReport)
43+
}
44+
3045
"return a valid report" in {
3146
val reader = GoParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_go.out"))
3247

0 commit comments

Comments
 (0)