diff --git a/src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt b/src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt index 0a193d0c..59c777e3 100644 --- a/src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt +++ b/src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt @@ -56,7 +56,9 @@ abstract class NodeSetupTask : BaseTask() { private fun deleteExistingNode() { projectHelper.delete { - delete(nodeDir.get().dir("../")) + delete(nodeDir.get().dir("../").asFileTree.matching { + include("node-v*/**") + }) } } diff --git a/src/test/groovy/com/github/gradle/AbstractIntegTest.groovy b/src/test/groovy/com/github/gradle/AbstractIntegTest.groovy index 63058189..207df14c 100644 --- a/src/test/groovy/com/github/gradle/AbstractIntegTest.groovy +++ b/src/test/groovy/com/github/gradle/AbstractIntegTest.groovy @@ -61,10 +61,11 @@ abstract class AbstractIntegTest extends Specification { return new File(temporaryFolder.getRoot(), name) } - protected final void writeFile(final String name, final String text) { + protected final File writeFile(final String name, final String text) { File file = createFile(name) file.parentFile.mkdirs() file << text + return file } protected final void writePackageJson(final String text) { diff --git a/src/test/groovy/com/github/gradle/node/task/NodeSetupTask_integTest.groovy b/src/test/groovy/com/github/gradle/node/task/NodeSetupTask_integTest.groovy index ef91ad7d..a0de1726 100644 --- a/src/test/groovy/com/github/gradle/node/task/NodeSetupTask_integTest.groovy +++ b/src/test/groovy/com/github/gradle/node/task/NodeSetupTask_integTest.groovy @@ -47,4 +47,34 @@ node { gv << GRADLE_VERSIONS_UNDER_TEST } + def 'nodeSetup should only delete old node versions (#gv.version)'() { + given: + gradleVersion = gv + + def badVersion = "node-v18.17.0" + def goodVersion = "18.17.1" + createFile("build.gradle") << """plugins { + id "com.github.node-gradle.node" + } + + node { + version = "${goodVersion}" + download = true + workDir = file("build/node") + }""" + def badFile = writeFile("build/node/$badVersion/bin/node.js", "console.log(\"bad\");") + def goodFile = writeFile("build/node/important.txt", "should not be deleted by nodeSetup") + + when: + def result1 = build("nodeSetup") + + then: + result1.task(":nodeSetup").outcome == TaskOutcome.SUCCESS + !badFile.exists() + goodFile.exists() + + where: + gv << GRADLE_VERSIONS_UNDER_TEST + } + }