22
33import static org .junit .Assert .*;
44
5+ import java .io .File ;
56import java .nio .charset .Charset ;
67import java .util .List ;
78
89import hudson .EnvVars ;
10+ import hudson .Functions ;
911import hudson .model .FreeStyleBuild ;
1012import hudson .model .TaskListener ;
1113import hudson .model .FreeStyleProject ;
@@ -39,33 +41,54 @@ public TestJob(String script, String scriptType, boolean hideEnvironmentVariable
3941 }
4042
4143 final static String UNIX_SCRIPT = "unixScript" ;
44+ final static String BATCH_SCRIPT = "batchScript" ;
4245
4346 final static String SCRIPT_SIMPLE_VARIABLES = "echo var1=one\n "
4447 + "echo var2=two\n "
4548 + "echo var3=three" ;
4649
47- final static String SCRIPT_DEPENDENT_VARIABLES = "echo var1=one\n "
50+ final static String SCRIPT_DEPENDENT_VARIABLES_UNIX = "echo var1=one\n "
4851 + "echo var2='$var1 two'\n "
4952 + "echo var3='yo $var4'\n "
5053 + "echo var4='three ${var2}'" ;
5154
52- final static String SCRIPT_OVERRIDDEN_VARIABLES = "echo var1=one\n "
55+ final static String SCRIPT_DEPENDENT_VARIABLES_BATCH = "echo var1=one\n "
56+ + "echo var2=$var1 two\n "
57+ + "echo var3=yo $var4\n "
58+ + "echo var4=three ${var2}" ;
59+
60+ final static String SCRIPT_OVERRIDDEN_VARIABLES_UNIX = "echo var1=one\n "
5361 + "echo var1+something='not one'\n "
5462 + "echo var2+something='two'" ;
5563
64+ final static String SCRIPT_OVERRIDDEN_VARIABLES_BATCH = "echo var1=one\n "
65+ + "echo var1+something=not one\n "
66+ + "echo var2+something=two" ;
67+
5668 final static String SCRIPT_UTF8 = "echo UTFstr=mąż" ;
5769
58- final static String SCRIPT_SHEBANG = "#!/bin/cat\n "
70+ final static String SCRIPT_SHEBANG_UNIX = "#!/bin/cat\n "
5971 + "hello=world" ;
6072
73+ // batch script does not have shebang
74+ final static String SCRIPT_SHEBANG_BATCH = "echo hello=world" ;
75+
6176 public void testWithEmptyScript () throws Exception {
62- TestJob job = new TestJob ("" , UNIX_SCRIPT , true );
77+ String scriptType = UNIX_SCRIPT ;
78+ if (Functions .isWindows ()) {
79+ scriptType = BATCH_SCRIPT ;
80+ }
81+ TestJob job = new TestJob ("" , scriptType , true );
6382 assertEquals (Result .SUCCESS , job .build .getResult ());
6483 }
6584
6685 @ Test
6786 public void testWithSimpleVariables () throws Exception {
68- TestJob job = new TestJob (SCRIPT_SIMPLE_VARIABLES , UNIX_SCRIPT , true );
87+ String scriptType = UNIX_SCRIPT ;
88+ if (Functions .isWindows ()) {
89+ scriptType = BATCH_SCRIPT ;
90+ }
91+ TestJob job = new TestJob (SCRIPT_SIMPLE_VARIABLES , scriptType , true );
6992 assertEquals (Result .SUCCESS , job .build .getResult ());
7093
7194 EnvVars vars = job .build .getEnvironment (job .listener );
@@ -76,7 +99,13 @@ public void testWithSimpleVariables() throws Exception {
7699
77100 @ Test
78101 public void testWithDependentVariables () throws Exception {
79- TestJob job = new TestJob (SCRIPT_DEPENDENT_VARIABLES , UNIX_SCRIPT , true );
102+ String scriptType = UNIX_SCRIPT ;
103+ String script = SCRIPT_DEPENDENT_VARIABLES_UNIX ;
104+ if (Functions .isWindows ()) {
105+ scriptType = BATCH_SCRIPT ;
106+ script = SCRIPT_DEPENDENT_VARIABLES_BATCH ;
107+ }
108+ TestJob job = new TestJob (script , scriptType , true );
80109 assertEquals (Result .SUCCESS , job .build .getResult ());
81110
82111 EnvVars vars = job .build .getEnvironment (job .listener );
@@ -87,41 +116,67 @@ public void testWithDependentVariables() throws Exception {
87116 }
88117
89118 @ Test
90- public void testWithOverridenVariables () throws Exception {
91- TestJob job = new TestJob (SCRIPT_OVERRIDDEN_VARIABLES , UNIX_SCRIPT , true );
119+ public void testWithOverriddenVariables () throws Exception {
120+ String scriptType = UNIX_SCRIPT ;
121+ String script = SCRIPT_OVERRIDDEN_VARIABLES_UNIX ;
122+ if (Functions .isWindows ()) {
123+ scriptType = BATCH_SCRIPT ;
124+ script = SCRIPT_OVERRIDDEN_VARIABLES_BATCH ;
125+ }
126+ TestJob job = new TestJob (script , scriptType , true );
92127 assertEquals (Result .SUCCESS , job .build .getResult ());
93128
94129 EnvVars vars = job .build .getEnvironment (job .listener );
95- assertEquals ("not one: one" , vars .get ("var1" ));
130+ assertEquals ("not one" + File . pathSeparatorChar + " one" , vars .get ("var1" ));
96131 assertEquals ("two" , vars .get ("var2" ));
97132 }
98133
99134 @ Test
100135 public void testReadingFileFromSCM () throws Exception {
101- TestJob job = new TestJob ("cat envs" , UNIX_SCRIPT , true );
136+ String scriptType = UNIX_SCRIPT ;
137+ String script = "cat envs" ;
138+ if (Functions .isWindows ()) {
139+ scriptType = BATCH_SCRIPT ;
140+ script = "type envs" ;
141+ }
142+ TestJob job = new TestJob (script , scriptType , true );
102143 assertEquals (Result .SUCCESS , job .build .getResult ());
103144 assertEquals ("bar" , job .build .getEnvironment (job .listener ).get ("foo_var" ));
104145 }
105146
106147 @ Test
107148 public void testWorkingDirectory () throws Exception {
108- TestJob job = new TestJob ("echo hi >was_run" , UNIX_SCRIPT , true );
149+ String scriptType = UNIX_SCRIPT ;
150+ if (Functions .isWindows ()) {
151+ scriptType = BATCH_SCRIPT ;
152+ }
153+ TestJob job = new TestJob ("echo hi >was_run" , scriptType , true );
109154
110155 // Make sure that the $PWD of the script is $WORKSPACE.
111156 assertTrue (job .build .getWorkspace ().child ("was_run" ).exists ());
112157 }
113158
114159 @ Test
115160 public void testWithShebang () throws Exception {
116- TestJob job = new TestJob (SCRIPT_SHEBANG , UNIX_SCRIPT , true );
161+ String scriptType = UNIX_SCRIPT ;
162+ String script = SCRIPT_SHEBANG_UNIX ;
163+ if (Functions .isWindows ()) {
164+ scriptType = BATCH_SCRIPT ;
165+ script = SCRIPT_SHEBANG_BATCH ;
166+ }
167+ TestJob job = new TestJob (script , scriptType , true );
117168
118169 assertEquals (Result .SUCCESS , job .build .getResult ());
119170 EnvVars vars = job .build .getEnvironment (job .listener );
120171 assertEquals ("world" , vars .get ("hello" ));
121172 }
122173
123174 public void testUTFHandling () throws Exception {
124- TestJob job = new TestJob (SCRIPT_UTF8 , UNIX_SCRIPT , true );
175+ String scriptType = UNIX_SCRIPT ;
176+ if (Functions .isWindows ()) {
177+ scriptType = BATCH_SCRIPT ;
178+ }
179+ TestJob job = new TestJob (SCRIPT_UTF8 , scriptType , true );
125180 assertEquals (Result .SUCCESS , job .build .getResult ());
126181
127182 EnvVars vars = job .build .getEnvironment (job .listener );
@@ -130,7 +185,11 @@ public void testUTFHandling() throws Exception {
130185
131186 @ Test
132187 public void testHideEnvironmentVariablesValues () throws Exception {
133- TestJob job = new TestJob (SCRIPT_SIMPLE_VARIABLES , UNIX_SCRIPT , true );
188+ String scriptType = UNIX_SCRIPT ;
189+ if (Functions .isWindows ()) {
190+ scriptType = BATCH_SCRIPT ;
191+ }
192+ TestJob job = new TestJob (SCRIPT_SIMPLE_VARIABLES , scriptType , true );
134193 assertEquals (Result .SUCCESS , job .build .getResult ());
135194 List <String > logs = job .build .getLog (10 );
136195
@@ -141,7 +200,11 @@ public void testHideEnvironmentVariablesValues() throws Exception {
141200
142201 @ Test
143202 public void testShowEnvironmentVariablesValues () throws Exception {
144- TestJob job = new TestJob (SCRIPT_SIMPLE_VARIABLES , UNIX_SCRIPT , false );
203+ String scriptType = UNIX_SCRIPT ;
204+ if (Functions .isWindows ()) {
205+ scriptType = BATCH_SCRIPT ;
206+ }
207+ TestJob job = new TestJob (SCRIPT_SIMPLE_VARIABLES , scriptType , false );
145208 assertEquals (Result .SUCCESS , job .build .getResult ());
146209 List <String > logs = job .build .getLog (10 );
147210
0 commit comments