Skip to content

Commit ded242c

Browse files
committed
chore: make tests great again (windows)
1 parent 5c26983 commit ded242c

File tree

5 files changed

+113
-48
lines changed

5 files changed

+113
-48
lines changed

src/main/java/com/lookout/jenkins/EnvironmentScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public ListBoxModel doFillScriptTypeItems() {
300300
ListBoxModel items = new ListBoxModel(
301301
new ListBoxModel.Option(Commands.UNIX_SCRIPT_DISPLAY_NAME, Commands.UNIX_SCRIPT),
302302
new ListBoxModel.Option(Commands.BATCH_SCRIPT_DISPLAY_NAME, Commands.BATCH_SCRIPT),
303-
new ListBoxModel.Option(Commands.PROWER_SHELL_DISPLAY_NAME, Commands.POWER_SHELL));
303+
new ListBoxModel.Option(Commands.POWER_SHELL_DISPLAY_NAME, Commands.POWER_SHELL));
304304
return items;
305305
}
306306
}

src/main/java/com/lookout/jenkins/commands/Commands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Commands {
66
public final static String UNIX_SCRIPT_DISPLAY_NAME = "Unix script";
77

88
public final static String POWER_SHELL = "powerShell";
9-
public final static String PROWER_SHELL_DISPLAY_NAME = "Powershell script";
9+
public final static String POWER_SHELL_DISPLAY_NAME = "Powershell script";
1010

1111
public final static String BATCH_SCRIPT = "batchScript";
1212
public final static String BATCH_SCRIPT_DISPLAY_NAME = "Batch script";

src/test/java/com/lookout/jenkins/EnvironmentScriptMatrixTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.jvnet.hudson.test.JenkinsRule;
1414

1515
import hudson.FilePath;
16+
import hudson.Functions;
1617
import hudson.matrix.Axis;
1718
import hudson.matrix.AxisList;
1819
import hudson.matrix.MatrixRun;
@@ -42,6 +43,11 @@ public MatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
4243
// race conditions when concurrently updating the 'counter' file.
4344
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));
4445

46+
String scriptType = UNIX_SCRIPT;
47+
if (Functions.isWindows()) {
48+
scriptType = BATCH_SCRIPT;
49+
}
50+
4551
project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
4652
project.getBuildWrappersList()
4753
.add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
@@ -52,28 +58,44 @@ public MatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
5258
}
5359
}
5460

55-
final static String SCRIPT_COUNTER = "file='%s/counter'\n"
61+
final static String SCRIPT_COUNTER_UNIX = "file='%s/counter'\n"
5662
+ "if [ -f $file ]; then\n"
5763
+ " i=$(($(cat $file)+1))\n"
5864
+ "else\n"
5965
+ " i=1\n"
6066
+ "fi\n"
61-
+ "echo 1 >was_run\n"
62-
+ "echo $i >$file\n"
67+
+ "echo 1 > was_run\n"
68+
+ "echo $i > $file\n"
69+
+ "echo seen=yes";
70+
71+
final static String SCRIPT_COUNTER_BATCH = "set file=%s\\counter\n"
72+
+ "if exist %%file%% (\n"
73+
+ " set /p i=< %%file%%\n"
74+
+ " set /a i+=1\n"
75+
+ ") else (\n"
76+
+ " set i=1\n"
77+
+ ")\n"
78+
+ "echo 1 > was_run\n"
79+
+ "echo %%i%% > %%file%%\n"
6380
+ "echo seen=yes";
6481

6582
// Generate a random directory that we pass to the shell script.
6683
File tempDir;
6784

6885
String script;
69-
String scriptType = "unixScript";
86+
final static String UNIX_SCRIPT = "unixScript";
87+
final static String BATCH_SCRIPT = "batchScript";
7088
boolean hideGeneratedValue = Boolean.TRUE;
7189

