10
10
import hudson .model .Result ;
11
11
import hudson .slaves .DumbSlave ;
12
12
import hudson .slaves .EnvironmentVariablesNodeProperty ;
13
+ import hudson .tasks .BatchFile ;
13
14
import hudson .tasks .Shell ;
14
15
import java .io .File ;
15
16
import java .io .Serializable ;
26
27
import org .jenkinsci .plugins .workflow .steps .StepConfigTester ;
27
28
import org .jenkinsci .plugins .workflow .support .visualization .table .FlowGraphTable ;
28
29
import org .jenkinsci .plugins .workflow .support .visualization .table .FlowGraphTable .Row ;
29
- import org .junit .Assert ;
30
+ import static org .junit .Assert .* ;
30
31
import org .junit .Assume ;
31
32
import org .junit .ClassRule ;
32
33
import org .junit .Rule ;
37
38
import org .jvnet .hudson .test .TestExtension ;
38
39
import org .kohsuke .stapler .DataBoundConstructor ;
39
40
40
- public class ShellStepTest extends Assert {
41
+ public class ShellStepTest {
41
42
42
43
@ ClassRule
43
44
public static BuildWatcher buildWatcher = new BuildWatcher ();
@@ -116,17 +117,17 @@ public boolean apply(File tmp) {
116
117
DumbSlave slave = j .createSlave ("slave" , null , null );
117
118
FreeStyleProject f = j .createFreeStyleProject ("f" ); // the control
118
119
f .setAssignedNode (slave );
119
- f .getBuildersList ().add (new Shell ("echo PATH=$PATH" ));
120
+ f .getBuildersList ().add (Functions . isWindows () ? new BatchFile ( "echo Path=%Path%" ) : new Shell ("echo PATH=$PATH" ));
120
121
WorkflowJob p = j .jenkins .createProject (WorkflowJob .class , "p" );
121
- p .setDefinition (new CpsFlowDefinition ("node('slave') {sh 'echo PATH=$PATH'}" , true ));
122
+ p .setDefinition (new CpsFlowDefinition ("node('slave') {isUnix() ? sh( 'echo PATH=$PATH') : bat('echo Path=%Path%') }" , true ));
122
123
// First check: syntax recommended in /help/system-config/nodeEnvironmentVariables.html.
123
- slave .getNodeProperties ().add (new EnvironmentVariablesNodeProperty (new EnvironmentVariablesNodeProperty .Entry ("PATH+ACME" , "/opt/acme/bin" )));
124
- j .assertLogContains (":/opt/acme/bin:/" , j .buildAndAssertSuccess (f )); // JRE also gets prepended
125
- j .assertLogContains ("PATH=/opt/acme/bin:/" , j .buildAndAssertSuccess (p ));
124
+ slave .getNodeProperties ().add (new EnvironmentVariablesNodeProperty (new EnvironmentVariablesNodeProperty .Entry ("PATH+ACME" , Functions . isWindows () ? "C: \\ acme \\ bin" : "/opt/acme/bin" )));
125
+ j .assertLogContains (Functions . isWindows () ? ";C: \\ acme \\ bin;" : ":/opt/acme/bin:/" , j .buildAndAssertSuccess (f )); // JRE also gets prepended
126
+ j .assertLogContains (Functions . isWindows () ? "Path=C: \\ acme \\ bin;" : "PATH=/opt/acme/bin:/" , j .buildAndAssertSuccess (p ));
126
127
// Second check: recursive expansion.
127
- slave .getNodeProperties ().replace (new EnvironmentVariablesNodeProperty (new EnvironmentVariablesNodeProperty .Entry ("PATH" , "/opt/acme/bin:$PATH" )));
128
- j .assertLogContains (":/opt/acme/bin:/" , j .buildAndAssertSuccess (f ));
129
- j .assertLogContains ("PATH=/opt/acme/bin:/" , j .buildAndAssertSuccess (p ));
128
+ slave .getNodeProperties ().replace (new EnvironmentVariablesNodeProperty (new EnvironmentVariablesNodeProperty .Entry ("PATH" , Functions . isWindows () ? "C: \\ acme \\ bin;$PATH" : "/opt/acme/bin:$PATH" )));
129
+ j .assertLogContains (Functions . isWindows () ? ";C: \\ acme \\ bin;" : ":/opt/acme/bin:/" , j .buildAndAssertSuccess (f ));
130
+ j .assertLogContains (Functions . isWindows () ? "Path=C: \\ acme \\ bin;" : "PATH=/opt/acme/bin:/" , j .buildAndAssertSuccess (p ));
130
131
}
131
132
132
133
@ Test public void launcherDecorator () throws Exception {
@@ -177,7 +178,7 @@ public DescriptorImpl() {
177
178
@ Issue ("JENKINS-40734" )
178
179
@ Test public void envWithShellChar () throws Exception {
179
180
WorkflowJob p = j .jenkins .createProject (WorkflowJob .class , "p" );
180
- p .setDefinition (new CpsFlowDefinition ("node {withEnv(['MONEY=big$$money']) {sh 'echo \" MONEY=$MONEY\" '}}" , true ));
181
+ p .setDefinition (new CpsFlowDefinition ("node {withEnv(['MONEY=big$$money']) {isUnix() ? sh( 'echo \" MONEY=$MONEY\" ') : bat('echo \" MONEY=%MONEY% \" ') }}" , true ));
181
182
j .assertLogContains ("MONEY=big$$money" , j .buildAndAssertSuccess (p ));
182
183
}
183
184
@@ -208,6 +209,7 @@ public DescriptorImpl() {
208
209
209
210
@ Issue ("JENKINS-26133" )
210
211
@ Test public void returnStdout () throws Exception {
212
+ Assume .assumeTrue ("TODO Windows equivalent TBD" , new File ("/usr/bin/tr" ).canExecute ());
211
213
WorkflowJob p = j .jenkins .createProject (WorkflowJob .class , "p" );
212
214
p .setDefinition (new CpsFlowDefinition ("def msg; node {msg = sh(script: 'echo hello world | tr [a-z] [A-Z]', returnStdout: true).trim()}; echo \" it said ${msg}\" " ));
213
215
j .assertLogContains ("it said HELLO WORLD" , j .assertBuildStatusSuccess (p .scheduleBuild2 (0 )));
@@ -218,7 +220,7 @@ public DescriptorImpl() {
218
220
@ Issue ("JENKINS-26133" )
219
221
@ Test public void returnStatus () throws Exception {
220
222
WorkflowJob p = j .jenkins .createProject (WorkflowJob .class , "p" );
221
- p .setDefinition (new CpsFlowDefinition ("node {echo \" truth is ${sh script: 'true', returnStatus: true} but falsity is ${sh script: 'false', returnStatus: true}\" }" ));
223
+ p .setDefinition (new CpsFlowDefinition ("node {echo \" truth is ${isUnix() ? sh( script: 'true', returnStatus: true) : bat(script: 'echo', returnStatus: true) } but falsity is ${isUnix() ? sh( script: 'false', returnStatus: true) : bat(script: 'type nonexistent' , returnStatus: true) }\" }" , true ));
222
224
j .assertLogContains ("truth is 0 but falsity is 1" , j .assertBuildStatusSuccess (p .scheduleBuild2 (0 )));
223
225
}
224
226
0 commit comments