Skip to content
Permalink
Browse files

Merge pull request #64 from gabloe/master

[Fixed JENKINS-49754] - Prevent PowerShell stdout pollution when using returnStdout
  • Loading branch information...
svanoort committed Mar 7, 2018
2 parents 6671b39 + 96a020c commit fb3cf82462d8156824592afb2f5e98e2158afd21
@@ -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)));
@@ -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);
@@ -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);
}
@@ -155,19 +155,58 @@
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);
}

0 comments on commit fb3cf82

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