Skip to content

Commit

Permalink
Improve Spotless ktfmt & detekt integration
Browse files Browse the repository at this point in the history
1. Let integration_tests to use SpotlessPlugin.
2. Bump ktfmt to 0.49.
3. Re-format existing Kotlin files with new ktfmt.
4. Add detekt to modules with Kotlin code.
5. Fix existing detekt issues with some Suppress.

Signed-off-by: utzcoz <utzcoz@outlook.com>
  • Loading branch information
utzcoz committed May 4, 2024
1 parent da1352f commit 8f2e01e
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check_code_style.yml
Expand Up @@ -55,6 +55,6 @@ jobs:
run: |
./gradlew spotlessCheck
- name: Check Kotlin Code Style
- name: Check Kotlin Quality
run: |
./gradlew detekt
Expand Up @@ -11,7 +11,7 @@ class SpotlessPlugin implements Plugin<Project> {
kotlin {
// Add configurations for Kotlin files
target '**/*.kt'
ktfmt('0.42').googleStyle()
ktfmt('0.49').googleStyle()
}
groovy {
// Add configurations for Groovy files
Expand All @@ -21,7 +21,6 @@ class SpotlessPlugin implements Plugin<Project> {
// Add configurations for Groovy Gradle files
target('*.gradle', "**/*.gradle")
}

}
}
}
5 changes: 5 additions & 0 deletions integration_tests/kotlin/build.gradle
@@ -1,7 +1,12 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.robolectric.gradle.RoboJavaModulePlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: RoboJavaModulePlugin
apply plugin: 'kotlin'
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

compileKotlin {
compilerOptions.jvmTarget = JvmTarget.JVM_1_8
}
Expand Down
Expand Up @@ -22,7 +22,6 @@ class CustomShadowImageViewTest {
val shadowImageView = Shadow.extract<CustomShadowImageView>(imageView)
assertThat(shadowImageView).isNotNull()
assertThat(shadowImageView.realImageView).isSameInstanceAs(imageView)
val resourceId = Int.MAX_VALUE
imageView.performLongClick()
assertThat(shadowImageView.longClickPerformed).isTrue()
}
Expand Down
10 changes: 2 additions & 8 deletions integration_tests/mockito-kotlin/build.gradle
@@ -1,18 +1,12 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.robolectric.gradle.RoboJavaModulePlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: RoboJavaModulePlugin
apply plugin: 'kotlin'
apply plugin: "com.diffplug.spotless"
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

spotless {
kotlin {
target '**/*.kt'
ktfmt('0.42').googleStyle()
}
}

compileKotlin {
compilerOptions.jvmTarget = JvmTarget.JVM_1_8
}
Expand Down
10 changes: 2 additions & 8 deletions integration_tests/mockk/build.gradle
@@ -1,18 +1,12 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.robolectric.gradle.RoboJavaModulePlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: RoboJavaModulePlugin
apply plugin: 'kotlin'
apply plugin: "com.diffplug.spotless"
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

spotless {
kotlin {
target '**/*.kt'
ktfmt('0.42').googleStyle()
}
}

compileKotlin {
compilerOptions.jvmTarget = JvmTarget.JVM_1_8
}
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/roborazzi/build.gradle
@@ -1,8 +1,13 @@
import org.robolectric.gradle.AndroidProjectConfigPlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: 'com.android.library'
apply plugin: AndroidProjectConfigPlugin
apply plugin: 'kotlin-android'
apply plugin: "io.github.takahirom.roborazzi"
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

