Skip to content

Commit

Permalink
Update detekt and fix violations (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
technoir42 authored Aug 2, 2024
1 parent d267561 commit 93032a0
Show file tree
Hide file tree
Showing 92 changed files with 314 additions and 837 deletions.
2 changes: 1 addition & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sample: sample/**/*
build:
- '**/*.build.gradle'
- buildSrc/**/*
- default-detekt-config.yml
- detekt.yml
- .buildsystem/**/*
git:
- .gitignore
Expand Down
21 changes: 10 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.jetbrains.kotlin.jvm") apply false
id("io.gitlab.arturbosch.detekt")
}

configure<DetektExtension> {
debug = true
version = "1.0.1"

input = files(rootProject.projectDir.absolutePath)
filters = ".*/resources/.*,.*/build/.*,.*/sample-app/.*"
config = files("${rootProject.projectDir}/default-detekt-config.yml")
baseline = file("${rootProject.projectDir}/reports/baseline.xml")
id("io.gitlab.arturbosch.detekt") apply false
}

allprojects {
group = "com.github.badoo.marathon"

plugins.withId("org.jetbrains.kotlin.jvm") {
plugins.apply("io.gitlab.arturbosch.detekt")

dependencies.add("implementation", dependencies.platform(Libraries.kotlinBom))
dependencies.add("implementation", dependencies.platform(Libraries.kotlinCoroutinesBom))
}
Expand All @@ -34,6 +26,13 @@ allprojects {
}
}

