Skip to content

Commit

Permalink
Update paket restore UP-TO-DATE handling and clean pattern
Browse files Browse the repository at this point in the history
Description
===========

We defined the `UP-TO-DATE` spec as always false for testing reasons in
the past. This pull requests adds tests and `upToDateWhen` specs to
mimic `paket restore`'s own update check.

This pull request brings also a change in the paket base plugin. Paket
base will apply the `LifecycleBase` plugin to be able to adjust the
`clean` task. The `clean` task will delete all paket generated
directories: `.paket`, `packages` and `paket-files`.

Changes
=======

![IMPROVE] `paketRestore` *UP-TO-DATE* check
![ADD] clean task and configure deletion of paket directories
  • Loading branch information
Larusso committed Apr 10, 2018
1 parent eedab71 commit df3a37f
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package wooga.gradle.paket.base

import org.gradle.language.base.plugins.LifecycleBasePlugin
import spock.lang.Shared
import spock.lang.Unroll
import wooga.gradle.paket.PaketIntegrationBaseSpec
import wooga.gradle.paket.get.PaketGetPlugin

Expand All @@ -38,5 +41,24 @@ class PaketBaseIntegrationSpec extends PaketIntegrationBaseSpec {
return [PaketBasePlugin.INIT_TASK_NAME]
}

@Shared
private List<String> paketDirectories = ["packages", ".paket", "paket-files"]

