Skip to content

Commit

Permalink
Merge pull request #64 from gabloe/master
Browse files Browse the repository at this point in the history
[Fixed JENKINS-49754] - Prevent PowerShell stdout pollution when using returnStdout
  • Loading branch information
svanoort committed Mar 7, 2018
2 parents 6671b39 + 96a020c commit fb3cf82
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 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 Down Expand Up @@ -115,11 +115,15 @@ public String getScript() {
if (launcher.isUnix()) {
// There is no need to add a BOM with Open PowerShell
c.getPowerShellScriptFile(ws).write(scriptWithExit, "UTF-8");
c.getPowerShellWrapperFile(ws).write(scriptWrapper, "UTF-8");
if (!capturingOutput) {
c.getPowerShellWrapperFile(ws).write(scriptWrapper, "UTF-8");
}
} else {
// Write the Windows PowerShell scripts out with a UTF8 BOM
writeWithBom(c.getPowerShellScriptFile(ws), scriptWithExit);
writeWithBom(c.getPowerShellWrapperFile(ws), scriptWrapper);
if (!capturingOutput) {
writeWithBom(c.getPowerShellWrapperFile(ws), scriptWrapper);
}
}

Launcher.ProcStarter ps = launcher.launch().cmds(args).envs(escape(envVars)).pwd(ws).quiet(true);
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 @@ -155,19 +155,58 @@ public class PowershellScriptTest {
c.writeLog(ws, baos);
assertTrue(c.exitStatus(ws, launcher, listener).intValue() != 0);
assertThat(baos.toString(), containsString("explicit error"));
assertEquals("Hello, World!\r\n", new String(c.getOutput(ws, launcher)));
if (launcher.isUnix()) {
assertEquals("Hello, World!\n", new String(c.getOutput(ws, launcher)));
} else {
assertEquals("Hello, World!\r\n", new String(c.getOutput(ws, launcher)));
}
c.cleanup(ws);
}

@Test public void verbose() throws Exception {
DurableTask task = new PowershellScript("$VerbosePreference = \"Continue\"; Write-Verbose \"Hello, World!\"");
@Test public void noStdoutPollution() throws Exception {
DurableTask task = new PowershellScript("$VerbosePreference = \"Continue\"; " +
"$WarningPreference = \"Continue\"; " +
"$DebugPreference = \"Continue\"; " +
"Write-Verbose \"Hello, Verbose!\"; " +
"Write-Warning \"Hello, Warning!\"; " +
"Write-Debug \"Hello, Debug!\"; " +
"Write-Output \"Success\"");
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);
assertTrue(c.exitStatus(ws, launcher, listener).intValue() == 0);
assertThat(baos.toString(), containsString("Hello, Verbose!"));
assertThat(baos.toString(), containsString("Hello, Warning!"));
assertThat(baos.toString(), containsString("Hello, Debug!"));
if (launcher.isUnix()) {
assertEquals("Success\n", new String(c.getOutput(ws, launcher)));
} else {
assertEquals("Success\r\n", new String(c.getOutput(ws, launcher)));
}
c.cleanup(ws);
}

@Test public void specialStreams() throws Exception {
DurableTask task = new PowershellScript("$VerbosePreference = \"Continue\"; " +
"$WarningPreference = \"Continue\"; " +
"$DebugPreference = \"Continue\"; " +
"Write-Verbose \"Hello, Verbose!\"; " +
"Write-Warning \"Hello, Warning!\"; " +
"Write-Debug \"Hello, Debug!\";");
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("VERBOSE: Hello, Verbose!"));
assertThat(baos.toString(), containsString("WARNING: Hello, Warning!"));
assertThat(baos.toString(), containsString("DEBUG: Hello, Debug!"));
c.cleanup(ws);
}

Expand Down

0 comments on commit fb3cf82

Please sign in to comment.