diff --git a/src/test/groovy/hudson/plugins/gradle/AbstractIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/AbstractIntegrationTest.groovy new file mode 100644 index 00000000..590644c0 --- /dev/null +++ b/src/test/groovy/hudson/plugins/gradle/AbstractIntegrationTest.groovy @@ -0,0 +1,17 @@ +package hudson.plugins.gradle + +import org.junit.Rule +import org.junit.rules.RuleChain +import org.jvnet.hudson.test.JenkinsRule +import spock.lang.Specification + +class AbstractIntegrationTest extends Specification { + final JenkinsRule j = new JenkinsRule() + final GradleInstallationRule gradleInstallationRule = new GradleInstallationRule(j) + @Rule + public final RuleChain rules = RuleChain.outerRule(j).around(gradleInstallationRule) + + Map getDefaults() { + [gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: true, switches: '--no-daemon'] + } +} diff --git a/src/test/groovy/hudson/plugins/gradle/BuildScanIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/BuildScanIntegrationTest.groovy new file mode 100644 index 00000000..ec02dcbb --- /dev/null +++ b/src/test/groovy/hudson/plugins/gradle/BuildScanIntegrationTest.groovy @@ -0,0 +1,32 @@ +package hudson.plugins.gradle + +import hudson.model.FreeStyleProject +import org.jvnet.hudson.test.CreateFileBuilder + +class BuildScanIntegrationTest extends AbstractIntegrationTest { + def 'build scan is discovered'() { + given: + gradleInstallationRule.addInstallation() + FreeStyleProject p = j.createFreeStyleProject() + p.buildersList.add(new CreateFileBuilder("build.gradle", """ +plugins { + id 'com.gradle.build-scan' version '1.6' +} + +buildScan { + licenseAgreementUrl = 'https://gradle.com/terms-of-service' + licenseAgree = 'yes' +} + +task hello << { println 'Hello' }""")) + p.buildersList.add(new Gradle(tasks: 'hello', *: defaults, switches: '-Dscan --no-daemon')) + + when: + def build = j.buildAndAssertSuccess(p) + + then: + def action = build.getAction(BuildScanAction) + action.scanUrl.contains('gradle.com') + new URL(action.scanUrl) + } +} diff --git a/src/test/groovy/hudson/plugins/gradle/CliIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/CliIntegrationTest.groovy new file mode 100644 index 00000000..efc27c6e --- /dev/null +++ b/src/test/groovy/hudson/plugins/gradle/CliIntegrationTest.groovy @@ -0,0 +1,67 @@ +package hudson.plugins.gradle + +import hudson.cli.CLICommandInvoker +import net.sf.json.JSON +import net.sf.json.JSONArray +import net.sf.json.JSONObject + +class CliIntegrationTest extends AbstractIntegrationTest { + def 'list installations through CLI'() { + when: + CLICommandInvoker.Result result = new CLICommandInvoker(j, "get-gradle").invoke() + + then: + assertCLIResult(result, '{}') + + when: + gradleInstallationRule.addInstallations("inst1") + result = new CLICommandInvoker(j, "get-gradle").invoke() + + then: + assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"]}')) + + when: + gradleInstallationRule.addInstallations("inst1", "inst2") + result = new CLICommandInvoker(j, "get-gradle").invoke() + + then: + assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"],"inst2":["%s"]}')) + + when: + result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=inst1") + + then: + assertCLIResult(result, expectedOutputForVersion('["%s"]')) + + when: + result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=unknown") + + then: + assertCLIError(result, 'Requested gradle installation not found: unknown') + } + + private static void assertCLIResult(CLICommandInvoker.Result result, String expectedOutput) { + assert result.returnCode() == 0 + + JSON expectedJson, resultJson + + if (expectedOutput.startsWith("[")) { + expectedJson = JSONArray.fromObject(expectedOutput) + resultJson = JSONArray.fromObject(result.stdout().trim()) + } else { + expectedJson = JSONObject.fromObject(expectedOutput) + resultJson = JSONObject.fromObject(result.stdout().trim()) + } + + assert resultJson == expectedJson + } + + private static void assertCLIError(CLICommandInvoker.Result result, String expectedOutput) { + assert result.returnCode() == 1 + assert result.stderr().trim() == expectedOutput + } + + private String expectedOutputForVersion(String output) { + return String.format(output, gradleInstallationRule.gradleVersion, gradleInstallationRule.gradleVersion) + } +} diff --git a/src/test/groovy/hudson/plugins/gradle/GradlePluginIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/GradlePluginIntegrationTest.groovy index ab6f3f4a..c3e9b0c2 100644 --- a/src/test/groovy/hudson/plugins/gradle/GradlePluginIntegrationTest.groovy +++ b/src/test/groovy/hudson/plugins/gradle/GradlePluginIntegrationTest.groovy @@ -29,35 +29,21 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm import com.gargoylesoftware.htmlunit.html.HtmlPage import com.google.common.base.Joiner import hudson.EnvVars -import hudson.cli.CLICommandInvoker import hudson.model.FreeStyleBuild import hudson.model.FreeStyleProject import hudson.model.Result import hudson.tools.InstallSourceProperty import hudson.util.VersionNumber -import net.sf.json.JSON -import net.sf.json.JSONArray -import net.sf.json.JSONObject -import org.junit.Rule -import org.junit.rules.RuleChain import org.jvnet.hudson.test.CreateFileBuilder -import org.jvnet.hudson.test.JenkinsRule import org.jvnet.hudson.test.JenkinsRule.WebClient -import spock.lang.Specification import spock.lang.Unroll import static org.jvnet.hudson.test.JenkinsRule.getLog - /** * Tests for the Gradle build step. */ @Unroll -class GradlePluginIntegrationTest extends Specification { - private final JenkinsRule j = new JenkinsRule() - private final GradleInstallationRule gradleInstallationRule = new GradleInstallationRule(j) - @Rule - public final RuleChain rules = RuleChain.outerRule(j).around(gradleInstallationRule) - +class GradlePluginIntegrationTest extends AbstractIntegrationTest { def 'run the default tasks'() { given: gradleInstallationRule.addInstallation() @@ -100,32 +86,6 @@ class GradlePluginIntegrationTest extends Specification { getLog(build).contains "Hello" } - def 'build scan is discovered'() { - given: - gradleInstallationRule.addInstallation() - FreeStyleProject p = j.createFreeStyleProject() - p.buildersList.add(new CreateFileBuilder("build.gradle", """ -plugins { - id 'com.gradle.build-scan' version '1.6' -} - -buildScan { - licenseAgreementUrl = 'https://gradle.com/terms-of-service' - licenseAgree = 'yes' -} - -task hello << { println 'Hello' }""")) - p.buildersList.add(new Gradle(tasks: 'hello', *: defaults, switches: '-Dscan --no-daemon')) - - when: - def build = j.buildAndAssertSuccess(p) - - then: - def action = build.getAction(BuildScanAction) - action.scanUrl.contains('gradle.com') - new URL(action.scanUrl) - } - def 'wrapper in base dir'() { given: gradleInstallationRule.addInstallation() @@ -248,44 +208,6 @@ task hello << { println 'Hello' }""")) installationConfigured() } - def 'list installations through CLI'() { - when: - CLICommandInvoker.Result result = new CLICommandInvoker(j, "get-gradle").invoke() - - then: - assertCLIResult(result, '{}') - - when: - gradleInstallationRule.addInstallations("inst1") - result = new CLICommandInvoker(j, "get-gradle").invoke() - - then: - assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"]}')) - - when: - gradleInstallationRule.addInstallations("inst1", "inst2") - result = new CLICommandInvoker(j, "get-gradle").invoke() - - then: - assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"],"inst2":["%s"]}')) - - when: - result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=inst1") - - then: - assertCLIResult(result, expectedOutputForVersion('["%s"]')) - - when: - result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=unknown") - - then: - assertCLIError(result, 'Requested gradle installation not found: unknown') - } - - Map getDefaults() { - [gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: true, switches: '--no-daemon'] - } - private void installationConfigured() { GradleInstallation[] installations = j.get(Gradle.DescriptorImpl).getInstallations() assert installations.size() == 1 @@ -305,30 +227,4 @@ task hello << { println 'Hello' }""")) assert installers.size() == 1 assert installers.get(GradleInstaller) } - - private void assertCLIResult(hudson.cli.CLICommandInvoker.Result result, String expectedOutput) { - assert result.returnCode() == 0 - - JSON expectedJson, resultJson - - if (expectedOutput.startsWith("[")) { - expectedJson = JSONArray.fromObject(expectedOutput) - resultJson = JSONArray.fromObject(result.stdout().trim()) - } else { - expectedJson = JSONObject.fromObject(expectedOutput) - resultJson = JSONObject.fromObject(result.stdout().trim()) - } - - assert resultJson == expectedJson - } - - private void assertCLIError(hudson.cli.CLICommandInvoker.Result result, String expectedOutput) { - assert result.returnCode() == 1 - assert result.stderr().trim() == expectedOutput - } - - private String expectedOutputForVersion(String output) { - return String.format(output, gradleInstallationRule.gradleVersion, gradleInstallationRule.gradleVersion) - } - } diff --git a/src/test/groovy/hudson/plugins/gradle/PropertyPassingIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/PropertyPassingIntegrationTest.groovy index cc2db61a..a7857e6e 100644 --- a/src/test/groovy/hudson/plugins/gradle/PropertyPassingIntegrationTest.groovy +++ b/src/test/groovy/hudson/plugins/gradle/PropertyPassingIntegrationTest.groovy @@ -9,22 +9,13 @@ import hudson.model.TextParameterDefinition import hudson.model.TextParameterValue import hudson.model.queue.QueueTaskFuture import hudson.remoting.Launcher -import org.junit.Rule -import org.junit.rules.RuleChain import org.jvnet.hudson.test.CreateFileBuilder -import org.jvnet.hudson.test.JenkinsRule -import spock.lang.Specification import spock.lang.Unroll import static org.jvnet.hudson.test.JenkinsRule.getLog @Unroll -class PropertyPassingIntegrationTest extends Specification { - private final JenkinsRule j = new JenkinsRule() - private final GradleInstallationRule gradleInstallationRule = new GradleInstallationRule(j) - @Rule - public final RuleChain rules = RuleChain.outerRule(j).around(gradleInstallationRule) - +class PropertyPassingIntegrationTest extends AbstractIntegrationTest { def "pass '#escapedPropertyValue' via parameter in system properties"() { given: gradleInstallationRule.addInstallation() @@ -137,10 +128,6 @@ class PropertyPassingIntegrationTest extends Specification { ] } - Map getDefaults() { - [gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: false, switches: '--no-daemon'] - } - private static boolean createBuildScript(FreeStyleProject p, String buildScript) { p.buildersList.add(new CreateFileBuilder("build.gradle", buildScript)) }