Skip to content
Permalink
Browse files

[FIXED JENKINS-45109] Making StepNode.typeFunctionName honor metastep…

…s, for example to improve build log output.
  • Loading branch information...
jglick committed Jun 23, 2017
1 parent face7df commit 41b356172c5753f49f5dbde22915f00958c2f431
@@ -240,6 +240,12 @@
<version>2.1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>config-file-provider</artifactId>
<version>2.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cloudbees</groupId>
<artifactId>groovy-cps</artifactId>
@@ -33,12 +33,19 @@

import java.io.ObjectStreamException;
import java.util.Collections;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.DescribableParameter;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.actions.ArgumentsAction;

/**
* {@link AtomNode} for executing {@link Step} without body closure.
*
* @author Kohsuke Kawaguchi
*/
@SuppressWarnings("deprecation")
public class StepAtomNode extends AtomNode implements StepNode {

private String descriptorId;
@@ -76,9 +83,29 @@ protected String getTypeDisplayName() {
return d!=null ? d.getDisplayName() : descriptorId;
}

static @CheckForNull String effectiveFunctionName(@Nonnull org.jenkinsci.plugins.workflow.graph.StepNode node) {
StepDescriptor d = node.getDescriptor();
if (d == null) {
return null;
}
if (d.isMetaStep()) {
DescribableParameter p = new DescribableModel<>(d.clazz).getFirstRequiredParameter();
if (p != null) {
Object arg = ArgumentsAction.getArguments((FlowNode) node).get(p.getName());
if (arg instanceof UninstantiatedDescribable) {
String symbol = ((UninstantiatedDescribable) arg).getSymbol();
if (symbol != null) {
return symbol;
}
}
}
}
return d.getFunctionName();
}

@Override
protected String getTypeFunctionName() {
StepDescriptor d = getDescriptor();
return d != null ? d.getFunctionName() : descriptorId;
String fn = effectiveFunctionName(this);
return fn != null ? fn : descriptorId;
}
}
@@ -38,12 +38,13 @@ protected String getTypeDisplayName() {

@Override
protected String getTypeFunctionName() {
boolean isBody = getStartNode().isBody();
StepStartNode startNode = getStartNode();
boolean isBody = startNode.isBody();
if (isBody) {
return "}";
} else {
StepDescriptor d = getDescriptor();
return "// " + (d != null ? d.getFunctionName() : getStartNode().getStepName());
String fn = StepAtomNode.effectiveFunctionName(startNode);
return "// " + (fn != null ? fn : startNode.getStepName());
}
}

@@ -56,8 +56,8 @@ protected String getTypeFunctionName() {
if (isBody()) {
return "{";
} else {
StepDescriptor d = getDescriptor();
return (d != null ? d.getFunctionName() : descriptorId);
String fn = StepAtomNode.effectiveFunctionName(this);
return fn != null ? fn : descriptorId;
}
}

@@ -0,0 +1,77 @@
/*
* The MIT License
*
* Copyright 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.cps.nodes;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import hudson.model.Result;
import java.util.List;
import static org.hamcrest.Matchers.*;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
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.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class StepNodeTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public JenkinsRule r = new JenkinsRule();

@Issue("JENKINS-45109")
@Test public void metastepConsole() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" configFileProvider([]) {\n" +
" writeFile text: '''<testsuite name='a'><testcase name='c'><error>failed</error></testcase></testsuite>''', file: 'x.xml'\n" +
" junit 'x.xml'\n" +
" }\n" +
"}", true));
WorkflowRun b = r.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0));
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
assertThat(coreStepNodes, hasSize(1));
assertEquals("junit", coreStepNodes.get(0).getDisplayFunctionName());
List<FlowNode> coreWrapperStepNodes = 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(coreWrapperStepNodes, hasSize(1));
assertEquals("configFileProvider", coreWrapperStepNodes.get(0).getDisplayFunctionName());
r.assertLogContains("[Pipeline] junit", b);
r.assertLogContains("[Pipeline] configFileProvider", b);
r.assertLogContains("[Pipeline] // configFileProvider", b);
}

}

0 comments on commit 41b3561

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