Skip to content

Commit

Permalink
clean: Use List instaed of Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Sep 24, 2021
1 parent 8070ba9 commit be8916e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
14 changes: 6 additions & 8 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,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)

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions src/test/scala/com/codacy/rules/ReportRulesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 {
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit be8916e

Please sign in to comment.