Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Gradle TestKit for integration tests #247

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,3 @@ gradlePlugin {
}
}

/**
* This is temporary until config cache serialization is fixed when writing tests
* More in https://github.com/gradle/gradle/issues/25898
*/
tasks.withType(Test).configureEach {
Provider<String> jdkVersionForTestsEnvVariable = providers.environmentVariable("JDK_VERSION_FOR_TESTS")
Integer jdkVersionForTests = jdkVersionForTestsEnvVariable.isPresent() ? jdkVersionForTestsEnvVariable.get().toInteger() : 8
if(jdkVersionForTests >= 17) {
jvmArgs = [
'--add-opens=java.base/java.lang=ALL-UNNAMED',
'--add-opens=java.base/java.util=ALL-UNNAMED',
'--add-opens=java.base/java.lang.invoke=ALL-UNNAMED',
'--add-opens=java.base/java.net=ALL-UNNAMED',
'--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED'

]
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package nebula.plugin.release

/**
* Tests for {@link GitVersioningIntegrationSpec}.
* Tests for {@link GitVersioningIntegrationTestKitSpec}.
*/
class GitVersioningIntegrationSpecSpec extends GitVersioningIntegrationSpec {
class GitVersioningIntegrationSpecSpec extends GitVersioningIntegrationTestKitSpec {
@Override def setupBuild() {}

def 'inferred version is parsed'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package nebula.plugin.release

import com.github.zafarkhaja.semver.Version
import nebula.test.IntegrationSpec
import nebula.test.IntegrationTestKitSpec
import org.ajoberstar.grgit.Grgit

import java.nio.file.Files

abstract class GitVersioningIntegrationSpec extends IntegrationSpec {
abstract class GitVersioningIntegrationTestKitSpec extends IntegrationTestKitSpec {
protected Grgit git
protected Grgit originGit

Expand All @@ -33,18 +34,21 @@ abstract class GitVersioningIntegrationSpec extends IntegrationSpec {
origin.mkdirs()

['build.gradle', 'settings.gradle'].each {
Files.move(new File(projectDir, it).toPath(), new File(origin, it).toPath())
def file = new File(projectDir, it)
if(!file.exists()) {
file.createNewFile()
}
Files.move(file.toPath(), new File(origin, it).toPath())
}

originGit = Grgit.init(dir: origin)

originGit.add(patterns: ['build.gradle', 'settings.gradle', '.gitignore', 'gradle.properties'] as Set)
originGit.add(patterns: ['build.gradle', 'settings.gradle', '.gitignore'] as Set)
originGit.commit(message: 'Initial checkout')

git = Grgit.clone(dir: projectDir, uri: origin.absolutePath) as Grgit

new File(projectDir, '.gitignore') << '''.gradle-test-kit/
.gradle/
new File(projectDir, '.gitignore') << '''.gradle-test-kit
.gradle
build/
gradle.properties'''.stripIndent()

Expand All @@ -53,6 +57,8 @@ gradle.properties'''.stripIndent()

setupBuild()


git.add(patterns: ['build.gradle', 'settings.gradle', '.gitignore'])
git.commit(message: 'Setup')
git.push()
}
Expand All @@ -73,8 +79,8 @@ gradle.properties'''.stripIndent()
}

def Version inferredVersionForTask(String... args) {
def result = runTasksSuccessfully(args)
inferredVersion(result.standardOutput)
def result = runTasks(args)
inferredVersion(result.output)
}

def Version inferredVersion(String standardOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ import spock.lang.Unroll

@Ignore("We need to visit the configuration/evaluation of extension")
@Issue("Inconsistent versioning for SNAPSHOT stage https://github.com/nebula-plugins/nebula-release-plugin/issues/187")
class Issue187IntegrationSpec extends GitVersioningIntegrationSpec {
class Issue187IntegrationSpec extends GitVersioningIntegrationTestKitSpec {
@Override
def setupBuild() {
fork = false
buildFile << """
plugins {
id 'com.netflix.nebula.release'
id 'java'
}
ext.dryRun = true
group = 'test'
${applyPlugin(ReleasePlugin)}
${applyPlugin(JavaPlugin)}

""".stripIndent()

git.add(patterns: ['build.gradle', '.gitignore'] as Set)
Expand All @@ -45,16 +47,16 @@ release {
"""

when:
def resultBuild = runTasksSuccessfully('build')
def resultBuild = runTasks('build')

then:
resultBuild.standardOutput.contains('version: 0.3.0-SNAPSHOT')
resultBuild.output.contains('version: 0.3.0-SNAPSHOT')

when:
def resultSnapshot = runTasksSuccessfully('snapshot')
def resultSnapshot = runTasks('snapshot')

then:
resultSnapshot.standardOutput.contains('version: 0.3.0-SNAPSHOT')
resultSnapshot.output.contains('version: 0.3.0-SNAPSHOT')
}

@Unroll
Expand All @@ -66,16 +68,16 @@ release {
"""

when:
def resultBuild = runTasksSuccessfully('build', "-Prelease.scope=${scope}")
def resultBuild = runTasks('build', "-Prelease.scope=${scope}")

then:
resultBuild.standardOutput.contains("version: ${expectedVersion}")
resultBuild.output.contains("version: ${expectedVersion}")

when:
def resultSnapshot = runTasksSuccessfully('snapshot', "-Prelease.scope=${scope}")
def resultSnapshot = runTasks('snapshot', "-Prelease.scope=${scope}")

then:
resultSnapshot.standardOutput.contains("version: ${expectedVersion}")
resultSnapshot.output.contains("version: ${expectedVersion}")

where:
scope | expectedVersion
Expand All @@ -87,16 +89,16 @@ release {
@Unroll
def 'infer #expectedVersion for #task task when not using snapshot strategy'() {
when:
def resultBuild = runTasksSuccessfully('build')
def resultBuild = runTasks('build')

then:
resultBuild.standardOutput.contains('version: 0.3.0-dev')
resultBuild.output.contains('version: 0.3.0-dev')

when:
def resultSnapshot = runTasksSuccessfully(task)
def resultSnapshot = runTasks(task)

then:
resultSnapshot.standardOutput.contains("version: $expectedVersion")
resultSnapshot.output.contains("version: $expectedVersion")

where:
task | expectedVersion
Expand All @@ -108,16 +110,16 @@ release {
@Unroll
def 'infer release version #expectedReleaseVersion and build version #expectedBuildVersion for #task task when not using snapshot strategy with scope #scope'() {
when:
def resultBuild = runTasksSuccessfully('build', "-Prelease.scope=${scope}")
def resultBuild = runTasks('build', "-Prelease.scope=${scope}")

then:
resultBuild.standardOutput.contains("version: $expectedBuildVersion")
resultBuild.output.contains("version: $expectedBuildVersion")

when:
def resultSnapshot = runTasksSuccessfully(task, "-Prelease.scope=${scope}")
def resultSnapshot = runTasks(task, "-Prelease.scope=${scope}")

then:
resultSnapshot.standardOutput.contains("version: $expectedReleaseVersion")
resultSnapshot.output.contains("version: $expectedReleaseVersion")

where:
task | scope | expectedReleaseVersion | expectedBuildVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package nebula.plugin.release

import org.gradle.api.plugins.JavaPlugin

class ReleasePluginConfiguredVersionIntegrationSpec extends GitVersioningIntegrationSpec {
class ReleasePluginConfiguredVersionIntegrationSpec extends GitVersioningIntegrationTestKitSpec {

@Override
def setupBuild() {
buildFile << """
plugins {
id 'com.netflix.nebula.release'
}
allprojects {
${applyPlugin(ReleasePlugin)}
apply plugin: 'com.netflix.nebula.release'
}

subprojects {
ext.dryRun = true
group = 'test'
${applyPlugin(JavaPlugin)}
apply plugin: 'java'
}

version = '1.0.0'
Expand All @@ -35,10 +36,10 @@ class ReleasePluginConfiguredVersionIntegrationSpec extends GitVersioningIntegra

def 'should fail build if version is set in build file'() {
when:
def results = runTasksWithFailure('final')
def results = runTasksAndFail('final')

then:
results.standardError.contains('version should not be set in build file when using nebula-release plugin. Instead use `-Prelease.version` parameter')
results.output.contains('version should not be set in build file when using nebula-release plugin. Instead use `-Prelease.version` parameter')
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package nebula.plugin.release

import org.gradle.api.plugins.JavaPlugin

import static org.codehaus.groovy.runtime.StackTraceUtils.extractRootCause

class ReleasePluginGitStateIntegrationSpec extends GitVersioningIntegrationSpec {
class ReleasePluginGitStateIntegrationSpec extends GitVersioningIntegrationTestKitSpec {

@Override
def setupBuild() {
buildFile << """
${applyPlugin(ReleasePlugin)}
${applyPlugin(JavaPlugin)}
plugins {
id 'com.netflix.nebula.release'
id 'java'
}
"""
}

Expand All @@ -19,24 +18,24 @@ class ReleasePluginGitStateIntegrationSpec extends GitVersioningIntegrationSpec
buildFile << "// force an uncommitted change to file"

when:
def finalFail = runTasksWithFailure("final")
def finalFail = runTasksAndFail("final")

then:
extractRootCause(finalFail.failure).message.contains('require all changes to be committed into Git')
finalFail.output.contains('require all changes to be committed into Git')

when:
def candidateFail = runTasksWithFailure("candidate")
def candidateFail = runTasksAndFail("candidate")

then:
extractRootCause(candidateFail.failure).message.contains('require all changes to be committed into Git')
candidateFail.output.contains('require all changes to be committed into Git')
}

def 'ensure plugin does NOT throw an error when a good init tag is present'() {
setup:
['my-feature-branch', 'super-duper', 'v1.0', 'v0.1.0'].each { git.tag.add(name: it) }

expect:
runTasksSuccessfully("devSnapshot")
runTasks("devSnapshot")
}

}
Loading
Loading