Skip to content
Permalink
Browse files

[JENKINS-45101] Implement argumentsToString for step & wrap to look f…

…or a delegate configured with exactly one argument.
  • Loading branch information...
jglick committed Jun 30, 2017
1 parent 1cd4d63 commit b5b30ec451e2b9b7865c3e40cf8decd084cacc72
16 pom.xml
@@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.30</version>
<version>2.31</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -64,7 +64,7 @@
<properties>
<jenkins.version>1.642.3</jenkins.version>
<workflow-step-api-plugin.version>2.11</workflow-step-api-plugin.version>
<workflow-cps-plugin.version>2.30</workflow-cps-plugin.version>
<workflow-cps-plugin.version>2.32</workflow-cps-plugin.version>
<workflow-support-plugin.version>2.14</workflow-support-plugin.version>
</properties>
<dependencies>
@@ -156,6 +156,12 @@
<artifactId>mock-javamail</artifactId>
<version>1.9</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
@@ -175,5 +181,11 @@
<version>1.28</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ansicolor</artifactId>
<version>0.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -36,15 +36,17 @@
import hudson.model.TaskListener;
import hudson.tasks.Builder;
import hudson.tasks.Publisher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;

import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.kohsuke.stapler.DataBoundConstructor;

/**
@@ -122,6 +124,27 @@ public boolean isMetaStep() {
return ImmutableSet.of(Run.class, FilePath.class, Launcher.class, TaskListener.class);
}

@Override public String argumentsToString(Map<String, Object> namedArgs) {
return super.argumentsToString(delegateArguments(namedArgs.get("delegate")));
}

@SuppressWarnings("unchecked")
static @CheckForNull Map<String, Object> delegateArguments(@CheckForNull Object delegate) {
if (delegate instanceof UninstantiatedDescribable) {
// TODO JENKINS-45101 getStepArgumentsAsString does not resolve its arguments
// thus delegate.model == null and we cannot inspect DescribableModel.soleRequiredParameter
// thus for, e.g., `junit testResults: '*.xml', keepLongStdio: true` we will get null
return new HashMap<>(((UninstantiatedDescribable) delegate).getArguments());
} else if (delegate instanceof Map) {
Map<String, Object> r = new HashMap<>();
r.putAll((Map) delegate);
r.remove(DescribableModel.CLAZZ);
return r;
} else {
return null;
}
}

}

}
@@ -161,6 +161,10 @@ public SimpleBuildWrapper getDelegate() {
return ImmutableSet.of(Run.class, FilePath.class, Launcher.class, TaskListener.class, EnvVars.class);
}

@Override public String argumentsToString(Map<String, Object> namedArgs) {
return super.argumentsToString(CoreStep.DescriptorImpl.delegateArguments(namedArgs.get("delegate")));
}

}

}
@@ -32,9 +32,14 @@
import java.util.List;
import javax.mail.internet.InternetAddress;
import jenkins.plugins.mailer.tasks.i18n.Messages;
import org.hamcrest.Matchers;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.workflow.actions.ArgumentsAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.SnippetizerTester;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import static org.junit.Assert.*;
@@ -108,12 +113,15 @@
+ " writeFile text: '''<testsuite name='a'><testcase name='a1'/><testcase name='a2'><error>a2 failed</error></testcase></testsuite>''', file: 'a.xml'\n"
+ " writeFile text: '''<testsuite name='b'><testcase name='b1'/><testcase name='b2'/></testsuite>''', file: 'b.xml'\n"
+ " junit '*.xml'\n"
+ "}"));
+ "}", true));
WorkflowRun b = r.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0).get());
TestResultAction a = b.getAction(TestResultAction.class);
assertNotNull(a);
assertEquals(4, a.getTotalCount());
assertEquals(1, a.getFailCount());
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
assertThat(coreStepNodes, Matchers.hasSize(1));
assertEquals("*.xml", ArgumentsAction.getStepArgumentsAsString(coreStepNodes.get(0)));
}

@Test public void javadoc() throws Exception {
@@ -122,9 +130,12 @@
"node {\n"
+ " writeFile text: 'hello world', file: 'docs/index.html'\n"
+ " step([$class: 'JavadocArchiver', javadocDir: 'docs'])\n"
+ "}"));
r.assertBuildStatusSuccess(p.scheduleBuild2(0));
+ "}", true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertEquals("hello world", r.createWebClient().getPage(p, "javadoc/").getWebResponse().getContentAsString());
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
assertThat(coreStepNodes, Matchers.hasSize(1));
assertEquals("docs", ArgumentsAction.getStepArgumentsAsString(coreStepNodes.get(0)));
}

@Test public void mailer() throws Exception {
@@ -24,6 +24,8 @@

package org.jenkinsci.plugins.workflow.steps;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Functions;
@@ -49,14 +51,21 @@
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.tasks.SimpleBuildWrapper;
import org.hamcrest.Matchers;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.workflow.actions.ArgumentsAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.SnippetizerTester;
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
@@ -220,6 +229,27 @@
}
}

@Issue("JENKINS-45101")
@Test public void argumentsToString() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) {}\n" +
"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), Predicates.and(new NodeStepTypePredicate("wrap"), new Predicate<FlowNode>() {
@Override public boolean apply(FlowNode n) {
return n instanceof StepStartNode && !((StepStartNode) n).isBody();
}
}));
assertThat(coreStepNodes, Matchers.hasSize(1));
assertEquals("xterm", ArgumentsAction.getStepArgumentsAsString(coreStepNodes.get(0)));
}
});
}

// TODO add @LocalData serialForm test proving compatibility with executions dating back to workflow 1.4.3 on 1.580.1

// TODO add to jenkins-test-harness

0 comments on commit b5b30ec

Please sign in to comment.
You can’t perform that action at this time.