From 701236c698ed54b51d4ae289205a14273f53f836 Mon Sep 17 00:00:00 2001 From: Jeremy Pope Date: Mon, 12 Dec 2022 14:27:51 -0500 Subject: [PATCH 1/2] Merge changes from mcorsillo-chwy/JENKINS-60423_errorActionPreference --- .../hudson/plugins/powershell/PowerShell.java | 24 ++++++++++--------- .../powershell/PowerShell/config.jelly | 10 +++++--- .../PowerShell/help-errorAction.html | 6 +++++ .../PowerShell/help-stopOnError.html | 3 --- .../plugins/powershell/PowerShellTest.java | 16 ++++++------- 5 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html delete mode 100644 src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html diff --git a/src/main/java/hudson/plugins/powershell/PowerShell.java b/src/main/java/hudson/plugins/powershell/PowerShell.java index 320dbde..19ecd19 100644 --- a/src/main/java/hudson/plugins/powershell/PowerShell.java +++ b/src/main/java/hudson/plugins/powershell/PowerShell.java @@ -27,14 +27,14 @@ public class PowerShell extends CommandInterpreter { private Integer unstableReturn; - private final boolean stopOnError; + private String errorAction; private transient TaskListener listener; @DataBoundConstructor - public PowerShell(String command, boolean stopOnError, boolean useProfile, Integer unstableReturn) { + public PowerShell(String command, String errorAction, boolean useProfile, Integer unstableReturn) { super(command); - this.stopOnError = stopOnError; + this.errorAction = errorAction; this.useProfile = useProfile; this.unstableReturn = unstableReturn; } @@ -53,10 +53,6 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListene } } - public boolean isStopOnError() { - return stopOnError; - } - public boolean isUseProfile() { return useProfile; } @@ -130,10 +126,16 @@ public String[] buildCommandLine(FilePath script) { @Override protected String getContents() { StringBuilder sb = new StringBuilder(); - if (stopOnError) { - sb.append("$ErrorActionPreference=\"Stop\""); - } else { - sb.append("$ErrorActionPreference=\"Continue\""); + switch (errorAction) { + case "stopOnError": + sb.append("$ErrorActionPreference=\"Stop\""); + break; + case "continueOnError": + sb.append("$ErrorActionPreference=\"Continue\""); + break; + default: + sb.append("#No override to ErrorActionPreference selected"); + break; } sb.append(System.lineSeparator()); sb.append(command); diff --git a/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly b/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly index acbdc42..a2bf9dc 100644 --- a/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly +++ b/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly @@ -29,9 +29,13 @@ THE SOFTWARE. codemirror-config="mode: 'text/x-groovy', lineNumbers: true, matchBrackets: true, onBlur: function(editor){editor.save()}"/> - - - + + + diff --git a/src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html b/src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html new file mode 100644 index 0000000..c2f36f0 --- /dev/null +++ b/src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html @@ -0,0 +1,6 @@ +
+

Provides an option to override the system's default ErrorActionPreference. "Stop on Error" stops the script when any step fails (similar to Shell set -e). "Continue on Error" writes the error to the console, but does not stop the script. If selected, this inserts `$ErrorActionPreference=Stop` or `$ErrorActionPreference=Continue` to the beginning of the script. As such, these options do not allow for a param() block at the beginning of your script. +
+
See documentation. +

+
\ No newline at end of file diff --git a/src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html b/src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html deleted file mode 100644 index 58cdb39..0000000 --- a/src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html +++ /dev/null @@ -1,3 +0,0 @@ -
-

Stops script when some step fails. Similar to Shell set -e. Translates to $ErrorActionPreference Stop or Continue. See documentation.

-
\ No newline at end of file diff --git a/src/test/java/hudson/plugins/powershell/PowerShellTest.java b/src/test/java/hudson/plugins/powershell/PowerShellTest.java index d7edff7..795e557 100644 --- a/src/test/java/hudson/plugins/powershell/PowerShellTest.java +++ b/src/test/java/hudson/plugins/powershell/PowerShellTest.java @@ -28,7 +28,7 @@ public class PowerShellTest { @Test public void testConfigRoundtrip() throws Exception { FreeStyleProject p = r.createFreeStyleProject(); - PowerShell orig = new PowerShell("script", true, true, null); + PowerShell orig = new PowerShell("script", "noOverride", true, null); p.getBuildersList().add(orig); JenkinsRule.WebClient webClient = r.createWebClient(); @@ -44,7 +44,7 @@ public void testBuildSuccess() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", true, true, null)); + project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", "noOverride", true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -56,7 +56,7 @@ public void testBuildSuccess() throws Exception { public void testBuildBadCommandFails() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("wrong command", true, true, null)); + project1.getBuildersList().add(new PowerShell("wrong command", "noOverride", true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -68,7 +68,7 @@ public void testBuildBadCommandFails() throws Exception { public void testBuildBadCommandsSucceeds() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("wrong command", false, true, null)); + project1.getBuildersList().add(new PowerShell("wrong command", "noOverride", true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -81,7 +81,7 @@ public void testBuildAndDisableProject() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", true, true, null)); + project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", "noOverride", true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -95,7 +95,7 @@ public void testBuildAndDisableProject() throws Exception { public void testBuildUnstableEnabledSucceeds() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("exit 0", true, true, 123)); + project1.getBuildersList().add(new PowerShell("exit 0", "noOverride", true, 123)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -107,7 +107,7 @@ public void testBuildUnstableEnabledSucceeds() throws Exception { public void testBuildUnstableEnabledBadCommandFails() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("exit 1", true, true, 123)); + project1.getBuildersList().add(new PowerShell("exit 1", "noOverride", true, 123)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -119,7 +119,7 @@ public void testBuildUnstableEnabledBadCommandFails() throws Exception { public void testBuildUnstableEnabledBadCommandUnstableErrorCode() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("exit 123", true, true, 123)); + project1.getBuildersList().add(new PowerShell("exit 123", "noOverride", true, 123)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); From 57e609e00b672f21f3e53b95c4d1a1859b2f9646 Mon Sep 17 00:00:00 2001 From: Jeremy Pope Date: Mon, 12 Dec 2022 15:42:30 -0500 Subject: [PATCH 2/2] Revert back to previous behavior, but remove setting Continue for ErrorActionPreference if stopOnError is false --- .../hudson/plugins/powershell/PowerShell.java | 24 ++++++++----------- .../powershell/PowerShell/config.jelly | 10 +++----- .../PowerShell/help-errorAction.html | 6 ----- .../PowerShell/help-stopOnError.html | 5 ++++ .../plugins/powershell/PowerShellTest.java | 16 ++++++------- 5 files changed, 26 insertions(+), 35 deletions(-) delete mode 100644 src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html create mode 100644 src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html diff --git a/src/main/java/hudson/plugins/powershell/PowerShell.java b/src/main/java/hudson/plugins/powershell/PowerShell.java index 19ecd19..7db1acc 100644 --- a/src/main/java/hudson/plugins/powershell/PowerShell.java +++ b/src/main/java/hudson/plugins/powershell/PowerShell.java @@ -27,14 +27,14 @@ public class PowerShell extends CommandInterpreter { private Integer unstableReturn; - private String errorAction; + private final boolean stopOnError; private transient TaskListener listener; @DataBoundConstructor - public PowerShell(String command, String errorAction, boolean useProfile, Integer unstableReturn) { + public PowerShell(String command, boolean stopOnError, boolean useProfile, Integer unstableReturn) { super(command); - this.errorAction = errorAction; + this.stopOnError = stopOnError; this.useProfile = useProfile; this.unstableReturn = unstableReturn; } @@ -53,6 +53,10 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListene } } + public boolean isStopOnError() { + return stopOnError; + } + public boolean isUseProfile() { return useProfile; } @@ -126,18 +130,10 @@ public String[] buildCommandLine(FilePath script) { @Override protected String getContents() { StringBuilder sb = new StringBuilder(); - switch (errorAction) { - case "stopOnError": - sb.append("$ErrorActionPreference=\"Stop\""); - break; - case "continueOnError": - sb.append("$ErrorActionPreference=\"Continue\""); - break; - default: - sb.append("#No override to ErrorActionPreference selected"); - break; + if (stopOnError) { + sb.append("$ErrorActionPreference=\"Stop\""); + sb.append(System.lineSeparator()); } - sb.append(System.lineSeparator()); sb.append(command); sb.append(System.lineSeparator()); sb.append("exit $LastExitCode"); diff --git a/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly b/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly index a2bf9dc..acbdc42 100644 --- a/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly +++ b/src/main/resources/hudson/plugins/powershell/PowerShell/config.jelly @@ -29,13 +29,9 @@ THE SOFTWARE. codemirror-config="mode: 'text/x-groovy', lineNumbers: true, matchBrackets: true, onBlur: function(editor){editor.save()}"/>
- - - + + + diff --git a/src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html b/src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html deleted file mode 100644 index c2f36f0..0000000 --- a/src/main/resources/hudson/plugins/powershell/PowerShell/help-errorAction.html +++ /dev/null @@ -1,6 +0,0 @@ -
-

Provides an option to override the system's default ErrorActionPreference. "Stop on Error" stops the script when any step fails (similar to Shell set -e). "Continue on Error" writes the error to the console, but does not stop the script. If selected, this inserts `$ErrorActionPreference=Stop` or `$ErrorActionPreference=Continue` to the beginning of the script. As such, these options do not allow for a param() block at the beginning of your script. -
-
See documentation. -

-
\ No newline at end of file diff --git a/src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html b/src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html new file mode 100644 index 0000000..cf34921 --- /dev/null +++ b/src/main/resources/hudson/plugins/powershell/PowerShell/help-stopOnError.html @@ -0,0 +1,5 @@ +
+

Stops script when some step fails. Similar to Shell set -e. Translates to inserting the line '$ErrorActionPreference = "Stop"' into the beginning of your command block. See documentation on ErrorActionPreference. +
However, this can break scripts that have advanced parameter blocks, specifically those utilizing '[CmdletBinding()]' in the beginning as this must be the first executable line in a scriptblock, resulting in an 'UnexpectedAttribute' error. +

+
\ No newline at end of file diff --git a/src/test/java/hudson/plugins/powershell/PowerShellTest.java b/src/test/java/hudson/plugins/powershell/PowerShellTest.java index 795e557..d7edff7 100644 --- a/src/test/java/hudson/plugins/powershell/PowerShellTest.java +++ b/src/test/java/hudson/plugins/powershell/PowerShellTest.java @@ -28,7 +28,7 @@ public class PowerShellTest { @Test public void testConfigRoundtrip() throws Exception { FreeStyleProject p = r.createFreeStyleProject(); - PowerShell orig = new PowerShell("script", "noOverride", true, null); + PowerShell orig = new PowerShell("script", true, true, null); p.getBuildersList().add(orig); JenkinsRule.WebClient webClient = r.createWebClient(); @@ -44,7 +44,7 @@ public void testBuildSuccess() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", "noOverride", true, null)); + project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", true, true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -56,7 +56,7 @@ public void testBuildSuccess() throws Exception { public void testBuildBadCommandFails() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("wrong command", "noOverride", true, null)); + project1.getBuildersList().add(new PowerShell("wrong command", true, true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -68,7 +68,7 @@ public void testBuildBadCommandFails() throws Exception { public void testBuildBadCommandsSucceeds() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("wrong command", "noOverride", true, null)); + project1.getBuildersList().add(new PowerShell("wrong command", false, true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -81,7 +81,7 @@ public void testBuildAndDisableProject() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", "noOverride", true, null)); + project1.getBuildersList().add(new PowerShell("echo 'Hello World!'", true, true, null)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -95,7 +95,7 @@ public void testBuildAndDisableProject() throws Exception { public void testBuildUnstableEnabledSucceeds() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("exit 0", "noOverride", true, 123)); + project1.getBuildersList().add(new PowerShell("exit 0", true, true, 123)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -107,7 +107,7 @@ public void testBuildUnstableEnabledSucceeds() throws Exception { public void testBuildUnstableEnabledBadCommandFails() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("exit 1", "noOverride", true, 123)); + project1.getBuildersList().add(new PowerShell("exit 1", true, true, 123)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get(); @@ -119,7 +119,7 @@ public void testBuildUnstableEnabledBadCommandFails() throws Exception { public void testBuildUnstableEnabledBadCommandUnstableErrorCode() throws Exception { Assume.assumeTrue(isPowerShellAvailable()); FreeStyleProject project1 = r.createFreeStyleProject("project1"); - project1.getBuildersList().add(new PowerShell("exit 123", "noOverride", true, 123)); + project1.getBuildersList().add(new PowerShell("exit 123", true, true, 123)); QueueTaskFuture freeStyleBuildQueueTaskFuture = project1.scheduleBuild2(0); FreeStyleBuild build = freeStyleBuildQueueTaskFuture.get();