Skip to content

Commit

Permalink
Merge pull request #113 from ivanmolodec/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 authored Mar 9, 2024
2 parents 262cd7a + bef4a4e commit ad528ca
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 45 deletions.
12 changes: 8 additions & 4 deletions src/ru/pulsar/jenkins/library/IStepExecutor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
17 changes: 11 additions & 6 deletions src/ru/pulsar/jenkins/library/StepExecutor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/ru/pulsar/jenkins/library/steps/Cmd.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions src/ru/pulsar/jenkins/library/steps/EdtValidate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
30 changes: 30 additions & 0 deletions src/ru/pulsar/jenkins/library/steps/RingCommand.groovy
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
27 changes: 15 additions & 12 deletions test/unit/groovy/ru/pulsar/jenkins/library/steps/CmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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())
);
}

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions vars/cmd.groovy
Original file line number Diff line number Diff line change
@@ -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()
}
9 changes: 9 additions & 0 deletions vars/ringCommand.groovy
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit ad528ca

Please sign in to comment.