Skip to content

Commit

Permalink
Add tests for up-to-date check
Browse files Browse the repository at this point in the history
  • Loading branch information
ksoichiro committed Dec 17, 2016
1 parent c407627 commit be02815
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class GenerateBuildInfoTask extends DefaultTask {
}

void generateGitProperties() {
if (!extension.gitPropertiesEnabled && !hasDependency) {
if (!shouldGenerateGitProperties()) {
return
}
if (gitInfo.missing && extension.warnIfGitDirectoryIsMissing) {
Expand Down Expand Up @@ -119,7 +119,7 @@ class GenerateBuildInfoTask extends DefaultTask {
}

void mergeManifest() {
if (!project.plugins.hasPlugin(JavaPlugin) || !extension.manifestEnabled) {
if (!shouldMergeManifest()) {
return
}

Expand Down Expand Up @@ -191,7 +191,7 @@ class GenerateBuildInfoTask extends DefaultTask {

def validate() {
// If all options are disabled, skip this task not to cache the result
if (!extension.gitPropertiesEnabled && !hasDependency && !extension.manifestEnabled) {
if (!shouldGenerateGitProperties() && !shouldMergeManifest()) {
throw new StopExecutionException()
}
if (gitInfo.missing) {
Expand All @@ -206,6 +206,14 @@ class GenerateBuildInfoTask extends DefaultTask {
}
}

boolean shouldGenerateGitProperties() {
extension.gitPropertiesEnabled || hasDependency
}

boolean shouldMergeManifest() {
project.plugins.hasPlugin(JavaPlugin) && extension.manifestEnabled
}

static boolean hasDependency(Project project, String group, String name) {
if (!project.plugins.hasPlugin(JavaPlugin)) {
return false
Expand Down
168 changes: 168 additions & 0 deletions src/test/groovy/com/github/ksoichiro/build/info/FunctionalTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,172 @@ class FunctionalTest {
assertFalse(manifestAttrs.containsKey('Build-Os-Name'))
assertFalse(manifestAttrs.containsKey('Build-Os-Version'))
}

@Test
public void upToDateWhenCommitIdDoesNotChange() {
def buildFileContent = """\
|plugins {
| id '${PLUGIN_ID}'
|}
|apply plugin: 'java'
|buildInfo {
| gitPropertiesEnabled true
|}
|""".stripMargin().stripIndent()
buildFile.text = buildFileContent

def result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)

result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.UP_TO_DATE)

// Removing output causes build
new File("${rootDir}/build/resources/main/git.properties").delete()

result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)
}

@Test
public void doNotSkipWhenCommitIdChanges() {
def buildFileContent = """\
|plugins {
| id '${PLUGIN_ID}'
|}
|apply plugin: 'java'
|buildInfo {
| gitPropertiesEnabled true
|}
|""".stripMargin().stripIndent()
buildFile.text = buildFileContent

def result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

println "1st"
println new File("${rootDir}/build/resources/main/git.properties").text
assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)

new File(rootDir, "README.md").text = """\
|# Test
|""".stripMargin().stripIndent()
grgit.add(patterns: ['README.md'])
grgit.commit(message: 'Add readme')

result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

println "2nd"
println new File("${rootDir}/build/resources/main/git.properties").text
assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)
}

@Test
public void skipWhenAllOfTheFeaturesAreDisabled() {
def buildFileContent = """\
|plugins {
| id '${PLUGIN_ID}'
|}
|apply plugin: 'java'
|buildInfo {
| gitPropertiesEnabled false
| manifestEnabled false
|}
|""".stripMargin().stripIndent()
buildFile.text = buildFileContent

def result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

println result.output
// Throwing StopExecutionException results in SUCCESS (not SKIPPED or UP_TO_DATE)
assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)
}

@Test
public void skipWhenGitStatusIsDirtyAndSomethingChanges() {
def buildFileContent = """\
|plugins {
| id '${PLUGIN_ID}'
|}
|apply plugin: 'java'
|buildInfo {
| gitPropertiesEnabled true
|}
|""".stripMargin().stripIndent()
buildFile.text = buildFileContent

// Make working copy dirty
new File(rootDir, "README.md").text = """\
|# Test
|""".stripMargin().stripIndent()

def result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

println "1st"
println new File("${rootDir}/build/resources/main/git.properties").text
assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)

// Update dirty working copy
new File(rootDir, "README.md").text = """\
|# Test
|
|This is a test.
|""".stripMargin().stripIndent()

result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

// Even when some files are changed, it is assumed to be up-to-date because the commit does not change
println "2nd"
println new File("${rootDir}/build/resources/main/git.properties").text
assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.UP_TO_DATE)

// Commit causes build
grgit.add(patterns: ['README.md'])
grgit.commit(message: 'Add readme')

result = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(GenerateBuildInfoTask.NAME)
.withPluginClasspath(pluginClasspath)
.build()

// Even when some files are changed, it is assumed to be up-to-date because the commit does not change
println "3rd"
println new File("${rootDir}/build/resources/main/git.properties").text
assertEquals(result.task(":${GenerateBuildInfoTask.NAME}").getOutcome(), TaskOutcome.SUCCESS)
}
}

0 comments on commit be02815

Please sign in to comment.