Skip to content

Commit

Permalink
TS-38628 Registry and reworked builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Avanatiker committed Dec 13, 2024
1 parent 68181f5 commit 12d4308
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 376 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.teamscale.test_impacted.engine

import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import com.teamscale.test_impacted.engine.executor.TestwiseCoverageCollectingExecutionListener
import com.teamscale.test_impacted.test_descriptor.TestDescriptorResolverRegistry.getTestDescriptorResolver
import com.teamscale.test_impacted.test_descriptor.TestDescriptorUtils.getAvailableTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.teamscale.test_impacted.engine

import com.teamscale.client.TestDetails
import com.teamscale.report.ReportUtils.writeTestExecutionReport
import com.teamscale.report.ReportUtils.writeTestListReport
import com.teamscale.report.testwise.model.TestExecution
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import java.io.File
import java.io.IOException
import java.util.logging.Level
import java.util.logging.Logger

/** Class for writing test data to a report directory. */
open class TestDataWriter(private val reportDirectory: File) {
/** Writes the given test executions to a report file. */
fun dumpTestExecutions(testExecutions: List<TestExecution>) {
val file = File(reportDirectory, "test-execution.json")
try {
writeTestExecutionReport(file, testExecutions)
} catch (e: IOException) {
LOGGER.log(Level.SEVERE, e) { "Error while writing report to file: $file" }
}
}

/** Writes the given test details to a report file. */
fun dumpTestDetails(testDetails: List<TestDetails>) {
val file = File(reportDirectory, "test-list.json")
try {
writeTestListReport(file, ArrayList(testDetails))
} catch (e: IOException) {
LOGGER.log(Level.SEVERE, e) { "Error while writing report to file: $file" }
}
}

companion object {
private val LOGGER = createLogger()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.teamscale.test_impacted.engine

import org.junit.platform.commons.util.ClassLoaderUtils
import org.junit.platform.engine.TestEngine
import java.util.*
import java.util.function.Function
import java.util.stream.Collectors

/** The test engine registry containing all */
open class TestEngineRegistry(
includedTestEngineIds: Set<String>,
excludedTestEngineIds: Set<String>
) : Iterable<TestEngine> {
private val testEnginesById: Map<String, TestEngine>

init {
var otherTestEngines = loadOtherTestEngines(excludedTestEngineIds)

// If there are no test engines set we don't need to filter but simply use all other test engines.
if (includedTestEngineIds.isNotEmpty()) {
otherTestEngines = otherTestEngines.filter { testEngine ->
includedTestEngineIds.contains(testEngine.id)
}
}

testEnginesById = otherTestEngines.associateBy { it.id }
}

/**
* Uses the [ServiceLoader] to discover all [TestEngine]s but the [ImpactedTestEngine] and the
* excluded test engines.
*/
private fun loadOtherTestEngines(excludedTestEngineIds: Set<String>) =
ServiceLoader.load(
TestEngine::class.java, ClassLoaderUtils.getDefaultClassLoader()
).filter {
ImpactedTestEngine.ENGINE_ID != it.id && !excludedTestEngineIds.contains(it.id)
}

/** Returns the [TestEngine] for the engine id or null if none is present. */
fun getTestEngine(engineId: String) = testEnginesById[engineId]

override fun iterator() =
testEnginesById.values.sortedBy { it.id }.iterator()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class ImpactedTestsProvider(
private val endCommit: CommitDescriptor,
private val endRevision: String,
private val repository: String,
@JvmField val partition: String,
val partition: String,
private val includeNonImpacted: Boolean,
private val includeAddedTests: Boolean,
private val includeFailedAndSkipped: Boolean
Expand All @@ -36,8 +36,7 @@ open class ImpactedTestsProvider(
val response = client
.getImpactedTests(
availableTestDetails, baseline, baselineRevision, endCommit, endRevision, repository,
listOf(partition),
includeNonImpacted, includeAddedTests, includeFailedAndSkipped
listOf(partition), includeNonImpacted, includeAddedTests, includeFailedAndSkipped
)

if (response.isSuccessful) {
Expand Down Expand Up @@ -75,7 +74,7 @@ open class ImpactedTestsProvider(
availableTestDetails: List<ClusteredTestDetails>
): Boolean {
val returnedTests = testClusters.stream().mapToLong {
it.tests!!.size.toLong()
it.tests?.size?.toLong() ?: 0
}.sum()
if (!includeNonImpacted) {
logger.info { "Received $returnedTests impacted tests of ${availableTestDetails.size} available tests." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ImpactedTestsSorter(private val impactedTestsProvider: ImpactedTestsProvid

val testClusters = impactedTestsProvider.getImpactedTestsFromTeamscale(availableTests.testList)

if (testClusters == null) {
if (testClusters.isEmpty()) {
ImpactedTestEngine.LOGGER.fine { "Falling back to execute all!" }
return
}
Expand Down
Loading

0 comments on commit 12d4308

Please sign in to comment.