Skip to content

Commit

Permalink
Merge pull request #444 from codacy/support-file-globs
Browse files Browse the repository at this point in the history
feature: Support globs in --coverage-reports
  • Loading branch information
rubencodacy committed Feb 7, 2023
2 parents 5d63025 + 63ad6d9 commit 2faff0f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Usage: codacy-coverage-reporter report
--sleep-time <Sets a specified time, in milliseconds, to be used when waiting between retries. By default, the value is 10 seconds>
--num-retries <Sets a number of retries in case of failure. By default, the value is 3 times>
--language | -l <language associated with your coverage report>
--coverage-reports | -r <your project coverage file name>
--coverage-reports | -r <your project coverage file name (supports globs)>
--partial <if the report is partial>
--prefix <the project path prefix>
--force-coverage-parser <your coverage parser>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ case class Report(
language: Option[String],
@Hidden @Name("f")
forceLanguage: Int @@ Counter = Tag.of(0),
@Name("r") @ValueDescription("your project coverage file name")
@Name("r") @ValueDescription("your project coverage file name (supports globs)")
coverageReports: Option[List[File]],
@ValueDescription("if the report is partial")
partial: Int @@ Counter = Tag.of(0),
Expand Down
24 changes: 22 additions & 2 deletions src/main/scala/com/codacy/rules/ReportRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.codacy.transformation.PathPrefixer
import wvlet.log.LogSupport

import java.io.File
import java.nio.file.Files
import java.nio.file.{FileSystems, Files}
import scala.collection.JavaConverters._

class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
Expand Down Expand Up @@ -222,6 +222,8 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
report.fileReports.flatMap(file => Languages.forPath(file.filename).map(_.name)).distinct
}

private val fileSystems = FileSystems.getDefault()

/**
* Guess the report file
*
Expand Down Expand Up @@ -262,7 +264,25 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
else
Right(foundFiles)
case value =>
Right(value)
Right {
try {
value.flatMap { file =>
val foundWithGlob = {
val matcher = fileSystems.getPathMatcher(s"glob:$file")
projectFiles.filter(f => matcher.matches(f.toPath()))
}
// When the glob doesn't match any file, we return it
// as a file so the later stages will fail instead of silently
// filtering it
if (foundWithGlob.isEmpty) file :: Nil
else foundWithGlob
}.distinct
} catch {
case util.control.NonFatal(e) =>
logger.debug(s"""Failed to read "$value" as file glob. Cause $e""")
value
}
}
}
}

Expand Down
44 changes: 42 additions & 2 deletions src/test/scala/com/codacy/rules/ReportRulesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
}

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

reportEither should be('right)
reportEither.right.value should be(files)
Expand All @@ -197,6 +198,45 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
reportEither should be('right)
reportEither.right.value.map(_.toString) should be(List("lcov.info", "lcov.dat", "foo.lcov"))
}

"support file globs" in {
val projectFiles =
List(new File("foo.lcov"), new File("lcov.info"), new File("bar.lcov"), new File("foobar.txt"))
val reportEither = components.reportRules.guessReportFiles(new File("*.lcov") :: Nil, projectFiles)

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

"return files untouched when glob doesn't find anything" in {
val reportEither = components.reportRules.guessReportFiles(new File("unexisting.xml") :: Nil, projectFiles = Nil)

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

"support file globs with directories" in {
val projectFiles =
List(
new File("target/dir1/coverage.xml"),
new File("dir2/lcov.info"),
new File("dir1/bar.lcov"),
new File("dir2/other.xml")
)
val reportEither = components.reportRules.guessReportFiles(new File("**/*.xml") :: Nil, projectFiles)

reportEither should be('right)
reportEither.right.value.map(_.toString) should be(List("target/dir1/coverage.xml", "dir2/other.xml"))
}

"remove duplicates when using file globs" in {
val projectFiles = List(new File("coverage.xml"))
val reportEither =
components.reportRules.guessReportFiles(new File("*.xml") :: new File("coverage.xml") :: Nil, projectFiles)

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

"validateFileAccess" should {
Expand Down

0 comments on commit 2faff0f

Please sign in to comment.