plugins.withId("io.gitlab.arturbosch.detekt") {
configure<DetektExtension> {
buildUponDefaultConfig = true
config.from("$rootDir/detekt.yml")
}
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/kotlin/com/malinskiy/marathon/Marathon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import com.malinskiy.marathon.vendor.VendorConfiguration
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.coroutineContext

private val log = MarathonLogging.logger {}

class Marathon(
val configuration: Configuration,
private val deviceProvider: DeviceProvider,
Expand Down Expand Up @@ -92,7 +90,7 @@ class Marathon(
logger.debug { "Created scheduler" }

if (configuration.outputDir.exists()) {
log.info { "Output ${configuration.outputDir} already exists" }
logger.info { "Output ${configuration.outputDir} already exists" }
configuration.outputDir.deleteRecursively()
}
configuration.outputDir.mkdirs()
Expand All @@ -107,19 +105,19 @@ class Marathon(
val tests = applyTestFilters(parsedTests)
val shard = prepareTestShard(tests, analytics)

log.info("Scheduling ${tests.size} tests")
log.debug(tests.joinToString(", ") { it.toTestName() })
logger.info("Scheduling ${tests.size} tests")
logger.debug(tests.joinToString(", ") { it.toTestName() })
scheduler.addTests(shard)
}

override suspend fun stopAndWaitForCompletion(): Boolean {
try {
scheduler.stopAndWaitForCompletion()
onFinish(analytics, deviceProvider, attachmentManager)
} catch (throwable: Throwable) {
} catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
// We don't want to catch these. If an exception was thrown, we should fail the execution
log.error("Error occurred while finishing tests run", throwable)
throw throwable
logger.error("Error occurred while finishing tests run", e)
throw e
} finally {
hook.uninstall()
}
Expand All @@ -146,7 +144,7 @@ class Marathon(
attachmentManager.terminate()
try {
tracker.close()
} catch (e: Throwable) {
} catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
throw ReportGenerationException("Failed to generate test run report with exception", e)
}
}
Expand Down
15 changes: 9 additions & 6 deletions core/src/main/kotlin/com/malinskiy/marathon/ShutdownHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ class ShutdownHook(
block()
}

fun install() {
fun install(): Boolean {
return when (debug) {
true -> try {
runtime.addShutdownHook(this)
} catch (e: IllegalStateException) {
} catch (e: SecurityException) {
true
} catch (ignored: IllegalStateException) {
false
} catch (ignored: SecurityException) {
false
}
else -> Unit
else -> true
}
}

Expand All @@ -29,9 +32,9 @@ class ShutdownHook(
true -> try {
runtime.removeShutdownHook(this)
true
} catch (e: IllegalStateException) {
} catch (ignored: IllegalStateException) {
false
} catch (e: SecurityException) {
} catch (ignored: SecurityException) {
false
}
else -> true
Expand Down
26 changes: 8 additions & 18 deletions core/src/main/kotlin/com/malinskiy/marathon/actor/StateMachine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
return transition
}

fun with(init: GraphBuilder<STATE, EVENT, SIDE_EFFECT>.() -> Unit): StateMachine<STATE, EVENT, SIDE_EFFECT> {
return create(graph.copy(initialState = state), init)
}
fun with(init: GraphBuilder<STATE, EVENT, SIDE_EFFECT>.() -> Unit): StateMachine<STATE, EVENT, SIDE_EFFECT> =
create(graph.copy(initialState = state), init)

private fun STATE.getTransition(event: EVENT): Transition<STATE, EVENT, SIDE_EFFECT> {
for ((eventMatcher, createTransitionTo) in getDefinition().transitions) {
Expand Down Expand Up @@ -183,9 +182,8 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
onTransitionListeners.add(listener)
}

fun build(): Graph<STATE, EVENT, SIDE_EFFECT> {
return Graph(requireNotNull(initialState), stateDefinitions, onTransitionListeners.toList())
}
fun build(): Graph<STATE, EVENT, SIDE_EFFECT> =
Graph(requireNotNull(initialState), stateDefinitions, onTransitionListeners.toList())

inner class StateDefinitionBuilder<S : STATE> {

Expand All @@ -207,16 +205,12 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc

inline fun <reified E : EVENT> on(
noinline createTransitionTo: S.(E) -> Graph.State.TransitionTo<STATE, SIDE_EFFECT>
) {
return on(any(), createTransitionTo)
}
) = on(any(), createTransitionTo)

inline fun <reified E : EVENT> on(
event: E,
noinline createTransitionTo: S.(E) -> Graph.State.TransitionTo<STATE, SIDE_EFFECT>
) {
return on(eq(event), createTransitionTo)
}
) = on(eq(event), createTransitionTo)

fun onEnter(listener: S.(EVENT) -> Unit) = with(stateDefinition) {
onEnterListeners.add { state, cause ->
Expand Down Expand Up @@ -246,15 +240,11 @@ class StateMachine<STATE : Any, EVENT : Any, SIDE_EFFECT : Any> private construc
companion object {
fun <STATE : Any, EVENT : Any, SIDE_EFFECT : Any> create(
init: GraphBuilder<STATE, EVENT, SIDE_EFFECT>.() -> Unit
): StateMachine<STATE, EVENT, SIDE_EFFECT> {
return create(null, init)
}
): StateMachine<STATE, EVENT, SIDE_EFFECT> = create(null, init)

private fun <STATE : Any, EVENT : Any, SIDE_EFFECT : Any> create(
graph: Graph<STATE, EVENT, SIDE_EFFECT>?,
init: GraphBuilder<STATE, EVENT, SIDE_EFFECT>.() -> Unit
): StateMachine<STATE, EVENT, SIDE_EFFECT> {
return StateMachine(GraphBuilder(graph).apply(init).build())
}
): StateMachine<STATE, EVENT, SIDE_EFFECT> = StateMachine(GraphBuilder(graph).apply(init).build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.malinskiy.marathon.execution.Configuration
import com.malinskiy.marathon.io.AttachmentManager
import com.malinskiy.marathon.io.FileManager
import com.malinskiy.marathon.io.FileType
import com.malinskiy.marathon.log.MarathonLogging
import com.malinskiy.marathon.report.allure.AllureReporter
import com.malinskiy.marathon.report.attachment.AttachmentTestEventInflator
import com.malinskiy.marathon.report.device.DeviceInfoJsonReporter
Expand Down Expand Up @@ -41,9 +40,6 @@ internal class TrackerFactory(
private val timer: Timer,
private val track: Track
) {

val log = MarathonLogging.logger("TrackerFactory")

fun create(): TrackerInternal {
val defaultTrackers = mutableListOf<TrackerInternal>(createExecutionReportGenerator())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import com.malinskiy.marathon.test.Test
import java.time.Instant

internal class NoOpMetricsProvider : MetricsProvider {
override fun executionTime(test: Test, percentile: Double, limit: Instant): Double {
return 0.0
}
override fun executionTime(test: Test, percentile: Double, limit: Instant): Double = 0.0

override fun successRate(test: Test, limit: Instant): Double {
return 0.0
}
override fun successRate(test: Test, limit: Instant): Double = 0.0

override fun close() {}
override fun close() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ data class ExecutionReport(
return summaries
}

@Suppress("LongMethod")
private fun compilePoolSummary(poolId: DevicePoolId): PoolSummary {
val devices = deviceConnectedEvents.filter { it.poolId == poolId }.map { it.device }.distinctBy { it.serialNumber }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.malinskiy.marathon.execution.Configuration
class CacheServiceFactory(private val configuration: Configuration) {

fun createCacheService(): CacheService {
if (configuration.cache.local !is LocalCacheConfiguration.Disabled) throw IllegalArgumentException("Local cache is not supported yet")
require(configuration.cache.local is LocalCacheConfiguration.Disabled) { "Local cache is not supported yet" }

return when (configuration.cache.remote) {
is RemoteCacheConfiguration.Enabled -> GradleHttpCacheService(configuration.cache.remote)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package com.malinskiy.marathon.cache

class NoOpCacheService : CacheService {

override suspend fun load(key: CacheKey, reader: CacheEntryReader): Boolean {
return false
}
override suspend fun load(key: CacheKey, reader: CacheEntryReader): Boolean = false

override suspend fun store(key: CacheKey, writer: CacheEntryWriter) {
}

override fun close() {
}
override fun close() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ sealed class LocalCacheConfiguration {
val removeUnusedEntriesAfterDays: Int
) : LocalCacheConfiguration()

object Disabled : LocalCacheConfiguration()
data object Disabled : LocalCacheConfiguration()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ sealed class RemoteCacheConfiguration {
val credentials: Credentials? = null
) : RemoteCacheConfiguration()

object Disabled : RemoteCacheConfiguration()
data object Disabled : RemoteCacheConfiguration()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class TestCacheKey(

private val description = "CacheKey{test=Test(${test.toTestName()}, key=$key)}"

override fun toString(): String = description

override fun equals(other: Any?): Boolean {
val otherKey = other as? TestCacheKey ?: return false
return otherKey.key == key
}

override fun hashCode(): Int = key.hashCode()

override fun toString(): String = description
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,4 @@ class TestResultEntryReader(

private suspend inline fun ByteReadChannel.readDeviceFeatures(): Collection<DeviceFeature> =
(0 until readInt()).map { DeviceFeature.values()[readInt()] }

private companion object {
private const val CACHED_BATCH_ID = "cached"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum class AttachmentType {
VIDEO,
LOG;

fun toMimeType() = when (this) {
fun toMimeType(): String = when (this) {
SCREENSHOT -> "image/gif"
VIDEO -> "video/mp4"
LOG -> "text/plain"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class DevicePoolActor(

// Requests a batch of tests for a random device from the list of devices not running tests at the moment.
// When @avoidingDevice is not null, attemtps to send the request for any other device whenever available.
@Suppress("NestedBlockDepth")
private suspend fun maybeRequestBatch(avoidingDevice: Device? = null) {
val availableDevices = devices.values.asSequence()
.map { it as DeviceActor }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sealed class DevicePoolMessage {
data class AddDevice(val device: Device) : FromScheduler()
data class AddTests(val shard: TestShard) : FromScheduler()
data class RemoveDevice(val device: Device) : FromScheduler()
object RequestStop : FromScheduler()
data object RequestStop : FromScheduler()
}

sealed class FromDevice(val device: Device) : DevicePoolMessage() {
Expand All @@ -19,8 +19,8 @@ sealed class DevicePoolMessage {
}

sealed class FromQueue : DevicePoolMessage() {
object Notify : FromQueue()
object Terminated : FromQueue()
data object Notify : FromQueue()
data object Terminated : FromQueue()
data class ExecuteBatch(val device: DeviceInfo, val batch: TestBatch) : FromQueue()
}
}
32 changes: 4 additions & 28 deletions core/src/main/kotlin/com/malinskiy/marathon/execution/Filter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ data class SimpleClassnameFilter(@JsonProperty("regex") val regex: Regex) : Test

override fun hashCode(): Int = regex.hashCode()

override fun toString(): String {
return "SimpleClassnameFilter(regex=$regex)"
}
override fun toString(): String = "SimpleClassnameFilter(regex=$regex)"
}

data class FullyQualifiedClassnameFilter(@JsonProperty("regex") val regex: Regex) : TestFilter {
Expand All @@ -41,9 +39,7 @@ data class FullyQualifiedClassnameFilter(@JsonProperty("regex") val regex: Regex

override fun hashCode(): Int = regex.hashCode()

override fun toString(): String {
return "FullyQualifiedClassnameFilter(regex=$regex)"
}
override fun toString(): String = "FullyQualifiedClassnameFilter(regex=$regex)"
}

data class TestPackageFilter(@JsonProperty("regex") val regex: Regex) : TestFilter {
Expand All @@ -57,9 +53,7 @@ data class TestPackageFilter(@JsonProperty("regex") val regex: Regex) : TestFilt

override fun hashCode(): Int = regex.hashCode()

override fun toString(): String {
return "TestPackageFilter(regex=$regex)"
}
override fun toString(): String = "TestPackageFilter(regex=$regex)"
}

data class AnnotationFilter(@JsonProperty("regex") val regex: Regex) : TestFilter {
Expand All @@ -73,23 +67,5 @@ data class AnnotationFilter(@JsonProperty("regex") val regex: Regex) : TestFilte

override fun hashCode(): Int = regex.hashCode()

override fun toString(): String {
return "AnnotationFilter(regex=$regex)"
}
}

data class TestMethodFilter(@JsonProperty("regex") val regex: Regex) : TestFilter {
override fun filter(tests: List<Test>): List<Test> = tests.filter { regex.matches(it.method) }
override fun filterNot(tests: List<Test>): List<Test> = tests.filterNot { regex.matches(it.method) }

override fun equals(other: Any?): Boolean {
if (other !is TestMethodFilter) return false
return regex.toString().contentEquals(other.regex.toString())
}

override fun hashCode(): Int = regex.hashCode()

override fun toString(): String {
return "TestMethodFilter(regex=$regex)"
}
override fun toString(): String = "AnnotationFilter(regex=$regex)"
}
Loading

0 comments on commit 93032a0

Please sign in to comment.