android {
compileSdk 34
namespace 'org.robolectric.integration.roborazzi'
Expand Down
Expand Up @@ -114,6 +114,7 @@ private fun registerActivityToPackageManager(activity: String) {
.addActivityIfNotPresent(ComponentName(appContext.packageName, activity))
}

@Suppress("ForbiddenComment")
private fun hardwareRendererEnvironment(block: () -> Unit) {
val originalHwrdrOption =
System.getProperty(RoborazziCaptureTest.USE_HARDWARE_RENDERER_NATIVE_ENV, null)
Expand Down
10 changes: 2 additions & 8 deletions integration_tests/sdkcompat/build.gradle
@@ -1,18 +1,12 @@
import org.robolectric.gradle.AndroidProjectConfigPlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: 'com.android.library'
apply plugin: AndroidProjectConfigPlugin
apply plugin: 'kotlin-android'
apply plugin: "com.diffplug.spotless"
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

spotless {
kotlin {
target '**/*.kt'
ktfmt('0.42').googleStyle()
}
}

android {
compileSdk 29
namespace 'org.robolectric.integrationtests.sdkcompat'
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/sparsearray/build.gradle
@@ -1,8 +1,12 @@
import org.robolectric.gradle.AndroidProjectConfigPlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: 'com.android.library'
apply plugin: AndroidProjectConfigPlugin
apply plugin: 'kotlin-android'
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

android {
compileSdk 34
namespace 'org.robolectric.sparsearray'
Expand All @@ -27,6 +31,7 @@ android {
}
}
}

dependencies {
compileOnly AndroidSdk.MAX_SDK.coordinates
implementation project(path: ':shadowapi', configuration: 'default')
Expand Down
11 changes: 3 additions & 8 deletions plugins/maven-dependency-resolver/build.gradle
@@ -1,18 +1,13 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.robolectric.gradle.DeployedRoboJavaModulePlugin
import org.robolectric.gradle.RoboJavaModulePlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: RoboJavaModulePlugin
apply plugin: DeployedRoboJavaModulePlugin
apply plugin: 'kotlin'
apply plugin: "com.diffplug.spotless"

spotless {
kotlin {
target '**/*.kt'
ktfmt('0.42').googleStyle()
}
}
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

tasks.withType(GenerateModuleMetadata).configureEach {
// We don't want to release gradle module metadata now to avoid
Expand Down
Expand Up @@ -15,6 +15,7 @@ class MavenRoboSettingsTest {
private var originalMavenRepositoryPassword: String? = null
private var originalMavenRepositoryProxyHost: String? = null
private var originalMavenProxyPort = 0

@Before
fun setUp() {
originalMavenRepositoryId = MavenRoboSettings.getMavenRepositoryId()
Expand Down
Expand Up @@ -39,7 +39,7 @@ class MavenDependencyResolverTest {
PROXY_HOST,
PROXY_PORT,
localRepositoryDir,
executorService
executorService,
)
mavenDependencyResolver = TestMavenDependencyResolver()
}
Expand Down Expand Up @@ -151,7 +151,7 @@ class MavenDependencyResolverTest {
proxyHost: String?,
proxyPort: Int,
localRepositoryDir: File,
executorService: ExecutorService
executorService: ExecutorService,
): MavenArtifactFetcher {
return mavenArtifactFetcher
}
Expand All @@ -169,14 +169,15 @@ class MavenDependencyResolverTest {
}
}

@Suppress("LongParameterList")
internal class TestMavenArtifactFetcher(
repositoryUrl: String?,
repositoryUserName: String?,
repositoryPassword: String?,
proxyHost: String?,
proxyPort: Int,
localRepositoryDir: File,
private val executorService: ExecutorService
private val executorService: ExecutorService,
) :
MavenArtifactFetcher(
repositoryUrl,
Expand All @@ -185,7 +186,7 @@ class MavenDependencyResolverTest {
proxyHost,
proxyPort,
localRepositoryDir,
executorService
executorService,
) {
var numRequests = 0
private set
Expand All @@ -199,7 +200,7 @@ class MavenDependencyResolverTest {
return super.call()
}
},
executorService
executorService,
)
}
}
Expand All @@ -216,7 +217,7 @@ class MavenDependencyResolverTest {
arrayOf(
DependencyJar("group", "artifact", "1"),
DependencyJar("org.group2", "artifact2-name", "2.4.5"),
DependencyJar("org.robolectric", "android-all", "10-robolectric-5803371")
DependencyJar("org.robolectric", "android-all", "10-robolectric-5803371"),
)

init {
Expand All @@ -240,20 +241,20 @@ class MavenDependencyResolverTest {
val jarContents = "$mavenJarArtifact jar contents"
Files.write(
jarContents.toByteArray(StandardCharsets.UTF_8),
File(REPOSITORY_DIR, mavenJarArtifact.jarPath())
File(REPOSITORY_DIR, mavenJarArtifact.jarPath()),
)
Files.write(
sha512(jarContents).toByteArray(),
File(REPOSITORY_DIR, mavenJarArtifact.jarSha512Path())
File(REPOSITORY_DIR, mavenJarArtifact.jarSha512Path()),
)
val pomContents = "$mavenJarArtifact pom contents"
Files.write(
pomContents.toByteArray(StandardCharsets.UTF_8),
File(REPOSITORY_DIR, mavenJarArtifact.pomPath())
File(REPOSITORY_DIR, mavenJarArtifact.pomPath()),
)
Files.write(
sha512(pomContents).toByteArray(),
File(REPOSITORY_DIR, mavenJarArtifact.pomSha512Path())
File(REPOSITORY_DIR, mavenJarArtifact.pomSha512Path()),
)
} catch (e: MalformedURLException) {
throw AssertionError(e)
Expand All @@ -269,13 +270,13 @@ class MavenDependencyResolverTest {
Files.write(jarContents.toByteArray(), File(REPOSITORY_DIR, mavenJarArtifact.jarPath()))
Files.write(
sha512("No the same content").toByteArray(),
File(REPOSITORY_DIR, mavenJarArtifact.jarSha512Path())
File(REPOSITORY_DIR, mavenJarArtifact.jarSha512Path()),
)
val pomContents = "$mavenJarArtifact pom contents"
Files.write(pomContents.toByteArray(), File(REPOSITORY_DIR, mavenJarArtifact.pomPath()))
Files.write(
sha512("Really not the same content").toByteArray(),
File(REPOSITORY_DIR, mavenJarArtifact.pomSha512Path())
File(REPOSITORY_DIR, mavenJarArtifact.pomSha512Path()),
)
} catch (e: MalformedURLException) {
throw AssertionError(e)
Expand Down
10 changes: 2 additions & 8 deletions utils/build.gradle
@@ -1,20 +1,14 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.robolectric.gradle.DeployedRoboJavaModulePlugin
import org.robolectric.gradle.RoboJavaModulePlugin
import org.robolectric.gradle.SpotlessPlugin

apply plugin: RoboJavaModulePlugin
apply plugin: 'kotlin'
apply plugin: DeployedRoboJavaModulePlugin
apply plugin: "com.diffplug.spotless"
apply plugin: SpotlessPlugin
apply plugin: "io.gitlab.arturbosch.detekt"

spotless {
kotlin {
target '**/*.kt'
ktfmt('0.42').googleStyle()
}
}

tasks.withType(GenerateModuleMetadata).configureEach {
// We don't want to release gradle module metadata now to avoid
// potential compatibility problems.
Expand Down
Expand Up @@ -109,6 +109,7 @@ class PerfStatsCollectorTest {

private class FakeClock : Clock {
private var timeNs = 0

override fun nanoTime(): Long {
return timeNs.toLong()
}
Expand Down
16 changes: 8 additions & 8 deletions utils/src/test/java/org/robolectric/util/SchedulerTest.kt
Expand Up @@ -212,10 +212,10 @@ class SchedulerTest {
transcript.add("two")
scheduler.postDelayed(AddToTranscript("three"), 1000)
},
1000
1000,
)
},
1000
1000,
)
scheduler.advanceBy(1000)
assertThat(transcript).containsExactly("one")
Expand Down Expand Up @@ -278,7 +278,7 @@ class SchedulerTest {
scheduler.post { order.add(4) }
order.add(2)
},
0
0,
)
scheduler.postDelayed({ order.add(3) }, 0)
scheduler.runOneTask()
Expand All @@ -302,7 +302,7 @@ class SchedulerTest {
scheduler.post { order.add(3) }
order.add(2)
},
0
0,
)
assertWithMessage("order").that(order).containsExactly(1, 2, 3)
assertWithMessage("size").that(scheduler.size()).isEqualTo(0)
Expand All @@ -317,7 +317,7 @@ class SchedulerTest {
scheduler.postAtFrontOfQueue { order.add(3) }
order.add(2)
},
0
0,
)
scheduler.postDelayed({ order.add(4) }, 0)
scheduler.advanceToLastPostedRunnable()
Expand All @@ -335,7 +335,7 @@ class SchedulerTest {
scheduler.postAtFrontOfQueue { order.add(3) }
order.add(2)
},
0
0,
)
assertWithMessage("order").that(order).containsExactly(1, 2, 3)
assertWithMessage("size").that(scheduler.size()).isEqualTo(0)
Expand All @@ -351,7 +351,7 @@ class SchedulerTest {
scheduler.postDelayed({ order.add(3) }, 1)
order.add(2)
},
0
0,
)
assertWithMessage("order:before").that(order).containsExactly(1, 2)
assertWithMessage("size:before").that(scheduler.size()).isEqualTo(1)
Expand All @@ -371,7 +371,7 @@ class SchedulerTest {
scheduler.postDelayed({ order.add(3) }, 1)
order.add(2)
},
0
0,
)
assertWithMessage("order").that(order).containsExactly(1, 2, 3)
assertWithMessage("size").that(scheduler.size()).isEqualTo(0)
Expand Down

0 comments on commit 8f2e01e

Please sign in to comment.