Skip to content

Commit

Permalink
feat: #276 use java nio to scan for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
rohwerj committed Sep 25, 2023
1 parent 96cdae4 commit df45121
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProcessTestCoveragePlugin : Plugin {
context.addExtensions(ProcessTestCoverageSensor::class.java, ProcessTestCoverageProjectSensor::class.java)
context.addExtension(ProcessTestCoveragePage::class.java)
context.addExtension(
PropertyDefinition.builder(ReportPathsProvider.REPORT_PATHS_PROPERTY_KEY)
PropertyDefinition.builder(ReportsProvider.REPORT_PATHS_PROPERTY_KEY)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.category("Process Test Coverage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class ProcessTestCoverageProjectSensor : ProjectSensor {
}

override fun execute(context: SensorContext) {
val reportPathsProvider = ReportPathsProvider(context)
val reportsProvider = ReportsProvider(context)
val importer = ReportImporter(context)
importReports(reportPathsProvider, importer)
importReports(reportsProvider, importer)
}

private fun importReports(reportPathsProvider: ReportPathsProvider, importer: ReportImporter) {
val reportPaths = reportPathsProvider.getPaths()
private fun importReports(reportsProvider: ReportsProvider, importer: ReportImporter) {
val reportPaths = reportsProvider.getProjectReports()
if (reportPaths.isEmpty()) {
LOG.info("No report imported, no coverage information will be imported by Process Test Coverage Report Importer")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class ProcessTestCoverageSensor : Sensor {
}

override fun execute(context: SensorContext) {
val reportPathsProvider = ReportPathsProvider(context)
val reportsProvider = ReportsProvider(context)
val importer = ReportImporter(context)
importReports(reportPathsProvider, importer)
importReports(reportsProvider, importer)
}

private fun importReports(reportPathsProvider: ReportPathsProvider, importer: ReportImporter) {
val reportPaths = reportPathsProvider.getPaths()
private fun importReports(reportsProvider: ReportsProvider, importer: ReportImporter) {
val reportPaths = reportsProvider.getModuleReports()
if (reportPaths.isEmpty()) {
LOG.info("No report imported, no coverage information will be imported by Process Test Coverage Report Importer")
return
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.camunda.community.process_test_coverage.sonar

import org.sonar.api.batch.sensor.SensorContext
import org.sonar.api.utils.log.Logger
import org.sonar.api.utils.log.Loggers
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import kotlin.streams.toList as streamToList


class ReportsProvider(
private val context: SensorContext
) {

companion object {
private val LOG: Logger = Loggers.get(ReportsProvider::class.java)
private val DEFAULT_PATHS = arrayOf("target/process-test-coverage/**/report.json")
const val REPORT_PATHS_PROPERTY_KEY = "sonar.process-test-coverage.jsonReportPaths"
}

fun getProjectReports(): Collection<Path> {
val pathPattern = getPathPattern("**/")
val matcher = FileSystems.getDefault().getPathMatcher("glob:$pathPattern")
return Files.find(context.fileSystem().baseDir().toPath(), Int.MAX_VALUE, { path, _ ->
matcher.matches(path)
}).streamToList()
}

fun getModuleReports(): Collection<Path> {
val pathPattern = getPathPattern("")
val matcher = FileSystems.getDefault().getPathMatcher("glob:$pathPattern")
return Files.find(context.fileSystem().baseDir().toPath(), Int.MAX_VALUE, { path, _ ->
val relativePath = context.fileSystem().baseDir().toPath().relativize(path)
matcher.matches(relativePath)
}).streamToList()
}

private fun getPathPattern(prefix: String): String {
val paths = context.config().getStringArray(REPORT_PATHS_PROPERTY_KEY)
.filter { it.isNotEmpty() }
.plus(DEFAULT_PATHS)
LOG.info("Configured paths are $paths")
val pattern = paths.joinToString(prefix = "{", postfix = "}", separator = ",") { "$prefix$it" }
LOG.info("Using pattern $pattern for reports")
return pattern
}

}

This file was deleted.

0 comments on commit df45121

Please sign in to comment.