Does TestBalloon have an equivalent for JUnit's `@...
# testballoon
a
Does TestBalloon have an equivalent for JUnit's
@TempDir
? The directory is only deleted if the test passes, or on CI. On local failures the directory path is logged to console, so it can be checked.
👀 1
o
Not out of the box. Would this one do the job?
Copy code
@OptIn(ExperimentalPathApi::class)
@TestRegistering
fun TestSuiteScope.testWithDirectory(
    name: String,
    testConfig: TestConfig = TestConfig,
    action: suspend Test.ExecutionScope.(temporaryDirectory: Path) -> Unit
) = testSuiteInScope.test(name, testConfig = testConfig) {
    val temporaryDirectory = Files.createTempDirectory("test-$name")
    try {
        action(temporaryDirectory)
        temporaryDirectory.deleteRecursively()
    } catch (exception: Throwable) {
        if (testPlatform.environment("CI") == null) {
            println("Temporary directory: file://${temporaryDirectory.toAbsolutePath()}")
        } else {
            temporaryDirectory.deleteRecursively()
        }
        throw exception
    }
}
Invoked like so:
Copy code
val mySuite by testSuite {
    testWithDirectory("min") { directory ->
        (directory / "my-result.txt").writeText("hello")
        assertEquals(4, min(5, 3))
    }
}
There are probably ways to make it even simpler, e.g. if a fixture's
closeWith
had access to the test result, so that it could delete on failure. And eventually, this seems to be a good use case for platform-independent test metadata (which is on the map), so that we could have it on browsers, too. What does your use case look like? Would something from the above options be a good fit or do you have something even better in mind?
a
thanks! I'll have a play around with your suggestion
👍 1
the usecase is testing Gradle plugins. I want to set up a test project in a temp dir, but if the test fails it's convenient to open it in an IDE to investigate.
1
m
Yea, I have the exact same use case
Currently, I always keep directories around, even on success and collect the stale directories on next run