Skip to content

Commit

Permalink
[JENKINS-42847] Include automated test for CLI command (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evaristo Gutiérrez authored and wolfs committed Mar 31, 2017
1 parent 0103221 commit 4060b4f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
Expand Up @@ -31,9 +31,20 @@ class GradleInstallationRule extends TestWatcher {
}

void addInstallation() {
addInstallations(gradleVersion)
}

void addInstallations(String... installationNames) {
def gradleInstallationDescriptor = j.jenkins.getDescriptorByType(hudson.plugins.gradle.GradleInstallation.DescriptorImpl)
gradleInstallationDescriptor.setInstallations(
new GradleInstallation(gradleVersion, "", [new InstallSourceProperty([new GradleInstaller(gradleVersion)])]))

GradleInstallation[] installations = new GradleInstallation[installationNames.size()]

for (int i = 0; i < installationNames.size(); i++) {
String name = installationNames[i]
installations[i] = new GradleInstallation(name, "", [new InstallSourceProperty([new GradleInstaller(gradleVersion)])])
}

gradleInstallationDescriptor.setInstallations(installations)

assert gradleInstallationDescriptor.getInstallations()
}
Expand Down
Expand Up @@ -28,6 +28,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlButton
import com.gargoylesoftware.htmlunit.html.HtmlForm
import com.gargoylesoftware.htmlunit.html.HtmlPage
import com.google.common.base.Joiner
import hudson.cli.CLICommandInvoker
import hudson.model.Cause
import hudson.model.FreeStyleBuild
import hudson.model.FreeStyleProject
Expand All @@ -39,6 +40,9 @@ import hudson.model.TextParameterValue
import hudson.remoting.Launcher
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
Expand Down Expand Up @@ -299,6 +303,40 @@ 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']
}
Expand All @@ -317,4 +355,30 @@ 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)
}

}

0 comments on commit 4060b4f

Please sign in to comment.