Skip to content

Commit

Permalink
Add support for Configuration Cache (cortinico#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico authored Sep 5, 2022
1 parent 34b47d6 commit ef3abe2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 45 deletions.
2 changes: 0 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Please add your entries according to this format.

## Unreleased

- Add support for Gradle Configuration Cache
- KtFmt to 0.40

## Version 0.9.0 *(2022-08-29)*

- KtFmt to 0.39
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ import com.ncorti.ktfmt.gradle.FormattingOptionsBean
import com.ncorti.ktfmt.gradle.KtfmtExtension
import java.io.File
import java.io.IOException
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.Path
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.runBlocking
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.file.FileCollection
import org.gradle.api.provider.Property
import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
Expand All @@ -38,8 +33,6 @@ import org.gradle.api.tasks.options.Option
*/
abstract class KtfmtBaseTask : SourceTask() {

private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

@get:Input @get:Optional internal var bean: FormattingOptionsBean? = null

@get:Option(
Expand All @@ -61,20 +54,16 @@ abstract class KtfmtBaseTask : SourceTask() {
@TaskAction
@VisibleForTesting
internal fun taskAction() {

runBlocking(scope.coroutineContext) { execute() }
scope.cancel()
runBlocking(Dispatchers.IO) { execute() }
}

protected abstract suspend fun execute()

@OptIn(ExperimentalPathApi::class)
@VisibleForTesting
@Suppress("TooGenericExceptionCaught", "SpreadOperator")
internal fun processFile(input: File): KtfmtResult {

// The task should try to read from the ktfmt{} extension before falling back to default
// values.
// The task should try to read from the ktfmt{} extension
// before falling back to default values.
if (bean == null) {
bean =
try {
Expand Down Expand Up @@ -115,9 +104,8 @@ abstract class KtfmtBaseTask : SourceTask() {
}
}

internal suspend fun processFileCollection(input: FileCollection): List<KtfmtResult> {
return input.map { scope.async { processFile(it) } }.awaitAll()
}

@VisibleForTesting @Internal internal fun isScopeActive() = scope.isActive
internal suspend fun processFileCollection(input: FileCollection): List<KtfmtResult> =
coroutineScope {
input.map { async { processFile(it) } }.awaitAll()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,6 @@ internal class KtfmtBaseTaskTest {
assertThat(underTest.outputs.files.toList()).isEqualTo(underTest.source.files.toList())
}

@Test
fun `scope is not canceled before running`() {
val project = ProjectBuilder.builder().build()
project.pluginManager.apply("org.jetbrains.kotlin.jvm")
project.pluginManager.apply("com.ncorti.ktfmt.gradle")

val underTest = project.tasks.getByName("ktfmtFormatMain") as KtfmtBaseTask

assertThat(underTest.isScopeActive()).isTrue()
}

@Test
fun `scope is canceled after running`() {
val project = ProjectBuilder.builder().build()
project.pluginManager.apply("org.jetbrains.kotlin.jvm")
project.pluginManager.apply("com.ncorti.ktfmt.gradle")

val underTest = project.tasks.getByName("ktfmtFormatMain") as KtfmtBaseTask
underTest.taskAction()

assertThat(underTest.isScopeActive()).isFalse()
}

@Test
fun `processFile returns success for valid file`() {
val underTest = project.tasks.getByName("ktfmtFormatMain") as KtfmtBaseTask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ internal class KtfmtCheckTaskIntegrationTest {
assertThat(result.output).contains("[ktfmt] Valid formatting")
}

@Test
fun `format task uses configuration cache correctly`() {
createTempFile(content = "val answer = 42\n")
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("--configuration-cache", "ktfmtCheckMain")
.build()

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("--configuration-cache", "ktfmtCheckMain")
.build()

assertThat(result.output).contains("Reusing configuration cache.")
}

@Test
fun `check task validates all the file with a failure`() {
createTempFile(content = "val answer = `\n", fileName = "File1.kt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ internal class KtfmtFormatTaskIntegrationTest {
assertThat(result.output).contains("[ktfmt] Successfully reformatted 1 files with Ktfmt")
}

@Test
fun `format task uses configuration cache correctly`() {
createTempFile(content = "val answer = 42\n")
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("--configuration-cache", "ktfmtFormatMain")
.build()

val result =
GradleRunner.create()
.withProjectDir(tempDir)
.withPluginClasspath()
.withArguments("--configuration-cache", "ktfmtFormatMain")
.build()

assertThat(result.output).contains("Reusing configuration cache.")
}

@Test
fun `format task reformats all the file even with a failure`() {
val file1 = createTempFile(content = "val answer = `", fileName = "File1.kt")
Expand Down

0 comments on commit ef3abe2

Please sign in to comment.