Skip to content

Commit d189847

Browse files
committed
- Added a konsistTest module to enforce coding conventions.
1 parent d5c5fdd commit d189847

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ plugins {
99
alias(libs.plugins.serialization) apply false
1010
alias(libs.plugins.googleSecrets) apply false
1111
alias(libs.plugins.jetbrainsKotlinJvm) apply false
12+
alias(libs.plugins.androidKotlinMultiplatformLibrary) apply false
1213
}

gradle/libs.versions.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ navigation-compose = "2.9.0"
2525
ktlint = "12.2.0"
2626
secrets = "2.0.1"
2727
jetbrainsKotlinJvm = "2.1.10"
28+
kotlinStdlib = "2.1.10"
29+
runner = "1.6.2"
30+
core = "1.6.1"
2831

2932
[libraries]
3033
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigation-compose" }
@@ -59,6 +62,9 @@ koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin" }
5962
koin-compose = { module = "io.insert-koin:koin-compose" }
6063
koin-compose-viewmodel = { module = "io.insert-koin:koin-compose-viewmodel", version.ref = "koin" }
6164
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
65+
kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlinStdlib" }
66+
androidx-runner = { group = "androidx.test", name = "runner", version.ref = "runner" }
67+
androidx-core = { group = "androidx.test", name = "core", version.ref = "core" }
6268

6369
[plugins]
6470
androidApplication = { id = "com.android.application", version.ref = "agp" }
@@ -70,3 +76,4 @@ serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref
7076
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
7177
googleSecrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" }
7278
jetbrainsKotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrainsKotlinJvm" }
79+
androidKotlinMultiplatformLibrary = { id = "com.android.kotlin.multiplatform.library", version.ref = "agp" }

konsistTest/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plugins {
2+
kotlin("jvm")
3+
id("java")
4+
}
5+
6+
kotlin {
7+
jvmToolchain(21)
8+
}
9+
10+
dependencies {
11+
testImplementation("com.lemonappdev:konsist:0.17.2")
12+
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.11.3")
13+
testImplementation(kotlin("test"))
14+
}
15+
16+
tasks.test {
17+
useJUnitPlatform()
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.example.konsisttest
2+
3+
class MyClass {
4+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import com.lemonappdev.konsist.api.Konsist
2+
import org.junit.jupiter.api.Assertions.assertTrue
3+
import org.junit.jupiter.api.Test
4+
5+
class SampleKonsistTest {
6+
@Test
7+
fun `no direct usage of androidx compose text should be allowed except designSystem`() {
8+
checkNoDirectUsageExceptAllowed(
9+
componentName = "androidx.compose.material.Text",
10+
excludePaths = arrayOf(),
11+
)
12+
}
13+
}
14+
15+
fun checkNoDirectUsageExceptAllowed(
16+
componentName: String,
17+
excludePaths: Array<String>,
18+
) {
19+
val offendingFiles = Konsist.scopeFromProject()
20+
.files
21+
.filter { file ->
22+
val normalizedFilePath = file.path.replace("\\", "/").lowercase()
23+
excludePaths.none { normalizedFilePath.contains(it.replace("\\", "/").lowercase()) }
24+
}
25+
.filter { file ->
26+
file.imports.any { it.name == componentName }
27+
}
28+
29+
assertTrue(offendingFiles.isEmpty()) {
30+
"Found forbidden imports of $componentName in files:\n" +
31+
offendingFiles.joinToString("\n") { it.path }
32+
}
33+
}

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ dependencyResolutionManagement {
2929
}
3030

3131
include(":composeApp")
32-
include(":custom-ktlint-rules")
32+
include(":custom-ktlint-rules")
33+
include(":konsistTest")

0 commit comments

Comments
 (0)