From 290fdf6370c696eb1321da3363189992a84c36d0 Mon Sep 17 00:00:00 2001 From: Sergey Chelombitko Date: Tue, 25 Jun 2024 18:50:33 +0100 Subject: [PATCH] Avoid using Koin GlobalContext --- .../kotlin/com/malinskiy/marathon/Marathon.kt | 42 ++++--------------- .../com/malinskiy/marathon/di/Modules.kt | 11 +++-- .../marathon/vendor/VendorConfiguration.kt | 10 ----- .../scenario/DeviceFilteringScenario.kt | 1 - .../scenario/DisconnectingScenarios.kt | 1 - .../marathon/scenario/SuccessScenarios.kt | 1 - .../marathon/scenario/UncompletedScenarios.kt | 1 - .../malinskiy/marathon/spek/DeclareMock.kt | 27 ------------ .../com/malinskiy/marathon/spek/KoinSpek.kt | 41 ------------------ .../marathon/worker/WorkerRunnable.kt | 27 ++++++------ .../marathon/android/AndroidConfiguration.kt | 19 +-------- .../malinskiy/marathon/android/di/Modules.kt | 3 ++ .../marathon/android/spek/KoinSpek.kt | 41 ------------------ .../android/AndroidDeviceTestRunnerSpek.kt | 3 -- .../marathon/android/spek/KoinSpek.kt | 41 ------------------ .../test/MarathonFactoryExtensions.kt | 13 ------ .../marathon/test/StubLogsProvider.kt | 10 +++++ .../test/StubMarathonLogConfigurator.kt | 8 ++++ .../marathon/test/TestVendorConfiguration.kt | 40 ++++++++---------- .../test/factory/ConfigurationFactory.kt | 17 ++------ .../marathon/test/factory/MarathonFactory.kt | 6 ++- 21 files changed, 73 insertions(+), 290 deletions(-) delete mode 100644 core/src/test/kotlin/com/malinskiy/marathon/spek/DeclareMock.kt delete mode 100644 core/src/test/kotlin/com/malinskiy/marathon/spek/KoinSpek.kt delete mode 100644 vendor/vendor-android/base/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt delete mode 100644 vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt delete mode 100644 vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/MarathonFactoryExtensions.kt create mode 100644 vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubLogsProvider.kt create mode 100644 vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubMarathonLogConfigurator.kt diff --git a/core/src/main/kotlin/com/malinskiy/marathon/Marathon.kt b/core/src/main/kotlin/com/malinskiy/marathon/Marathon.kt index c267314d9..93f295d4b 100644 --- a/core/src/main/kotlin/com/malinskiy/marathon/Marathon.kt +++ b/core/src/main/kotlin/com/malinskiy/marathon/Marathon.kt @@ -20,6 +20,7 @@ import com.malinskiy.marathon.execution.TestParser import com.malinskiy.marathon.execution.TestShard import com.malinskiy.marathon.execution.progress.ProgressReporter import com.malinskiy.marathon.io.AttachmentManager +import com.malinskiy.marathon.log.MarathonLogConfigurator import com.malinskiy.marathon.log.MarathonLogging import com.malinskiy.marathon.report.logs.LogsProvider import com.malinskiy.marathon.test.Test @@ -27,7 +28,6 @@ import com.malinskiy.marathon.test.toTestName import com.malinskiy.marathon.time.Timer import com.malinskiy.marathon.vendor.VendorConfiguration import kotlinx.coroutines.runBlocking -import org.koin.core.context.stopKoin import java.util.* import kotlin.coroutines.coroutineContext @@ -35,14 +35,18 @@ private val log = MarathonLogging.logger {} class Marathon( val configuration: Configuration, + private val componentInfoExtractor: ComponentInfoExtractor, + private val deviceProvider: DeviceProvider, private val tracker: TrackerInternal, private val analytics: Analytics, private val testCacheLoader: TestCacheLoader, private val testCacheSaver: TestCacheSaver, + private val testParser: TestParser, private val cachedTestsReporter: CacheTestReporter, private val progressReporter: ProgressReporter, private val attachmentManager: AttachmentManager, private val strictRunChecker: StrictRunChecker, + private val logConfigurator: MarathonLogConfigurator, private val logsProvider: LogsProvider, private val track: Track, private val timer: Timer @@ -53,41 +57,13 @@ class Marathon( private val configurationValidator = LogicalConfigurationValidator() private val strictRunProcessor = StrictRunProcessor(configuration.strictRunFilterConfiguration) - private lateinit var deviceProvider: DeviceProvider - private lateinit var testParser: TestParser private lateinit var scheduler: Scheduler private lateinit var hook: ShutdownHook private fun configureLogging(vendorConfiguration: VendorConfiguration) { MarathonLogging.debug = configuration.debug - vendorConfiguration.logConfigurator()?.configure(vendorConfiguration) - } - - private suspend fun loadDeviceProvider(vendorConfiguration: VendorConfiguration): DeviceProvider { - val vendorDeviceProvider = vendorConfiguration.deviceProvider() - ?: ServiceLoader.load(DeviceProvider::class.java).first() - - vendorDeviceProvider.initialize(configuration.vendorConfiguration) - return vendorDeviceProvider - } - - private fun loadTestParser(vendorConfiguration: VendorConfiguration): TestParser { - val vendorTestParser = vendorConfiguration.testParser() - if (vendorTestParser != null) { - return vendorTestParser - } - val loader = ServiceLoader.load(TestParser::class.java) - return loader.first() - } - - private fun loadComponentInfoExtractor(vendorConfiguration: VendorConfiguration): ComponentInfoExtractor { - val componentInfoExtractor = vendorConfiguration.componentInfoExtractor() - if (componentInfoExtractor != null) { - return componentInfoExtractor - } - val loader = ServiceLoader.load(ComponentInfoExtractor::class.java) - return loader.first() + logConfigurator.configure(vendorConfiguration) } fun run() = runBlocking { @@ -113,7 +89,7 @@ class Marathon( suspend fun runAsync(): Boolean { start() - val componentInfo = loadComponentInfoExtractor(configuration.vendorConfiguration).extract(configuration) + val componentInfo = componentInfoExtractor.extract(configuration) scheduleTests(componentInfo) return stopAndWaitForCompletion() @@ -122,8 +98,6 @@ class Marathon( override suspend fun start() { configureLogging(configuration.vendorConfiguration) - testParser = loadTestParser(configuration.vendorConfiguration) - deviceProvider = loadDeviceProvider(configuration.vendorConfiguration) logger.debug { "Finished loading device provider" } configurationValidator.validate(configuration) @@ -177,8 +151,6 @@ class Marathon( throw throwable } finally { hook.uninstall() - - stopKoin() } return progressReporter.aggregateResult() } diff --git a/core/src/main/kotlin/com/malinskiy/marathon/di/Modules.kt b/core/src/main/kotlin/com/malinskiy/marathon/di/Modules.kt index afa04eb2a..a6ce4572c 100644 --- a/core/src/main/kotlin/com/malinskiy/marathon/di/Modules.kt +++ b/core/src/main/kotlin/com/malinskiy/marathon/di/Modules.kt @@ -20,18 +20,18 @@ import com.malinskiy.marathon.cache.test.key.VersionNameProvider import com.malinskiy.marathon.execution.Configuration import com.malinskiy.marathon.execution.ConfigurationStrictRunChecker import com.malinskiy.marathon.execution.StrictRunChecker -import com.malinskiy.marathon.json.FileSerializer import com.malinskiy.marathon.execution.progress.ProgressReporter import com.malinskiy.marathon.io.AttachmentManager import com.malinskiy.marathon.io.CachedFileHasher import com.malinskiy.marathon.io.FileHasher import com.malinskiy.marathon.io.FileManager import com.malinskiy.marathon.io.Md5FileHasher +import com.malinskiy.marathon.json.FileSerializer import com.malinskiy.marathon.time.SystemTimer import com.malinskiy.marathon.time.Timer import org.koin.core.KoinApplication -import org.koin.core.context.startKoin import org.koin.core.definition.DefinitionFactory +import org.koin.dsl.koinApplication import org.koin.dsl.module import java.io.File import java.time.Clock @@ -68,7 +68,7 @@ fun coreModule(timer: Timer?) = module { single { timer ?: SystemTimer(get()) } single { ProgressReporter(get()) } single { ConfigurationStrictRunChecker(get()) } - single { Marathon(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) } + single { Marathon(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) } } fun KoinApplication.marathonConfiguration(configuration: Configuration): KoinApplication { @@ -76,12 +76,11 @@ fun KoinApplication.marathonConfiguration(configuration: Configuration): KoinApp return this } -fun marathonStartKoin(configuration: Configuration, timer: Timer? = null): KoinApplication { - return startKoin { +fun marathonStartKoin(configuration: Configuration, timer: Timer? = null): KoinApplication = + koinApplication { marathonConfiguration(configuration) modules(timer?.let { coreModule(timer) } ?: coreModule) modules(cacheModule) modules(analyticsModule) modules(configuration.vendorConfiguration.modules()) } -} diff --git a/core/src/main/kotlin/com/malinskiy/marathon/vendor/VendorConfiguration.kt b/core/src/main/kotlin/com/malinskiy/marathon/vendor/VendorConfiguration.kt index a1a00d04e..66cec168d 100644 --- a/core/src/main/kotlin/com/malinskiy/marathon/vendor/VendorConfiguration.kt +++ b/core/src/main/kotlin/com/malinskiy/marathon/vendor/VendorConfiguration.kt @@ -1,20 +1,10 @@ package com.malinskiy.marathon.vendor -import com.malinskiy.marathon.cache.test.key.ComponentCacheKeyProvider import com.malinskiy.marathon.device.DeviceFeature -import com.malinskiy.marathon.device.DeviceProvider -import com.malinskiy.marathon.execution.ComponentInfoExtractor -import com.malinskiy.marathon.execution.TestParser -import com.malinskiy.marathon.log.MarathonLogConfigurator import org.koin.core.module.Module interface VendorConfiguration { - fun logConfigurator(): MarathonLogConfigurator? - fun testParser(): TestParser? - fun deviceProvider(): DeviceProvider? fun preferableRecorderType(): DeviceFeature? - fun componentInfoExtractor(): ComponentInfoExtractor? - fun componentCacheKeyProvider(): ComponentCacheKeyProvider? fun modules(): List = emptyList() } diff --git a/core/src/test/kotlin/com/malinskiy/marathon/scenario/DeviceFilteringScenario.kt b/core/src/test/kotlin/com/malinskiy/marathon/scenario/DeviceFilteringScenario.kt index c8d5d522d..3fede0d4c 100644 --- a/core/src/test/kotlin/com/malinskiy/marathon/scenario/DeviceFilteringScenario.kt +++ b/core/src/test/kotlin/com/malinskiy/marathon/scenario/DeviceFilteringScenario.kt @@ -10,7 +10,6 @@ import com.malinskiy.marathon.test.setupMarathon import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runTest import org.amshove.kluent.shouldBe diff --git a/core/src/test/kotlin/com/malinskiy/marathon/scenario/DisconnectingScenarios.kt b/core/src/test/kotlin/com/malinskiy/marathon/scenario/DisconnectingScenarios.kt index 762b01952..2b1251be5 100644 --- a/core/src/test/kotlin/com/malinskiy/marathon/scenario/DisconnectingScenarios.kt +++ b/core/src/test/kotlin/com/malinskiy/marathon/scenario/DisconnectingScenarios.kt @@ -11,7 +11,6 @@ import com.malinskiy.marathon.time.Timer import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runTest import org.amshove.kluent.mock diff --git a/core/src/test/kotlin/com/malinskiy/marathon/scenario/SuccessScenarios.kt b/core/src/test/kotlin/com/malinskiy/marathon/scenario/SuccessScenarios.kt index 8968bdc11..b7552492a 100644 --- a/core/src/test/kotlin/com/malinskiy/marathon/scenario/SuccessScenarios.kt +++ b/core/src/test/kotlin/com/malinskiy/marathon/scenario/SuccessScenarios.kt @@ -10,7 +10,6 @@ import com.malinskiy.marathon.test.setupMarathon import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runTest import org.amshove.kluent.shouldBe diff --git a/core/src/test/kotlin/com/malinskiy/marathon/scenario/UncompletedScenarios.kt b/core/src/test/kotlin/com/malinskiy/marathon/scenario/UncompletedScenarios.kt index ffd50e442..8a40588d0 100644 --- a/core/src/test/kotlin/com/malinskiy/marathon/scenario/UncompletedScenarios.kt +++ b/core/src/test/kotlin/com/malinskiy/marathon/scenario/UncompletedScenarios.kt @@ -12,7 +12,6 @@ import com.malinskiy.marathon.time.Timer import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runTest import org.amshove.kluent.mock diff --git a/core/src/test/kotlin/com/malinskiy/marathon/spek/DeclareMock.kt b/core/src/test/kotlin/com/malinskiy/marathon/spek/DeclareMock.kt deleted file mode 100644 index 8bad39cd4..000000000 --- a/core/src/test/kotlin/com/malinskiy/marathon/spek/DeclareMock.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.malinskiy.marathon.spek - -import org.jetbrains.spek.api.dsl.TestBody -import org.koin.core.context.GlobalContext -import org.koin.core.definition.BeanDefinition -import org.koin.core.qualifier.Qualifier -import org.koin.test.mock.declareMockedDefinition -import org.koin.test.mock.getDefinition - -/** - * Declare & Create a mock in Koin container for given type - * - * @author Arnaud Giuliani - */ -inline fun TestBody.declareMock( - qualifier: Qualifier? = null, - noinline stubbing: (T.() -> Unit)? = null -): T { - val koin = GlobalContext.get().koin - val clazz = T::class - - val foundDefinition: BeanDefinition = getDefinition(clazz, koin, qualifier) - - koin.declareMockedDefinition(foundDefinition, stubbing) - - return koin.get() -} \ No newline at end of file diff --git a/core/src/test/kotlin/com/malinskiy/marathon/spek/KoinSpek.kt b/core/src/test/kotlin/com/malinskiy/marathon/spek/KoinSpek.kt deleted file mode 100644 index 5ab82cc09..000000000 --- a/core/src/test/kotlin/com/malinskiy/marathon/spek/KoinSpek.kt +++ /dev/null @@ -1,41 +0,0 @@ -package com.malinskiy.marathon.spek - -import com.malinskiy.marathon.di.analyticsModule -import org.jetbrains.spek.api.dsl.Spec -import org.koin.core.Koin -import org.koin.core.context.GlobalContext -import org.koin.core.context.startKoin -import org.koin.core.context.stopKoin -import org.koin.core.parameter.ParametersDefinition -import org.koin.core.qualifier.Qualifier - -fun Spec.getKoin(): Koin = GlobalContext.get().koin - -fun Spec.initKoin() { - beforeEachTest { - startKoin { - modules(analyticsModule) - } - } - - afterEachTest { - stopKoin() - } -} - -inline fun Spec.get( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null -): T = - getKoin().get(qualifier, parameters) - -inline fun Spec.inject( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null -): Lazy = - getKoin().inject(qualifier, parameters) - -inline fun Spec.bind( - noinline parameters: ParametersDefinition? = null -): S = - getKoin().bind(parameters) \ No newline at end of file diff --git a/marathon-gradle-plugin/src/main/kotlin/com/malinskiy/marathon/worker/WorkerRunnable.kt b/marathon-gradle-plugin/src/main/kotlin/com/malinskiy/marathon/worker/WorkerRunnable.kt index fb42ad35e..442eb0ff6 100644 --- a/marathon-gradle-plugin/src/main/kotlin/com/malinskiy/marathon/worker/WorkerRunnable.kt +++ b/marathon-gradle-plugin/src/main/kotlin/com/malinskiy/marathon/worker/WorkerRunnable.kt @@ -1,7 +1,6 @@ package com.malinskiy.marathon.worker import com.malinskiy.marathon.Marathon -import com.malinskiy.marathon.MarathonRunner import com.malinskiy.marathon.di.marathonStartKoin import com.malinskiy.marathon.execution.ComponentInfo import com.malinskiy.marathon.execution.Configuration @@ -17,25 +16,27 @@ class WorkerRunnable( private val log = MarathonLogging.logger {} - private lateinit var marathon: MarathonRunner - override fun run() = runBlocking { log.debug("Starting Marathon worker") val application = marathonStartKoin(configuration) - marathon = application.koin.get() - marathon.start() - - for (component in componentsChannel) { - log.debug("Scheduling tests for $component") - marathon.scheduleTests(component) + try { + val marathon = application.koin.get() + marathon.start() + + for (component in componentsChannel) { + log.debug("Scheduling tests for $component") + marathon.scheduleTests(component) + } + + log.debug("Waiting for completion") + stopAndWaitForCompletion(marathon) + } finally { + application.close() } - - log.debug("Waiting for completion") - stopAndWaitForCompletion() } - private suspend fun stopAndWaitForCompletion() { + private suspend fun stopAndWaitForCompletion(marathon: Marathon) { val success = marathon.stopAndWaitForCompletion() val shouldReportFailure = !configuration.ignoreFailures diff --git a/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/AndroidConfiguration.kt b/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/AndroidConfiguration.kt index 8d540dd99..316ec5054 100644 --- a/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/AndroidConfiguration.kt +++ b/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/AndroidConfiguration.kt @@ -2,15 +2,8 @@ package com.malinskiy.marathon.android import com.malinskiy.marathon.android.di.androidModule import com.malinskiy.marathon.android.serial.SerialStrategy -import com.malinskiy.marathon.cache.test.key.ComponentCacheKeyProvider import com.malinskiy.marathon.device.DeviceFeature -import com.malinskiy.marathon.device.DeviceProvider -import com.malinskiy.marathon.execution.ComponentInfoExtractor -import com.malinskiy.marathon.execution.TestParser -import com.malinskiy.marathon.log.MarathonLogConfigurator import com.malinskiy.marathon.vendor.VendorConfiguration -import org.koin.core.KoinComponent -import org.koin.core.get import org.koin.core.module.Module import java.io.File @@ -34,20 +27,10 @@ data class AndroidConfiguration( val installOptions: String = DEFAULT_INSTALL_OPTIONS, val preferableRecorderType: DeviceFeature? = null, val serialStrategy: SerialStrategy = SerialStrategy.AUTOMATIC -) : VendorConfiguration, KoinComponent { +) : VendorConfiguration { private val koinModules = listOf(androidModule) + implementationModules - override fun testParser(): TestParser? = get() - - override fun componentInfoExtractor(): ComponentInfoExtractor? = get() - - override fun componentCacheKeyProvider(): ComponentCacheKeyProvider? = get() - - override fun deviceProvider(): DeviceProvider? = get() - - override fun logConfigurator(): MarathonLogConfigurator = AndroidLogConfigurator() - override fun preferableRecorderType(): DeviceFeature? = preferableRecorderType override fun modules() = koinModules diff --git a/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/di/Modules.kt b/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/di/Modules.kt index 9f09593cf..9f2908d4d 100644 --- a/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/di/Modules.kt +++ b/vendor/vendor-android/base/src/main/kotlin/com/malinskiy/marathon/android/di/Modules.kt @@ -2,6 +2,7 @@ package com.malinskiy.marathon.android.di import com.malinskiy.marathon.android.AndroidComponentCacheKeyProvider import com.malinskiy.marathon.android.AndroidComponentInfoExtractor +import com.malinskiy.marathon.android.AndroidLogConfigurator import com.malinskiy.marathon.android.AndroidTestParser import com.malinskiy.marathon.android.ApkFileHasher import com.malinskiy.marathon.android.executor.logcat.LogcatCollector @@ -12,6 +13,7 @@ import com.malinskiy.marathon.cache.test.key.ComponentCacheKeyProvider import com.malinskiy.marathon.execution.ComponentInfoExtractor import com.malinskiy.marathon.execution.TestParser import com.malinskiy.marathon.io.CachedFileHasher +import com.malinskiy.marathon.log.MarathonLogConfigurator import com.malinskiy.marathon.report.logs.LogsProvider import org.koin.dsl.module @@ -23,4 +25,5 @@ val androidModule = module { single { get() } single { get() } single { LogcatEventsAdapter(get()) } + single { AndroidLogConfigurator() } } diff --git a/vendor/vendor-android/base/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt b/vendor/vendor-android/base/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt deleted file mode 100644 index 5ab82cc09..000000000 --- a/vendor/vendor-android/base/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt +++ /dev/null @@ -1,41 +0,0 @@ -package com.malinskiy.marathon.spek - -import com.malinskiy.marathon.di.analyticsModule -import org.jetbrains.spek.api.dsl.Spec -import org.koin.core.Koin -import org.koin.core.context.GlobalContext -import org.koin.core.context.startKoin -import org.koin.core.context.stopKoin -import org.koin.core.parameter.ParametersDefinition -import org.koin.core.qualifier.Qualifier - -fun Spec.getKoin(): Koin = GlobalContext.get().koin - -fun Spec.initKoin() { - beforeEachTest { - startKoin { - modules(analyticsModule) - } - } - - afterEachTest { - stopKoin() - } -} - -inline fun Spec.get( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null -): T = - getKoin().get(qualifier, parameters) - -inline fun Spec.inject( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null -): Lazy = - getKoin().inject(qualifier, parameters) - -inline fun Spec.bind( - noinline parameters: ParametersDefinition? = null -): S = - getKoin().bind(parameters) \ No newline at end of file diff --git a/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/AndroidDeviceTestRunnerSpek.kt b/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/AndroidDeviceTestRunnerSpek.kt index 8757f5337..35b51d748 100644 --- a/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/AndroidDeviceTestRunnerSpek.kt +++ b/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/AndroidDeviceTestRunnerSpek.kt @@ -9,7 +9,6 @@ import com.malinskiy.marathon.android.ddmlib.DdmlibAndroidDevice import com.malinskiy.marathon.android.ddmlib.toTestIdentifier import com.malinskiy.marathon.android.serial.SerialStrategy import com.malinskiy.marathon.execution.Configuration -import com.malinskiy.marathon.spek.initKoin import com.malinskiy.marathon.test.MetaProperty import com.malinskiy.marathon.test.Test import com.malinskiy.marathon.test.TestBatch @@ -28,8 +27,6 @@ import java.time.Clock class AndroidDeviceTestRunnerSpek : Spek( { - initKoin() - describe("AndroidDeviceTestRunner") { it("should handle ignored tests before execution") { val ddmsDevice = mock() diff --git a/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt b/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt deleted file mode 100644 index 5ab82cc09..000000000 --- a/vendor/vendor-android/ddmlib/src/test/kotlin/com/malinskiy/marathon/android/spek/KoinSpek.kt +++ /dev/null @@ -1,41 +0,0 @@ -package com.malinskiy.marathon.spek - -import com.malinskiy.marathon.di.analyticsModule -import org.jetbrains.spek.api.dsl.Spec -import org.koin.core.Koin -import org.koin.core.context.GlobalContext -import org.koin.core.context.startKoin -import org.koin.core.context.stopKoin -import org.koin.core.parameter.ParametersDefinition -import org.koin.core.qualifier.Qualifier - -fun Spec.getKoin(): Koin = GlobalContext.get().koin - -fun Spec.initKoin() { - beforeEachTest { - startKoin { - modules(analyticsModule) - } - } - - afterEachTest { - stopKoin() - } -} - -inline fun Spec.get( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null -): T = - getKoin().get(qualifier, parameters) - -inline fun Spec.inject( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null -): Lazy = - getKoin().inject(qualifier, parameters) - -inline fun Spec.bind( - noinline parameters: ParametersDefinition? = null -): S = - getKoin().bind(parameters) \ No newline at end of file diff --git a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/MarathonFactoryExtensions.kt b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/MarathonFactoryExtensions.kt deleted file mode 100644 index 0f6b3a06e..000000000 --- a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/MarathonFactoryExtensions.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.malinskiy.marathon.test - -import com.malinskiy.marathon.Marathon -import com.malinskiy.marathon.test.factory.MarathonFactory -import kotlinx.coroutines.runBlocking - -fun setupMarathon(f: suspend MarathonFactory.() -> Unit): Marathon { - return runBlocking { - val marathonFactory = MarathonFactory() - f(marathonFactory) - marathonFactory.build() - } -} diff --git a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubLogsProvider.kt b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubLogsProvider.kt new file mode 100644 index 000000000..850884af0 --- /dev/null +++ b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubLogsProvider.kt @@ -0,0 +1,10 @@ +package com.malinskiy.marathon.test + +import com.malinskiy.marathon.report.logs.BatchLogs +import com.malinskiy.marathon.report.logs.LogReport +import com.malinskiy.marathon.report.logs.LogsProvider + +class StubLogsProvider : LogsProvider { + override fun getFullReport(): LogReport = LogReport(emptyMap()) + override suspend fun getBatchReport(batchId: String): BatchLogs? = null +} diff --git a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubMarathonLogConfigurator.kt b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubMarathonLogConfigurator.kt new file mode 100644 index 000000000..c5dd5f17f --- /dev/null +++ b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/StubMarathonLogConfigurator.kt @@ -0,0 +1,8 @@ +package com.malinskiy.marathon.test + +import com.malinskiy.marathon.log.MarathonLogConfigurator +import com.malinskiy.marathon.vendor.VendorConfiguration + +class StubMarathonLogConfigurator : MarathonLogConfigurator { + override fun configure(vendorConfiguration: VendorConfiguration) = Unit +} diff --git a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/TestVendorConfiguration.kt b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/TestVendorConfiguration.kt index f3e3aa52b..b1abb97a5 100644 --- a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/TestVendorConfiguration.kt +++ b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/TestVendorConfiguration.kt @@ -2,35 +2,29 @@ package com.malinskiy.marathon.test import com.malinskiy.marathon.cache.test.key.ComponentCacheKeyProvider import com.malinskiy.marathon.device.DeviceFeature +import com.malinskiy.marathon.device.DeviceProvider +import com.malinskiy.marathon.execution.ComponentInfoExtractor import com.malinskiy.marathon.execution.TestParser -import com.malinskiy.marathon.report.logs.BatchLogs -import com.malinskiy.marathon.report.logs.LogReport +import com.malinskiy.marathon.log.MarathonLogConfigurator import com.malinskiy.marathon.report.logs.LogsProvider import com.malinskiy.marathon.vendor.VendorConfiguration import org.koin.core.module.Module import org.koin.dsl.module -class TestVendorConfiguration( - var testParser: TestParser, - var deviceProvider: StubDeviceProvider, - var componentInfoExtractor: StubComponentInfoExtractor, - var componentCacheKeyProvider: StubComponentCacheKeyProvider -) : VendorConfiguration { - override fun testParser() = testParser - override fun componentInfoExtractor() = componentInfoExtractor - override fun deviceProvider() = deviceProvider - override fun componentCacheKeyProvider(): ComponentCacheKeyProvider? = componentCacheKeyProvider - override fun logConfigurator() = null +class TestVendorConfiguration : VendorConfiguration { + val deviceProvider = StubDeviceProvider() + val testParser = Mocks.TestParser.DEFAULT + + private val testModule = module { + single { StubComponentCacheKeyProvider() } + single { StubComponentInfoExtractor() } + single { deviceProvider } + single { StubLogsProvider() } + single { testParser } + single { StubMarathonLogConfigurator() } + } + override fun preferableRecorderType(): DeviceFeature? = null - override fun modules(): List = - listOf(module { - single { componentCacheKeyProvider } - single { - object : LogsProvider { - override fun getFullReport(): LogReport = LogReport(emptyMap()) - override suspend fun getBatchReport(batchId: String): BatchLogs? = null - } - } - }) + override fun modules(): List = listOf(testModule) } diff --git a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/ConfigurationFactory.kt b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/ConfigurationFactory.kt index 5838d5ae5..32dc1001a 100644 --- a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/ConfigurationFactory.kt +++ b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/ConfigurationFactory.kt @@ -13,10 +13,6 @@ import com.malinskiy.marathon.execution.strategy.PoolingStrategy import com.malinskiy.marathon.execution.strategy.RetryStrategy import com.malinskiy.marathon.execution.strategy.ShardingStrategy import com.malinskiy.marathon.execution.strategy.SortingStrategy -import com.malinskiy.marathon.test.Mocks -import com.malinskiy.marathon.test.StubComponentCacheKeyProvider -import com.malinskiy.marathon.test.StubComponentInfoExtractor -import com.malinskiy.marathon.test.StubDeviceProvider import com.malinskiy.marathon.test.Test import com.malinskiy.marathon.test.TestVendorConfiguration import kotlinx.coroutines.channels.Channel @@ -27,12 +23,7 @@ fun configuration(block: ConfigurationFactory.() -> Unit = {}) = ConfigurationFa class ConfigurationFactory { var name = "DEFAULT_TEST_CONFIG" var outputDir = Files.createTempDirectory("test-run").toFile() - var vendorConfiguration = TestVendorConfiguration( - Mocks.TestParser.DEFAULT, - StubDeviceProvider(), - StubComponentInfoExtractor(), - StubComponentCacheKeyProvider() - ) + var vendorConfiguration = TestVendorConfiguration() var debug: Boolean? = null var batchingStrategy: BatchingStrategy? = null var customAnalyticsTracker: Tracker? = null @@ -60,12 +51,12 @@ class ConfigurationFactory { var noDevicesTimeoutMillis: Long? = null fun tests(block: () -> List) { - val testParser = vendorConfiguration.testParser() - (testParser as Mocks.VendorTestParser).tests = block.invoke() + val testParser = vendorConfiguration.testParser + testParser.tests = block.invoke() } fun devices(f: suspend (Channel) -> Unit) { - val stubDeviceProvider = vendorConfiguration.deviceProvider() + val stubDeviceProvider = vendorConfiguration.deviceProvider stubDeviceProvider.providingLogic = f } diff --git a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/MarathonFactory.kt b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/MarathonFactory.kt index 3dee4c152..578b15409 100644 --- a/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/MarathonFactory.kt +++ b/vendor/vendor-test/src/main/kotlin/com/malinskiy/marathon/test/factory/MarathonFactory.kt @@ -3,6 +3,7 @@ package com.malinskiy.marathon.test.factory import com.malinskiy.marathon.Marathon import com.malinskiy.marathon.di.marathonStartKoin import com.malinskiy.marathon.time.Timer +import org.koin.core.context.GlobalContext class MarathonFactory { private val configurationFactory: ConfigurationFactory = ConfigurationFactory() @@ -12,7 +13,8 @@ class MarathonFactory { fun configuration(block: ConfigurationFactory.() -> Unit) = configurationFactory.apply(block) fun build(): Marathon { - val marathonStartKoin = marathonStartKoin(configurationFactory.build(), timer) - return marathonStartKoin.koin.get() + val application = marathonStartKoin(configurationFactory.build(), timer) + GlobalContext.start(application) + return application.koin.get() } }