From c490ce9211ae3a4cd999e38cad0fdfc664fd23e1 Mon Sep 17 00:00:00 2001 From: Florian Wilhelm Date: Thu, 4 Jun 2020 17:53:06 +0200 Subject: [PATCH] Use go-based npmExecuteScripts, mavenBuild in buildExecute (#1622) --- resources/default_pipeline_environment.yml | 3 + test/groovy/BuildExecuteTest.groovy | 88 ++++++++++++++++++---- vars/buildExecute.groovy | 15 +++- 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/resources/default_pipeline_environment.yml b/resources/default_pipeline_environment.yml index 73ad6e9f6..6b6e5793d 100644 --- a/resources/default_pipeline_environment.yml +++ b/resources/default_pipeline_environment.yml @@ -340,6 +340,9 @@ steps: mavenExecute: dockerImage: 'maven:3.5-jdk-7' logSuccessfulMavenTransfers: false + buildExecute: + npmInstall: true + npmRunScripts: [] mtaBuild: buildTarget: 'NEO' mtaBuildTool: cloudMbt diff --git a/test/groovy/BuildExecuteTest.groovy b/test/groovy/BuildExecuteTest.groovy index f563b0899..5f5031762 100644 --- a/test/groovy/BuildExecuteTest.groovy +++ b/test/groovy/BuildExecuteTest.groovy @@ -15,7 +15,10 @@ import static org.hamcrest.CoreMatchers.containsString import static org.hamcrest.CoreMatchers.hasItem import static org.hamcrest.CoreMatchers.is import static org.hamcrest.CoreMatchers.nullValue +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertFalse import static org.junit.Assert.assertThat +import static org.junit.Assert.assertTrue class BuildExecuteTest extends BasePiperTest { private ExpectedException exception = ExpectedException.none() @@ -34,9 +37,10 @@ class BuildExecuteTest extends BasePiperTest { def dockerMockArgs = [:] class DockerMock { - DockerMock(name){ + DockerMock(name) { dockerMockArgs.name = name } + def build(image, options) { return [image: image, options: options] } @@ -67,22 +71,31 @@ class BuildExecuteTest extends BasePiperTest { @Test void testMaven() { - def buildToolCalled = false - helper.registerAllowedMethod('mavenExecute', [Map.class], {m -> + boolean buildToolCalled = false + boolean installOptionSet = false + helper.registerAllowedMethod('mavenBuild', [Map.class], { m -> buildToolCalled = true return }) + helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m -> + installOptionSet = m['install'] + return + }) + helper.registerAllowedMethod('fileExists', [String.class], { s -> + return s == 'package.json' + }) stepRule.step.buildExecute( script: nullScript, buildTool: 'maven', ) - assertThat(buildToolCalled, is(true)) + assertTrue(buildToolCalled) + assertTrue(installOptionSet) } @Test void testMta() { def buildToolCalled = false - helper.registerAllowedMethod('mtaBuild', [Map.class], {m -> + helper.registerAllowedMethod('mtaBuild', [Map.class], { m -> buildToolCalled = true return }) @@ -96,7 +109,7 @@ class BuildExecuteTest extends BasePiperTest { @Test void testNpm() { def buildToolCalled = false - helper.registerAllowedMethod('npmExecute', [Map.class], {m -> + helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m -> buildToolCalled = true return }) @@ -107,11 +120,56 @@ class BuildExecuteTest extends BasePiperTest { assertThat(buildToolCalled, is(true)) } + @Test + void testNpmWithScripts() { + boolean actualValue = false + helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m -> + actualValue = (m['runScripts'][0] == 'foo' && m['runScripts'][1] == 'bar') + return + }) + stepRule.step.buildExecute( + script: nullScript, + buildTool: 'npm', + npmRunScripts: ['foo', 'bar'] + ) + assertTrue(actualValue) + } + + @Test + void testNpmWithInstallFalse() { + boolean actualValue = true + helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m -> + actualValue = m['install'] + return + }) + stepRule.step.buildExecute( + script: nullScript, + buildTool: 'npm', + npmInstall: false + ) + assertFalse(actualValue) + } + + @Test + void testNpmWithInstallTrue() { + boolean actualValue = false + helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m -> + actualValue = m['install'] + return + }) + stepRule.step.buildExecute( + script: nullScript, + buildTool: 'npm', + npmInstall: true + ) + assertTrue(actualValue) + } + @Test void testDocker() { binding.setVariable('docker', new DockerMock('test')) - def pushParams= [:] - helper.registerAllowedMethod('containerPushToRegistry', [Map.class], {m -> + def pushParams = [:] + helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m -> pushParams = m return }) @@ -132,8 +190,8 @@ class BuildExecuteTest extends BasePiperTest { void testDockerWithEnv() { nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0') binding.setVariable('docker', new DockerMock('test')) - def pushParams= [:] - helper.registerAllowedMethod('containerPushToRegistry', [Map.class], {m -> + def pushParams = [:] + helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m -> pushParams = m return }) @@ -152,8 +210,8 @@ class BuildExecuteTest extends BasePiperTest { @Test void testDockerNoPush() { binding.setVariable('docker', new DockerMock('test')) - def pushParams= [:] - helper.registerAllowedMethod('containerPushToRegistry', [Map.class], {m -> + def pushParams = [:] + helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m -> pushParams = m return }) @@ -172,7 +230,7 @@ class BuildExecuteTest extends BasePiperTest { @Test void testKaniko() { def kanikoParams = [:] - helper.registerAllowedMethod('kanikoExecute', [Map.class], {m -> + helper.registerAllowedMethod('kanikoExecute', [Map.class], { m -> kanikoParams = m return }) @@ -191,7 +249,7 @@ class BuildExecuteTest extends BasePiperTest { @Test void testKanikoNoPush() { def kanikoParams = [:] - helper.registerAllowedMethod('kanikoExecute', [Map.class], {m -> + helper.registerAllowedMethod('kanikoExecute', [Map.class], { m -> kanikoParams = m return }) @@ -211,7 +269,7 @@ class BuildExecuteTest extends BasePiperTest { void testSwitchToKaniko() { shellCallRule.setReturnValue('docker ps -q > /dev/null', 1) def kanikoParams = [:] - helper.registerAllowedMethod('kanikoExecute', [Map.class], {m -> + helper.registerAllowedMethod('kanikoExecute', [Map.class], { m -> kanikoParams = m return }) diff --git a/vars/buildExecute.groovy b/vars/buildExecute.groovy index ab7d53509..7cc1ba867 100644 --- a/vars/buildExecute.groovy +++ b/vars/buildExecute.groovy @@ -3,7 +3,6 @@ import com.sap.piper.GenerateDocumentation import com.sap.piper.Utils import com.sap.piper.ConfigurationHelper -import groovy.text.GStringTemplateEngine import groovy.transform.Field import static com.sap.piper.Prerequisites.checkScript @@ -22,7 +21,7 @@ import static com.sap.piper.Prerequisites.checkScript ] @Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([ - /** Only for Docker builds on the local deamon: Defines the build options for the build.*/ + /** Only for Docker builds on the local daemon: Defines the build options for the build.*/ 'containerBuildOptions', /** For custom build types: Defines the command to be executed within the `dockerImage` in order to execute the build. */ 'dockerCommand', @@ -30,6 +29,10 @@ import static com.sap.piper.Prerequisites.checkScript 'dockerImage', /** For Docker builds only (mandatory): tag of the image to be built. */ 'dockerImageTag', + /** For buildTool npm: Execute npm install (boolean, default 'true') */ + 'npmInstall', + /** For buildTool npm: List of npm run scripts to execute */ + 'npmRunScripts' ]) @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS @@ -68,13 +71,17 @@ void call(Map parameters = [:]) { switch(config.buildTool){ case 'maven': - mavenExecute script: script + mavenBuild script: script + // in case node_modules exists we assume npm install was executed by maven clean install + if (fileExists('package.json') && !fileExists('node_modules')) { + npmExecuteScripts script: script, install: true + } break case 'mta': mtaBuild script: script break case 'npm': - npmExecute script: script + npmExecuteScripts script: script, install: config.npmInstall, runScripts: config.npmRunScripts break case ['docker', 'kaniko']: DockerUtils dockerUtils = new DockerUtils(script)