Running into a Build Cache issue with TestKit. I'm...
# community-support
m
Running into a Build Cache issue with TestKit. I'm following the docs on how to enable the build-cache in tests by both passing
--build-cache
to my
GradleRunner
invocation and having
org.gradle.caching=true
in the project's
gradle.properties
, but I always get this console output from my test:
Copy code
Caching disabled for Kotlin DSL script compilation (Settings/TopLevel/stage2) because:
      Build cache is disabled
I can share more in the below thread, just wanted to make sure I'm not missing something obvious/not aware of something new the docs haven't specified? Using
org.gradle.caching=true
in my root project's
gradle.properties
file seems to work just fine
To make testing easier, I've defined some fixtures that'll internally use a
TemporaryFolder
to create directories for each shim Project. I'm creating a
gradle.properties
file directly under that temp folder (where the root project is) to pass
org.gradle.caching=true
. I have a test that this is compatible with the configuration cache that passes (fancy APIs are fairly simple in their impl):
Copy code
@Test
    fun `should be configuration-cacheable`() {
        root.enableConfigurationCache()
        androidApp.dependency(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, HAMCREST_DEPENDENCY_NAME)
        androidLib.dependency(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, HAMCREST_DEPENDENCY_NAME)
        javaLib.dependency(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, HAMCREST_DEPENDENCY_NAME)

        @Suppress("UNUSED_VARIABLE")
        val firstBuildResult = root.run(CheckTestDependencyLeak.NAME)
        val secondBuildResult = root.run(CheckTestDependencyLeak.NAME)

        secondBuildResult.assertUsingConfigurationCache()
    }
But this fails:
Copy code
@Test
    fun `should be cacheable`() {
        root.enableBuildCache()
        androidApp.dependency(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, HAMCREST_DEPENDENCY_NAME)
        androidLib.dependency(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, HAMCREST_DEPENDENCY_NAME)
        javaLib.dependency(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, HAMCREST_DEPENDENCY_NAME)

        @Suppress("UNUSED_VARIABLE")
        val firstBuildResult = root.run(CheckTestDependencyLeak.NAME).printOutput()
        androidApp.deleteBuildDir()
        androidLib.deleteBuildDir()
        javaLib.deleteBuildDir()
        val secondBuildResult = root.run(CheckTestDependencyLeak.NAME).printOutput()

        secondBuildResult.assertCached(":app:${CheckTestDependencyLeak.NAME}")
        secondBuildResult.assertCached(":javaLib:${CheckTestDependencyLeak.NAME}")
        secondBuildResult.assertCached(":androidLib:${CheckTestDependencyLeak.NAME}")
    }
Those
enable*
methods:
Copy code
fun enableBuildCache() {
        gradleProperty("org.gradle.caching", true)
    }

    fun enableConfigurationCache() {
        gradleProperty("org.gradle.configuration-cache", true)
    }
Those will eventually write down to
gradle.properties
Oh and the failure is because of that
build cache is disabled
message - I get a
UP-TO-DATE
instead of a
FROM-CACHE
outcome
a
Copy code
Caching disabled for Kotlin DSL script compilation (Settings/TopLevel/stage2) because:
      Build cache is disabled
is a message for compilation of
settings.gradle.kts
. You probably should ignore it. Build cache is not ready yet when
settings.gradle.kts
is compiled, since it's configured in the
settings.gradle.kts
. Message
Build cache is disabled
is a bit unfortunate in that case.
😢 1
t
Is it really because of "build cache is disabled"? I'd be OK with Gradle preferring the current build dir to the build cache if it's already up-to-date (I must admit I don't know what it actually does). In a test I wrote, I delete the build dir in between runs.
m
So it is enabled, the messaging there is just a bit unfortunate?
Is it really because of "build cache is disabled"?
That was my assumption from the messaging, but if that's a bad signal then there's something wrong with my cacheable-task attempt
a
So it is enabled, the messaging there is just a bit unfortunate?
Message in that case should say something like:
Build cache is not ready yet
You should check a message for a task you are testing, to get better information if it is cached or not
E.g. for
:help
with
--info
Copy code
Caching disabled for task ':help' because:
  Produces only non-cacheable console output
t
In a test I wrote, I delete the build dir in between runs.
Did you try that? (not necessarily to keep it in the end, but at least to test if it gives a different outcome)
m
I did indeed, that's what these lines do:
Copy code
androidApp.deleteBuildDir()
 androidLib.deleteBuildDir()
t
Oh, sorry, I had overlooked them! (isn't it strange that you even get an UP-TO-DATE then?)
m
No worries! Yeah I think this is a misconfiguration on my part, I was so focused on that messaging that I definitely missed something here - will update when/if I find it!