Skip to content

Commit

Permalink
WIP. Proving that DAGP can handle suspend functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
autonomousapps committed Aug 3, 2023
1 parent cbd5cd0 commit 1723ad6
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class AbstractJvmSpec extends AbstractFunctionalSpec {
@SuppressWarnings('unused')
def cleanup() {
if (gradleProject != null && shouldClean) {
clean(gradleProject.rootDir)
// clean(gradleProject.rootDir)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.autonomousapps.jvm

import com.autonomousapps.android.AbstractAndroidSpec
import com.autonomousapps.jvm.projects.KotlinSuspendProject

import static com.autonomousapps.advice.truth.BuildHealthSubject.buildHealth
import static com.autonomousapps.utils.Runner.build
import static com.google.common.truth.Truth.assertAbout

@SuppressWarnings('GroovyAssignabilityCheck')
final class KotlinSuspendSpec extends AbstractJvmSpec {

def "can detect functions that take suspend parameters (#gradleVersion)"() {
given:
def project = new KotlinSuspendProject()
gradleProject = project.gradleProject
when:
build(gradleVersion, gradleProject.rootDir, 'buildHealth')
then:
assertAbout(buildHealth())
.that(project.actualBuildHealth())
.isEquivalentIgnoringModuleAdvice(project.expectedBuildHealth)
where:
gradleVersion << gradleVersions()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.autonomousapps.jvm.projects

import com.autonomousapps.AbstractProject
import com.autonomousapps.kit.*
import com.autonomousapps.model.ProjectAdvice

import static com.autonomousapps.AdviceHelper.actualProjectAdvice
import static com.autonomousapps.AdviceHelper.emptyProjectAdviceFor
import static com.autonomousapps.kit.Dependency.project

final class KotlinSuspendProject extends AbstractProject {

final GradleProject gradleProject

KotlinSuspendProject() {
this.gradleProject = build()
}

private GradleProject build() {
def builder = newGradleProjectBuilder()
builder.withRootProject { root ->
root.gradleProperties = GradleProperties.minimalJvmProperties()
}
builder.withSubproject('consumer') { consumer ->
consumer.withBuildScript { bs ->
bs.plugins = [Plugin.kotlinPluginNoVersion]
bs.dependencies = [
project('implementation', ':producer')
]
}
consumer.sources = consumerSources
}
builder.withSubproject('producer') { producer ->
producer.withBuildScript { bs ->
bs.plugins = [Plugin.kotlinPluginNoVersion]
}
producer.sources = producerSources
}

def project = builder.build()
project.writer().write()
return project
}

private static final List<Source> consumerSources = [
new Source(
SourceType.KOTLIN, 'Consumer.kt', 'com/example/consumer',
'''\
package com.example.consumer
import com.example.producer.*
internal class Consumer {
fun doTheThing() {
complicatedThing(Datum()) {
getApiResult()
}
}
suspend fun getApiResult(): ApiResult<String, String> = TODO()
}'''.stripIndent()
)
]

private static final List<Source> producerSources = [
new Source(
SourceType.KOTLIN, 'Producer.kt', 'com/example/producer',
'''\
package com.example.producer
fun <E : Any> complicatedThing(
datum: Datum,
body: suspend () -> ApiResult<*, E>
): Unit = TODO()
class Datum
class ApiResult<A, B>'''.stripIndent()
)
]

Set<ProjectAdvice> actualBuildHealth() {
return actualProjectAdvice(gradleProject)
}

final Set<ProjectAdvice> expectedBuildHealth = emptyProjectAdviceFor(':consumer', ':producer',)
}

0 comments on commit 1723ad6

Please sign in to comment.