7290
@Before
7391
public void setUp() throws IOException {
7492
// Generate a random directory that we pass to the shell script.
7593
tempDir = Files.createTempDirectory("tmp").toFile();
76-
script = String.format(SCRIPT_COUNTER, tempDir.getPath());
94+
String scriptCounter = SCRIPT_COUNTER_UNIX;
95+
if (Functions.isWindows()) {
96+
scriptCounter = SCRIPT_COUNTER_BATCH;
97+
}
98+
script = String.format(scriptCounter, tempDir.getPath());
7799
}
78100

79101
@Test

src/test/java/com/lookout/jenkins/EnvironmentScriptMultiMatrixTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.file.Files;
1515

1616
import hudson.FilePath;
17+
import hudson.Functions;
1718
import hudson.matrix.Axis;
1819
import hudson.matrix.AxisList;
1920
import hudson.matrix.MatrixRun;
@@ -43,6 +44,11 @@ public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Excepti
4344
// race conditions when concurrently updating the 'counter' file.
4445
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));
4546

47+
String scriptType = UNIX_SCRIPT;
48+
if (Functions.isWindows()) {
49+
scriptType = BATCH_SCRIPT;
50+
}
51+
4652
project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
4753
project.getBuildWrappersList()
4854
.add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
@@ -53,28 +59,44 @@ public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Excepti
5359
}
5460
}
5561

56-
final static String SCRIPT_COUNTER = "file='%s/counter'\n"
62+
final static String SCRIPT_COUNTER_UNIX = "file='%s/counter'\n"
5763
+ "if [ -f $file ]; then\n"
5864
+ " i=$(($(cat $file)+1))\n"
5965
+ "else\n"
6066
+ " i=1\n"
6167
+ "fi\n"
62-
+ "echo 1 >was_run\n"
63-
+ "echo $i >$file\n"
68+
+ "echo 1 > was_run\n"
69+
+ "echo $i > $file\n"
70+
+ "echo seen=yes";
71+
72+
final static String SCRIPT_COUNTER_BATCH = "set file=%s\\counter\n"
73+
+ "if exist %%file%% (\n"
74+
+ " set /p i=< %%file%%\n"
75+
+ " set /a i+=1\n"
76+
+ ") else (\n"
77+
+ " set i=1\n"
78+
+ ")\n"
79+
+ "echo 1 > was_run\n"
80+
+ "echo %%i%% > %%file%%\n"
6481
+ "echo seen=yes";
6582

6683
// Generate a random directory that we pass to the shell script.
6784
File tempDir;
6885

6986
String script;
70-
String scriptType = "unixScript";
87+
final static String UNIX_SCRIPT = "unixScript";
88+
final static String BATCH_SCRIPT = "batchScript";
7189
boolean hideGeneratedValue = Boolean.TRUE;
7290

7391
@Before
7492
public void setUp() throws IOException {
7593
// Generate a random directory that we pass to the shell script.
7694
tempDir = Files.createTempDirectory("tmp").toFile();
77-
script = String.format(SCRIPT_COUNTER, tempDir);
95+
String scriptCounter = SCRIPT_COUNTER_UNIX;
96+
if (Functions.isWindows()) {
97+
scriptCounter = SCRIPT_COUNTER_BATCH;
98+
}
99+
script = String.format(scriptCounter, tempDir);
78100
}
79101

80102
@Test

src/test/java/com/lookout/jenkins/EnvironmentScriptTest.java

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.*;
44

5+
import java.io.File;
56
import java.nio.charset.Charset;
67
import java.util.List;
78

@@ -40,42 +41,54 @@ public TestJob(String script, String scriptType, boolean hideEnvironmentVariable
4041
}
4142

4243
final static String UNIX_SCRIPT = "unixScript";
43-
final static String POWER_SHELL = "powerShell";
44+
final static String BATCH_SCRIPT = "batchScript";
4445

4546
final static String SCRIPT_SIMPLE_VARIABLES = "echo var1=one\n"
4647
+ "echo var2=two\n"
4748
+ "echo var3=three";
4849

