From 4111b0d5dc041072b0f3b2df9d0e7bcf5296b6db Mon Sep 17 00:00:00 2001 From: Andrew Ochsner Date: Mon, 20 Sep 2021 13:37:09 -0500 Subject: [PATCH 1/4] fix regex for lcov files, fix test lcov to ensure they aren't found --- .java-version | 2 +- .../{invalid_report.lcov => invalid_report.testlcov} | 0 .../test/resources/{test_lcov.lcov => test_lcov.testlcov} | 0 .../test/scala/com/codacy/parsers/LCOVParserTest.scala | 6 +++--- src/main/scala/com/codacy/rules/ReportRules.scala | 2 +- src/test/scala/com/codacy/rules/ReportRulesSpec.scala | 8 ++++++++ 6 files changed, 13 insertions(+), 5 deletions(-) rename coverage-parser/src/test/resources/{invalid_report.lcov => invalid_report.testlcov} (100%) rename coverage-parser/src/test/resources/{test_lcov.lcov => test_lcov.testlcov} (100%) diff --git a/.java-version b/.java-version index 3dedf4b8..62593409 100644 --- a/.java-version +++ b/.java-version @@ -1 +1 @@ -oracle64-1.8.0.25 +1.8 diff --git a/coverage-parser/src/test/resources/invalid_report.lcov b/coverage-parser/src/test/resources/invalid_report.testlcov similarity index 100% rename from coverage-parser/src/test/resources/invalid_report.lcov rename to coverage-parser/src/test/resources/invalid_report.testlcov diff --git a/coverage-parser/src/test/resources/test_lcov.lcov b/coverage-parser/src/test/resources/test_lcov.testlcov similarity index 100% rename from coverage-parser/src/test/resources/test_lcov.lcov rename to coverage-parser/src/test/resources/test_lcov.testlcov diff --git a/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala b/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala index 65582f51..d1026fea 100644 --- a/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala +++ b/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala @@ -11,7 +11,7 @@ class LCOVParserTest extends WordSpec with BeforeAndAfterAll with Matchers with "LCOVParser" should { "identify if report is invalid" in { - val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/invalid_report.lcov")) + val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/invalid_report.testlcov")) reader.isLeft shouldBe true } @@ -26,12 +26,12 @@ class LCOVParserTest extends WordSpec with BeforeAndAfterAll with Matchers with } "identify if report is valid" in { - val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.lcov")) + val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.testlcov")) reader.isRight shouldBe true } "return a valid report" in { - val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.lcov")) + val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.testlcov")) val testReport = CoverageReport( 86, diff --git a/src/main/scala/com/codacy/rules/ReportRules.scala b/src/main/scala/com/codacy/rules/ReportRules.scala index 4dcd4d5d..23ba7d42 100644 --- a/src/main/scala/com/codacy/rules/ReportRules.scala +++ b/src/main/scala/com/codacy/rules/ReportRules.scala @@ -203,7 +203,7 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport { private[rules] def guessReportFiles(files: List[File], pathIterator: Iterator[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 diff --git a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala index 1c721a54..89e88adb 100644 --- a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala +++ b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala @@ -149,6 +149,14 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi reportEither should be('right) reportEither.right.value should be(List(new File("coverage-xml", "index.xml"))) } + + "find an lcov report" in { + val fileIterator = Iterator(new File("lcov.info"), new File("lcov.dat"), new File("foo.lcov"), new File("foobar.txt")) + val reportEither = components.reportRules.guessReportFiles(List.empty[File], fileIterator) + + reportEither should be('right) + reportEither.right.value.map(_.toString) should be(List("lcov.info", "lcov.dat", "foo.lcov")) + } } "validateFileAccess" should { From e58b7dbbcaea22ded6d0d960b7cad05e03ce8c4f Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Fri, 24 Sep 2021 12:51:22 +0200 Subject: [PATCH 2/4] clean: Revert some changes and run scalafmt --- .java-version | 2 +- .../{invalid_report.testlcov => invalid_report.lcov} | 0 .../test/resources/{test_lcov.testlcov => test_lcov.lcov} | 0 .../src/test/scala/com/codacy/parsers/LCOVParserTest.scala | 6 +++--- src/test/scala/com/codacy/rules/ReportRulesSpec.scala | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) rename coverage-parser/src/test/resources/{invalid_report.testlcov => invalid_report.lcov} (100%) rename coverage-parser/src/test/resources/{test_lcov.testlcov => test_lcov.lcov} (100%) diff --git a/.java-version b/.java-version index 62593409..3dedf4b8 100644 --- a/.java-version +++ b/.java-version @@ -1 +1 @@ -1.8 +oracle64-1.8.0.25 diff --git a/coverage-parser/src/test/resources/invalid_report.testlcov b/coverage-parser/src/test/resources/invalid_report.lcov similarity index 100% rename from coverage-parser/src/test/resources/invalid_report.testlcov rename to coverage-parser/src/test/resources/invalid_report.lcov diff --git a/coverage-parser/src/test/resources/test_lcov.testlcov b/coverage-parser/src/test/resources/test_lcov.lcov similarity index 100% rename from coverage-parser/src/test/resources/test_lcov.testlcov rename to coverage-parser/src/test/resources/test_lcov.lcov diff --git a/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala b/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala index d1026fea..65582f51 100644 --- a/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala +++ b/coverage-parser/src/test/scala/com/codacy/parsers/LCOVParserTest.scala @@ -11,7 +11,7 @@ class LCOVParserTest extends WordSpec with BeforeAndAfterAll with Matchers with "LCOVParser" should { "identify if report is invalid" in { - val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/invalid_report.testlcov")) + val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/invalid_report.lcov")) reader.isLeft shouldBe true } @@ -26,12 +26,12 @@ class LCOVParserTest extends WordSpec with BeforeAndAfterAll with Matchers with } "identify if report is valid" in { - val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.testlcov")) + val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.lcov")) reader.isRight shouldBe true } "return a valid report" in { - val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.testlcov")) + val reader = LCOVParser.parse(new File("."), new File("coverage-parser/src/test/resources/test_lcov.lcov")) val testReport = CoverageReport( 86, diff --git a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala index 89e88adb..c68f31d7 100644 --- a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala +++ b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala @@ -151,7 +151,8 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi } "find an lcov report" in { - val fileIterator = Iterator(new File("lcov.info"), new File("lcov.dat"), new File("foo.lcov"), new File("foobar.txt")) + val fileIterator = + Iterator(new File("lcov.info"), new File("lcov.dat"), new File("foo.lcov"), new File("foobar.txt")) val reportEither = components.reportRules.guessReportFiles(List.empty[File], fileIterator) reportEither should be('right) From 8070ba972fe805c05eedf5b8b012f2e3411de3c2 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Fri, 24 Sep 2021 13:42:53 +0200 Subject: [PATCH 3/4] fix: Fix failing test to not use the root directory --- .../scala/com/codacy/rules/ReportRules.scala | 15 +++++++++++---- .../scala/com/codacy/rules/ReportRulesSpec.scala | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/scala/com/codacy/rules/ReportRules.scala b/src/main/scala/com/codacy/rules/ReportRules.scala index 23ba7d42..630a2116 100644 --- a/src/main/scala/com/codacy/rules/ReportRules.scala +++ b/src/main/scala/com/codacy/rules/ReportRules.scala @@ -55,10 +55,14 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport { } def codacyCoverage(config: ReportConfig): Either[String, String] = { + codacyCoverage(config, rootProjectDirIterator) + } + + def codacyCoverage(config: ReportConfig, projectFiles: TraversableOnce[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) { @@ -196,11 +200,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], + pathIterator: TraversableOnce[File] + ): Either[String, List[File]] = { val JacocoRegex = """(jacoco.*\.xml)""".r val CoberturaRegex = """(cobertura\.xml)""".r val LCOVRegex = """(lcov\.info|lcov\.dat|.*\.lcov)""".r diff --git a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala index c68f31d7..f826a2f1 100644 --- a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala +++ b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala @@ -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[Seq[File]] = None + ) = { val reportRules = new ReportRules(coverageServices) val reportConfig = ReportConfig( @@ -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) } @@ -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(Seq.empty)) } "it is not able to parse report file" in { From be8916e610842364d16ee25048f3d61bf0848086 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Fri, 24 Sep 2021 13:55:25 +0200 Subject: [PATCH 4/4] clean: Use List instaed of Iterator --- .../scala/com/codacy/rules/ReportRules.scala | 14 +++++------ .../com/codacy/rules/ReportRulesSpec.scala | 24 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/scala/com/codacy/rules/ReportRules.scala b/src/main/scala/com/codacy/rules/ReportRules.scala index 630a2116..5dcbeba9 100644 --- a/src/main/scala/com/codacy/rules/ReportRules.scala +++ b/src/main/scala/com/codacy/rules/ReportRules.scala @@ -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], @@ -55,10 +56,10 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport { } def codacyCoverage(config: ReportConfig): Either[String, String] = { - codacyCoverage(config, rootProjectDirIterator) + codacyCoverage(config, rootProjectFiles) } - def codacyCoverage(config: ReportConfig, projectFiles: TraversableOnce[File]): Either[String, String] = { + def codacyCoverage(config: ReportConfig, projectFiles: List[File]): Either[String, String] = { withCommitUUID(config.baseConfig) { commitUUID => logAuthenticationToken(config) @@ -204,10 +205,7 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport { * @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: TraversableOnce[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|lcov\.dat|.*\.lcov)""".r @@ -220,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 { diff --git a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala index f826a2f1..697cf8b7 100644 --- a/src/test/scala/com/codacy/rules/ReportRulesSpec.scala +++ b/src/test/scala/com/codacy/rules/ReportRulesSpec.scala @@ -38,7 +38,7 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi coverageServices: CoverageServices, coverageReports: List[String], success: Boolean, - projectFiles: Option[Seq[File]] = None + projectFiles: Option[List[File]] = None ) = { val reportRules = new ReportRules(coverageServices) val reportConfig = @@ -65,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, projectFiles = Some(Seq.empty)) + assertCodacyCoverage(coverageServices, List(), success = false, projectFiles = Some(List.empty)) } "it is not able to parse report file" in { @@ -130,40 +130,40 @@ 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 fileIterator = - Iterator(new File("lcov.info"), new File("lcov.dat"), new File("foo.lcov"), new File("foobar.txt")) - val reportEither = components.reportRules.guessReportFiles(List.empty[File], fileIterator) + 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"))