diff --git a/CHANGELOG.md b/CHANGELOG.md index c111e281..09e57c92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed * Build Cache enabled, Mocks are not created +* Relaxer not picked correctly up on multiple test runs ### Security diff --git a/buildSrc/src/main/kotlin/tech/antibytes/gradle/kmock/config/KMockPublishingConfiguration.kt b/buildSrc/src/main/kotlin/tech/antibytes/gradle/kmock/config/KMockPublishingConfiguration.kt index 3ffa896a..c9ec3783 100644 --- a/buildSrc/src/main/kotlin/tech/antibytes/gradle/kmock/config/KMockPublishingConfiguration.kt +++ b/buildSrc/src/main/kotlin/tech/antibytes/gradle/kmock/config/KMockPublishingConfiguration.kt @@ -67,7 +67,7 @@ open class KMockPublishingConfiguration { gitWorkDirectory = "dev", url = "https://$gitHubOwnerPath/maven-dev", username = username, - password = passwordGitHubRepos + password = passwordGitHubRepos, ), GitRepositoryConfiguration( name = "Snapshot", diff --git a/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/Relaxer.kt b/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/Relaxer.kt new file mode 100644 index 00000000..85eac465 --- /dev/null +++ b/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/Relaxer.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved. + * + * Use of this source code is governed by Apache v2.0 + */ + +package tech.antibytes.kmock.example + +import tech.antibytes.kfixture.PublicApi +import tech.antibytes.kfixture.fixture +import tech.antibytes.kfixture.kotlinFixture +import tech.antibytes.kmock.Relaxer +import kotlin.native.concurrent.ThreadLocal +import kotlin.reflect.KClass + +@ThreadLocal +object Fixture { + var fixture: PublicApi.Fixture? = null +} + +@Relaxer +@Suppress("UNUSED_PARAMETER") +internal inline fun relax(id: String): T { + if (Fixture.fixture == null) { + Fixture.fixture = kotlinFixture() + } + + return Fixture.fixture!!.fixture() +} + +@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER") +internal fun relax(id: String, type0: KClass, type1: KClass>): T { + return Fixture.fixture!!.fixture() as T +} + +@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER") +internal fun relax(id: String, type0: KClass): T { + return Fixture.fixture!!.fixture() as T +} diff --git a/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/SampleControllerAutoStubRelaxedSpec.kt b/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/SampleControllerAutoStubRelaxedSpec.kt index 2dac3c0d..cba10042 100644 --- a/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/SampleControllerAutoStubRelaxedSpec.kt +++ b/examples/src/commonTest/kotlin/tech/antibytes/kmock/example/SampleControllerAutoStubRelaxedSpec.kt @@ -11,12 +11,10 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import tech.antibytes.kfixture.PublicApi import tech.antibytes.kfixture.fixture import tech.antibytes.kfixture.kotlinFixture import tech.antibytes.kfixture.listFixture import tech.antibytes.kmock.MockCommon -import tech.antibytes.kmock.Relaxer import tech.antibytes.kmock.example.contract.ExampleContract import tech.antibytes.kmock.example.contract.ExampleContract.SampleDomainObject import tech.antibytes.kmock.example.contract.ExampleContract.SampleLocalRepository @@ -36,36 +34,9 @@ import tech.antibytes.util.test.coroutine.runBlockingTestWithTimeoutInScope import tech.antibytes.util.test.fulfils import tech.antibytes.util.test.mustBe import kotlin.js.JsName -import kotlin.native.concurrent.ThreadLocal -import kotlin.reflect.KClass import kotlin.test.BeforeTest import kotlin.test.Test -@ThreadLocal -object Fixture { - var fixture: PublicApi.Fixture? = null -} - -@Relaxer -@Suppress("UNUSED_PARAMETER") -internal inline fun relax(id: String): T { - if (Fixture.fixture == null) { - Fixture.fixture = kotlinFixture() - } - - return Fixture.fixture!!.fixture() -} - -@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER") -internal fun relax(id: String, type0: KClass, type1: KClass>): T { - return Fixture.fixture!!.fixture() as T -} - -@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER") -internal fun relax(id: String, type0: KClass): T { - return Fixture.fixture!!.fixture() as T -} - @MockCommon( SampleRemoteRepository::class, SampleLocalRepository::class, diff --git a/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/ProcessorContract.kt b/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/ProcessorContract.kt index 576b02fd..7c181c2d 100644 --- a/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/ProcessorContract.kt +++ b/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/ProcessorContract.kt @@ -50,7 +50,8 @@ import tech.antibytes.kmock.Relaxer as RelaxationAnnotation internal interface ProcessorContract { data class Relaxer( val packageName: String, - val functionName: String + val functionName: String, + val source: KSFile, ) data class Options( diff --git a/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregator.kt b/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregator.kt index 2b0df941..de32568f 100644 --- a/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregator.kt +++ b/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregator.kt @@ -55,9 +55,11 @@ internal class KMockRelaxationAggregator( return if (annotatedSymbol is KSFunctionDeclaration) { validateRelaxer(annotatedSymbol) + Relaxer( annotatedSymbol.packageName.asString(), - annotatedSymbol.simpleName.asString() + annotatedSymbol.simpleName.asString(), + annotatedSymbol.containingFile!! ) } else { null diff --git a/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/mock/KMockGenerator.kt b/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/mock/KMockGenerator.kt index 3dffa2e6..2f4b963b 100644 --- a/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/mock/KMockGenerator.kt +++ b/kmock-processor/src/main/kotlin/tech/antibytes/kmock/processor/mock/KMockGenerator.kt @@ -476,6 +476,14 @@ internal class KMockGenerator( return mock.build() } + private fun List.amendRelaxer(relaxer: Relaxer?): List { + return if (relaxer == null) { + this + } else { + this.toMutableList().also { it.add(relaxer.source) } + } + } + private fun writeMock( template: KSClassDeclaration, parents: TemplateMultiSource?, @@ -520,7 +528,7 @@ internal class KMockGenerator( file.build().writeTo( codeGenerator = codeGenerator, aggregating = true, - originatingKSFiles = dependencies + originatingKSFiles = dependencies.amendRelaxer(relaxer) ) } diff --git a/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/KMockProcessorSpec.kt b/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/KMockProcessorSpec.kt index 8f856277..cd368c61 100644 --- a/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/KMockProcessorSpec.kt +++ b/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/KMockProcessorSpec.kt @@ -289,7 +289,7 @@ class KMockProcessorSpec { val entryPointGenerator: MockFactoryEntryPointGenerator = mockk() val filter: ProcessorContract.SourceFilter = mockk() - val relaxer = Relaxer(fixture.fixture(), fixture.fixture()) + val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk()) val illegal: List = listOf(mockk()) @@ -487,7 +487,7 @@ class KMockProcessorSpec { val entryPointGenerator: MockFactoryEntryPointGenerator = mockk() val filter: ProcessorContract.SourceFilter = mockk() - val relaxer = Relaxer(fixture.fixture(), fixture.fixture()) + val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk()) val illegal: List = listOf(mockk()) @@ -853,7 +853,7 @@ class KMockProcessorSpec { val entryPointGenerator: MockFactoryEntryPointGenerator = mockk() val filter: ProcessorContract.SourceFilter = mockk() - val relaxer = Relaxer(fixture.fixture(), fixture.fixture()) + val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk()) val illegal: List = listOf(mockk()) @@ -1011,7 +1011,7 @@ class KMockProcessorSpec { val entryPointGenerator: MockFactoryEntryPointGenerator = mockk() val filter: ProcessorContract.SourceFilter = mockk() - val relaxer = Relaxer(fixture.fixture(), fixture.fixture()) + val relaxer = Relaxer(fixture.fixture(), fixture.fixture(), mockk()) val illegal: List = listOf(mockk()) diff --git a/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregatorSpec.kt b/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregatorSpec.kt index 584a80ef..498598c0 100644 --- a/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregatorSpec.kt +++ b/kmock-processor/src/test/kotlin/tech/antibytes/kmock/processor/aggregation/KMockRelaxationAggregatorSpec.kt @@ -9,6 +9,7 @@ package tech.antibytes.kmock.processor.aggregation import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSFile import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.google.devtools.ksp.symbol.KSTypeParameter import com.google.devtools.ksp.symbol.KSTypeReference @@ -105,6 +106,7 @@ class KMockRelaxationAggregatorSpec { every { source.packageName.asString() } returns packageName every { source.simpleName.asString() } returns functionName + every { source.containingFile } returns mockk() every { logger.error(any()) } just Runs @@ -151,6 +153,7 @@ class KMockRelaxationAggregatorSpec { every { source.packageName.asString() } returns packageName every { source.simpleName.asString() } returns functionName + every { source.containingFile } returns mockk() every { logger.error(any()) } just Runs @@ -197,6 +200,7 @@ class KMockRelaxationAggregatorSpec { every { source.packageName.asString() } returns packageName every { source.simpleName.asString() } returns functionName + every { source.containingFile } returns mockk() every { logger.error(any()) } just Runs @@ -247,6 +251,7 @@ class KMockRelaxationAggregatorSpec { every { source.packageName.asString() } returns packageName every { source.simpleName.asString() } returns functionName + every { source.containingFile } returns mockk() every { logger.error(any()) } just Runs @@ -293,6 +298,7 @@ class KMockRelaxationAggregatorSpec { every { source.packageName.asString() } returns packageName every { source.simpleName.asString() } returns functionName + every { source.containingFile } returns mockk() every { logger.error(any()) } just Runs @@ -322,6 +328,7 @@ class KMockRelaxationAggregatorSpec { val packageName: String = fixture.fixture() val parameter: KSValueParameter = mockk() val typeParameter: KSTypeParameter = mockk() + val dependency: KSFile = mockk() every { resolver.getSymbolsWithAnnotation(any(), any()) @@ -339,6 +346,7 @@ class KMockRelaxationAggregatorSpec { every { source.packageName.asString() } returns packageName every { source.simpleName.asString() } returns functionName + every { source.containingFile } returns dependency every { logger.error(any()) } just Runs @@ -348,7 +356,7 @@ class KMockRelaxationAggregatorSpec { // Then verify(exactly = 0) { logger.error(any()) } - actual mustBe ProcessorContract.Relaxer(packageName, functionName) + actual mustBe ProcessorContract.Relaxer(packageName, functionName, dependency) verify(exactly = 1) { resolver.getSymbolsWithAnnotation(Relaxer::class.qualifiedName!!, false) }