49-
final static String SCRIPT_DEPENDENT_VARIABLES = "echo var1=one\n"
50+
final static String SCRIPT_DEPENDENT_VARIABLES_UNIX = "echo var1=one\n"
5051
+ "echo var2='$var1 two'\n"
5152
+ "echo var3='yo $var4'\n"
5253
+ "echo var4='three ${var2}'";
5354

54-
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"
5561
+ "echo var1+something='not one'\n"
5662
+ "echo var2+something='two'";
5763

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+
5868
final static String SCRIPT_UTF8 = "echo UTFstr=mąż";
5969

60-
final static String SCRIPT_SHEBANG = "#!/bin/cat\n"
70+
final static String SCRIPT_SHEBANG_UNIX = "#!/bin/cat\n"
6171
+ "hello=world";
6272

73+
// batch script does not have shebang
74+
final static String SCRIPT_SHEBANG_BATCH = "echo hello=world";
75+
6376
public void testWithEmptyScript() throws Exception {
64-
String script = UNIX_SCRIPT;
77+
String scriptType = UNIX_SCRIPT;
6578
if (Functions.isWindows()) {
66-
script = POWER_SHELL;
79+
scriptType = BATCH_SCRIPT;
6780
}
68-
TestJob job = new TestJob("", script, true);
81+
TestJob job = new TestJob("", scriptType, true);
6982
assertEquals(Result.SUCCESS, job.build.getResult());
7083
}
7184

7285
@Test
7386
public void testWithSimpleVariables() throws Exception {
74-
String script = UNIX_SCRIPT;
87+
String scriptType = UNIX_SCRIPT;
7588
if (Functions.isWindows()) {
76-
script = POWER_SHELL;
89+
scriptType = BATCH_SCRIPT;
7790
}
78-
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, script, true);
91+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, true);
7992
assertEquals(Result.SUCCESS, job.build.getResult());
8093

