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

Migrate snippet generator tests from workflow-cps #27

Merged
merged 6 commits into from
Jul 16, 2019
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
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<scm-api.version>2.2.7</scm-api.version>
<workflow-step-api-plugin.version>2.18</workflow-step-api-plugin.version>
<workflow-support-plugin.version>3.1</workflow-support-plugin.version>
<workflow-cps-plugin.version>2.62</workflow-cps-plugin.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -88,7 +89,14 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.62</version>
<version>${workflow-cps-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-cps-plugin.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public DescriptorImpl() {
super(BuildTriggerStepExecution.class);
}

// Note: This is necessary because the JSON format of the parameters produced by config.jelly when
// using the snippet generator does not match what would be neccessary for databinding to work automatically.
// For non-snippet generator use, this is unnecessary.
@Override public Step newInstance(StaplerRequest req, JSONObject formData) throws FormException {
jglick marked this conversation as resolved.
Show resolved Hide resolved
BuildTriggerStep step = (BuildTriggerStep) super.newInstance(req, formData);
// Cf. ParametersDefinitionProperty._doBuild:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.BooleanParameterDefinition;
import hudson.model.BooleanParameterValue;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Computer;
Expand All @@ -21,6 +22,7 @@
import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.model.queue.QueueTaskFuture;
Expand All @@ -45,6 +47,7 @@
import static org.hamcrest.Matchers.nullValue;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.cps.SnippetizerTester;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -549,4 +552,47 @@ public void invalidChoiceParameterValue() throws Exception {
j.assertLogContains("Value for choice parameter 'letter' is 'c', but valid choices are [a, b]",
j.assertBuildStatus(Result.FAILURE, us.scheduleBuild2(0)));
}

@Test public void snippetizerRoundTrip() throws Exception {
SnippetizerTester st = new SnippetizerTester(j);
BuildTriggerStep step = new BuildTriggerStep("downstream");
st.assertRoundTrip(step, "build 'downstream'");
step.setParameters(Arrays.asList(new StringParameterValue("branch", "default"), new BooleanParameterValue("correct", true)));
// Note: This does not actually test the format of the JSON produced by the snippet generator for parameters, see generateSnippet* for tests of that behavior.
st.assertRoundTrip(step, "build job: 'downstream', parameters: [string(name: 'branch', value: 'default'), booleanParam(name: 'correct', value: true)]");
}

@Issue("JENKINS-26093")
@Test public void generateSnippetForBuildTrigger() throws Exception {
SnippetizerTester st = new SnippetizerTester(j);
MockFolder d1 = j.createFolder("d1");
FreeStyleProject ds = d1.createProject(FreeStyleProject.class, "ds");
MockFolder d2 = j.createFolder("d2");
WorkflowJob us = d2.createProject(WorkflowJob.class, "us");
ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", ""), new BooleanParameterDefinition("flag", false, "")));
String snippet = "build job: '../d1/ds', parameters: [string(name: 'key', value: 'stuff'), booleanParam(name: 'flag', value: true)]";
st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'../d1/ds', 'parameter': [{'name':'key', 'value':'stuff'}, {'name':'flag', 'value':true}]}", snippet, us.getAbsoluteUrl() + "configure");
}

@Issue("JENKINS-29739")
@Test public void generateSnippetForBuildTriggerSingle() throws Exception {
SnippetizerTester st = new SnippetizerTester(j);
FreeStyleProject ds = j.jenkins.createProject(FreeStyleProject.class, "ds1");
FreeStyleProject us = j.jenkins.createProject(FreeStyleProject.class, "us1");
ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", "")));
String snippet = "build job: 'ds1', parameters: [string(name: 'key', value: 'stuff')]";
st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'ds1', 'parameter': {'name':'key', 'value':'stuff'}}", snippet, us.getAbsoluteUrl() + "configure");
}

@Test public void generateSnippetForBuildTriggerNone() throws Exception {
SnippetizerTester st = new SnippetizerTester(j);
FreeStyleProject ds = j.jenkins.createProject(FreeStyleProject.class, "ds0");
FreeStyleProject us = j.jenkins.createProject(FreeStyleProject.class, "us0");
st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'ds0'}", "build 'ds0'", us.getAbsoluteUrl() + "configure");
}

@Test
public void buildStepDocs() throws Exception {
SnippetizerTester.assertDocGeneration(BuildTriggerStep.class);
}
}