Skip to content

Commit f97d452

Browse files
authored
test: drop the usage of Gradle's internal TemporaryFolder (#1317)
1 parent 4260c97 commit f97d452

File tree

1 file changed

+51
-58
lines changed
  • src/test/kotlin/org/danilopianini/gradle/test

1 file changed

+51
-58
lines changed

src/test/kotlin/org/danilopianini/gradle/test/Tests.kt

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import io.kotest.matchers.shouldBe
1010
import io.kotest.matchers.string.shouldContain
1111
import io.kotest.matchers.string.shouldNotContain
1212
import java.io.File
13-
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder
13+
import java.nio.file.Path
14+
import kotlin.io.path.absolutePathString
15+
import kotlin.io.path.createTempDirectory
16+
import kotlin.io.path.writeText
1417
import org.gradle.testkit.runner.BuildResult
1518
import org.gradle.testkit.runner.GradleRunner
1619
import org.gradle.testkit.runner.TaskOutcome
@@ -24,61 +27,54 @@ class Tests :
2427
checkNotNull(testkitProperties) {
2528
"No file testkit-gradle.properties was generated"
2629
}
27-
val scan =
28-
ClassGraph()
29-
.enableAllInfo()
30-
.acceptPackages(Tests::class.java.`package`.name)
31-
.scan()
32-
scan
33-
.getResourcesWithLeafName("test.yaml")
34-
.flatMap { resource ->
35-
log.debug("Found test list in {}", resource)
36-
val yamlFile = File(resource.classpathElementFile.absolutePath + "/" + resource.path)
37-
val testConfiguration =
38-
Config {
39-
addSpec(Root)
40-
}.from.yaml.inputStream(resource.open())
41-
testConfiguration[Root.tests].map { it to yamlFile.parentFile }
42-
}.forEach { (test, location) ->
43-
log.debug("Test to be executed: {} from {}", test, location)
44-
val testFolder =
45-
folder {
46-
location.copyRecursively(this.root)
47-
}
48-
log.debug("Test has been copied into {} and is ready to get executed", testFolder)
49-
test.description {
50-
File(testFolder.root, "gradle.properties").writeText(testkitProperties)
51-
val result =
52-
GradleRunner
53-
.create()
54-
.withProjectDir(testFolder.root)
55-
.withPluginClasspath()
56-
.withArguments(test.configuration.tasks + test.configuration.options)
57-
.run { if (test.expectation.failure.isEmpty()) build() else buildAndFail() }
58-
println(result.tasks)
59-
println(result.output)
60-
test.expectation.output_contains.forEach {
61-
result.output shouldContain it
62-
}
63-
test.expectation.output_doesnt_contain.forEach {
64-
result.output shouldNotContain it
65-
}
66-
test.expectation.success.forEach {
67-
result.outcomeOf(it) shouldBe TaskOutcome.SUCCESS
68-
}
69-
test.expectation.failure.forEach {
70-
result.outcomeOf(it) shouldBe TaskOutcome.FAILED
71-
}
72-
test.expectation.file_exists.forEach {
73-
val file =
74-
File("${testFolder.root.absolutePath}/${it.name}").apply {
75-
shouldExist()
76-
shouldBeAFile()
77-
}
78-
it.validate(file)
79-
}
30+
val scan = ClassGraph().enableAllInfo()
31+
.acceptPackages(Tests::class.java.`package`.name)
32+
.scan()
33+
scan.getResourcesWithLeafName("test.yaml").flatMap { resource ->
34+
log.debug("Found test list in {}", resource)
35+
val yamlFile = File(resource.classpathElementFile.absolutePath + "/" + resource.path)
36+
val testConfiguration =
37+
Config {
38+
addSpec(Root)
39+
}.from.yaml.inputStream(resource.open())
40+
testConfiguration[Root.tests].map { it to yamlFile.parentFile }
41+
}.forEach { (test, location) ->
42+
log.debug("Test to be executed: {} from {}", test, location)
43+
val testFolder = folder { location.copyRecursively(toFile()) }
44+
log.debug("Test has been copied into {} and is ready to get executed", testFolder)
45+
test.description {
46+
testFolder.resolve("gradle.properties").writeText(testkitProperties)
47+
val result =
48+
GradleRunner
49+
.create()
50+
.withProjectDir(testFolder.toFile())
51+
.withPluginClasspath()
52+
.withArguments(test.configuration.tasks + test.configuration.options)
53+
.run { if (test.expectation.failure.isEmpty()) build() else buildAndFail() }
54+
println(result.tasks)
55+
println(result.output)
56+
test.expectation.output_contains.forEach {
57+
result.output shouldContain it
58+
}
59+
test.expectation.output_doesnt_contain.forEach {
60+
result.output shouldNotContain it
61+
}
62+
test.expectation.success.forEach {
63+
result.outcomeOf(it) shouldBe TaskOutcome.SUCCESS
64+
}
65+
test.expectation.failure.forEach {
66+
result.outcomeOf(it) shouldBe TaskOutcome.FAILED
67+
}
68+
test.expectation.file_exists.forEach {
69+
val file =
70+
File("${testFolder.absolutePathString()}/${it.name}").apply {
71+
shouldExist()
72+
shouldBeAFile()
73+
}
74+
it.validate(file)
8075
}
8176
}
77+
}
8278
},
8379
) {
8480
companion object {
@@ -88,9 +84,6 @@ class Tests :
8884
"Task $name was not present among the executed tasks"
8985
}
9086

91-
private fun folder(closure: TemporaryFolder.() -> Unit) = TemporaryFolder().apply {
92-
create()
93-
closure()
94-
}
87+
private fun folder(closure: Path.() -> Unit) = createTempDirectory("plugin-test").apply(closure)
9588
}
9689
}

0 commit comments

Comments
 (0)