diff --git a/src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/NpmStep.groovy b/src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/NpmStep.groovy new file mode 100644 index 0000000..ac410cb --- /dev/null +++ b/src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/NpmStep.groovy @@ -0,0 +1,28 @@ +package uk.gov.hmrc.jenkinsjobbuilders.domain.step + +import static uk.gov.hmrc.jenkinsjobbuilders.domain.step.ShellStep.shellStep + +class NpmStep implements Step { + + private static final String EOL = System.getProperty("line.separator") + private final Step step + + private NpmStep(String bashScript, List npmCommands) { + this.step = shellStep(sbtCommands.inject(bashScript + EOL) { + string, item -> string + EOL + "npm run $item" + }) + } + + static Step npmStep(List commands) { + npmStep("", commands) + } + + static Step npmStep(String bashScript, List commands) { + new NpmStep(bashScript, commands) + } + + @Override + Closure toDsl() { + step.toDsl() + } +} diff --git a/src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/NpmStepSpec.groovy b/src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/NpmStepSpec.groovy new file mode 100644 index 0000000..ffd7039 --- /dev/null +++ b/src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/NpmStepSpec.groovy @@ -0,0 +1,41 @@ +package uk.gov.hmrc.jenkinsjobbuilders.domain.step + +import javaposse.jobdsl.dsl.Job +import spock.lang.Specification +import uk.gov.hmrc.jenkinsjobbuilders.domain.JobParents +import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder + +import static uk.gov.hmrc.jenkinsjobbuilders.domain.step.NpmStep.npmStep + +@Mixin(JobParents) +class NpmStepSpec extends Specification { + + void 'test XML output'() { + given: + JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). + withSteps(npmStep(["test"])) + + when: + Job job = jobBuilder.build(jobParent()) + + then: + with(job.node) { + builders.'hudson.tasks.Shell' [0].command.text().contains("npm run test") + } + } + + void 'test prepended bash script'() { + given: + JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). + withSteps(npmStep("nvm use 4.8.4", ["test"])) + + when: + Job job = jobBuilder.build(jobParent()) + + then: + with(job.node) { + builders.'hudson.tasks.Shell' [0].command.text().contains("nvm use 4.8.4") + builders.'hudson.tasks.Shell' [0].command.text().contains("npm run test") + } + } +}