Skip to content

Commit

Permalink
Fixing JENKINS-49754
Browse files Browse the repository at this point in the history
  • Loading branch information
gabloe committed Feb 28, 2018
1 parent 5133350 commit 7d40a5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Expand Up @@ -71,7 +71,7 @@ public String getScript() {
if (capturingOutput) {
cmd = String.format(". '%s'; Execute-AndWriteOutput -MainScript '%s' -OutputFile '%s' -LogFile '%s' -ResultFile '%s' -CaptureOutput;",
quote(c.getPowerShellHelperFile(ws)),
quote(c.getPowerShellWrapperFile(ws)),
quote(c.getPowerShellScriptFile(ws)),
quote(c.getOutputFile(ws)),
quote(c.getLogFile(ws)),
quote(c.getResultFile(ws)));
Expand All @@ -95,16 +95,10 @@ public String getScript() {
args.addAll(Arrays.asList(powershellArgs.split(" ")));
args.addAll(Arrays.asList("-Command", cmd));

// Exception propagation does not occur in legacy PowerShell versions. This means that an exception thrown in an inner PowerShell process
// does not propagate to the outer process regardless of $ErrorActionPreference selection. This ensures the exit code is non-zero if an error occurs regardless of PowerShell version.
String scriptWrapper = String.format("[CmdletBinding()]\r\n" +
"param()\r\n" +
"& %s %s -File '%s';\r\n" +
"if ($Error) {\r\n" +
" exit 1;\r\n" +
"} else {\r\n" +
" exit $LASTEXITCODE;\r\n" +
"}", powershellBinary, powershellArgs, quote(c.getPowerShellScriptFile(ws)));
"exit $LASTEXITCODE;", powershellBinary, powershellArgs, quote(c.getPowerShellScriptFile(ws)));

// Add an explicit exit to the end of the script so that exit codes are propagated
String scriptWithExit = script + "\r\nexit $LASTEXITCODE;";
Expand Down
Expand Up @@ -124,7 +124,7 @@ public class PowershellScriptTest {
}

@Test public void implicitError() throws Exception {
Controller c = new PowershellScript("MyBogus-Cmdlet").launch(new EnvVars(), ws, launcher, listener);
Controller c = new PowershellScript("$ErrorActionPreference = 'Stop'; MyBogus-Cmdlet").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
Expand Down Expand Up @@ -161,13 +161,40 @@ public class PowershellScriptTest {

@Test public void verbose() throws Exception {
DurableTask task = new PowershellScript("$VerbosePreference = \"Continue\"; Write-Verbose \"Hello, World!\"");
task.captureOutput();
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(0, c.exitStatus(ws, launcher).intValue());
assertThat(baos.toString(), containsString("VERBOSE: Hello, World!\r\n"));
c.cleanup(ws);
}

@Test public void debug() throws Exception {
DurableTask task = new PowershellScript("$DebugPreference = \"Continue\"; Write-Debug \"Hello, World!\"");
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(0, c.exitStatus(ws, launcher).intValue());
assertThat(baos.toString(), containsString("DEBUG: Hello, World!\r\n"));
c.cleanup(ws);
}

@Test public void warning() throws Exception {
DurableTask task = new PowershellScript("$WarningPreference = \"Continue\"; Write-Warning \"Hello, World!\"");
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(0, c.exitStatus(ws, launcher).intValue());
assertEquals("VERBOSE: Hello, World!\r\n", new String(c.getOutput(ws, launcher)));
assertThat(baos.toString(), containsString("WARNING: Hello, World!\r\n"));
c.cleanup(ws);
}

Expand Down

0 comments on commit 7d40a5d

Please sign in to comment.