Skip to content

Commit

Permalink
fix: TMS-29853: fix stringSpec, shouldSpec, refactor, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taipoxinous committed Nov 29, 2024
1 parent 38dc781 commit 9e5bd4b
Show file tree
Hide file tree
Showing 22 changed files with 832 additions and 308 deletions.
3 changes: 2 additions & 1 deletion testit-adapter-kotest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "ru.testit"
version = "0.1.1"
version = "0.2.0"

java {
withJavadocJar()
Expand All @@ -32,6 +32,7 @@ dependencies {
implementation("ch.qos.logback:logback-core:1.5.8")
implementation("io.kotest:kotest-framework-datatest:5.9.1")

testImplementation("io.mockk:mockk:1.13.12")
testImplementation(libs.kotest.assertions.core)
testImplementation(libs.kotest.runner.junit5)
testImplementation(libs.jackson.module.kotlin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ru.testit.listener
import ru.testit.models.ItemStatus
import ru.testit.models.StepResult
import ru.testit.models.TestItContext
import ru.testit.models.TestResult
import ru.testit.models.TestResultCommon
import java.util.Objects.nonNull
import java.util.function.Consumer

Expand All @@ -12,8 +12,8 @@ object Consumers {
/**
* Sets the item status and optional throwable for a test result.
*/
fun setStatus(status: ItemStatus, throwable: Throwable?): Consumer<TestResult> {
return Consumer<TestResult> { result: TestResult ->
fun setStatus(status: ItemStatus, throwable: Throwable?): Consumer<TestResultCommon> {
return Consumer<TestResultCommon> { result: TestResultCommon ->
result.itemStatus = status
if (nonNull(throwable)) {
result.throwable = throwable
Expand All @@ -36,8 +36,8 @@ object Consumers {
/**
* Sets the context properties for a test result.
*/
fun setContext(context: TestItContext): Consumer<TestResult> {
return Consumer<TestResult> { result: TestResult ->
fun setContext(context: TestItContext): Consumer<TestResultCommon> {
return Consumer<TestResultCommon> { result: TestResultCommon ->
result.externalId = context.externalId ?: result.externalId
result.description = context.description ?: result.description
result.workItemIds = context.workItemIds ?: result.workItemIds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import io.kotest.core.test.TestResult
import kotlin.reflect.KClass

class TestItReporter(
private val isStepContainers: Boolean = false,
) : BeforeTestListener, AfterTestListener, InstantiationErrorListener, ProjectListener,
BeforeSpecListener, AfterSpecListener, BeforeInvocationListener, AfterInvocationListener,
AfterEachListener, FinalizeSpecListener, AfterContainerListener {

val writer = TestItWriter(isStepContainers)
val writer = TestItWriter()

// beforeAll analogue
override suspend fun beforeSpec(spec: Spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import java.lang.reflect.InvocationTargetException
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
import ru.testit.models.TestResult as TestItTestResult
import ru.testit.models.TestResultCommon as TestItTestResult




class TestItWriter (
private var isStepContainers: Boolean = false
) {
class TestItWriter () {
private val LOGGER = LoggerFactory.getLogger(javaClass)
private val debug = AdapterUtils.debug(LOGGER)

Expand All @@ -41,37 +39,35 @@ class TestItWriter (
* Checks `testCase` to be valid test or [TestItReporter.isStepContainers] to be true
*/
private val isTestOrContainersEnabled: (testCase: TestCase) -> Boolean = {
testCase: TestCase -> testCase.type == TestType.Test || isStepContainers }
testCase: TestCase -> testCase.type == TestType.Container
|| testCase.type == TestType.Test || testCase.type == TestType.Dynamic }

private val executableTestService = ExecutableTestService(
executableTest = ThreadLocal.withInitial { ExecutableTest() }
)
private val stepService = StepService(
adapterManager = adapterManager,
uuids = uuids,
executableTestService = executableTestService,
isStepContainers = isStepContainers
executableTestService = executableTestService
)

private val testService = TestService(
adapterManager = adapterManager,
uuids = uuids,
executableTestService = executableTestService,
isStepContainers = isStepContainers
executableTestService = executableTestService
)

private val fixtureService = FixtureService(
adapterManager = adapterManager,
executableTestService = executableTestService,
testService = testService,
isStepContainers = isStepContainers
testService = testService
)


/**
* Used for `instantiationError` error handling
*/
suspend fun onInstantiationError(kclass: KClass<*>, t: Throwable) {
fun onInstantiationError(kclass: KClass<*>, t: Throwable) {
var uuid = UUID.randomUUID()

var result = TestItTestResult(
Expand All @@ -90,7 +86,7 @@ class TestItWriter (
/**
* @see runContainers
*/
suspend fun onBeforeAll(spec: Spec) {
fun onBeforeAll(spec: Spec) {
var rootTestName = spec.rootTests()[0].name.testName
debug("Before all: {}",rootTestName)
runContainers(rootTestName)
Expand All @@ -99,7 +95,7 @@ class TestItWriter (
/**
* @see stopContainers
*/
suspend fun onAfterAll(spec: Spec) {
fun onAfterAll(spec: Spec) {
var rootTestName = spec.rootTests()[0].name.testName
debug("After all: {}", rootTestName)
stopContainers(rootTestName)
Expand All @@ -117,7 +113,7 @@ class TestItWriter (
* @see FixtureService.registerAfterTestFixture
*/
fun registerBeforeAfterExtensions(testCase: TestCase) {
if (testCase.isStep(isStepContainers)) {
if (testCase.isStep()) {
return
}
executableTestService.refreshUuid()
Expand All @@ -139,7 +135,7 @@ class TestItWriter (
* @see FixtureService.onBeforeTestOk
*/
fun finishBeforeTestIfExists(testCase: TestCase) {
if (testCase.isStep(isStepContainers)) {
if (testCase.isStep()) {
return
}
// write beforeTest successful results
Expand All @@ -156,7 +152,7 @@ class TestItWriter (
* @see FixtureService.updateAfterTestTime
*/
fun onAfterTestInvocation(testCase: TestCase) {
if (testCase.isStep(isStepContainers)) {
if (testCase.isStep()) {
return
}
if (AdapterUtils.isAfterTestRegistered(testCase)) {
Expand All @@ -171,18 +167,17 @@ class TestItWriter (
* @see StepService.onStepStart
* @see onTestStart
*/
suspend fun onBeforeTestInvocation(testCase: TestCase) {
fun onBeforeTestInvocation(testCase: TestCase) {
if (!isTestOrContainersEnabled(testCase)) {
return
}
var isContainer = testCase.type.name == "Container"
var isStep = testCase.isStep(isStepContainers);

var isStep = testCase.isStep();
if (isContainer) {
if (!isStepContainers) {
debug("we are inside default container", "")
return
}
// if (!isStepContainers) {
// debug("we are inside default container", "")
// return
// }
debug("we are in step container", "")
}
if (isStep) {
Expand All @@ -204,16 +199,12 @@ class TestItWriter (
return
}
var isContainer = testCase.type.name == "Container"
var isStepContainer = isContainer && isStepContainers

if (isContainer && !isStepContainers) {
return
}
if (testCase.isStep(isStepContainers)) {
if (testCase.isStep()) {
return stepService.stopStepWithResult(testCase, result)
}
// no mark in describe
if (isContainer && !testCase.isStepContainer(isStepContainers)) {
if (isContainer && !testCase.isStepContainer()) {
return
}
testService.stopTestWithResult(testCase, result)
Expand All @@ -236,7 +227,7 @@ class TestItWriter (
* @see TestService.onTestStart
* @see AdapterManager.updateClassContainer
*/
private suspend fun onTestStart(testCase: TestCase) {
private fun onTestStart(testCase: TestCase) {
debug("Intercept test: {}", testCase.name)
val testName = testCase.spec.rootTests()[0].name.testName
// val uuid = getExecTestWithUuid()
Expand All @@ -254,7 +245,7 @@ class TestItWriter (
* Create new test run.
* Init main and class containers using [AdapterManager] api
*/
private suspend fun runContainers(rootTestName: String) {
private fun runContainers(rootTestName: String) {
adapterManager.startTests()
lastMainContainerId = UUID.randomUUID().toString()
val mainContainer = MainContainer(
Expand All @@ -268,7 +259,7 @@ class TestItWriter (
adapterManager.startClassContainer(lastMainContainerId!!, classContainer)
}

private suspend fun stopContainers(rootTestName: String) {
private fun stopContainers(rootTestName: String) {
adapterManager.stopClassContainer(Utils.getHash(rootTestName))
adapterManager.stopMainContainer(lastMainContainerId!!)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package ru.testit.services
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.engine.extensions.ExtensionException
import org.jetbrains.annotations.VisibleForTesting
import org.slf4j.LoggerFactory
import ru.testit.models.ClassContainer
import ru.testit.models.FixtureResult
import ru.testit.models.ItemStage
import ru.testit.models.ItemStatus
import ru.testit.utils.*
import java.util.*
import kotlin.reflect.KVisibility

class FixtureService (
private val adapterManager: AdapterManager,
private val executableTestService: ExecutableTestService,
private val testService: TestService,
private val isStepContainers: Boolean
private val testService: TestService
) {
var beforeFixtureUUID: String? = null
var afterFixtureUUID: String? = null
Expand Down Expand Up @@ -88,7 +89,7 @@ class FixtureService (
*/
suspend fun handleFixturesFails(testCase: TestCase, result: TestResult,
beforeTestStart: Long, afterTestStart: Long): Boolean {
if (testCase.isStep(isStepContainers)) {
if (testCase.isStep()) {
return false
}
var isAfterTestFailed = false
Expand Down Expand Up @@ -125,8 +126,8 @@ class FixtureService (
return false
}


private fun onAfterTestFailed(testCase: TestCase, start: Long, stop: Long) {
@VisibleForTesting
internal fun onAfterTestFailed(testCase: TestCase, start: Long, stop: Long) {
var exception = testCase.afterTestThrowable()
var tearDownName = testCase.teardownName() ?: "TearDown"
debug("After test finished with error: {}", exception.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.testit.services
import io.kotest.common.TestPath
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import org.jetbrains.annotations.VisibleForTesting
import org.slf4j.LoggerFactory
import ru.testit.listener.Consumers
import ru.testit.models.ItemStatus
Expand All @@ -13,8 +14,7 @@ import java.util.concurrent.ConcurrentHashMap
class StepService (
private val adapterManager: AdapterManager,
private val uuids: ConcurrentHashMap<TestPath, String>,
private val executableTestService: ExecutableTestService,
private val isStepContainers: Boolean
private val executableTestService: ExecutableTestService
) {
private val LOGGER = LoggerFactory.getLogger(javaClass)

Expand Down Expand Up @@ -61,7 +61,7 @@ class StepService (
}
if (result is TestResult.Ignored) {
debug("Step ignored: {}", step.name)
return updateStepAndStop(stepUuid, ItemStatus.SKIPPED, result.errorOrNull!!)
return updateStepAndStop(stepUuid, ItemStatus.SKIPPED, result.errorOrNull)
}
if (result is TestResult.Failure || result is TestResult.Error) {
debug("Step failed: {}", step.name)
Expand All @@ -80,7 +80,8 @@ class StepService (
executableTestService.setAsFailedBy(cause)
}

private fun getStepUuid(step: TestCase): String {
@VisibleForTesting
internal fun getStepUuid(step: TestCase): String {
val stepPath: String = step.parent!!.name.testName +
step.name.testName
return Utils.getHash(stepPath)
Expand Down
Loading

0 comments on commit 9e5bd4b

Please sign in to comment.