Skip to content

Commit

Permalink
Avoid snapshotting sourceLink.localDirectory input (#2807)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-g committed Jan 13, 2023
1 parent 303c937 commit ac932d4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Expand Up @@ -20,8 +20,39 @@ class BasicCachingIntegrationTest(override val versions: BuildVersions) : Abstra

@Test
fun execute() {
runAndAssertOutcomeAndContents(TaskOutcome.SUCCESS)
runAndAssertOutcomeAndContents(TaskOutcome.FROM_CACHE)
}

@Test
fun localDirectoryPointingToRoot() {
fun String.findAndReplace(oldValue: String, newValue: String): String {
assertTrue(oldValue in this, "Expected to replace '$oldValue'")
return replace(oldValue, newValue)
}
val projectKts = projectDir.resolve("build.gradle.kts")

projectKts.readText()
.findAndReplace("localDirectory.set(file(\"src/main\"))", "localDirectory.set(projectDir)")
.findAndReplace("integration-tests/gradle/projects/it-basic/src/main", "integration-tests/gradle/projects/it-basic")
.also { projectKts.writeText(it) }

runAndAssertOutcomeAndContents(TaskOutcome.SUCCESS)
projectDir.resolve("unrelated.txt").writeText("modified")
// despite projectDir is used as an input in localDirectory, changing its contents shouldn't invalidate the cache
runAndAssertOutcomeAndContents(TaskOutcome.FROM_CACHE)

projectKts.readText()
.findAndReplace("localDirectory.set(projectDir)", "localDirectory.set(file(\"src\"))")
.also { projectKts.writeText(it) }
// changing localDirectory path invalidates cached task results
runAndAssertOutcome(TaskOutcome.SUCCESS)
runAndAssertOutcome(TaskOutcome.FROM_CACHE)
}


private fun runAndAssertOutcomeAndContents(expectedOutcome: TaskOutcome) {
runAndAssertOutcome(expectedOutcome)
File(projectDir, "build/dokka/html").assertHtmlOutputDir()
}

private fun runAndAssertOutcome(expectedOutcome: TaskOutcome) {
Expand All @@ -35,7 +66,5 @@ class BasicCachingIntegrationTest(override val versions: BuildVersions) : Abstra
).buildRelaxed()

assertEquals(expectedOutcome, assertNotNull(result.task(":dokkaHtml")).outcome)

File(projectDir, "build/dokka/html").assertHtmlOutputDir()
}
}
Expand Up @@ -21,11 +21,11 @@ class GradleRelocatedCachingIntegrationTest(override val versions: BuildVersions

@Test
fun execute() {
runAndAssertOutcome(projectFolder(1), TaskOutcome.SUCCESS)
runAndAssertOutcome(projectFolder(2), TaskOutcome.FROM_CACHE)
runAndAssertOutcomeAndContents(projectFolder(1), TaskOutcome.SUCCESS)
runAndAssertOutcomeAndContents(projectFolder(2), TaskOutcome.FROM_CACHE)
}

private fun runAndAssertOutcome(project: File, expectedOutcome: TaskOutcome) {
private fun runAndAssertOutcomeAndContents(project: File, expectedOutcome: TaskOutcome) {
val result = createGradleRunner("clean", "dokkaHtml", "-i", "-s", "-Dorg.gradle.caching.debug=true", "--build-cache")
.withProjectDir(project)
.buildRelaxed()
Expand Down
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.dokka.gradle

import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
import org.jetbrains.dokka.DokkaConfigurationBuilder
import org.jetbrains.dokka.SourceLinkDefinitionImpl
Expand Down Expand Up @@ -30,15 +31,27 @@ class GradleSourceLinkBuilder(
/**
* Path to the local source directory. The path must be relative to the root of current project.
*
* This path is used to find relative paths of the source files from which the documentation is built.
* These relative paths are then combined with the base url of a source code hosting service specified with
* the [remoteUrl] property to create source links for each declaration.
*
* Example:
*
* ```kotlin
* projectDir.resolve("src")
* ```
*/
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
@Internal // changing contents of the directory should not invalidate the task
val localDirectory: Property<File?> = project.objects.safeProperty()

/**
* The relative path to [localDirectory] from the project directory. Declared as an input to invalidate the task if that path changes.
* Should not be used anywhere directly.
*/
@Suppress("unused")
@get:Input
internal val localDirectoryPath: Provider<String?> =
localDirectory.map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPath }

/**
* URL of source code hosting service that can be accessed by documentation readers,
Expand Down

0 comments on commit ac932d4

Please sign in to comment.