@Unroll
def "task :#taskToRun deletes paket directories"() {
given: "a directory with paket directories"
def paketDirectories = paketDirectories.collect { new File(projectDir, it) }
paketDirectories.each { it.mkdirs() }
assert paketDirectories.every { it.exists() && it.isDirectory() }

when:
runTasksSuccessfully(taskToRun)

then:
paketDirectories.every { !it.exists() }

where:
taskToRun = LifecycleBasePlugin.CLEAN_TASK_NAME
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class PaketInstallIntegrationSpec extends PaketIntegrationDependencyFileSpec {
return [
PaketGetPlugin.INSTALL_TASK_NAME,
PaketGetPlugin.UPDATE_TASK_NAME,
PaketGetPlugin.RESTORE_TASK_NAME,
PaketGetPlugin.OUTDATED_TASK_NAME
]
}
Expand All @@ -58,7 +57,7 @@ class PaketInstallIntegrationSpec extends PaketIntegrationDependencyFileSpec {
assert !packagesDir.exists()

when:
def result = runTasksSuccessfully('paketInstall')
def result = runTasksSuccessfully(taskToRun)

then: "paket runs and creates the packages directory"
packagesDir.exists()
Expand All @@ -67,7 +66,7 @@ class PaketInstallIntegrationSpec extends PaketIntegrationDependencyFileSpec {
result.standardOutput =~ /(?ms)Resolving packages for group Main:.*- $nuget/

where:
taskToRun << bootstrapTestCases
taskToRun << [PaketGetPlugin.INSTALL_TASK_NAME, PaketGetPlugin.UPDATE_TASK_NAME]
}

@Unroll
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package wooga.gradle.paket.get

import spock.lang.Shared
import spock.lang.Unroll
import wooga.gradle.paket.PaketIntegrationBaseSpec

class PaketRestoreIntegrationSpec extends PaketIntegrationBaseSpec {

def setup() {
buildFile << """
group = 'test'
${applyPlugin(PaketGetPlugin)}
""".stripIndent()
}

@Override
Object getBootstrapTestCases() {
return [
PaketGetPlugin.RESTORE_TASK_NAME
]
}

@Unroll
def "task :#taskToRun fails when paket.lock is missing"() {
given: "a paket dependency file"
createFile("paket.dependencies") << """
source https://nuget.org/api/v2
nuget Mini
""".stripIndent()

when:
def result = runTasksWithFailure(taskToRun)

then:
result.standardError.contains("specified for property 'paketLock' does not exist")

where:
taskToRun << [PaketGetPlugin.RESTORE_TASK_NAME]
}

@Shared
private List<String> paketDirectories = ["packages", ".paket", "paket-files"]

@Unroll
def "task :#taskToRun restores when deleting paket dir: #dirToDelete"() {
given: "a paket dependency file"
createFile("paket.dependencies") << """
source https://nuget.org/api/v2
nuget Mini
""".stripIndent()

and: "the future paket files"
def paketDirectories = paketDirectories.collect {new File(projectDir, it)}
def restoreCacheFile = new File(projectDir, "paket-files/paket.restore.cached")

and: "a paket install run to create the lock file and packages directory"
runTasksSuccessfully("paketInstall")

assert paketDirectories.every {it.exists()}
assert !restoreCacheFile.exists()

when: "deleting packages directory"
new File(projectDir, dirToDelete).delete()

and: "running paket restore"
runTasksSuccessfully(taskToRun)

then:
paketDirectories.every {it.exists()}
restoreCacheFile.exists()

where:
taskToRun = PaketGetPlugin.RESTORE_TASK_NAME
dirToDelete << paketDirectories
}

@Unroll
def "task :#taskToRun caches last restore state"() {
given: "a paket dependency file"
createFile("paket.dependencies") << """
source https://nuget.org/api/v2
nuget Mini
""".stripIndent()

and: "the future paket files"
def restoreCacheFile = new File(projectDir, "paket-files/paket.restore.cached")

and: "a paket install run to create the lock file and packages directory"
runTasksSuccessfully("paketInstall")

and: "deleting packages directory"
new File(projectDir, "packages").delete()

and: "running paket restore"
runTasksSuccessfully(taskToRun)

when: "running restore again"
def result = runTasksSuccessfully(taskToRun)

then:
result.wasUpToDate(taskToRun)

when: "deleting the cache file"
restoreCacheFile.delete()
result = runTasksSuccessfully(taskToRun)

then:
!result.wasUpToDate(taskToRun)

where:
taskToRun = PaketGetPlugin.RESTORE_TASK_NAME
}
}
17 changes: 10 additions & 7 deletions src/main/groovy/wooga/gradle/paket/base/PaketBasePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ package wooga.gradle.paket.base
import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.Delete
import org.gradle.buildinit.tasks.internal.TaskConfiguration
import org.gradle.language.base.plugins.LifecycleBasePlugin
import wooga.gradle.paket.base.internal.DefaultPaketPluginExtension
import wooga.gradle.paket.base.tasks.internal.AbstractPaketTask
import wooga.gradle.paket.base.tasks.PaketBootstrap
Expand All @@ -45,12 +48,13 @@ class PaketBasePlugin implements Plugin<Project> {
this.project = project

final extension = project.extensions.create(EXTENSION_NAME, DefaultPaketPluginExtension, project)

project.pluginManager.apply(LifecycleBasePlugin)
configurePaketTasks(project, extension)
addBootstrapTask(project, extension)
addInitTask(project, extension)
setConfigurations(project)
setupPaketTasks(project)
setCleanTargets(project)
}

private static void setupPaketTasks(final Project project) {
Expand Down Expand Up @@ -106,12 +110,6 @@ class PaketBasePlugin implements Plugin<Project> {
taskConvention.map("executable", { extension.getBootstrapperExecutable() })
taskConvention.map("bootstrapURL", { extension.getPaketBootstrapperUrl() })
taskConvention.map("paketVersion", { extension.getVersion() })

/*
taskConvention.map("outputFiles", {
project.files(extension.getExecutable(), extension.getBootstrapperExecutable())
})
*/
}

private static void setConfigurations(final Project project) {
Expand All @@ -120,4 +118,9 @@ class PaketBasePlugin implements Plugin<Project> {
configuration.description = "paket nupkg archive"
configuration.transitive = false
}

private static void setCleanTargets(final Project project) {
final clean = project.tasks.getByName(LifecycleBasePlugin.CLEAN_TASK_NAME) as Delete
clean.delete(project.file("paket-files"), project.file("packages"), project.file(".paket"))
}
}
25 changes: 23 additions & 2 deletions src/main/groovy/wooga/gradle/paket/get/tasks/PaketRestore.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,40 @@

package wooga.gradle.paket.get.tasks

import wooga.gradle.paket.internal.PaketCommand
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import wooga.gradle.paket.base.tasks.internal.AbstractPaketTask
import wooga.gradle.paket.internal.PaketCommand

/**
* A task to invoke {@code paket restore} command.
*/
class PaketRestore extends AbstractPaketTask {

@InputFile
File getPaketLock() {
project.file("paket.lock")
}

@OutputFile
File getRestoreCacheOut() {
project.file("paket-files/paket.restore.cached")
}

PaketRestore() {
super(PaketRestore.class)
description = "Download the dependencies specified by the paket.lock file into the packages/ directory."
paketCommand = PaketCommand.RESTORE
outputs.upToDateWhen { false }
outputs.upToDateWhen(new Spec<PaketRestore>() {
@Override
boolean isSatisfiedBy(PaketRestore restore) {
if(!restore.getRestoreCacheOut().exists() || !restore.getPaketLock().exists()) {
return false
}

restore.getPaketLock().text == restore.getRestoreCacheOut().text
}
})
}
}

0 comments on commit df3a37f

Please sign in to comment.