8194
EnvVars vars = job.build.getEnvironment(job.listener);
@@ -86,11 +99,13 @@ public void testWithSimpleVariables() throws Exception {
8699

87100
@Test
88101
public void testWithDependentVariables() throws Exception {
89-
String script = UNIX_SCRIPT;
102+
String scriptType = UNIX_SCRIPT;
103+
String script = SCRIPT_DEPENDENT_VARIABLES_UNIX;
90104
if (Functions.isWindows()) {
91-
script = POWER_SHELL;
105+
scriptType = BATCH_SCRIPT;
106+
script = SCRIPT_DEPENDENT_VARIABLES_BATCH;
92107
}
93-
TestJob job = new TestJob(SCRIPT_DEPENDENT_VARIABLES, script, true);
108+
TestJob job = new TestJob(script, scriptType, true);
94109
assertEquals(Result.SUCCESS, job.build.getResult());
95110

96111
EnvVars vars = job.build.getEnvironment(job.listener);
@@ -101,61 +116,67 @@ public void testWithDependentVariables() throws Exception {
101116
}
102117

103118
@Test
104-
public void testWithOverridenVariables() throws Exception {
105-
String script = UNIX_SCRIPT;
119+
public void testWithOverriddenVariables() throws Exception {
120+
String scriptType = UNIX_SCRIPT;
121+
String script = SCRIPT_OVERRIDDEN_VARIABLES_UNIX;
106122
if (Functions.isWindows()) {
107-
script = POWER_SHELL;
123+
scriptType = BATCH_SCRIPT;
124+
script = SCRIPT_OVERRIDDEN_VARIABLES_BATCH;
108125
}
109-
TestJob job = new TestJob(SCRIPT_OVERRIDDEN_VARIABLES, script, true);
126+
TestJob job = new TestJob(script, scriptType, true);
110127
assertEquals(Result.SUCCESS, job.build.getResult());
111128

112129
EnvVars vars = job.build.getEnvironment(job.listener);
113-
assertEquals("not one:one", vars.get("var1"));
130+
assertEquals("not one" + File.pathSeparatorChar + "one", vars.get("var1"));
114131
assertEquals("two", vars.get("var2"));
115132
}
116133

117134
@Test
118135
public void testReadingFileFromSCM() throws Exception {
119-
String script = UNIX_SCRIPT;
136+
String scriptType = UNIX_SCRIPT;
137+
String script = "cat envs";
120138
if (Functions.isWindows()) {
121-
script = POWER_SHELL;
139+
scriptType = BATCH_SCRIPT;
140+
script = "type envs";
122141
}
123-
TestJob job = new TestJob("cat envs", script, true);
142+
TestJob job = new TestJob(script, scriptType, true);
124143
assertEquals(Result.SUCCESS, job.build.getResult());
125144
assertEquals("bar", job.build.getEnvironment(job.listener).get("foo_var"));
126145
}
127146

128147
@Test
129148
public void testWorkingDirectory() throws Exception {
130-
String script = UNIX_SCRIPT;
149+
String scriptType = UNIX_SCRIPT;
131150
if (Functions.isWindows()) {
132-
script = POWER_SHELL;
151+
scriptType = BATCH_SCRIPT;
133152
}
134-
TestJob job = new TestJob("echo hi >was_run", script, true);
153+
TestJob job = new TestJob("echo hi >was_run", scriptType, true);
135154

136155
// Make sure that the $PWD of the script is $WORKSPACE.
137156
assertTrue(job.build.getWorkspace().child("was_run").exists());
138157
}
139158

140159
@Test
141160
public void testWithShebang() throws Exception {
142-
String script = UNIX_SCRIPT;
161+
String scriptType = UNIX_SCRIPT;
162+
String script = SCRIPT_SHEBANG_UNIX;
143163
if (Functions.isWindows()) {
144-
script = POWER_SHELL;
164+
scriptType = BATCH_SCRIPT;
165+
script = SCRIPT_SHEBANG_BATCH;
145166
}
146-
TestJob job = new TestJob(SCRIPT_SHEBANG, script, true);
167+
TestJob job = new TestJob(script, scriptType, true);
147168

148169
assertEquals(Result.SUCCESS, job.build.getResult());
149170
EnvVars vars = job.build.getEnvironment(job.listener);
150171
assertEquals("world", vars.get("hello"));
151172
}
152173

153174
public void testUTFHandling() throws Exception {
154-
String script = UNIX_SCRIPT;
175+
String scriptType = UNIX_SCRIPT;
155176
if (Functions.isWindows()) {
156-
script = POWER_SHELL;
177+
scriptType = BATCH_SCRIPT;
157178
}
158-
TestJob job = new TestJob(SCRIPT_UTF8, script, true);
179+
TestJob job = new TestJob(SCRIPT_UTF8, scriptType, true);
159180
assertEquals(Result.SUCCESS, job.build.getResult());
160181

161182
EnvVars vars = job.build.getEnvironment(job.listener);
@@ -164,11 +185,11 @@ public void testUTFHandling() throws Exception {
164185

165186
@Test
166187
public void testHideEnvironmentVariablesValues() throws Exception {
167-
String script = UNIX_SCRIPT;
188+
String scriptType = UNIX_SCRIPT;
168189
if (Functions.isWindows()) {
169-
script = POWER_SHELL;
190+
scriptType = BATCH_SCRIPT;
170191
}
171-
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, script, true);
192+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, true);
172193
assertEquals(Result.SUCCESS, job.build.getResult());
173194
List<String> logs = job.build.getLog(10);
174195

@@ -179,11 +200,11 @@ public void testHideEnvironmentVariablesValues() throws Exception {
179200

180201
@Test
181202
public void testShowEnvironmentVariablesValues() throws Exception {
182-
String script = UNIX_SCRIPT;
203+
String scriptType = UNIX_SCRIPT;
183204
if (Functions.isWindows()) {
184-
script = POWER_SHELL;
205+
scriptType = BATCH_SCRIPT;
185206
}
186-
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, script, false);
207+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, false);
187208
assertEquals(Result.SUCCESS, job.build.getResult());
188209
List<String> logs = job.build.getLog(10);
189210

0 commit comments

Comments
 (0)