Skip to content
Permalink
Browse files

Fixing JENKINS-49754

  • Loading branch information...
gabloe committed Feb 28, 2018
1 parent 5133350 commit 7d40a5d772c752b8b580683815dbda40101d97d7
@@ -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)));
@@ -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;";
@@ -124,7 +124,7 @@
}

@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);
}
@@ -161,13 +161,40 @@

@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);
}

0 comments on commit 7d40a5d

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