From f7a9510675858e397acece2a0c79239473745dd3 Mon Sep 17 00:00:00 2001 From: Luis Ventura Date: Tue, 9 Apr 2024 11:37:03 +0100 Subject: [PATCH] fix: Improve algorithm of path fixing --- .../scala/com/codacy/rules/ReportRules.scala | 26 ++++++++++++------- .../transformation/FileNameMatcher.scala | 3 +-- .../GitFileNameUpdaterAndFilter.scala | 7 +++-- .../GitFileNameUpdaterAndFilterSpec.scala | 4 ++- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main/scala/com/codacy/rules/ReportRules.scala b/src/main/scala/com/codacy/rules/ReportRules.scala index 3697d6ca..1d330e2b 100644 --- a/src/main/scala/com/codacy/rules/ReportRules.scala +++ b/src/main/scala/com/codacy/rules/ReportRules.scala @@ -9,6 +9,7 @@ import com.codacy.parsers.CoverageParser import com.codacy.plugins.api.languages.Languages import com.codacy.rules.commituuid.CommitUUIDProvider import com.codacy.rules.file.GitFileFetcher +import com.codacy.transformation.FileNameMatcher.getFilenameFromPath import com.codacy.transformation.{GitFileNameUpdaterAndFilter, PathPrefixer, Transformation} import wvlet.log.LogSupport @@ -33,6 +34,7 @@ class ReportRules(coverageServices: => CoverageServices, gitFileFetcher: GitFile commitUUID: String ): Either[String, String] = { val finalConfig = config.copy(partial = partial) + val acceptableFileNames = gitFileFetcher.forCommit(commitUUID) files .map { file => @@ -42,7 +44,7 @@ class ReportRules(coverageServices: => CoverageServices, gitFileFetcher: GitFile report <- CoverageParser.parse(rootProjectDir, file, forceParser = config.forceCoverageParser).map { coverageResult => logger.info(s"Coverage parser used is ${coverageResult.parser}") - transform(coverageResult.report)(finalConfig, commitUUID) + transform(coverageResult.report)(finalConfig, commitUUID, acceptableFileNames) } _ <- storeReport(report, file) language <- guessReportLanguage(finalConfig.languageOpt, report, file.getAbsolutePath) @@ -323,15 +325,21 @@ class ReportRules(coverageServices: => CoverageServices, gitFileFetcher: GitFile } } - private def transform(report: CoverageReport)(config: ReportConfig, commitUUID: String): CoverageReport = { - - val acceptableFileNames = gitFileFetcher.forCommit(commitUUID) - + private def transform( + report: CoverageReport + )(config: ReportConfig, commitUUID: String, acceptableFileNames: Either[String, Seq[String]]): CoverageReport = { val transformations: Seq[Transformation] = acceptableFileNames - .fold(error => { - logger.warn(s"Report files will not be matched against git files, reason: $error") - Seq(new PathPrefixer(config.prefix)) - }, filenames => Seq(new PathPrefixer(config.prefix), new GitFileNameUpdaterAndFilter(filenames))) + .fold( + error => { + logger.warn(s"Report files will not be matched against git files, reason: $error") + Seq(new PathPrefixer(config.prefix)) + }, + filenames => + Seq(new PathPrefixer(config.prefix), { + val acceptableFileNamesMap = filenames.groupBy(getFilenameFromPath).view.toMap + new GitFileNameUpdaterAndFilter(acceptableFileNamesMap) + }) + ) transformations.foldLeft(report) { (report, transformation) => transformation.execute(report) diff --git a/src/main/scala/com/codacy/transformation/FileNameMatcher.scala b/src/main/scala/com/codacy/transformation/FileNameMatcher.scala index 46c5ad0b..12c94499 100644 --- a/src/main/scala/com/codacy/transformation/FileNameMatcher.scala +++ b/src/main/scala/com/codacy/transformation/FileNameMatcher.scala @@ -6,14 +6,13 @@ import scala.util.Try object FileNameMatcher { def matchAndReturnName(filename: String, fileNames: Seq[String]): Option[String] = { - fileNames .filter(name => isTheSameFile(filename, name)) .sortBy(name => Math.abs(filename.length - name.length)) .headOption } - private def getFilenameFromPath(filename: String): String = { + def getFilenameFromPath(filename: String): String = { Try(Paths.get(filename).getFileName.toString).getOrElse(filename) } diff --git a/src/main/scala/com/codacy/transformation/GitFileNameUpdaterAndFilter.scala b/src/main/scala/com/codacy/transformation/GitFileNameUpdaterAndFilter.scala index 0b9cb32b..c84e3f36 100644 --- a/src/main/scala/com/codacy/transformation/GitFileNameUpdaterAndFilter.scala +++ b/src/main/scala/com/codacy/transformation/GitFileNameUpdaterAndFilter.scala @@ -1,8 +1,11 @@ package com.codacy.transformation import com.codacy.api.CoverageReport +import com.codacy.transformation.FileNameMatcher.getFilenameFromPath import wvlet.log.LogSupport -class GitFileNameUpdaterAndFilter(acceptableFileNames: Seq[String]) extends Transformation with LogSupport { +class GitFileNameUpdaterAndFilter(acceptableFileNamesMap: Map[String, Seq[String]]) + extends Transformation + with LogSupport { override def execute(report: CoverageReport): CoverageReport = { val fileReports = for { fileReport <- report.fileReports @@ -15,7 +18,7 @@ class GitFileNameUpdaterAndFilter(acceptableFileNames: Seq[String]) extends Tran private def matchAndReturnName(filename: String): Option[String] = { val maybeFilename = FileNameMatcher - .matchAndReturnName(filename, acceptableFileNames) + .matchAndReturnName(filename, acceptableFileNamesMap.getOrElse(getFilenameFromPath(filename), Seq.empty)) if (maybeFilename.isEmpty) logger diff --git a/src/test/scala/com/codacy/transformation/GitFileNameUpdaterAndFilterSpec.scala b/src/test/scala/com/codacy/transformation/GitFileNameUpdaterAndFilterSpec.scala index b9b6d502..3fc8b567 100644 --- a/src/test/scala/com/codacy/transformation/GitFileNameUpdaterAndFilterSpec.scala +++ b/src/test/scala/com/codacy/transformation/GitFileNameUpdaterAndFilterSpec.scala @@ -1,13 +1,15 @@ package com.codacy.transformation import com.codacy.api.{CoverageFileReport, CoverageReport} +import com.codacy.transformation.FileNameMatcher.getFilenameFromPath import org.scalatest.{Matchers, WordSpec} class GitFileNameUpdaterAndFilterSpec extends WordSpec with Matchers { private val acceptableFilenames = Seq("src/folder/file1.txt", "src/another-folder/file1.txt", "src/folder/file2.txt", "src/folder/file3.txt") - private val updaterAndFilter = new GitFileNameUpdaterAndFilter(acceptableFilenames) + private val acceptableFilenamesMap = acceptableFilenames.groupBy(getFilenameFromPath).view.toMap + private val updaterAndFilter = new GitFileNameUpdaterAndFilter(acceptableFilenamesMap) "execute" should { "update and match filename" in {