diff --git a/src/ru/pulsar/jenkins/library/IStepExecutor.groovy b/src/ru/pulsar/jenkins/library/IStepExecutor.groovy index 4e15fe6f..e9f9cb50 100644 --- a/src/ru/pulsar/jenkins/library/IStepExecutor.groovy +++ b/src/ru/pulsar/jenkins/library/IStepExecutor.groovy @@ -12,9 +12,9 @@ interface IStepExecutor { boolean isUnix() - int sh(String script, boolean returnStatus, String encoding) + def sh(String script, boolean returnStatus, boolean returnStdout, String encoding) - int bat(String script, boolean returnStatus, String encoding) + def bat(String script, boolean returnStatus, boolean returnStdout, String encoding) String libraryResource(String path) @@ -30,9 +30,13 @@ interface IStepExecutor { void echo(message) - int cmd(String script, boolean returnStatus) + def cmd(String script, boolean returnStatus, boolean returnStdout) - int cmd(String script) + def cmd(String script, boolean returnStatus) + + def cmd(String script) + + def ringCommand(String script) void tool(String toolName) diff --git a/src/ru/pulsar/jenkins/library/StepExecutor.groovy b/src/ru/pulsar/jenkins/library/StepExecutor.groovy index 59030cfa..e8ffe04f 100644 --- a/src/ru/pulsar/jenkins/library/StepExecutor.groovy +++ b/src/ru/pulsar/jenkins/library/StepExecutor.groovy @@ -23,13 +23,13 @@ class StepExecutor implements IStepExecutor { } @Override - int sh(String script, boolean returnStatus, String encoding) { - steps.sh script: script, returnStatus: returnStatus, encoding: encoding + def sh(String script, boolean returnStatus, boolean returnStdout, String encoding) { + steps.sh script: script, returnStatus: returnStatus, returnStdout: returnStdout, encoding: encoding } @Override - int bat(String script, boolean returnStatus, String encoding) { - steps.bat script: script, returnStatus: returnStatus, encoding: encoding + def bat(String script, boolean returnStatus, boolean returnStdout, String encoding) { + steps.bat script: script, returnStatus: returnStatus, returnStdout: returnStdout, encoding: encoding } @Override @@ -58,8 +58,13 @@ class StepExecutor implements IStepExecutor { } @Override - int cmd(String script, boolean returnStatus = false) { - return steps.cmd(script, returnStatus) + def cmd(String script, boolean returnStatus = false, boolean returnStdout = false) { + return steps.cmd(script, returnStatus, returnStdout) + } + + @Override + def ringCommand(String script) { + return steps.ringCommand(script) } @Override diff --git a/src/ru/pulsar/jenkins/library/steps/Cmd.groovy b/src/ru/pulsar/jenkins/library/steps/Cmd.groovy index f2037522..ead109c3 100644 --- a/src/ru/pulsar/jenkins/library/steps/Cmd.groovy +++ b/src/ru/pulsar/jenkins/library/steps/Cmd.groovy @@ -7,22 +7,28 @@ class Cmd implements Serializable { private String script; private boolean returnStatus + private boolean returnStdout private String encoding = 'UTF-8' - Cmd(String script, boolean returnStatus = false) { + Cmd(String script, boolean returnStatus = false, boolean returnStdout = false) { this.script = script this.returnStatus = returnStatus + this.returnStdout = returnStdout }; - int run() { + def run() { IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() - int returnValue + def returnValue + + if (returnStatus & returnStdout) { + steps.error("returnStatus and returnStdout are not supported at the same time") + } if (steps.isUnix()) { - returnValue = steps.sh("$script", returnStatus, encoding) + returnValue = steps.sh("$script", returnStatus, returnStdout, encoding) } else { - returnValue = steps.bat("chcp 65001 > nul \n$script", returnStatus, encoding) + returnValue = steps.bat("chcp 65001 > nul \n$script", returnStatus, returnStdout, encoding) } return returnValue diff --git a/src/ru/pulsar/jenkins/library/steps/DesignerToEdtFormatTransformation.groovy b/src/ru/pulsar/jenkins/library/steps/DesignerToEdtFormatTransformation.groovy index 8ff00731..f678d0cf 100644 --- a/src/ru/pulsar/jenkins/library/steps/DesignerToEdtFormatTransformation.groovy +++ b/src/ru/pulsar/jenkins/library/steps/DesignerToEdtFormatTransformation.groovy @@ -4,7 +4,6 @@ package ru.pulsar.jenkins.library.steps import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.ioc.ContextRegistry -import ru.pulsar.jenkins.library.utils.Constants import ru.pulsar.jenkins.library.utils.EDT import ru.pulsar.jenkins.library.utils.FileUtils import ru.pulsar.jenkins.library.utils.Logger @@ -45,10 +44,7 @@ class DesignerToEdtFormatTransformation implements Serializable { def ringCommand = "ring $edtVersionForRing workspace import --configuration-files \"$configurationRoot\" --project-name $PROJECT_NAME --workspace-location \"$workspaceDir\"" - def ringOpts = [Constants.DEFAULT_RING_OPTS] - steps.withEnv(ringOpts) { - steps.cmd(ringCommand) - } + steps.ringCommand(ringCommand) steps.zip(WORKSPACE, WORKSPACE_ZIP) steps.stash(WORKSPACE_ZIP_STASH, WORKSPACE_ZIP) diff --git a/src/ru/pulsar/jenkins/library/steps/EdtToDesignerFormatTransformation.groovy b/src/ru/pulsar/jenkins/library/steps/EdtToDesignerFormatTransformation.groovy index f80acf22..2dfa3908 100644 --- a/src/ru/pulsar/jenkins/library/steps/EdtToDesignerFormatTransformation.groovy +++ b/src/ru/pulsar/jenkins/library/steps/EdtToDesignerFormatTransformation.groovy @@ -5,7 +5,6 @@ import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.SourceFormat import ru.pulsar.jenkins.library.ioc.ContextRegistry -import ru.pulsar.jenkins.library.utils.Constants import ru.pulsar.jenkins.library.utils.EDT import ru.pulsar.jenkins.library.utils.FileUtils import ru.pulsar.jenkins.library.utils.Logger @@ -48,10 +47,7 @@ class EdtToDesignerFormatTransformation implements Serializable { def ringCommand = "ring $edtVersionForRing workspace export --workspace-location \"$workspaceDir\" --project \"$projectDir\" --configuration-files \"$configurationRoot\"" - def ringOpts = [Constants.DEFAULT_RING_OPTS] - steps.withEnv(ringOpts) { - steps.cmd(ringCommand) - } + steps.ringCommand(ringCommand) steps.zip(CONFIGURATION_DIR, CONFIGURATION_ZIP) steps.stash(CONFIGURATION_ZIP_STASH, CONFIGURATION_ZIP) diff --git a/src/ru/pulsar/jenkins/library/steps/EdtValidate.groovy b/src/ru/pulsar/jenkins/library/steps/EdtValidate.groovy index c24f812a..f766b9e0 100644 --- a/src/ru/pulsar/jenkins/library/steps/EdtValidate.groovy +++ b/src/ru/pulsar/jenkins/library/steps/EdtValidate.groovy @@ -4,7 +4,6 @@ import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.SourceFormat import ru.pulsar.jenkins.library.ioc.ContextRegistry -import ru.pulsar.jenkins.library.utils.Constants import ru.pulsar.jenkins.library.utils.EDT import ru.pulsar.jenkins.library.utils.FileUtils import ru.pulsar.jenkins.library.utils.Logger @@ -52,11 +51,8 @@ class EdtValidate implements Serializable { Logger.println("Выполнение валидации EDT") def ringCommand = "ring $edtVersionForRing workspace validate --workspace-location \"$workspaceLocation\" --file \"$resultFile\" $projectList" - def ringOpts = [Constants.DEFAULT_RING_OPTS] - steps.withEnv(ringOpts) { - steps.catchError { - steps.cmd(ringCommand) - } + steps.catchError { + steps.ringCommand(ringCommand) } steps.archiveArtifacts("$DesignerToEdtFormatTransformation.WORKSPACE/.metadata/.log") diff --git a/src/ru/pulsar/jenkins/library/steps/RingCommand.groovy b/src/ru/pulsar/jenkins/library/steps/RingCommand.groovy new file mode 100644 index 00000000..53617302 --- /dev/null +++ b/src/ru/pulsar/jenkins/library/steps/RingCommand.groovy @@ -0,0 +1,30 @@ +package ru.pulsar.jenkins.library.steps + +import ru.pulsar.jenkins.library.IStepExecutor +import ru.pulsar.jenkins.library.ioc.ContextRegistry +import ru.pulsar.jenkins.library.utils.Constants + +class RingCommand implements Serializable { + + private String script + private boolean returnStatus + private boolean returnStdout + + RingCommand(String script) { + this.script = script + this.returnStatus = false + this.returnStdout = true + }; + + def run() { + IStepExecutor steps = ContextRegistry.getContext().getStepExecutor() + + def ringOpts = [Constants.DEFAULT_RING_OPTS] + steps.withEnv(ringOpts) { + String ringMessage = steps.cmd(script, returnStatus, returnStdout) + if (ringMessage.contains("error")) { + steps.error(ringMessage) + } + } + } +} diff --git a/test/unit/groovy/ru/pulsar/jenkins/library/steps/CmdTest.java b/test/unit/groovy/ru/pulsar/jenkins/library/steps/CmdTest.java index 4d605480..a5518494 100644 --- a/test/unit/groovy/ru/pulsar/jenkins/library/steps/CmdTest.java +++ b/test/unit/groovy/ru/pulsar/jenkins/library/steps/CmdTest.java @@ -29,14 +29,17 @@ void runOk() { final String script = "echo hello"; Cmd cmd = new Cmd(script); + when(steps.bat(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(0); + when(steps.sh(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(0); + // when - int run = cmd.run(); + Object run = cmd.run(); // then verify(steps).isUnix(); assertThat(steps).satisfiesAnyOf( - steps -> verify(steps).bat(contains(script), eq(false), anyString()), - steps -> verify(steps).sh(contains(script), eq(false), anyString()) + steps -> verify(steps).bat(contains(script), eq(false), eq(false), anyString()), + steps -> verify(steps).sh(contains(script), eq(false), eq(false), anyString()) ); assertThat(run).isEqualTo(0); @@ -49,8 +52,8 @@ void runFailNoReturn() { Cmd cmd = new Cmd(script); String thrownText = "failed"; - when(steps.bat(anyString(), anyBoolean(), anyString())).thenThrow(new Error(thrownText)); - when(steps.sh(anyString(), anyBoolean(), anyString())).thenThrow(new Error(thrownText)); + when(steps.bat(anyString(), anyBoolean(), anyBoolean(), anyString())).thenThrow(new Error(thrownText)); + when(steps.sh(anyString(), anyBoolean(), anyBoolean(), anyString())).thenThrow(new Error(thrownText)); // when Throwable thrown = catchThrowable(cmd::run); @@ -59,8 +62,8 @@ void runFailNoReturn() { // then verify(steps).isUnix(); assertThat(steps).satisfiesAnyOf( - steps -> verify(steps).bat(contains(script), eq(false), anyString()), - steps -> verify(steps).sh(contains(script), eq(false), anyString()) + steps -> verify(steps).bat(contains(script), eq(false), eq(false), anyString()), + steps -> verify(steps).sh(contains(script), eq(false), eq(false), anyString()) ); } @@ -70,17 +73,17 @@ void runPassAndReturn() { final String script = "false"; Cmd cmd = new Cmd(script, true); - when(steps.bat(anyString(), anyBoolean(), anyString())).thenReturn(1); - when(steps.sh(anyString(), anyBoolean(), anyString())).thenReturn(1); + when(steps.bat(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(1); + when(steps.sh(anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(1); // when - int run = cmd.run(); + Object run = cmd.run(); // then verify(steps).isUnix(); assertThat(steps).satisfiesAnyOf( - steps -> verify(steps).bat(contains(script), eq(true), anyString()), - steps -> verify(steps).sh(contains(script), eq(true), anyString()) + steps -> verify(steps).bat(contains(script), eq(true), eq(false), anyString()), + steps -> verify(steps).sh(contains(script), eq(true), eq(false), anyString()) ); assertThat(run).isEqualTo(1); diff --git a/vars/cmd.groovy b/vars/cmd.groovy index 692b019e..810dcb4c 100644 --- a/vars/cmd.groovy +++ b/vars/cmd.groovy @@ -1,9 +1,9 @@ import ru.pulsar.jenkins.library.steps.Cmd import ru.pulsar.jenkins.library.ioc.ContextRegistry -int call(String script, boolean returnStatus = false) { +def call(String script, boolean returnStatus = false, boolean returnStdout = false ) { ContextRegistry.registerDefaultContext(this) - Cmd cmd = new Cmd(script, returnStatus) + Cmd cmd = new Cmd(script, returnStatus, returnStdout) return cmd.run() } diff --git a/vars/ringCommand.groovy b/vars/ringCommand.groovy new file mode 100644 index 00000000..bc646c0b --- /dev/null +++ b/vars/ringCommand.groovy @@ -0,0 +1,9 @@ +import ru.pulsar.jenkins.library.ioc.ContextRegistry +import ru.pulsar.jenkins.library.steps.RingCommand + +def call(String script ) { + ContextRegistry.registerDefaultContext(this) + + RingCommand ringCommand = new RingCommand(script) + return ringCommand.run() +} \ No newline at end of file