Skip to content

Commit

Permalink
refactor: Remove unnecessary parameter from UtPlSqlSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed May 22, 2024
1 parent 7114796 commit 734b300
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import org.simpleframework.xml.core.Persister
import org.sonar.api.batch.fs.InputFile
import org.sonar.api.batch.measure.Metric
import org.sonar.api.batch.sensor.SensorContext
import org.sonar.api.config.Configuration
import org.sonar.api.measures.CoreMetrics
import org.sonar.api.notifications.AnalysisWarnings
import org.sonar.api.utils.WildcardPattern
Expand All @@ -34,15 +33,14 @@ import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import java.io.File
import java.io.Serializable

class TestResultImporter(private val conf: Configuration,
private val objectLocator: ObjectLocator,
class TestResultImporter(private val objectLocator: ObjectLocator,
private val analysisWarnings: AnalysisWarnings) {

private val logger: Logger = Loggers.getLogger(TestResultImporter::class.java)

fun execute(context: SensorContext) {
val reports = conf.getStringArray(UtPlSqlSensor.TEST_REPORT_PATH_KEY).flatMap {
getReports(context.fileSystem().baseDir(), it)
val reports = context.config().getStringArray(UtPlSqlSensor.TEST_REPORT_PATH_KEY).flatMap {
getReports(context, it)
}

for (report in reports) {
Expand All @@ -52,15 +50,16 @@ class TestResultImporter(private val conf: Configuration,
}
}

private fun getReports(baseDir: File, reportPath: String): List<File> {
private fun getReports(context: SensorContext, reportPath: String): List<File> {
val pattern = WildcardPattern.create(reportPath)
val baseDir = context.fileSystem().baseDir()
val matchingFiles = baseDir
.walkTopDown()
.filter { it.isFile && pattern.match(it.relativeTo(baseDir).invariantSeparatorsPath) }
.toMutableList()

if (matchingFiles.isEmpty()) {
if (conf.hasKey(UtPlSqlSensor.TEST_REPORT_PATH_KEY)) {
if (context.config().hasKey(UtPlSqlSensor.TEST_REPORT_PATH_KEY)) {
val file = File(reportPath)
if (!file.exists()) {
val formattedMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ package org.sonar.plsqlopen.utplsql
import org.sonar.api.batch.sensor.Sensor
import org.sonar.api.batch.sensor.SensorContext
import org.sonar.api.batch.sensor.SensorDescriptor
import org.sonar.api.config.Configuration
import org.sonar.api.notifications.AnalysisWarnings
import org.sonar.plsqlopen.PlSql
import org.sonar.plsqlopen.symbols.ObjectLocator

class UtPlSqlSensor(
conf: Configuration,
objectLocator: ObjectLocator,
analysisWarnings: AnalysisWarnings) : Sensor {
class UtPlSqlSensor(objectLocator: ObjectLocator, analysisWarnings: AnalysisWarnings) : Sensor {

private val testResultImporter = TestResultImporter(conf, objectLocator, analysisWarnings)
private val testResultImporter = TestResultImporter(objectLocator, analysisWarnings)

override fun describe(descriptor: SensorDescriptor) {
descriptor.name("Z PL/SQL Analyzer - utPLSQL Report Importer").onlyOnLanguage(PlSql.KEY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import org.sonar.api.batch.fs.InputFile
import org.sonar.api.batch.fs.internal.TestInputFileBuilder
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor
import org.sonar.api.batch.sensor.internal.SensorContextTester
import org.sonar.api.config.internal.ConfigurationBridge
import org.sonar.api.config.internal.MapSettings
import org.sonar.api.measures.CoreMetrics
import org.sonar.api.notifications.AnalysisWarnings
import org.sonar.plsqlopen.PlSql
Expand All @@ -40,7 +38,6 @@ import java.io.File

class UtPlSqlSensorTest {

private lateinit var settings: MapSettings
private lateinit var sensor: UtPlSqlSensor
private lateinit var context: SensorContextTester
private lateinit var objectLocator: ObjectLocator
Expand All @@ -49,10 +46,9 @@ class UtPlSqlSensorTest {
@BeforeEach
fun setUp() {
context = SensorContextTester.create(File("src/test/resources/org/sonar/plsqlopen/utplsql/"))
settings = MapSettings()
objectLocator = mock()
analysisWarnings = spy()
sensor = UtPlSqlSensor(ConfigurationBridge(settings), objectLocator, analysisWarnings)
sensor = UtPlSqlSensor(objectLocator, analysisWarnings)
}

@Test
Expand All @@ -72,7 +68,7 @@ class UtPlSqlSensorTest {

whenever(objectLocator.findTestObject(any(), any())).thenReturn(null)

settings.setProperty(UtPlSqlSensor.TEST_REPORT_PATH_KEY, "test-report-with-paths.xml")
context.settings().setProperty(UtPlSqlSensor.TEST_REPORT_PATH_KEY, "test-report-with-paths.xml")
sensor.execute(context)

val key = testFile.key()
Expand All @@ -93,7 +89,7 @@ class UtPlSqlSensorTest {
whenever(objectLocator.findTestObject(eq("test_package"), any())).thenReturn(
MappedObject("", PlSqlGrammar.CREATE_PACKAGE_BODY, PlSqlFile.Type.TEST, testFile.path(), testFile))

settings.setProperty(UtPlSqlSensor.TEST_REPORT_PATH_KEY, "test-report-with-paths.xml")
context.settings().setProperty(UtPlSqlSensor.TEST_REPORT_PATH_KEY, "test-report-with-paths.xml")
sensor.execute(context)

val key = testFile.key()
Expand All @@ -106,7 +102,7 @@ class UtPlSqlSensorTest {

@Test
fun invalidReport() {
settings.setProperty(UtPlSqlSensor.TEST_REPORT_PATH_KEY, "doesnotexists.xml")
context.settings().setProperty(UtPlSqlSensor.TEST_REPORT_PATH_KEY, "doesnotexists.xml")
sensor.execute(context)
verify(analysisWarnings).addUnique("No utPLSQL test report was found for sonar.zpa.tests.reportPaths using pattern doesnotexists.xml")
}
Expand Down

0 comments on commit 734b300

Please sign in to comment.