Skip to content

Commit

Permalink
Merge pull request #352 from codacy/fix/lcov-file-regex
Browse files Browse the repository at this point in the history
fix: Fix regex for lcov filenames
  • Loading branch information
lolgab authored Sep 24, 2021
2 parents 3c86300 + be8916e commit a6b9181
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
19 changes: 12 additions & 7 deletions src/main/scala/com/codacy/rules/ReportRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import scala.collection.JavaConverters._
class ReportRules(coverageServices: => CoverageServices) extends LogSupport {

private val rootProjectDir = new File(System.getProperty("user.dir"))
private val rootProjectDirIterator = Files
private val rootProjectFiles = Files
.walk(rootProjectDir.toPath)
.iterator()
.asScala
.map(_.toFile)
.toList

private def sendFilesReportForCommit(
files: List[File],
Expand Down Expand Up @@ -55,10 +56,14 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
}

def codacyCoverage(config: ReportConfig): Either[String, String] = {
codacyCoverage(config, rootProjectFiles)
}

def codacyCoverage(config: ReportConfig, projectFiles: List[File]): Either[String, String] = {
withCommitUUID(config.baseConfig) { commitUUID =>
logAuthenticationToken(config)

val filesEither = guessReportFiles(config.coverageReports, rootProjectDirIterator)
val filesEither = guessReportFiles(config.coverageReports, projectFiles)

val operationResult = filesEither.flatMap { files =>
if (files.length > 1 && !config.partial) {
Expand Down Expand Up @@ -196,14 +201,14 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
*
* This function try to guess the report language based on common report file names.
*
* @param files coverage file option provided by the config
* @param pathIterator path iterator to search the files
* @param files coverage file option provided by the config
* @param paths paths to search the files
* @return the guessed report files on the right or an error on the left.
*/
private[rules] def guessReportFiles(files: List[File], pathIterator: Iterator[File]): Either[String, List[File]] = {
private[rules] def guessReportFiles(files: List[File], projectFiles: List[File]): Either[String, List[File]] = {
val JacocoRegex = """(jacoco.*\.xml)""".r
val CoberturaRegex = """(cobertura\.xml)""".r
val LCOVRegex = """(lcov(.info|.dat)|.*\.lcov)""".r
val LCOVRegex = """(lcov\.info|lcov\.dat|.*\.lcov)""".r
val CloverRegex = """(clover\.xml)""".r
val DotcoverRegex = """(dotcover\.xml)""".r
val OpencoverRegex = """(opencover\.xml)""".r
Expand All @@ -213,7 +218,7 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {

files match {
case value if value.isEmpty =>
val foundFiles = pathIterator
val foundFiles = projectFiles
.filter(
file =>
file.getName match {
Expand Down
39 changes: 29 additions & 10 deletions src/test/scala/com/codacy/rules/ReportRulesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
val baseConfig =
BaseConfig(ProjectTokenAuthenticationConfig(projToken), apiBaseUrl, None, debug = false, timeoutOpt = None)

def assertCodacyCoverage(coverageServices: CoverageServices, coverageReports: List[String], success: Boolean) = {
def assertCodacyCoverage(
coverageServices: CoverageServices,
coverageReports: List[String],
success: Boolean,
projectFiles: Option[List[File]] = None
) = {
val reportRules = new ReportRules(coverageServices)
val reportConfig =
ReportConfig(
Expand All @@ -46,7 +51,12 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
prefix = "",
forceCoverageParser = None
)
val result = reportRules.codacyCoverage(reportConfig)
val result = projectFiles match {
case Some(files) =>
reportRules.codacyCoverage(reportConfig, files)
case None =>
reportRules.codacyCoverage(reportConfig)
}

result should be(if (success) 'right else 'left)
}
Expand All @@ -55,7 +65,7 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
"it finds no report file" in {
val coverageServices = mock[CoverageServices]

assertCodacyCoverage(coverageServices, List(), success = false)
assertCodacyCoverage(coverageServices, List(), success = false, projectFiles = Some(List.empty))
}

"it is not able to parse report file" in {
Expand Down Expand Up @@ -120,35 +130,44 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi

"guessReportFiles" should {
"provide a report file" in {
val fileIterator = Iterator(new File("jacoco-coverage.xml"), new File("foobar.txt"))
val reportEither = components.reportRules.guessReportFiles(List.empty[File], fileIterator)
val projectFiles = List(new File("jacoco-coverage.xml"), new File("foobar.txt"))
val reportEither = components.reportRules.guessReportFiles(List.empty[File], projectFiles)

reportEither should be('right)
reportEither.right.value.map(_.toString) should be(List("jacoco-coverage.xml"))
}

"not provide a report file" in {
val fileIterator = Iterator(new File("foobar.txt"))
val reportEither = components.reportRules.guessReportFiles(List.empty[File], fileIterator)
val projectFiles = List(new File("foobar.txt"))
val reportEither = components.reportRules.guessReportFiles(List.empty[File], projectFiles)

reportEither should be('left)
}

"provide the available report file" in {
val files = List(new File("coverage.xml"))
val reportEither = components.reportRules.guessReportFiles(files, Iterator.empty)
val reportEither = components.reportRules.guessReportFiles(files, List.empty)

reportEither should be('right)
reportEither.right.value should be(files)
}

"only provide phpunit report file inside coverage-xml" in {
val fileIterator = Iterator(new File("index.xml"), new File("coverage-xml", "index.xml"))
val reportEither = components.reportRules.guessReportFiles(List.empty, fileIterator)
val projectFiles = List(new File("index.xml"), new File("coverage-xml", "index.xml"))
val reportEither = components.reportRules.guessReportFiles(List.empty, projectFiles)

reportEither should be('right)
reportEither.right.value should be(List(new File("coverage-xml", "index.xml")))
}

"find an lcov report" in {
val projectFiles =
List(new File("lcov.info"), new File("lcov.dat"), new File("foo.lcov"), new File("foobar.txt"))
val reportEither = components.reportRules.guessReportFiles(List.empty[File], projectFiles)

reportEither should be('right)
reportEither.right.value.map(_.toString) should be(List("lcov.info", "lcov.dat", "foo.lcov"))
}
}

"validateFileAccess" should {
Expand Down

0 comments on commit a6b9181

Please sign in to comment.